• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IJHack / QtPass / 23804171636

31 Mar 2026 02:59PM UTC coverage: 19.833% (+0.02%) from 19.817%
23804171636

Pull #870

github

web-flow
Merge eca8eff03 into 3a62426af
Pull Request #870: fix: modernize util.cpp - use range-based for loop

5 of 5 new or added lines in 1 file covered. (100.0%)

3 existing lines in 1 file now uncovered.

1000 of 5042 relevant lines covered (19.83%)

7.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.83
/src/util.cpp
1
// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "util.h"
4
#include <QDir>
5
#include <QFileInfo>
6
#ifdef Q_OS_WIN
7
#include <windows.h>
8
#else
9
#include <sys/time.h>
10
#endif
11
#include "qtpasssettings.h"
12

13
#ifdef QT_DEBUG
14
#include "debughelper.h"
15
#endif
16

17
QProcessEnvironment Util::_env;
18
bool Util::_envInitialised = false;
19

20
/**
21
 * @brief Util::initialiseEnvironment set the correct PATH for use with gpg, git
22
 * etc.
23
 */
24
void Util::initialiseEnvironment() {
5✔
25
  if (!_envInitialised) {
5✔
26
    _env = QProcessEnvironment::systemEnvironment();
1✔
27
#ifdef __APPLE__
28
    QString path = _env.value("PATH");
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
#ifdef Q_OS_WIN
37
    QString path = _env.value("PATH");
38
    if (!path.contains("C:\\Program Files\\WinGPG\\x86") &&
39
        QFile("C:\\Program Files\\WinGPG\\x86").exists())
40
      path += ";C:\\Program Files\\WinGPG\\x86";
41
    if (!path.contains("C:\\Program Files\\GnuPG\\bin") &&
42
        QFile("C:\\Program Files\\GnuPG\\bin").exists())
43
      path += ";C:\\Program Files\\GnuPG\\bin";
44
    _env.insert("PATH", path);
45
#endif
46
#ifdef QT_DEBUG
47
    dbg() << _env.value("PATH");
48
#endif
49
    _envInitialised = true;
1✔
50
  }
51
}
5✔
52

53
/**
54
 * @brief Util::findPasswordStore look for common .password-store folder
55
 * location.
56
 * @return
57
 */
58
auto Util::findPasswordStore() -> QString {
2✔
59
  QString path;
2✔
60
  initialiseEnvironment();
2✔
61
  if (_env.contains("PASSWORD_STORE_DIR")) {
4✔
UNCOV
62
    path = _env.value("PASSWORD_STORE_DIR");
×
63
  } else {
64
#ifdef Q_OS_WIN
65
    path = QDir::homePath() + QDir::separator() + "password-store" +
66
           QDir::separator();
67
#else
68
    path = QDir::homePath() + QDir::separator() + ".password-store" +
4✔
69
           QDir::separator();
70
#endif
71
  }
72
  return Util::normalizeFolderPath(path);
4✔
73
}
74

75
/**
76
 * @brief Util::normalizeFolderPath let's always end folders with a
77
 * QDir::separator()
78
 * @param path
79
 * @return
80
 */
81
auto Util::normalizeFolderPath(QString path) -> QString {
11✔
82
  if (!path.endsWith("/") && !path.endsWith(QDir::separator())) {
22✔
83
    path += QDir::separator();
6✔
84
  }
85
  return QDir::toNativeSeparators(path);
11✔
86
}
87

88
/**
89
 * @brief Util::findBinaryInPath search for executables.
90
 * @param binary
91
 * @return
92
 */
