QtPass  1.2.1
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 #else
10 #include <sys/time.h>
11 #endif
12 #include "qtpasssettings.h"
13 QProcessEnvironment Util::_env;
14 bool Util::_envInitialised;
15 
20 void Util::initialiseEnvironment() {
21  if (!_envInitialised) {
22  _env = QProcessEnvironment::systemEnvironment();
23 #ifdef __APPLE__
24  // TODO(annejan) checks here
25  QString path = _env.value("PATH");
26 
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  _envInitialised = true;
35  }
36 }
37 
44  QString path;
45  initialiseEnvironment();
46  if (_env.contains("PASSWORD_STORE_DIR")) {
47  path = _env.value("PASSWORD_STORE_DIR");
48  } else {
49 #ifdef Q_OS_WIN
50  path = QDir::homePath() + QDir::separator() + "password-store" +
51  QDir::separator();
52 #else
53  path = QDir::homePath() + QDir::separator() + ".password-store" +
54  QDir::separator();
55 #endif
56  }
57  return Util::normalizeFolderPath(path);
58 }
59 
66 QString Util::normalizeFolderPath(QString path) {
67  if (!path.endsWith("/") && !path.endsWith(QDir::separator()))
68  path += QDir::separator();
69  return QDir::toNativeSeparators(path);
70 }
71 
77 QString Util::findBinaryInPath(QString binary) {
78  initialiseEnvironment();
79 
80  QString ret = "";
81 
82  binary.prepend(QDir::separator());
83 
84  if (_env.contains("PATH")) {
85  QString path = _env.value("PATH");
86 
87  QStringList entries;
88 #ifndef Q_OS_WIN
89  entries = path.split(':');
90  if (entries.length() < 2) {
91 #endif
92  entries = path.split(';');
93 #ifndef Q_OS_WIN
94  }
95 #endif
96 
97  foreach (QString entry, entries) {
98  QScopedPointer<QFileInfo> qfi(new QFileInfo(entry.append(binary)));
99 #ifdef Q_OS_WIN
100  if (!qfi->exists())
101  qfi.reset(new QFileInfo(entry.append(".exe")));
102 
103 #endif
104  dbg() << entry;
105  if (!qfi->isExecutable())
106  continue;
107 
108  ret = qfi->absoluteFilePath();
109  break;
110  }
111  }
112 
113  return ret;
114 }
115 
121  return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"))
122  .exists() ||
123  (!QFile(QtPassSettings::getPassExecutable()).exists() &&
124  !QFile(QtPassSettings::getGpgExecutable()).exists());
125 }
126 
131 void Util::qSleep(int ms) {
132 #ifdef Q_OS_WIN
133  Sleep(uint(ms));
134 #else
135  struct timespec ts = {ms / 1000, (ms % 1000) * 1000 * 1000};
136  nanosleep(&ts, NULL);
137 #endif
138 }
139 
148 QString Util::getDir(const QModelIndex &index, bool forPass,
149  const QFileSystemModel &model,
150  const StoreModel &storeModel) {
151  QString abspath = QDir(QtPassSettings::getPassStore()).absolutePath() + '/';
152  if (!index.isValid())
153  return forPass ? "" : abspath;
154  QFileInfo info = model.fileInfo(storeModel.mapToSource(index));
155  QString filePath =
156  (info.isFile() ? info.absolutePath() : info.absoluteFilePath());
157  if (forPass) {
158  filePath = QDir(abspath).relativeFilePath(filePath);
159  }
160  filePath += '/';
161  return filePath;
162 }
163 
164 void Util::copyDir(const QString src, const QString dest) {
165  QDir srcDir(src);
166  if (srcDir.exists() == false) {
167  return;
168  }
169  srcDir.mkpath(dest);
170  foreach (QString dir, srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
171  copyDir(src + QDir::separator() + dir, dest + QDir::separator() + dir);
172  }
173 
174  foreach (QString file, srcDir.entryList(QDir::Files)) {
175  QFile::copy(src + QDir::separator() + file,
176  dest + QDir::separator() + file);
177  }
178 }
#define dbg()
Definition: debughelper.h:7
static QString findBinaryInPath(QString binary)
Util::findBinaryInPath search for executables.
Definition: util.cpp:77
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:16
static void copyDir(const QString src, const QString dest)
Definition: util.cpp:164
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 void qSleep(int ms)
Util::qSleep because . . windows sleep.
Definition: util.cpp:131
static QString findPasswordStore()
Util::findPasswordStore look for common .password-store folder location.
Definition: util.cpp:43
static bool checkConfig()
Util::checkConfig do we have prequisite settings?
Definition: util.cpp:120
static QString normalizeFolderPath(QString path)
Util::normalizeFolderPath let&#39;s always end folders with a QDir::separator()
Definition: util.cpp:66