-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
util.cpp
87 lines (71 loc) · 1.96 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <QDebug>
#include <QFileInfo>
#include <QProcessEnvironment>
#include <QString>
#include <QDir>
#include "util.h"
QProcessEnvironment Util::_env;
bool Util::_envInitialised;
void Util::initialiseEnvironment()
{
if (!_envInitialised) {
_env = QProcessEnvironment::systemEnvironment();
#ifdef __APPLE__
// TODO checks here
QString path = _env.value("PATH");
if (!path.contains("/usr/local/MacGPG2/bin") && QFile("/usr/local/MacGPG2/bin").exists()) {
path += ":/usr/local/MacGPG2/bin";
}
if (!path.contains("/usr/local/bin")) {
path += ":/usr/local/bin";
}
_env.insert("PATH", path);
#endif
_envInitialised = true;
}
}
QString Util::findPasswordStore()
{
QString path;
initialiseEnvironment();
if (_env.contains("PASSWORD_STORE_DIR")) {
path = _env.value("PASSWORD_STORE_DIR");
} else {
path = QDir::homePath()+"/.password-store/";
}
return Util::normalizeFolderPath(path);
}
QString Util::normalizeFolderPath(QString path) {
if (!path.endsWith("/") && !path.endsWith(QDir::separator())) {
path += '/';
}
return path;
}
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) {
QScopedPointer<QFileInfo> qfi(new QFileInfo(entry.append(binary)));
qDebug() << entry;
#ifdef Q_OS_WIN
if (!qfi->exists()) {
qfi.reset(new QFileInfo(entry.append(".exe")));
}
#endif
if (!qfi->isExecutable()) {
continue;
}
ret = qfi->absoluteFilePath();
break;
}
}
return ret;
}