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

IJHack / QtPass / 24291864496

11 Apr 2026 09:20PM UTC coverage: 20.927%. Remained the same
24291864496

push

github

web-flow
fix: move docstrings outside function bodies in UsersDialog (#974)

* fix: replace static variables with member variables in populateList

* refactor: move docstrings to header, add connectSignals declaration

* fix: move docstrings outside function bodies

1111 of 5309 relevant lines covered (20.93%)

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

27
  restoreDialogState();
×
28
  if (!loadGpgKeys()) {
×
29
    connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
×
30
    return;
×
31
  }
32

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

36
  connectSignals();
×
37
}
×
38

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

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

49
/**
50
 * @brief Restore dialog geometry from settings.
51
 */
52
void UsersDialog::restoreDialogState() {
×
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
  }
64
}
×
65

66
auto UsersDialog::loadGpgKeys() -> bool {
×
67
  QList<UserInfo> users = QtPassSettings::getPass()->listKeys();
×
68
  if (users.isEmpty()) {
×
69
    QMessageBox::critical(parentWidget(), tr("Keylist missing"),
×
70
                          tr("Could not fetch list of available GPG keys"));
×
71
    reject();
×
72
    return false;
73
  }
74

75
  markSecretKeys(users);
×
76

77
  m_userList = users;
78
  return true;
×
79
}
80

81
void UsersDialog::markSecretKeys(QList<UserInfo> &users) {
×
82
  QList<UserInfo> secret_keys = QtPassSettings::getPass()->listKeys("", true);
×
83
  QSet<QString> secretKeyIds;
84
  for (const UserInfo &sec : secret_keys) {
×
85
    secretKeyIds.insert(sec.key_id);
×
86
  }
87
  for (auto &user : users) {
×
88
    if (secretKeyIds.contains(user.key_id)) {
×
89
      user.have_secret = true;
×
90
    }
91
  }
92
}
×
93

94
void UsersDialog::loadRecipients() {
×
95
  int count = 0;
×
96
  QStringList recipients = QtPassSettings::getPass()->getRecipientString(
×
97
      m_dir.isEmpty() ? "" : m_dir, " ", &count);
×
98

99
  QList<UserInfo> selectedUsers =
100
      QtPassSettings::getPass()->listKeys(recipients);
×
101
  QSet<QString> selectedKeyIds;
102
  for (const UserInfo &sel : selectedUsers) {
×
103
    selectedKeyIds.insert(sel.key_id);
×
104
  }
105
  for (auto &user : m_userList) {
×
106
    if (selectedKeyIds.contains(user.key_id)) {
×
107
      user.enabled = true;
×
108
    }
109
  }
110

111
  if (count > selectedUsers.size()) {
×
112
    QStringList allRecipients = QtPassSettings::getPass()->getRecipientList(
×
113
        m_dir.isEmpty() ? "" : m_dir);
×
114
    QSet<QString> missingKeyRecipients;
115
    for (const QString &recipient : allRecipients) {
×
116
      if (!missingKeyRecipients.contains(recipient) &&
×
117
          QtPassSettings::getPass()->listKeys(recipient).empty()) {
×
118
        missingKeyRecipients.insert(recipient);
×
119
        UserInfo i;
×
120
        i.enabled = true;
×
121
        i.key_id = recipient;
×
122
        i.name = " ?? " + tr("Key not found in keyring");
×
123
        m_userList.append(i);
124
      }
×
125
    }
126
  }
127
}
×
128

129
/**
130
 * @brief UsersDialog::~UsersDialog basic destructor.
131
 */
132
UsersDialog::~UsersDialog() { delete ui; }
×
133

134
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
135
Q_DECLARE_METATYPE(UserInfo *)
136
Q_DECLARE_METATYPE(const UserInfo *)
137
#endif
138

139
/**
140
 * @brief UsersDialog::accept
141
 */
142
void UsersDialog::accept() {
×
143
  QtPassSettings::getPass()->Init(m_dir, m_userList);
×
144

145
  QDialog::accept();
×
146
}
×
147

