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

IJHack / QtPass / 24285449848

11 Apr 2026 03:20PM UTC coverage: 20.848% (-0.06%) from 20.907%
24285449848

push

github

web-flow
refactor: split UsersDialog constructor into helper methods (#968)

* refactor: split UsersDialog constructor into helper methods

* refactor: improve UsersDialog constructor split

* refactor: add docstrings and fix duplicate getRecipientString call

* fix: connect signals even when GPG keys fail to load

* fix: only wire reject button when GPG keys fail

---------

Co-authored-by: Anne Jan Brouwer <harrie@annejan.com>

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

6 existing lines in 1 file now uncovered.

1106 of 5305 relevant lines covered (20.85%)

7.74 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()) {
×
NEW
29
    connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
×
NEW
30
    return;
×
31
  }
32

NEW
33
  loadRecipients();
×
NEW
34
  populateList();
×
35

NEW
36
  connectSignals();
×
NEW
37
}
×
38

NEW
39
void UsersDialog::connectSignals() {
×
NEW
40
  connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
×
NEW
41
          &UsersDialog::accept);
×
NEW
42
  connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
×
NEW
43
  connect(ui->listWidget, &QListWidget::itemChanged, this,
×
NEW
44
          &UsersDialog::itemChange);
×
45

NEW
46
  ui->lineEdit->setClearButtonEnabled(true);
×
NEW
47
}
×
48

NEW
49
void UsersDialog::restoreDialogState() {
×
50
  /**
51
   * @brief Restore dialog geometry from settings.
52
   */
UNCOV
53
  QByteArray savedGeometry = QtPassSettings::getDialogGeometry("usersDialog");
×
54
  bool hasSavedGeometry = !savedGeometry.isEmpty();
55
  if (hasSavedGeometry) {
×
56
    restoreGeometry(savedGeometry);
×
57
  }
58
  if (QtPassSettings::isDialogMaximized("usersDialog")) {
×
59
    showMaximized();
×
60
  } else if (hasSavedGeometry) {
×
61
    move(QtPassSettings::getDialogPos("usersDialog"));
×
62
    resize(QtPassSettings::getDialogSize("usersDialog"));
×
63
  }
NEW
64
}
×
65

NEW
66
auto UsersDialog::loadGpgKeys() -> bool {
×
67
  /**
68
   * @brief Load GPG keys and determine secret key status.
69
   * @return true if successful, false if keys could not be loaded.
70
   */
71
  QList<UserInfo> users = QtPassSettings::getPass()->listKeys();
×
72
  if (users.isEmpty()) {
×
NEW
73
    QMessageBox::critical(parentWidget(), tr("Keylist missing"),
×
74
                          tr("Could not fetch list of available GPG keys"));
×
75
    reject();
×
76
    return false;
77
  }
78

NEW
79
  markSecretKeys(users);
×
80

81
  m_userList = users;
NEW
82
  return true;
×
83
}
84

NEW
85
void UsersDialog::markSecretKeys(QList<UserInfo> &users) {
×
86
  /**
87
   * @brief Mark which keys have secret counterparts.
88
   * @param users List of users to mark.
89
   */
UNCOV
90
  QList<UserInfo> secret_keys = QtPassSettings::getPass()->listKeys("", true);
×
91
  QSet<QString> secretKeyIds;
92
  for (const UserInfo &sec : secret_keys) {
×
93
    secretKeyIds.insert(sec.key_id);
×
94
  }
95
  for (auto &user : users) {
×
96
    if (secretKeyIds.contains(user.key_id)) {
×
97
      user.have_secret = true;
×
98
    }
99
  }
NEW
100
}
×
101

