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 "debughelper.h"
3 #include <QDir>
4 #include <QFileInfo>
5 #ifdef Q_OS_WIN
6 #include <windows.h>
7 #else
8 #include <sys/time.h>
9 #endif
10 #include "qtpasssettings.h"
11 QProcessEnvironment Util::_env;
12 bool Util::_envInitialised;
13 
18 void Util::initialiseEnvironment() {
19  if (!_envInitialised) {
20  _env = QProcessEnvironment::systemEnvironment();
21 #ifdef __APPLE__
22  // TODO(annejan) checks here
23  QString path = _env.value("PATH");
24 
25  if (!path.contains("/usr/local/MacGPG2/bin") &&
26  QFile("/usr/local/MacGPG2/bin").exists())
27  path += ":/usr/local/MacGPG2/bin";
28  if (!path.contains("/usr/local/bin"))
29  path += ":/usr/local/bin";
30  _env.insert("PATH", path);
31 #endif
32  _envInitialised = true;
33  }
34 }
35 
42  QString path;
43  initialiseEnvironment();
44  if (_env.contains("PASSWORD_STORE_DIR")) {
45  path = _env.value("PASSWORD_STORE_DIR");
46  } else {
47 #ifdef Q_OS_WIN
48  path = QDir::homePath() + QDir::separator() + "password-store" +
49  QDir::separator();
50 #else
51  path = QDir::homePath() + QDir::separator() + ".password-store" +
52  QDir::separator();
53 #endif
54  }
55  return Util::normalizeFolderPath(path);
56 }
57 
64 QString Util::normalizeFolderPath(QString path) {
65  if (!path.endsWith("/") && !path.endsWith(QDir::separator()))
66  path += QDir::separator();
67  return QDir::toNativeSeparators(path);
68 }
69 
75 QString Util::findBinaryInPath(QString binary) {
76  initialiseEnvironment();
77 
78  QString ret = "";
79 
80  binary.prepend(QDir::separator());
81 
82  if (_env.contains("PATH")) {
83  QString path = _env.value("PATH");
84 
85  QStringList entries;
86 #ifndef Q_OS_WIN
87  entries = path.split(':');
88  if (entries.length() < 2) {
89 #endif
90  entries = path.split(';');
91 #ifndef Q_OS_WIN
92  }
93 #endif
94 
95  foreach (QString entry, entries) {
96  QScopedPointer<QFileInfo> qfi(new QFileInfo(entry.append(binary)));
97 #ifdef Q_OS_WIN
98  if (!qfi->exists())
99  qfi.reset(new QFileInfo(entry.append(".exe")));
100 
101 #endif
102  if (!qfi->isExecutable())
103  continue;
104 
105  ret = qfi->absoluteFilePath();
106  break;
107  }
108  }
109 
110  return ret;
111 }
112 
118  return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"))
119  .exists() ||
120  (!QFile(QtPassSettings::getPassExecutable()).exists() &&
121  !QFile(QtPassSettings::getGpgExecutable()).exists());
122 }
123 
132 QString Util::getDir(const QModelIndex &index, bool forPass,
133  const QFileSystemModel &model,
134  const StoreModel &storeModel) {
135  QString abspath = QDir(QtPassSettings::getPassStore()).absolutePath() + '/';
136  if (!index.isValid())
137  return forPass ? "" : abspath;
138  QFileInfo info = model.fileInfo(storeModel.mapToSource(index));
139  QString filePath =
140  (info.isFile() ? info.absolutePath() : info.absoluteFilePath());
141  if (forPass) {
142  filePath = QDir(abspath).relativeFilePath(filePath);
143  }
144  filePath += '/';
145  return filePath;
146 }
147 
148 void Util::copyDir(const QString src, const QString dest) {
149  QDir srcDir(src);
150  if (srcDir.exists() == false) {
151  return;
152  }
153  srcDir.mkpath(dest);
154  foreach (QString dir, srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
155  copyDir(src + QDir::separator() + dir, dest + QDir::separator() + dir);
156  }
157 
158  foreach (QString file, srcDir.entryList(QDir::Files)) {
159  QFile::copy(src + QDir::separator() + file,
160  dest + QDir::separator() + file);
161  }
162 }
static QString findBinaryInPath(QString binary)
Util::findBinaryInPath search for executables.
Definition: util.cpp:75
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:148
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:132
static QString findPasswordStore()
Util::findPasswordStore look for common .password-store folder location.
Definition: util.cpp:41
static bool checkConfig()
Util::checkConfig do we have prequisite settings?
Definition: util.cpp:117
static QString normalizeFolderPath(QString path)
Util::normalizeFolderPath let&#39;s always end folders with a QDir::separator()
Definition: util.cpp:64