148
/**
149
 * @brief UsersDialog::closeEvent save window state on close.
150
 * @param event
151
 */
152
void UsersDialog::closeEvent(QCloseEvent *event) {
×
153
  QtPassSettings::setDialogGeometry("usersDialog", saveGeometry());
×
154
  if (!isMaximized()) {
×
155
    QtPassSettings::setDialogPos("usersDialog", pos());
×
156
    QtPassSettings::setDialogSize("usersDialog", size());
×
157
  }
158
  QtPassSettings::setDialogMaximized("usersDialog", isMaximized());
×
159
  event->accept();
160
}
×
161

162
/**
163
 * @brief UsersDialog::keyPressEvent clear the lineEdit when escape is pressed.
164
 * No action for Enter currently.
165
 * @param event
166
 */
167
void UsersDialog::keyPressEvent(QKeyEvent *event) {
×
168
  switch (event->key()) {
×
169
  case Qt::Key_Escape:
×
170
    ui->lineEdit->clear();
×
171
    break;
×
172
  default:
173
    break;
174
  }
175
}
×
176

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

203
/**
204
 * @brief UsersDialog::populateList update the view based on filter options
205
 * (such as searching).
206
 * @param filter
207
 */
208
void UsersDialog::populateList(const QString &filter) {
×
209
  if (filter != m_lastFilter) {
×
210
    m_lastFilter = filter;
×
211
    m_cachedNameFilter = QRegularExpression(
×
212
        QRegularExpression::wildcardToRegularExpression("*" + filter + "*"),
×
213
        QRegularExpression::CaseInsensitiveOption);
214
  }
215
  const QRegularExpression &nameFilter = m_cachedNameFilter;
×
216
  ui->listWidget->clear();
×
217

218
  for (int i = 0; i < m_userList.size(); ++i) {
×
219
    const auto &user = m_userList.at(i);
220
    if (!passesFilter(user, filter, nameFilter)) {
×
221
      continue;
×
222
    }
223

224
    auto *item = new QListWidgetItem(buildUserText(user), ui->listWidget);
×
225
    applyUserStyling(item, user);
×
226
    item->setCheckState(user.enabled ? Qt::Checked : Qt::Unchecked);
×
227
    item->setData(Qt::UserRole, QVariant::fromValue(i));
×
228
    ui->listWidget->addItem(item);
×
229
  }
230
}
×
231

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

251
/**
252
 * @brief Checks if a user's key has expired.
253
 * @param user User to check
254
 * @return true if user's key is expired
255
 */
256
auto UsersDialog::isUserExpired(const UserInfo &user) const -> bool {
×
257
  return user.expiry.toSecsSinceEpoch() > 0 &&
×
258
         QDateTime::currentDateTime() > user.expiry;
×
259
}
260

261
/**
262
 * @brief Builds display text for a user.
263
 * @param user User to format
264
 * @return Formatted user text
265
 */
266
QString UsersDialog::buildUserText(const UserInfo &user) const {
×
267
  QString text = user.name + "\n" + user.key_id;
×
268
  if (user.created.toSecsSinceEpoch() > 0) {
×
269
    text += " " + tr("created") + " " +
×
270
            QLocale::system().toString(user.created, QLocale::ShortFormat);
×
271
  }
272
  if (user.expiry.toSecsSinceEpoch() > 0) {
×
273
    text += " " + tr("expires") + " " +
×
274
            QLocale::system().toString(user.expiry, QLocale::ShortFormat);
×
275
  }
276
  return text;
×
277
}
278

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

309
/**
310
 * @brief UsersDialog::on_lineEdit_textChanged typing in the searchbox.
311
 * @param filter
312
 */
313
void UsersDialog::on_lineEdit_textChanged(const QString &filter) {
×
314
  populateList(filter);
×
315
}
×
316

317
/**
318
 * @brief UsersDialog::on_checkBox_clicked filtering.
319
 */
320
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