NEW
102
void UsersDialog::loadRecipients() {
×
103
  /**
104
   * @brief Load recipients and handle missing keys.
105
   */
UNCOV
106
  int count = 0;
×
UNCOV
107
  QStringList recipients = QtPassSettings::getPass()->getRecipientString(
×
108
      m_dir.isEmpty() ? "" : m_dir, " ", &count);
×
109

110
  QList<UserInfo> selectedUsers =
NEW
111
      QtPassSettings::getPass()->listKeys(recipients);
×
112
  QSet<QString> selectedKeyIds;
NEW
113
  for (const UserInfo &sel : selectedUsers) {
×
114
    selectedKeyIds.insert(sel.key_id);
×
115
  }
NEW
116
  for (auto &user : m_userList) {
×
117
    if (selectedKeyIds.contains(user.key_id)) {
×
118
      user.enabled = true;
×
119
    }
120
  }
121

NEW
122
  if (count > selectedUsers.size()) {
×
UNCOV
123
    QStringList allRecipients = QtPassSettings::getPass()->getRecipientList(
×
124
        m_dir.isEmpty() ? "" : m_dir);
×
125
    QSet<QString> missingKeyRecipients;
126
    for (const QString &recipient : allRecipients) {
×
NEW
127
      if (!missingKeyRecipients.contains(recipient) &&
×
128
          QtPassSettings::getPass()->listKeys(recipient).empty()) {
×
129
        missingKeyRecipients.insert(recipient);
×
130
        UserInfo i;
×
131
        i.enabled = true;
×
132
        i.key_id = recipient;
×
133
        i.name = " ?? " + tr("Key not found in keyring");
×
134
        m_userList.append(i);
135
      }
×
136
    }
137
  }
UNCOV
138
}
×
139

140
/**
141
 * @brief UsersDialog::~UsersDialog basic destructor.
142
 */
143
UsersDialog::~UsersDialog() { delete ui; }
×
144

145
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
146
Q_DECLARE_METATYPE(UserInfo *)
147
Q_DECLARE_METATYPE(const UserInfo *)
148
#endif
149

150
/**
151
 * @brief UsersDialog::accept
152
 */
153
void UsersDialog::accept() {
×
154
  QtPassSettings::getPass()->Init(m_dir, m_userList);
×
155

156
  QDialog::accept();
×
157
}
×
158

159
/**
160
 * @brief UsersDialog::closeEvent save window state on close.
161
 * @param event
162
 */
163
void UsersDialog::closeEvent(QCloseEvent *event) {
×
164
  QtPassSettings::setDialogGeometry("usersDialog", saveGeometry());
×
165
  if (!isMaximized()) {
×
166
    QtPassSettings::setDialogPos("usersDialog", pos());
×
167
    QtPassSettings::setDialogSize("usersDialog", size());
×
168
  }
169
  QtPassSettings::setDialogMaximized("usersDialog", isMaximized());
×
170
  event->accept();
171
}
×
172

173
/**
174
 * @brief UsersDialog::keyPressEvent clear the lineEdit when escape is pressed.
175
 * No action for Enter currently.
176
 * @param event
177
 */
178
void UsersDialog::keyPressEvent(QKeyEvent *event) {
×
179
  switch (event->key()) {
×
180
  case Qt::Key_Escape:
×
181
    ui->lineEdit->clear();
×
182
    break;
×
183
  default:
184
    break;
185
  }
186
}
×
187

188
/**
189
 * @brief UsersDialog::itemChange update the item information.
190
 * @param item
191
 */
192
void UsersDialog::itemChange(QListWidgetItem *item) {
×
193
  if (!item) {
×
194
    return;
×
195
  }
196
  bool ok = false;
×
197
  const int index = item->data(Qt::UserRole).toInt(&ok);
×
198
  if (!ok) {
×
199
#ifdef QT_DEBUG
200
    qWarning() << "UsersDialog::itemChange: invalid user index data for item";
201
#endif
202
    return;
203
  }
204
  if (index < 0 || index >= m_userList.size()) {
×
205
#ifdef QT_DEBUG
206
    qWarning() << "UsersDialog::itemChange: user index out of range:" << index
207
               << "valid range is [0," << (m_userList.size() - 1) << "]";
208
#endif
209
    return;
210
  }
211
  m_userList[index].enabled = item->checkState() == Qt::Checked;
×
212
}
213

214
/**
215
 * @brief UsersDialog::populateList update the view based on filter options
216
 * (such as searching).
217
 * @param filter
218
 */
