Skip to content

Commit 0da489a

Browse files
committed
Merge pull request #7 from shitbangs/dev/which
Replace which invocations with actual path resolution code \o/
2 parents dc09b8b + cd5a742 commit 0da489a

File tree

4 files changed

+78
-26
lines changed

4 files changed

+78
-26
lines changed

mainwindow.cpp

+4-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "mainwindow.h"
22
#include "ui_mainwindow.h"
3+
#include "util.h"
34
#include <QClipboard>
45
#include <QTimer>
56

@@ -50,38 +51,17 @@ void MainWindow::checkConfig() {
5051

5152
passExecutable = settings.value("passExecutable").toString();
5253
if (passExecutable == "") {
53-
process->start("which pass");
54-
process->waitForFinished();
55-
if (process->exitCode() == 0) {
56-
passExecutable = process->readAllStandardOutput().trimmed();
57-
settings.setValue("passExecutable", passExecutable);
58-
usePass = true;
59-
settings.setValue("usePass", "true");
60-
}
54+
passExecutable = Util::findBinaryInPath("pass");
6155
}
6256

6357
gitExecutable = settings.value("gitExecutable").toString();
6458
if (gitExecutable == "") {
65-
process->start("which git");
66-
process->waitForFinished();
67-
if (process->exitCode() == 0) {
68-
gitExecutable = process->readAllStandardOutput().trimmed();
69-
settings.setValue("gitExecutable", gitExecutable);
70-
}
59+
gitExecutable = Util::findBinaryInPath("git");
7160
}
7261

7362
gpgExecutable = settings.value("gpgExecutable").toString();
7463
if (gpgExecutable == "") {
75-
process->start("which gpg2");
76-
process->waitForFinished();
77-
if (process->exitCode() != 0) {
78-
process->start("which gpg");
79-
process->waitForFinished();
80-
}
81-
if (process->exitCode() == 0) {
82-
gpgExecutable = process->readAllStandardOutput().trimmed();
83-
settings.setValue("gpgExecutable", gpgExecutable);
84-
}
64+
gpgExecutable = Util::findBinaryInPath("gpg");
8565
}
8666

8767
if (passExecutable == "" && (gitExecutable == "" || gpgExecutable == "")) {

qtpass.pro

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ SOURCES += main.cpp\
2020
mainwindow.cpp \
2121
dialog.cpp \
2222
storemodel.cpp \
23-
singleapplication.cpp
23+
singleapplication.cpp \
24+
util.cpp
2425

2526
HEADERS += mainwindow.h \
2627
dialog.h \
2728
storemodel.h \
28-
singleapplication.h
29+
singleapplication.h \
30+
util.h
2931

3032
FORMS += mainwindow.ui \
3133
dialog.ui

util.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <QDebug>
2+
#include <QFileInfo>
3+
#include <QProcessEnvironment>
4+
#include <QString>
5+
#include "util.h"
6+
7+
QProcessEnvironment Util::_env;
8+
bool Util::_envInitialised;
9+
10+
void Util::initialiseEnvironment()
11+
{
12+
if (!_envInitialised) {
13+
_env = QProcessEnvironment::systemEnvironment();
14+
_envInitialised = true;
15+
}
16+
}
17+
18+
QString Util::findBinaryInPath(QString binary)
19+
{
20+
initialiseEnvironment();
21+
22+
QString ret = "";
23+
24+
binary.prepend("/");
25+
26+
if (_env.contains("PATH")) {
27+
QString path = _env.value("PATH");
28+
29+
QStringList entries = path.split(':');
30+
if (entries.length() < 2) {
31+
entries = path.split(';');
32+
}
33+
34+
foreach(QString entry, entries) {
35+
QFileInfo *qfi = new QFileInfo(entry.append(binary));
36+
qDebug() << entry;
37+
38+
#ifdef WINDOWS
39+
if (!qfi->exists()) {
40+
QFileInfo qfi = new QFileInfo(entry.append(".exe"));
41+
}
42+
#endif
43+
if (!qfi->isExecutable()) {
44+
continue;
45+
}
46+
47+
ret = qfi->absoluteFilePath();
48+
break;
49+
}
50+
}
51+
52+
return ret;
53+
}

util.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef UTIL_H
2+
#define UTIL_H
3+
4+
#include <QString>
5+
#include <QProcessEnvironment>
6+
7+
class Util
8+
{
9+
public:
10+
static QString findBinaryInPath(QString binary);
11+
private:
12+
static void initialiseEnvironment();
13+
static QProcessEnvironment _env;
14+
static bool _envInitialised;
15+
};
16+
17+
#endif // UTIL_H

0 commit comments

Comments
 (0)