QtPass  1.2.0-pre
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 "debughelper.h"
3 #include <QDir>
4 #include <QFileInfo>
5 #include <QProcessEnvironment>
6 #include <QString>
7 #ifdef Q_OS_WIN
8 #include <windows.h>
9 #include <bcrypt.h>
10 #else
11 #include <sys/time.h>
12 #endif
13 #include "qtpasssettings.h"
14 QProcessEnvironment Util::_env;
15 bool Util::_envInitialised;
16 
21 void Util::initialiseEnvironment() {
22  if (!_envInitialised) {
23  _env = QProcessEnvironment::systemEnvironment();
24 #ifdef __APPLE__
25  // TODO(annejan) checks here
26  QString path = _env.value("PATH");
27 
28  if (!path.contains("/usr/local/MacGPG2/bin") &&
29  QFile("/usr/local/MacGPG2/bin").exists())
30  path += ":/usr/local/MacGPG2/bin";
31  if (!path.contains("/usr/local/bin"))
32  path += ":/usr/local/bin";
33  _env.insert("PATH", path);
34 #endif
35  _envInitialised = true;
36  }
37 }
38 
45  QString path;
46  initialiseEnvironment();
47  if (_env.contains("PASSWORD_STORE_DIR")) {
48  path = _env.value("PASSWORD_STORE_DIR");
49  } else {
50 #ifdef Q_OS_WIN
51  path = QDir::homePath() + QDir::separator() + "password-store" +
52  QDir::separator();
53 #else
54  path = QDir::homePath() + QDir::separator() + ".password-store" +
55  QDir::separator();
56 #endif
57  }
58  return Util::normalizeFolderPath(path);
59 }
60 
67 QString Util::normalizeFolderPath(QString path) {
68  if (!path.endsWith("/") && !path.endsWith(QDir::separator()))
69  path += QDir::separator();
70  return QDir::toNativeSeparators(path);
71 }
72 
78 QString Util::findBinaryInPath(QString binary) {
79  initialiseEnvironment();
80 
81  QString ret = "";
82 
83  binary.prepend(QDir::separator());
84 
85  if (_env.contains("PATH")) {
86  QString path = _env.value("PATH");
87 
88  QStringList entries;
89 #ifndef Q_OS_WIN
90  entries = path.split(':');
91  if (entries.length() < 2) {
92 #endif
93  entries = path.split(';');
94 #ifndef Q_OS_WIN
95  }
96 #endif
97 
98  foreach (QString entry, entries) {
99  QScopedPointer<QFileInfo> qfi(new QFileInfo(entry.append(binary)));
100 #ifdef Q_OS_WIN
101  if (!qfi->exists())
102  qfi.reset(new QFileInfo(entry.append(".exe")));
103 
104 #endif
105  dbg() << entry;
106  if (!qfi->isExecutable())
107  continue;
108 
109  ret = qfi->absoluteFilePath();
110  break;
111  }
112  }
113 
114  return ret;
115 }
116 
122  return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"))
123  .exists() ||
124  (!QFile(QtPassSettings::getPassExecutable()).exists() &&
125  !QFile(QtPassSettings::getGpgExecutable()).exists());
126 }
127 
132 void Util::qSleep(int ms) {
133 #ifdef Q_OS_WIN
134  Sleep(uint(ms));
135 #else
136  struct timespec ts = {ms / 1000, (ms % 1000) * 1000 * 1000};
137  nanosleep(&ts, NULL);
138 #endif
139 }
140 
149 QString Util::getDir(const QModelIndex &index, bool forPass,
150  const QFileSystemModel &model,
151  const StoreModel &storeModel) {
152  QString abspath = QDir(QtPassSettings::getPassStore()).absolutePath() + '/';
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 += '/';
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 }
180 
181 int Util::rand() {
182 #ifdef Q_OS_WIN
183  quint32 ret = 0;
184  if (FAILED(BCryptGenRandom(NULL, (PUCHAR)&ret, sizeof(ret),
185  BCRYPT_USE_SYSTEM_PREFERRED_RNG)))
186  return qrand();
187  return ret % RAND_MAX;
188 #else
189  return qrand();
190 #endif
191 }
#define dbg()
Definition: debughelper.h:7
static QString findBinaryInPath(QString binary)
Util::findBinaryInPath search for executables.
Definition: util.cpp:78
static QString getPassStore(const QString &defaultValue=QVariant().toString())
static int rand()
Definition: util.cpp:181
static QString getPassExecutable(const QString &defaultValue=QVariant().toString())
The QSortFilterProxyModel for handling filesystem searches.
Definition: storemodel.h:16
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:149
static void qSleep(int ms)
Util::qSleep because . . windows sleep.
Definition: util.cpp:132
static QString findPasswordStore()
Util::findPasswordStore look for common .password-store folder location.
Definition: util.cpp:44
static bool checkConfig()
Util::checkConfig do we have prequisite settings?
Definition: util.cpp:121
static QString normalizeFolderPath(QString path)
Util::normalizeFolderPath let&#39;s always end folders with a QDir::separator()
Definition: util.cpp:67