219
void UsersDialog::populateList(const QString &filter) {
×
220
  static QString lastFilter;
×
221
  static QRegularExpression cachedNameFilter;
×
222
  if (filter != lastFilter) {
×
223
    lastFilter = filter;
×
224
    cachedNameFilter = QRegularExpression(
×
225
        QRegularExpression::wildcardToRegularExpression("*" + filter + "*"),
×
226
        QRegularExpression::CaseInsensitiveOption);
227
  }
228
  const QRegularExpression &nameFilter = cachedNameFilter;
229
  ui->listWidget->clear();
×
230

231
  for (int i = 0; i < m_userList.size(); ++i) {
×
232
    const auto &user = m_userList.at(i);
233
    if (!passesFilter(user, filter, nameFilter)) {
×
234
      continue;
×
235
    }
236

237
    auto *item = new QListWidgetItem(buildUserText(user), ui->listWidget);
×
238
    applyUserStyling(item, user);
×
239
    item->setCheckState(user.enabled ? Qt::Checked : Qt::Unchecked);
×
240
    item->setData(Qt::UserRole, QVariant::fromValue(i));
×
241
    ui->listWidget->addItem(item);
×
242
  }
243
}
×
244

245
/**
246
 * @brief Checks if a user passes the filter criteria.
247
 * @param user User to check
248
 * @param filter Filter string
249
 * @param nameFilter Compiled name filter regex
250
 * @return true if user passes filter
251
 */
252
bool UsersDialog::passesFilter(const UserInfo &user, const QString &filter,
×
253
                               const QRegularExpression &nameFilter) const {
254
  if (!filter.isEmpty() && !nameFilter.match(user.name).hasMatch()) {
×
255
    return false;
256
  }
257
  if (!user.isValid() && !ui->checkBox->isChecked()) {
×
258
    return false;
259
  }
260
  const bool expired = isUserExpired(user);
×
261
  return !(expired && !ui->checkBox->isChecked());
×
262
}
263

264
/**
265
 * @brief Checks if a user's key has expired.
266
 * @param user User to check
267
 * @return true if user's key is expired
268
 */
269
auto UsersDialog::isUserExpired(const UserInfo &user) const -> bool {
×
270
  return user.expiry.toSecsSinceEpoch() > 0 &&
×
271
         QDateTime::currentDateTime() > user.expiry;
×
272
}
273

274
/**
275
 * @brief Builds display text for a user.
276
 * @param user User to format
277
 * @return Formatted user text
278
 */
279
QString UsersDialog::buildUserText(const UserInfo &user) const {
×
280
  QString text = user.name + "\n" + user.key_id;
×
281
  if (user.created.toSecsSinceEpoch() > 0) {
×
282
    text += " " + tr("created") + " " +
×
283
            QLocale::system().toString(user.created, QLocale::ShortFormat);
×
284
  }
285
  if (user.expiry.toSecsSinceEpoch() > 0) {
×
286
    text += " " + tr("expires") + " " +
×
287
            QLocale::system().toString(user.expiry, QLocale::ShortFormat);
×
288
  }
289
  return text;
×
290
}
291

292
/**
293
 * @brief Applies visual styling to a user list item based on key status.
294
 * @param item List widget item to style
295
 * @param user User whose status determines styling
296
 */
297
void UsersDialog::applyUserStyling(QListWidgetItem *item,
×
298
                                   const UserInfo &user) const {
299
  const QString originalText = item->text();
×
300
  if (user.have_secret) {
×
301
    const QPalette palette = QApplication::palette();
×
302
    item->setForeground(palette.color(QPalette::Link));
×
303
    QFont font = item->font();
×
304
    font.setBold(true);
305
    item->setFont(font);
×
306
  } else if (!user.isValid()) {
×
307
    item->setBackground(Qt::darkRed);
×
308
    item->setForeground(Qt::white);
×
309
    item->setText(tr("[INVALID] ") + originalText);
×
310
  } else if (isUserExpired(user)) {
×
311
    item->setForeground(Qt::darkRed);
×
312
    item->setText(tr("[EXPIRED] ") + originalText);
×
313
  } else if (!user.fullyValid()) {
×
314
    item->setBackground(Qt::darkYellow);
×
315
    item->setForeground(Qt::white);
×
316
    item->setText(tr("[PARTIAL] ") + originalText);
×
317
  } else {
318
    item->setText(originalText);
×
319
  }
320
}
×
321

322
/**
323
 * @brief UsersDialog::on_lineEdit_textChanged typing in the searchbox.
324
 * @param filter
325
 */
326
void UsersDialog::on_lineEdit_textChanged(const QString &filter) {
×
327
  populateList(filter);
×
328
}
×
329

330
/**
331
 * @brief UsersDialog::on_checkBox_clicked filtering.
332
 */
333
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