| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "utilities.h" |
| 7 |
|
| 8 |
#include <QtCore/QCoreApplication> |
| 9 |
#include <QtCore/QFileInfo> |
| 10 |
#include <QtCore/QProcessEnvironment> |
| 11 |
#include <QtCore/QStringList> |
| 12 |
|
| 13 |
namespace Avogadro::QtGui::Utilities { |
| 14 |
|
| 15 |
QString libraryDirectory() |
| 16 |
{ |
| 17 |
return QString(AvogadroLibs_LIB_DIR); |
| 18 |
} |
| 19 |
|
| 20 |
QString dataDirectory() |
| 21 |
{ |
| 22 |
return QString(AvogadroLibs_DATA_DIR); |
| 23 |
} |
| 24 |
|
| 25 |
QString findExecutablePath(QString program) |
| 26 |
{ |
| 27 |
// we want to return the path to a program if it exists |
| 28 |
// using the PATH system environment variable |
| 29 |
QProcessEnvironment system = QProcessEnvironment::systemEnvironment(); |
| 30 |
QString path = system.value("PATH"); |
| 31 |
#ifdef Q_OS_WIN32 |
| 32 |
QStringList paths = path.split(';'); |
| 33 |
#else |
| 34 |
QStringList paths = path.split(':'); |
| 35 |
// check standard locations first |
| 36 |
paths.prepend("/usr/bin"); |
| 37 |
paths.prepend("/usr/local/bin"); |
| 38 |
#endif |
| 39 |
|
| 40 |
// check the current application directory too |
| 41 |
paths.prepend(QCoreApplication::applicationDirPath()); |
| 42 |
// and check "../bin" too |
| 43 |
paths.prepend(QCoreApplication::applicationDirPath() + "/../bin"); |
| 44 |
|
| 45 |
// check to see if we find the program in that path |
| 46 |
for (const auto& dir : paths) { |
| 47 |
QFileInfo test(dir + '/' + program); |
| 48 |
if (test.isExecutable()) { |
| 49 |
// must exist to be executable, so we're done |
| 50 |
return dir; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return QString(); |
| 55 |
} |
| 56 |
|
| 57 |
QStringList findExecutablePaths(QStringList programs) |
| 58 |
{ |
| 59 |
QStringList result; |
| 60 |
|
| 61 |
// we want to return the path to a program if it exists |
| 62 |
// using the PATH system environment variable |
| 63 |
QProcessEnvironment system = QProcessEnvironment::systemEnvironment(); |
| 64 |
QString path = system.value("PATH"); |
| 65 |
#ifdef Q_OS_WIN32 |
| 66 |
QStringList paths = path.split(';'); |
| 67 |
#else |
| 68 |
QStringList paths = path.split(':'); |
| 69 |
// check standard locations first |
| 70 |
paths.prepend("/usr/bin"); |
| 71 |
paths.prepend("/usr/local/bin"); |
| 72 |
#endif |
| 73 |
|
| 74 |
// check the current application directory too |
| 75 |
paths.prepend(QCoreApplication::applicationDirPath()); |
| 76 |
|
| 77 |
// loop through all programs and all possible paths |
| 78 |
for (const auto& program : programs) { |
| 79 |
for (const auto& dir : paths) { |
| 80 |
QFileInfo test(dir + '/' + program); |
| 81 |
if (test.isExecutable() && !result.contains(test.absoluteFilePath())) { |
| 82 |
// must exist to be executable, so add it to the list |
| 83 |
result << test.absoluteFilePath(); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return result; |
| 89 |
} |
| 90 |
|
| 91 |
} // namespace Avogadro::QtGui::Utilities |