Line data Source code
1 : // SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : #include "util.h"
4 : #include <QDir>
5 : #include <QFileInfo>
6 : #ifdef Q_OS_WIN
7 : #include <windows.h>
8 : #else
9 : #include <sys/time.h>
10 : #endif
11 : #include "qtpasssettings.h"
12 :
13 : #ifdef QT_DEBUG
14 : #include "debughelper.h"
15 : #endif
16 :
17 : QProcessEnvironment Util::_env;
18 : bool Util::_envInitialised;
19 :
20 : /**
21 : * @brief Util::initialiseEnvironment set the correct PATH for use with gpg, git
22 : * etc.
23 : */
24 0 : void Util::initialiseEnvironment() {
25 0 : if (!_envInitialised) {
26 0 : _env = QProcessEnvironment::systemEnvironment();
27 : #ifdef __APPLE__
28 : QString path = _env.value("PATH");
29 : if (!path.contains("/usr/local/MacGPG2/bin") &&
30 : QFile("/usr/local/MacGPG2/bin").exists())
31 : path += ":/usr/local/MacGPG2/bin";
32 : if (!path.contains("/usr/local/bin"))
33 : path += ":/usr/local/bin";
34 : _env.insert("PATH", path);
35 : #endif
36 : #ifdef Q_OS_WIN
37 : QString path = _env.value("PATH");
38 : if (!path.contains("C:\\Program Files\\WinGPG\\x86") &&
39 : QFile("C:\\Program Files\\WinGPG\\x86").exists())
40 : path += ";C:\\Program Files\\WinGPG\\x86";
41 : if (!path.contains("C:\\Program Files\\GnuPG\\bin") &&
42 : QFile("C:\\Program Files\\GnuPG\\bin").exists())
43 : path += ";C:\\Program Files\\GnuPG\bin";
44 : _env.insert("PATH", path);
45 : #endif
46 : #ifdef QT_DEBUG
47 : dbg() << _env.value("PATH");
48 : #endif
49 0 : _envInitialised = true;
50 : }
51 0 : }
52 :
53 : /**
54 : * @brief Util::findPasswordStore look for common .password-store folder
55 : * location.
56 : * @return
57 : */
58 0 : auto Util::findPasswordStore() -> QString {
59 0 : QString path;
60 0 : initialiseEnvironment();
61 0 : if (_env.contains("PASSWORD_STORE_DIR")) {
62 0 : path = _env.value("PASSWORD_STORE_DIR");
63 : } else {
64 : #ifdef Q_OS_WIN
65 : path = QDir::homePath() + QDir::separator() + "password-store" +
66 : QDir::separator();
67 : #else
68 0 : path = QDir::homePath() + QDir::separator() + ".password-store" +
69 0 : QDir::separator();
70 : #endif
71 : }
72 0 : return Util::normalizeFolderPath(path);
73 : }
74 :
75 : /**
76 : * @brief Util::normalizeFolderPath let's always end folders with a
77 : * QDir::separator()
78 : * @param path
79 : * @return
80 : */
81 216 : auto Util::normalizeFolderPath(QString path) -> QString {
82 432 : if (!path.endsWith("/") && !path.endsWith(QDir::separator())) {
83 108 : path += QDir::separator();
84 : }
85 216 : return QDir::toNativeSeparators(path);
86 : }
87 :
88 : /**
89 : * @brief Util::findBinaryInPath search for executables.
90 : * @param binary
91 : * @return
92 : */
93 0 : auto Util::findBinaryInPath(QString binary) -> QString {
94 0 : initialiseEnvironment();
95 :
96 0 : QString ret = "";
97 :
98 0 : binary.prepend(QDir::separator());
99 :
100 0 : if (_env.contains("PATH")) {
101 0 : QString path = _env.value("PATH");
102 :
103 0 : QStringList entries;
104 : #ifndef Q_OS_WIN
105 0 : entries = path.split(':');
106 0 : if (entries.length() < 2) {
107 : #endif
108 0 : entries = path.split(';');
109 : #ifndef Q_OS_WIN
110 : }
111 : #endif
112 :
113 0 : foreach (QString entry, entries) {
114 0 : QScopedPointer<QFileInfo> qfi(new QFileInfo(entry.append(binary)));
115 : #ifdef Q_OS_WIN
116 : if (!qfi->exists())
117 : qfi.reset(new QFileInfo(entry.append(".exe")));
118 :
119 : #endif
120 0 : if (!qfi->isExecutable()) {
121 : continue;
122 : }
123 :
124 0 : ret = qfi->absoluteFilePath();
125 : break;
126 : }
127 : }
128 : #ifdef Q_OS_WIN
129 : if (ret.isEmpty()) {
130 : binary.remove(0, 1);
131 : binary.prepend("wsl ");
132 : QString out, err;
133 : if (Executor::executeBlocking(binary, {"--version"}, &out, &err) == 0 &&
134 : !out.isEmpty() && err.isEmpty())
135 : ret = binary;
136 : }
137 : #endif
138 :
139 0 : return ret;
140 : }
141 :
142 : /**
143 : * @brief Util::checkConfig do we have prequisite settings?
144 : * @return
145 : */
146 0 : auto Util::checkConfig() -> bool {
147 0 : return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"))
148 0 : .exists() ||
149 0 : (QtPassSettings::isUsePass()
150 0 : ? !QtPassSettings::getPassExecutable().startsWith("wsl ") &&
151 0 : !QFile(QtPassSettings::getPassExecutable()).exists()
152 0 : : !QtPassSettings::getGpgExecutable().startsWith("wsl ") &&
153 0 : !QFile(QtPassSettings::getGpgExecutable()).exists());
154 : }
155 :
156 : /**
157 : * @brief Util::getDir get selectd folder path
158 : * @param index
159 : * @param forPass short or full path
160 : * @param model the filesystem model to operate on
161 : * @param storeModel our storemodel to operate on
162 : * @return path
163 : */
164 0 : auto Util::getDir(const QModelIndex &index, bool forPass,
165 : const QFileSystemModel &model, const StoreModel &storeModel)
166 : -> QString {
167 : QString abspath =
168 0 : QDir(QtPassSettings::getPassStore()).absolutePath() + QDir::separator();
169 : if (!index.isValid()) {
170 0 : return forPass ? "" : abspath;
171 : }
172 0 : QFileInfo info = model.fileInfo(storeModel.mapToSource(index));
173 : QString filePath =
174 0 : (info.isFile() ? info.absolutePath() : info.absoluteFilePath());
175 0 : if (forPass) {
176 0 : filePath = QDir(abspath).relativeFilePath(filePath);
177 : }
178 0 : filePath += QDir::separator();
179 : return filePath;
180 0 : }
181 :
182 : /**
183 : * @brief Util::copyDir
184 : * @param src
185 : * @param dest
186 : */
187 324 : void Util::copyDir(const QString &src, const QString &dest) {
188 324 : QDir srcDir(src);
189 324 : if (!srcDir.exists()) {
190 : return;
191 : }
192 324 : srcDir.mkpath(dest);
193 864 : foreach (QString dir, srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
194 648 : copyDir(src + QDir::separator() + dir, dest + QDir::separator() + dir);
195 : }
196 :
197 900 : foreach (QString file, srcDir.entryList(QDir::Files)) {
198 504 : QFile::copy(src + QDir::separator() + file,
199 504 : dest + QDir::separator() + file);
200 : }
201 324 : }
202 :
203 191 : auto Util::endsWithGpg() -> const QRegularExpression & {
204 263 : static const QRegularExpression expr{"\\.gpg$"};
205 191 : return expr;
206 : }
207 :
208 72 : auto Util::protocolRegex() -> const QRegularExpression & {
209 : static const QRegularExpression regex{
210 108 : "((?:https?|ftp|ssh|sftp|ftps|webdav|webdavs)://[^\\s<>\"\\)\\]\\[]+)"};
211 72 : return regex;
212 : }
213 :
214 72 : auto Util::newLinesRegex() -> const QRegularExpression & {
215 108 : static const QRegularExpression regex{"[\r\n]"};
216 72 : return regex;
217 : }
|