Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace which invocations with actual path resolution code #7

Merged
merged 2 commits into from
Aug 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "util.h"
#include <QClipboard>
#include <QTimer>

Expand Down Expand Up @@ -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 == "")) {
Expand Down
6 changes: 4 additions & 2 deletions qtpass.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <QDebug>
#include <QFileInfo>
#include <QProcessEnvironment>
#include <QString>
#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;
}
17 changes: 17 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef UTIL_H
#define UTIL_H

#include <QString>
#include <QProcessEnvironment>

class Util
{
public:
static QString findBinaryInPath(QString binary);
private:
static void initialiseEnvironment();
static QProcessEnvironment _env;
static bool _envInitialised;
};

#endif // UTIL_H