diff --git a/mainwindow.cpp b/mainwindow.cpp index fc49ee77d..5123118a3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,5 +1,6 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include "util.h" #include #include @@ -50,38 +51,17 @@ void MainWindow::checkConfig() { passExecutable = settings.value("passExecutable").toString(); if (passExecutable == "") { - process->start("which pass"); - process->waitForFinished(); - if (process->exitCode() == 0) { - passExecutable = process->readAllStandardOutput().trimmed(); - settings.setValue("passExecutable", passExecutable); - usePass = true; - settings.setValue("usePass", "true"); - } + passExecutable = Util::findBinaryInPath("pass"); } gitExecutable = settings.value("gitExecutable").toString(); if (gitExecutable == "") { - process->start("which git"); - process->waitForFinished(); - if (process->exitCode() == 0) { - gitExecutable = process->readAllStandardOutput().trimmed(); - settings.setValue("gitExecutable", gitExecutable); - } + gitExecutable = Util::findBinaryInPath("git"); } gpgExecutable = settings.value("gpgExecutable").toString(); if (gpgExecutable == "") { - process->start("which gpg2"); - process->waitForFinished(); - if (process->exitCode() != 0) { - process->start("which gpg"); - process->waitForFinished(); - } - if (process->exitCode() == 0) { - gpgExecutable = process->readAllStandardOutput().trimmed(); - settings.setValue("gpgExecutable", gpgExecutable); - } + gpgExecutable = Util::findBinaryInPath("gpg"); } if (passExecutable == "" && (gitExecutable == "" || gpgExecutable == "")) { diff --git a/qtpass.pro b/qtpass.pro index e81e24bc6..38661c3bf 100644 --- a/qtpass.pro +++ b/qtpass.pro @@ -20,12 +20,14 @@ SOURCES += main.cpp\ mainwindow.cpp \ dialog.cpp \ storemodel.cpp \ - singleapplication.cpp + singleapplication.cpp \ + util.cpp HEADERS += mainwindow.h \ dialog.h \ storemodel.h \ - singleapplication.h + singleapplication.h \ + util.h FORMS += mainwindow.ui \ dialog.ui diff --git a/util.cpp b/util.cpp new file mode 100644 index 000000000..08c816143 --- /dev/null +++ b/util.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include "util.h" + +QProcessEnvironment Util::_env; +bool Util::_envInitialised; + +void Util::initialiseEnvironment() +{ + if (!_envInitialised) { + _env = QProcessEnvironment::systemEnvironment(); + _envInitialised = true; + } +} + +QString Util::findBinaryInPath(QString binary) +{ + initialiseEnvironment(); + + QString ret = ""; + + binary.prepend("/"); + + if (_env.contains("PATH")) { + QString path = _env.value("PATH"); + + QStringList entries = path.split(':'); + if (entries.length() < 2) { + entries = path.split(';'); + } + + foreach(QString entry, entries) { + QFileInfo *qfi = new QFileInfo(entry.append(binary)); + qDebug() << entry; + +#ifdef WINDOWS + if (!qfi->exists()) { + QFileInfo qfi = new QFileInfo(entry.append(".exe")); + } +#endif + if (!qfi->isExecutable()) { + continue; + } + + ret = qfi->absoluteFilePath(); + break; + } + } + + return ret; +} diff --git a/util.h b/util.h new file mode 100644 index 000000000..4662a6c97 --- /dev/null +++ b/util.h @@ -0,0 +1,17 @@ +#ifndef UTIL_H +#define UTIL_H + +#include +#include + +class Util +{ +public: + static QString findBinaryInPath(QString binary); +private: + static void initialiseEnvironment(); + static QProcessEnvironment _env; + static bool _envInitialised; +}; + +#endif // UTIL_H