ProgramFinder: a set of functions to check if programs are installed
[lxde/liblxqt.git] / programfinder.cpp
CommitLineData
49e9d4f2
AM
1/* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * Razor - a lightweight, Qt based, desktop toolset
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
25#include "programfinder.h"
26#include <wordexp.h>
27#include <QtCore/QDir>
28#include <QtCore/QFileInfo>
29
30bool ProgramFinder::programExists(const QString& command)
31{
32 QString program = programName(command);
33 if (program[0] == QChar('/'))
34 {
35 QFileInfo fi(program);
36 return fi.isExecutable() && fi.isFile();
37 }
38
39 QString path = qgetenv("PATH");
40 foreach (const QString& dirName, path.split(":", QString::SkipEmptyParts))
41 {
42 QFileInfo fi(QDir(dirName), program);
43 if (fi.isExecutable() && fi.isFile())
44 return true;
45 }
46 return false;
47}
48
49QStringList ProgramFinder::findPrograms(const QStringList& programs)
50{
51 QStringList availPrograms;
52 foreach (const QString& program, programs)
53 if (programExists(program))
54 availPrograms.append(program);
55 return availPrograms;
56}
57
58QString ProgramFinder::programName(const QString& command)
59{
60 wordexp_t we;
61 if (wordexp(command.toStdString().c_str(), &we, WRDE_NOCMD) == 0)
62 if (we.we_wordc > 0)
63 return QString(we.we_wordv[0]);
64 return QString();
65}