Make lxqt globals a public header
[lxde/liblxqt.git] / lxqtprogramfinder.cpp
CommitLineData
49e9d4f2
AM
1/* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
b9223fe7 4 * LXQt - a lightweight, Qt based, desktop toolset
49e9d4f2
AM
5 * http://razor-qt.org
6 *
7 * Copyright (C) 2013 Alec Moskvin <alecm@gmx.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 * END_COMMON_COPYRIGHT_HEADER */
24
b6483430 25#include "lxqtprogramfinder.h"
49e9d4f2 26#include <wordexp.h>
e6389bfc
HJYP
27#include <QDir>
28#include <QFileInfo>
49e9d4f2 29
f05ba5af 30using namespace LXQt;
13d7ab75 31
f9cba1ba 32LXQT_API bool ProgramFinder::programExists(const QString& command)
49e9d4f2 33{
d7858989 34 const QString program = programName(command);
49e9d4f2
AM
35 if (program[0] == QChar('/'))
36 {
37 QFileInfo fi(program);
38 return fi.isExecutable() && fi.isFile();
39 }
40
d7858989 41 const QString path = qgetenv("PATH");
5bf85d40
LP
42 const QStringList dirs = path.split(":", QString::SkipEmptyParts);
43 for (const QString& dirName : dirs)
49e9d4f2 44 {
d7858989 45 const QFileInfo fi(QDir(dirName), program);
49e9d4f2
AM
46 if (fi.isExecutable() && fi.isFile())
47 return true;
48 }
49 return false;
50}
51
f9cba1ba 52LXQT_API QStringList ProgramFinder::findPrograms(const QStringList& commands)
49e9d4f2
AM
53{
54 QStringList availPrograms;
5bf85d40 55 for (const QString& program : commands)
49e9d4f2
AM
56 if (programExists(program))
57 availPrograms.append(program);
58 return availPrograms;
59}
60
f9cba1ba 61LXQT_API QString ProgramFinder::programName(const QString& command)
49e9d4f2
AM
62{
63 wordexp_t we;
14b3781a 64 if (wordexp(command.toLocal8Bit().constData(), &we, WRDE_NOCMD) == 0)
49e9d4f2
AM
65 if (we.we_wordc > 0)
66 return QString(we.we_wordv[0]);
67 return QString();
68}