QtPass  1.2.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  // TODO(annejan) checks here
27  QString path = _env.value("PATH");
28 
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  _envInitialised = true;
37  }
38 }
39 
46  QString path;
47  initialiseEnvironment();
48  if (_env.contains("PASSWORD_STORE_DIR")) {
49  path = _env.value("PASSWORD_STORE_DIR");
50  } else {
51 #ifdef Q_OS_WIN
52  path = QDir::homePath() + QDir::separator() + "password-store" +
53  QDir::separator();
54 #else
55  path = QDir::homePath() + QDir::separator() + ".password-store" +
56  QDir::separator();
57 #endif
58  }
59  return Util::normalizeFolderPath(path);
60 }
61 
68 QString Util::normalizeFolderPath(QString path) {
69  if (!path.endsWith("/") && !path.endsWith(QDir::separator()))
70  path += QDir::separator();
71  return QDir::toNativeSeparators(path);
72 }
73 
79 QString Util::findBinaryInPath(QString binary) {
80  initialiseEnvironment();
81 
82  QString ret = "";
83 
84  binary.prepend(QDir::separator());
85 
86  if (_env.contains("PATH")) {
87  QString path = _env.value("PATH");
88 
89  QStringList entries;
90 #ifndef Q_OS_WIN
91  entries = path.split(':');
92  if (entries.length() < 2) {
93 #endif
94  entries = path.split(';');
95 #ifndef Q_OS_WIN
96  }
97 #endif
98 
99  foreach (QString entry, entries) {
100  QScopedPointer<QFileInfo> qfi(new QFileInfo(entry.append(binary)));
101 #ifdef Q_OS_WIN
102  if (!qfi->exists())
103  qfi.reset(new QFileInfo(entry.append(".exe")));
104 
105 #endif
106  if (!qfi->isExecutable())
107  continue;
108 
109  ret = qfi->absoluteFilePath();
110  break;
111  }
112  }
113 #ifdef Q_OS_WIN
114  if (ret.isEmpty()) {
115  binary.remove(0, 1);
116  binary.prepend("wsl ");
117  QString out, err;
118  if (Executor::executeBlocking(binary, {"--version"}, &out, &err) == 0 &&
119  !out.isEmpty() && err.isEmpty())
120  ret = binary;
121  }
122 #endif
123 
124  return ret;
125 }
126 
132  return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"))
133  .exists() ||
134  (!QtPassSettings::getPassExecutable().startsWith("wsl ") &&
135  !QFile(QtPassSettings::getPassExecutable()).exists() &&
136  !QtPassSettings::getGpgExecutable().startsWith("wsl ") &&
137  !QFile(QtPassSettings::getGpgExecutable()).exists());
138 }
139 
148 QString Util::getDir(const QModelIndex &index, bool forPass,
149  const QFileSystemModel &model,
150  const StoreModel &storeModel) {
151  QString abspath =
152  QDir(QtPassSettings::getPassStore()).absolutePath() + QDir::separator();
153  if (!index.isValid())
154  return forPass ? "" : abspath;
155  QFileInfo info = model.fileInfo(storeModel.mapToSource(index));
156  QString filePath =
157  (info.isFile() ? info.absolutePath() : info.absoluteFilePath());
158  if (forPass) {
159  filePath = QDir(abspath).relativeFilePath(filePath);
160  }
161  filePath += QDir::separator();
162  return filePath;
163 }
164 
165 void Util::copyDir(const QString src, const QString dest) {
166  QDir srcDir(src);
167  if (srcDir.exists() == false) {
168  return;
169  }
170  srcDir.mkpath(dest);
171  foreach (QString dir, srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
172  copyDir(src + QDir::separator() + dir, dest + QDir::separator() + dir);
173  }
174 
175  foreach (QString file, srcDir.entryList(QDir::Files)) {
176  QFile::copy(src + QDir::separator() + file,
177  dest + QDir::separator() + file);
178  }
179 }
static QString findBinaryInPath(QString binary)
Util::findBinaryInPath search for executables.
Definition: util.cpp:79
static QString getPassStore(const QString &defaultValue=QVariant().toString())
static QString getPassExecutable(const QString &defaultValue=QVariant().toString())
The QSortFilterProxyModel for handling filesystem searches.
Definition: storemodel.h:12
static void copyDir(const QString src, const QString dest)
Definition: util.cpp:165
static QString getGpgExecutable(const QString &defaultValue=QVariant().toString())
static QString getDir(const QModelIndex &index, bool forPass, const QFileSystemModel &model, const StoreModel &storeModel)
Util::getDir get selectd folder path.
Definition: util.cpp:148
static QString findPasswordStore()
Util::findPasswordStore look for common .password-store folder location.
Definition: util.cpp:45
static bool checkConfig()
Util::checkConfig do we have prequisite settings?
Definition: util.cpp:131
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
static QString normalizeFolderPath(QString path)
Util::normalizeFolderPath let&#39;s always end folders with a QDir::separator()
Definition: util.cpp:68