summaryrefslogtreecommitdiffstats
path: root/util.cpp
blob: 74c84c1e1f8e0b1685d188c03da70bd82f229d56 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <QDebug>
#include <QFileInfo>
#include <QProcessEnvironment>
#include <QString>
#include <QDir>
#include "util.h"

QProcessEnvironment Util::_env;
bool Util::_envInitialised;

/**
 * @brief Util::initialiseEnvironment
 */
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;
    }
}

/**
 * @brief Util::findPasswordStore
 * @return
 */
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);
}

/**
 * @brief Util::normalizeFolderPath
 * @param path
 * @return
 */
QString Util::normalizeFolderPath(QString path) {
    if (!path.endsWith(QDir::separator())) {
        path += QDir::separator();
    }
    return path;
}


QString Util::findBinaryInPath(QString binary)
{
    initialiseEnvironment();

    QString ret = "";

    binary.prepend(QDir::separator());

    if (_env.contains("PATH")) {
        QString path = _env.value("PATH");

        QStringList entries;
#ifndef Q_OS_WIN
        entries = path.split(':');
        if (entries.length() < 2) {
#endif
            entries = path.split(';');
#ifndef Q_OS_WIN
        }
#endif

        foreach(QString entry, entries) {
            QScopedPointer<QFileInfo> qfi(new QFileInfo(entry.append(binary)));
#ifdef Q_OS_WIN
            if (!qfi->exists()) {
                qfi.reset(new QFileInfo(entry.append(".exe")));
            }
#endif
            qDebug() << entry;
            if (!qfi->isExecutable()) {
                continue;
            }

            ret = qfi->absoluteFilePath();
            break;
        }
    }

    return ret;
}

/**
 * @brief Util::checkConfig
 * @param passStore
 * @param passExecutable
 * @param gpgExecutable
 * @return
 */
bool Util::checkConfig(QString passStore, QString passExecutable, QString gpgExecutable)
{
    return !QFile(passStore + ".gpg-id").exists() || (!QFile(passExecutable).exists() && !QFile(gpgExecutable).exists());
}