93
auto Util::findBinaryInPath(QString binary) -> QString {
3✔
94
  initialiseEnvironment();
3✔
95

96
  QString ret;
3✔
97

98
  binary.prepend(QDir::separator());
3✔
99

100
  if (_env.contains("PATH")) {
6✔
101
    QString path = _env.value("PATH");
6✔
102

103
    QStringList entries;
3✔
104
#ifndef Q_OS_WIN
105
    entries = path.split(':');
6✔
106
    if (entries.length() < 2) {
3✔
107
#endif
UNCOV
108
      entries = path.split(';');
×
109
#ifndef Q_OS_WIN
110
    }
111
#endif
112

113
    for (const QString &entryConst : entries) {
57✔
114
      QString fullPath = entryConst + binary;
52✔
115
      QScopedPointer<QFileInfo> qfi(new QFileInfo(fullPath));
52✔
116
#ifdef Q_OS_WIN
117
      if (!qfi->exists()) {
118
        QString fullPathExe = fullPath + ".exe";
119
        qfi.reset(new QFileInfo(fullPathExe));
120
      }
121

122
#endif
123
      if (!qfi->isExecutable()) {
52✔
124
        continue;
125
      }
126

127
      ret = qfi->absoluteFilePath();
2✔
128
      break;
129
    }
130
  }
131
#ifdef Q_OS_WIN
132
  if (ret.isEmpty()) {
133
    binary.remove(0, 1);
134
    binary.prepend("wsl ");
135
    QString out, err;
136
    if (Executor::executeBlocking(binary, {"--version"}, &out, &err) == 0 &&
137
        !out.isEmpty() && err.isEmpty())
138
      ret = binary;
139
  }
140
#endif
141

142
  return ret;
3✔
143
}
144

145
/**
146
 * @brief Util::checkConfig do we have prerequisite settings?
147
 * @return
148
 */
149
auto Util::checkConfig() -> bool {
1✔
150
  return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id"))
2✔
151
              .exists() ||
1✔
152
         (QtPassSettings::isUsePass()
1✔
153
              ? !QtPassSettings::getPassExecutable().startsWith("wsl ") &&
1✔
154
                    !QFile(QtPassSettings::getPassExecutable()).exists()
1✔
155
              : !QtPassSettings::getGpgExecutable().startsWith("wsl ") &&
1✔
156
                    !QFile(QtPassSettings::getGpgExecutable()).exists());
2✔
157
}
158

159
/**
160
 * @brief Util::getDir get selected folder path
161
 * @param index
162
 * @param forPass short or full path
163
 * @param model the filesystem model to operate on
164
 * @param storeModel our storemodel to operate on
165
 * @return path
166
 */
167
auto Util::getDir(const QModelIndex &index, bool forPass,
3✔
168
                  const QFileSystemModel &model, const StoreModel &storeModel)
169
    -> QString {
170
  QString abspath =
171
      QDir(QtPassSettings::getPassStore()).absolutePath() + QDir::separator();
9✔
172
  if (!index.isValid()) {
173
    return forPass ? "" : abspath;
2✔
174
  }
175
  QFileInfo info = model.fileInfo(storeModel.mapToSource(index));
1✔
176
  QString filePath =
177
      (info.isFile() ? info.absolutePath() : info.absoluteFilePath());
1✔
178
  if (forPass) {
1✔
UNCOV
179
    filePath = QDir(abspath).relativeFilePath(filePath);
×
180
  }
181
  filePath += QDir::separator();
1✔
182
  return filePath;
183
}
1✔
184

185
/**
186
 * @brief Returns a regex to match .gpg file extensions.
187
 * @return Reference to static regex
188
 */
189
auto Util::endsWithGpg() -> const QRegularExpression & {
22✔
190
  static const QRegularExpression expr{"\\.gpg$"};
22✔
191
  return expr;
22✔
192
}
193

194
/**
195
 * @brief Returns a regex to match URL protocols.
196
 * @return Reference to static regex
197
 */
198
auto Util::protocolRegex() -> const QRegularExpression & {
3✔
199
  static const QRegularExpression regex{
200
      "((?:https?|ftp|ssh|sftp|ftps|webdav|webdavs)://[^\" <>\\)\\]\\[]+)"};
3✔
201
  return regex;
3✔
202
}
203

204
/**
205
 * @brief Returns a regex to match newline characters.
206
 * @return Reference to static regex
207
 */
208
auto Util::newLinesRegex() -> const QRegularExpression & {
2✔
209
  static const QRegularExpression regex{"[\r\n]"};
2✔
210
  return regex;
2✔
211
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc