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

IJHack / QtPass / 24284394706

11 Apr 2026 02:21PM UTC coverage: 20.88%. First build
24284394706

Pull #968

github

web-flow
Merge 0eb84d47a into 71fc52f07
Pull Request #968: refactor: split UsersDialog constructor into helper methods

0 of 25 new or added lines in 1 file covered. (0.0%)

1106 of 5297 relevant lines covered (20.88%)

7.75 hits per line

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

0.0
/src/usersdialog.cpp
1
// SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "usersdialog.h"
4
#include "qtpasssettings.h"
5
#include "ui_usersdialog.h"
6
#include <QApplication>
7
#include <QCloseEvent>
8
#include <QKeyEvent>
9
#include <QMessageBox>
10
#include <QRegularExpression>
11
#include <QSet>
12
#include <QWidget>
13
#include <utility>
14

15
#ifdef QT_DEBUG
16
#include "debughelper.h"
17
#endif
18
/**
19
 * @brief UsersDialog::UsersDialog basic constructor
20
 * @param parent
21
 */
22
UsersDialog::UsersDialog(QString dir, QWidget *parent)
×
23
    : QDialog(parent), ui(new Ui::UsersDialog), m_dir(std::move(dir)) {
×
24

25
  ui->setupUi(this);
×
26

NEW
27
  restoreDialogState();
×
NEW
28
  if (!loadGpgKeys()) {
×
29
    return;
30
  }
31

NEW
32
  populateList();
×
33

NEW
34
  connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
×
NEW
35
          &UsersDialog::accept);
×
NEW
36
  connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
×
NEW
37
  connect(ui->listWidget, &QListWidget::itemChanged, this,
×
NEW
38
          &UsersDialog::itemChange);
×
39

NEW
40
  ui->lineEdit->setClearButtonEnabled(true);
×
NEW
41
}
×
42

NEW
43
void UsersDialog::restoreDialogState() {
×
44
  QByteArray savedGeometry = QtPassSettings::getDialogGeometry("usersDialog");
×
45
  bool hasSavedGeometry = !savedGeometry.isEmpty();
46
  if (hasSavedGeometry) {
×
47
    restoreGeometry(savedGeometry);
×
48
  }
49
  if (QtPassSettings::isDialogMaximized("usersDialog")) {
×
50
    showMaximized();
×
51
  } else if (hasSavedGeometry) {
×
52
    move(QtPassSettings::getDialogPos("usersDialog"));
×
53
    resize(QtPassSettings::getDialogSize("usersDialog"));
×
54
  }
NEW
55
}
×
56

NEW
57
auto UsersDialog::loadGpgKeys() -> bool {
×
58
  QList<UserInfo> users = QtPassSettings::getPass()->listKeys();
×
59
  if (users.isEmpty()) {
×
NEW
60
    QMessageBox::critical(parentWidget(), tr("Keylist missing"),
×
61
                          tr("Could not fetch list of available GPG keys"));
×
62
    reject();
×
63
    return false;
64
  }
65

66
  QList<UserInfo> secret_keys = QtPassSettings::getPass()->listKeys("", true);
×
67
  QSet<QString> secretKeyIds;
68
  for (const UserInfo &sec : secret_keys) {
×
69
    secretKeyIds.insert(sec.key_id);
×
70
  }
71
  for (auto &user : users) {
×
72
    if (secretKeyIds.contains(user.key_id)) {
×
73
      user.have_secret = true;
×
74
    }
75
  }
76

77
  m_userList = users;
NEW
78
  loadRecipients();
×
79

80
  return true;
81
}
82

NEW
83
void UsersDialog::loadRecipients() {
×
84
  QList<UserInfo> selectedUsers = QtPassSettings::getPass()->listKeys(
NEW
85
      QtPassSettings::getPass()->getRecipientString(
×
NEW
86
          m_dir.isEmpty() ? "" : m_dir, " "));
×
87
  QSet<QString> selectedKeyIds;
NEW
88
  for (const UserInfo &sel : selectedUsers) {
×
89
    selectedKeyIds.insert(sel.key_id);
×
90
  }
NEW
91
  for (auto &user : m_userList) {
×
92
    if (selectedKeyIds.contains(user.key_id)) {
×
93
      user.enabled = true;
×
94
    }
95
  }
96

NEW
97
  int count = 0;
×
NEW
98
  QStringList recipients = QtPassSettings::getPass()->getRecipientString(
×
NEW
99
      m_dir.isEmpty() ? "" : m_dir, " ", &count);
×
100

NEW
101
  if (count > selectedUsers.size()) {
×
102
    QStringList allRecipients = QtPassSettings::getPass()->getRecipientList(
×
103
        m_dir.isEmpty() ? "" : m_dir);
×
104
    QSet<QString> missingKeyRecipients;
105
    for (const QString &recipient : allRecipients) {
×
NEW
106
      if (!missingKeyRecipients.contains(recipient) &&
×
107
          QtPassSettings::getPass()->listKeys(recipient).empty()) {
×
108
        missingKeyRecipients.insert(recipient);
×
109
        UserInfo i;
×
110
        i.enabled = true;
×
111
        i.key_id = recipient;
×
112
        i.name = " ?? " + tr("Key not found in keyring");
×
113
        m_userList.append(i);
114
      }
×
115
    }
116
  }
117
}
×
118

119
/**
120
 * @brief UsersDialog::~UsersDialog basic destructor.
121
 */
122
UsersDialog::~UsersDialog() { delete ui; }
×
123

124
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
125
Q_DECLARE_METATYPE(UserInfo *)
126
Q_DECLARE_METATYPE(const UserInfo *)
127
#endif
128

129
/**
130
 * @brief UsersDialog::accept
131
 */
132
void UsersDialog::accept() {
×
133
  QtPassSettings::getPass()->Init(m_dir, m_userList);
×
134

135
  QDialog::accept();
×
136
}
×
137

138
/**
139
 * @brief UsersDialog::closeEvent save window state on close.
140
 * @param event
141
 */
142
void UsersDialog::closeEvent(QCloseEvent *event) {
×
143
  QtPassSettings::setDialogGeometry("usersDialog", saveGeometry());
×
144
  if (!isMaximized()) {
×
145
    QtPassSettings::setDialogPos("usersDialog", pos());
×
146
    QtPassSettings::setDialogSize("usersDialog", size());
×
147
  }
148
  QtPassSettings::setDialogMaximized("usersDialog", isMaximized());
×
149
  event->accept();
150
}
×
151

152
/**
153
 * @brief UsersDialog::keyPressEvent clear the lineEdit when escape is pressed.
154
 * No action for Enter currently.
155
 * @param event
156
 */
157
void UsersDialog::keyPressEvent(QKeyEvent *event) {
×
158
  switch (event->key()) {
×
159
  case Qt::Key_Escape:
×
160
    ui->lineEdit->clear();
×
161
    break;
×
162
  default:
163
    break;
164
  }
165
}
×
166

167
/**
168
 * @brief UsersDialog::itemChange update the item information.
169
 * @param item
170
 */
171
void UsersDialog::itemChange(QListWidgetItem *item) {
×
172
  if (!item) {
×
173
    return;
×
174
  }
175
  bool ok = false;
×
176
  const int index = item->data(Qt::UserRole).toInt(&ok);
×
177
  if (!ok) {
×
178
#ifdef QT_DEBUG
179
    qWarning() << "UsersDialog::itemChange: invalid user index data for item";
180
#endif
181
    return;
182
  }
183
  if (index < 0 || index >= m_userList.size()) {
×
184
#ifdef QT_DEBUG
185
    qWarning() << "UsersDialog::itemChange: user index out of range:" << index
186
               << "valid range is [0," << (m_userList.size() - 1) << "]";
187
#endif
188
    return;
189
  }
190
  m_userList[index].enabled = item->checkState() == Qt::Checked;
×
191
}
192

193
/**
194
 * @brief UsersDialog::populateList update the view based on filter options
195
 * (such as searching).
196
 * @param filter
197
 */
