QtPass  1.3.3
Multi-platform GUI for pass, the standard unix password manager.
util.cpp
Go to the documentation of this file.
1 #include "util.h"
2 #include <QDir>
3 #include <QFileInfo>
4 #ifdef Q_OS_WIN
5 #include <windows.h>
6 #else
7 #include <sys/time.h>
8 #endif
9 #include "qtpasssettings.h"
10 
11 #ifdef QT_DEBUG
12 #include "debughelper.h"
13 #endif
14 
15 QProcessEnvironment Util::_env;
16 bool Util::_envInitialised;
17 
22 void Util::initialiseEnvironment() {
23  if (!_envInitialised) {
24  _env = QProcessEnvironment::systemEnvironment();
25 #ifdef __APPLE__
26  QString path = _env.value("PATH");
27  if (!path.contains("/usr/local/MacGPG2/bin") &&
28  QFile("/usr/local/MacGPG2/bin").exists())
29  path += ":/usr/local/MacGPG2/bin";
30  if (!path.contains("/usr/local/bin"))
31  path += ":/usr/local/bin";
32  _env.insert("PATH", path);
33 #endif
34 #ifdef Q_OS_WIN
35  QString path = _env.value("PATH");
36  if (!path.contains("C:\\Program Files\\WinGPG\\x86") &&
37  QFile("C:\\Program Files\\WinGPG\\x86").exists())
38  path += ";C:\\Program Files\\WinGPG\\x86";
39  if (!path.contains("C:\\Program Files\\GnuPG\\bin") &&
40  QFile("C:\\Program Files\\GnuPG\\bin").exists())
41  path += ";C:\\Program Files\\GnuPG\bin";
42  _env.insert("PATH", path);
43 #endif
44 #ifdef QT_DEBUG
45  dbg() << _env.value("PATH");
46 #endif
47  _envInitialised = true;
48  }
49 }
50 
57  QString path;
58  initialiseEnvironment();
59  if (_env.contains("PASSWORD_STORE_DIR")) {
60  path = _env.value("PASSWORD_STORE_DIR");
61  } else {
62 #ifdef Q_OS_WIN
63  path = QDir::homePath() + QDir::separator() + "password-store" +
64  QDir::separator();
65 #else
66  path = QDir::homePath() + QDir::separator() + ".password-store" +
67  QDir::separator();
68 #endif
69  }
70  return Util::normalizeFolderPath(path);
71 }
72 
79 QString Util::normalizeFolderPath(QString path) {
80  if (!path.endsWith("/") && !path.endsWith(QDir::separator()))
81  path += QDir::separator();
82  return QDir::toNativeSeparators(path);
83 }
84 
90 QString Util::findBinaryInPath(QString binary) {
91  initialiseEnvironment();
92 
93  QString ret = "";
94 
95  binary.prepend(QDir::separator());
96 
97  if (_env.contains("PATH")) {
98  QString path = _env.value("PATH");
99 
100  QStringList entries;
101 #ifndef Q_OS_WIN
102  entries = path.split(':');
103  if (entries.length() < 2) {
104 #endif
105  entries = path.split(';');
106 #ifndef Q_OS_WIN
107  }
108 #endif
109 
110  foreach (QString entry, entries) {
111  QScopedPointer<QFileInfo> qfi(new QFileInfo(entry.append(binary)));
112 #ifdef Q_OS_WIN
113  if (!qfi->exists())
114  qfi.reset(new QFileInfo(entry.append(".exe")));
115 
116 #endif
117  if (!qfi->isExecutable())
118  continue;
119 
120  ret = qfi->absoluteFilePath();
121  break;
122  }
123  }
124 #ifdef Q_OS_WIN
125  if (ret.isEmpty()) {
126  binary.remove(0, 1);
127  binary.prepend("wsl ");
128  QString out, err;
129  if (Executor::executeBlocking(binary, {"--version"}, &out, &err) == 0 &&
130  !out.isEmpty() && err.isEmpty())
131  ret = binary;
132  }
133 #endif
134 
135  return ret;
136 }
137 
143  return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"))
144  .exists() ||
146  ? !QtPassSettings::getPassExecutable().startsWith("wsl ") &&
147  !QFile(QtPassSettings::getPassExecutable()).exists()
148  : !QtPassSettings::getGpgExecutable().startsWith("wsl ") &&
149  !QFile(QtPassSettings::getGpgExecutable()).exists());
150 }
151 
160 QString Util::getDir(const QModelIndex &index, bool forPass,
161  const QFileSystemModel &model,
162  const StoreModel &storeModel) {
163  QString abspath =
164  QDir(QtPassSettings::getPassStore()).absolutePath() + QDir::separator();
165  if (!index.isValid())
166  return forPass ? "" : abspath;
167  QFileInfo info = model.fileInfo(storeModel.mapToSource(index));
168  QString filePath =
169  (info.isFile() ? info.absolutePath() : info.absoluteFilePath());
170  if (forPass) {
171  filePath = QDir(abspath).relativeFilePath(filePath);
172  }
173  filePath += QDir::separator();
174  return filePath;
175 }
176 
182 void Util::copyDir(const QString &src, const QString &dest) {
183  QDir srcDir(src);
184  if (!srcDir.exists()) {
185  return;
186  }
187  srcDir.mkpath(dest);
188  foreach (QString dir, srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
189  copyDir(src + QDir::separator() + dir, dest + QDir::separator() + dir);
190  }
191 
192  foreach (QString file, srcDir.entryList(QDir::Files)) {
193  QFile::copy(src + QDir::separator() + file,
194  dest + QDir::separator() + file);
195  }
196 }
Util::normalizeFolderPath
static QString normalizeFolderPath(QString path)
Util::normalizeFolderPath let's always end folders with a QDir::separator()
Definition: util.cpp:79
QtPassSettings::getGpgExecutable
static QString getGpgExecutable(const QString &defaultValue=QVariant().toString())
Definition: qtpasssettings.cpp:314
QtPassSettings::getPassExecutable
static QString getPassExecutable(const QString &defaultValue=QVariant().toString())
Definition: qtpasssettings.cpp:296
Util::findBinaryInPath
static QString findBinaryInPath(QString binary)
Util::findBinaryInPath search for executables.
Definition: util.cpp:90
Executor::executeBlocking
static int executeBlocking(QString app, const QStringList &args, QString input=QString(), QString *process_out=Q_NULLPTR, QString *process_err=Q_NULLPTR)
Executor::executeBlocking blocking version of the executor, takes input and presents it as stdin.
Definition: executor.cpp:160
debughelper.h
QtPassSettings::isUsePass
static bool isUsePass(const bool &defaultValue=QVariant().toBool())
Definition: qtpasssettings.cpp:149
StoreModel
The QSortFilterProxyModel for handling filesystem searches.
Definition: storemodel.h:12
Util::checkConfig
static bool checkConfig()
Util::checkConfig do we have prequisite settings?
Definition: util.cpp:142
dbg
#define dbg()
Definition: debughelper.h:7
Util::findPasswordStore
static QString findPasswordStore()
Util::findPasswordStore look for common .password-store folder location.
Definition: util.cpp:56
Util::getDir
static QString getDir(const QModelIndex &index, bool forPass, const QFileSystemModel &model, const StoreModel &storeModel)
Util::getDir get selectd folder path.
Definition: util.cpp:160
QtPassSettings::getPassStore
static QString getPassStore(const QString &defaultValue=QVariant().toString())
Definition: qtpasssettings.cpp:254
util.h
Util::copyDir
static void copyDir(const QString &src, const QString &dest)
Util::copyDir.
Definition: util.cpp:182
qtpasssettings.h