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::sys&temEnvironment();
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 
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 
136 QString Util::getDir(const QModelIndex &index, bool forPass,
137  const QFileSystemModel &model,
138  const StoreModel &storeModel) {
139  QString abspath = QDir(QtPassSettings::getPassStore()).absolutePath() + '/';
140  if (!index.isValid())
141  return forPass ? "" : abspath;
142  QFileInfo info = model.fileInfo(storeModel.mapToSource(index));
143  QString filePath =
144  (info.isFile() ? info.absolutePath() : info.absoluteFilePath());
145  if (forPass) {
146  filePath = QDir(abspath).relativeFilePath(filePath);
147  }
148  filePath += '/';
149  return filePath;
150 }
151 
152 void Util::copyDir(const QString& src, const QString& dest) {
153  QDir srcDir(src);
154  if (!srcDir.exists()) {
155  return;
156  }
157  srcDir.mkpath(dest);
158  foreach (QString dir, srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
159  copyDir(src + QDir::separator() + dir, dest + QDir::separator() + dir);
160  }
161 
162  foreach (QString file, srcDir.entryList(QDir::Files)) {
163  QFile::copy(src + QDir::separator() + file,
164  dest + QDir::separator() + file);
165  }
166 }
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:152
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:136
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:121
static QString normalizeFolderPath(QString path)
Util::normalizeFolderPath let&#39;s always end folders with a QDir::separator()
Definition: util.cpp:68