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  dbg() << entry;
103  if (!qfi->isExecutable())
104  continue;
105 
106  ret = qfi->absoluteFilePath();
107  break;
108  }
109  }
110 
111  return ret;
112 }
113 
119  return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"))
120  .exists() ||
121  (!QFile(QtPassSettings::getPassExecutable()).exists() &&
122  !QFile(QtPassSettings::getGpgExecutable()).exists());
123 }
124 
133 QString Util::getDir(const QModelIndex &index, bool forPass,
134  const QFileSystemModel &model,
135  const StoreModel &storeModel) {
136  QString abspath = QDir(QtPassSettings::getPassStore()).absolutePath() + '/';
137  if (!index.isValid())
138  return forPass ? "" : abspath;
139  QFileInfo info = model.fileInfo(storeModel.mapToSource(index));
140  QString filePath =
141  (info.isFile() ? info.absolutePath() : info.absoluteFilePath());
142  if (forPass) {
143  filePath = QDir(abspath).relativeFilePath(filePath);
144  }
145  filePath += '/';
146  return filePath;
147 }
148 
149 void Util::copyDir(const QString src, const QString dest) {
150  QDir srcDir(src);
151  if (srcDir.exists() == false) {
152  return;
153  }
154  srcDir.mkpath(dest);
155  foreach (QString dir, srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
156  copyDir(src + QDir::separator() + dir, dest + QDir::separator() + dir);
157  }
158 
159  foreach (QString file, srcDir.entryList(QDir::Files)) {
160  QFile::copy(src + QDir::separator() + file,
161  dest + QDir::separator() + file);
162  }
163 }
#define dbg()
Definition: debughelper.h:7
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:149
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:133
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:118
static QString normalizeFolderPath(QString path)
Util::normalizeFolderPath let&#39;s always end folders with a QDir::separator()
Definition: util.cpp:64