198
void UsersDialog::populateList(const QString &filter) {
×
199
  static QString lastFilter;
×
200
  static QRegularExpression cachedNameFilter;
×
201
  if (filter != lastFilter) {
×
202
    lastFilter = filter;
×
203
    cachedNameFilter = QRegularExpression(
×
204
        QRegularExpression::wildcardToRegularExpression("*" + filter + "*"),
×
205
        QRegularExpression::CaseInsensitiveOption);
206
  }
207
  const QRegularExpression &nameFilter = cachedNameFilter;
208
  ui->listWidget->clear();
×
209

210
  for (int i = 0; i < m_userList.size(); ++i) {
×
211
    const auto &user = m_userList.at(i);
212
    if (!passesFilter(user, filter, nameFilter)) {
×
213
      continue;
×
214
    }
215

216
    auto *item = new QListWidgetItem(buildUserText(user), ui->listWidget);
×
217
    applyUserStyling(item, user);
×
218
    item->setCheckState(user.enabled ? Qt::Checked : Qt::Unchecked);
×
219
    item->setData(Qt::UserRole, QVariant::fromValue(i));
×
220
    ui->listWidget->addItem(item);
×
221
  }
222
}
×
223

224
/**
225
 * @brief Checks if a user passes the filter criteria.
226
 * @param user User to check
227
 * @param filter Filter string
228
 * @param nameFilter Compiled name filter regex
229
 * @return true if user passes filter
230
 */
231
bool UsersDialog::passesFilter(const UserInfo &user, const QString &filter,
×
232
                               const QRegularExpression &nameFilter) const {
233
  if (!filter.isEmpty() && !nameFilter.match(user.name).hasMatch()) {
×
234
    return false;
235
  }
236
  if (!user.isValid() && !ui->checkBox->isChecked()) {
×
237
    return false;
238
  }
239
  const bool expired = isUserExpired(user);
×
240
  return !(expired && !ui->checkBox->isChecked());
×
241
}
242

243
/**
244
 * @brief Checks if a user's key has expired.
245
 * @param user User to check
246
 * @return true if user's key is expired
247
 */
248
auto UsersDialog::isUserExpired(const UserInfo &user) const -> bool {
×
249
  return user.expiry.toSecsSinceEpoch() > 0 &&
×
250
         QDateTime::currentDateTime() > user.expiry;
×
251
}
252

253
/**
254
 * @brief Builds display text for a user.
255
 * @param user User to format
256
 * @return Formatted user text
257
 */
258
QString UsersDialog::buildUserText(const UserInfo &user) const {
×
259
  QString text = user.name + "\n" + user.key_id;
×
260
  if (user.created.toSecsSinceEpoch() > 0) {
×
261
    text += " " + tr("created") + " " +
×
262
            QLocale::system().toString(user.created, QLocale::ShortFormat);
×
263
  }
264
  if (user.expiry.toSecsSinceEpoch() > 0) {
×
265
    text += " " + tr("expires") + " " +
×
266
            QLocale::system().toString(user.expiry, QLocale::ShortFormat);
×
267
  }
268
  return text;
×
269
}
270

271
/**
272
 * @brief Applies visual styling to a user list item based on key status.
273
 * @param item List widget item to style
274
 * @param user User whose status determines styling
275
 */
276
void UsersDialog::applyUserStyling(QListWidgetItem *item,
×
277
                                   const UserInfo &user) const {
278
  const QString originalText = item->text();
×
279
  if (user.have_secret) {
×
280
    const QPalette palette = QApplication::palette();
×
281
    item->setForeground(palette.color(QPalette::Link));
×
282
    QFont font = item->font();
×
283
    font.setBold(true);
284
    item->setFont(font);
×
285
  } else if (!user.isValid()) {
×
286
    item->setBackground(Qt::darkRed);
×
287
    item->setForeground(Qt::white);
×
288
    item->setText(tr("[INVALID] ") + originalText);
×
289
  } else if (isUserExpired(user)) {
×
290
    item->setForeground(Qt::darkRed);
×
291
    item->setText(tr("[EXPIRED] ") + originalText);
×
292
  } else if (!user.fullyValid()) {
×
293
    item->setBackground(Qt::darkYellow);
×
294
    item->setForeground(Qt::white);
×
295
    item->setText(tr("[PARTIAL] ") + originalText);
×
296
  } else {
297
    item->setText(originalText);
×
298
  }
299
}
×
300

301
/**
302
 * @brief UsersDialog::on_lineEdit_textChanged typing in the searchbox.
303
 * @param filter
304
 */
305
void UsersDialog::on_lineEdit_textChanged(const QString &filter) {
×
306
  populateList(filter);
×
307
}
×
308

309
/**
310
 * @brief UsersDialog::on_checkBox_clicked filtering.
311
 */
312
void UsersDialog::on_checkBox_clicked() { populateList(ui->lineEdit->text()); }
×
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