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

IJHack / QtPass / 24939220105

25 Apr 2026 07:49PM UTC coverage: 26.839% (-0.8%) from 27.609%
24939220105

Pull #1170

github

web-flow
Merge bfff1ab91 into 9048d2bb3
Pull Request #1170: feat: add Import key dialog to UsersDialog for issue #1167

0 of 93 new or added lines in 3 files covered. (0.0%)

33 existing lines in 2 files now uncovered.

1722 of 6416 relevant lines covered (26.84%)

27.63 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 "importkeydialog.h"
5
#include "qtpasssettings.h"
6
#include "ui_usersdialog.h"
7
#include <QApplication>
8
#include <QCloseEvent>
9
#include <QDateTime>
10
#include <QKeyEvent>
11
#include <QLineEdit>
12
#include <QListWidget>
13
#include <QMessageBox>
14
#include <QPushButton>
15
#include <QRegularExpression>
16
#include <QSet>
17
#include <QWidget>
18
#include <utility>
19

20
#ifdef QT_DEBUG
21
#include "debughelper.h"
22
#endif
23
/**
24
 * @brief UsersDialog::UsersDialog basic constructor
25
 * @param dir Password directory
26
 * @param parent
27
 */
28
UsersDialog::UsersDialog(const QString &dir, QWidget *parent)
×
29
    : QDialog(parent), ui(new Ui::UsersDialog), m_dir(dir) {
×
30

31
  ui->setupUi(this);
×
32

33
  restoreDialogState();
×
34
  if (!loadGpgKeys()) {
×
35
    connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
×
36
    return;
×
37
  }
38

39
  loadRecipients();
×
40
  populateList();
×
41

42
  connectSignals();
×
43
}
×
44

45
void UsersDialog::connectSignals() {
×
46
  connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
×
47
          &UsersDialog::accept);
×
48
  connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
×
49
  connect(ui->listWidget, &QListWidget::itemChanged, this,
×
50
          &UsersDialog::itemChange);
×
NEW
51
  connect(ui->importKeyButton, &QPushButton::clicked, this,
×
NEW
52
          &UsersDialog::on_importKeyButton_clicked);
×
53

54
  ui->lineEdit->setClearButtonEnabled(true);
×
55
}
×
56

57
/**
58
 * @brief Restore dialog geometry from settings.
59
 */
60
void UsersDialog::restoreDialogState() {
×
61
  QByteArray savedGeometry = QtPassSettings::getDialogGeometry("usersDialog");
×
62
  bool hasSavedGeometry = !savedGeometry.isEmpty();
63
  if (hasSavedGeometry) {
×
64
    restoreGeometry(savedGeometry);
×
65
  }
66
  if (QtPassSettings::isDialogMaximized("usersDialog")) {
×
67
    showMaximized();
×
68
  } else if (hasSavedGeometry) {
×
69
    move(QtPassSettings::getDialogPos("usersDialog"));
×
70
    resize(QtPassSettings::getDialogSize("usersDialog"));
×
71
  }
72
}
×
73

74
auto UsersDialog::loadGpgKeys() -> bool {
×
75
  QList<UserInfo> users = QtPassSettings::getPass()->listKeys();
×
76
  if (users.isEmpty()) {
×
77
    QMessageBox::critical(parentWidget(), tr("Keylist missing"),
×
78
                          tr("Could not fetch list of available GPG keys"));
×
79
    reject();
×
80
    return false;
81
  }
82

83
  markSecretKeys(users);
×
84

85
  m_userList = users;
86
  return true;
×
87
}
88

89
void UsersDialog::markSecretKeys(QList<UserInfo> &users) {
×
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
  }
100
}
×
101

102
void UsersDialog::loadRecipients() {
×
103
  int count = 0;
×
104
  QStringList recipients = QtPassSettings::getPass()->getRecipientString(
×
105
      m_dir.isEmpty() ? "" : m_dir, " ", &count);
×
106

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

119
  if (count > selectedUsers.size()) {
×
120
    QStringList allRecipients = QtPassSettings::getPass()->getRecipientList(
×
121
        m_dir.isEmpty() ? "" : m_dir);
×
122

123
    // Use bulk lookup to resolve all recipients at once (single gpg call)
124
    // This preserves the original email/UID resolution behavior
125
    QList<UserInfo> resolvedKeys =
126
        QtPassSettings::getPass()->listKeys(allRecipients);
×
127
    // Track resolved recipients by their resolved key_id
128
    QSet<QString> resolvedKeyIds;
129
    for (const UserInfo &key : resolvedKeys) {
×
130
      resolvedKeyIds.insert(key.key_id);
×
131
    }
132

133
    // Accept a recipient as resolved if GPG returned a key for it
134
    // (either its exact key_id, or GPG resolved email/UID/fingerprint to it)
135
    QSet<QString> resolvedRecipients;
136
    for (const UserInfo &key : resolvedKeys) {
×
137
      resolvedRecipients.insert(key.key_id);
×
138
      // Also add the name (email/UID) of resolved keys as valid resolved tokens
139
      // since GPG matched them to this key
140
      if (!key.name.isEmpty()) {
×
141
        resolvedRecipients.insert(
×
142
            key.name.section('@', 0, 0));    // email local part
×
143
        resolvedRecipients.insert(key.name); // full email/UID
×
144
      }
145
    }
146

147
    for (const QString &recipient : allRecipients) {
×
148
      if (!resolvedKeyIds.contains(recipient) &&
×
149
          !resolvedRecipients.contains(recipient) &&
×
150
          !selectedKeyIds.contains(recipient)) {
151
        UserInfo i;
×
152
        i.enabled = true;
×
153
        i.key_id = recipient;
×
154
        i.name = " ?? " + tr("Key not found in keyring");
×
155
        m_userList.append(i);
156
      }
×
157
    }
158
  }
159
}
×
160

161
/**
162
 * @brief UsersDialog::~UsersDialog basic destructor.
163
 */
164
UsersDialog::~UsersDialog() { delete ui; }
×
165

166
/**
167
 * @brief UsersDialog::accept
168
 */
169
void UsersDialog::accept() {
×
170
  QtPassSettings::getPass()->Init(m_dir, m_userList);
×
171

172
  QDialog::accept();
×
173
}
×
174

175
/**
176
 * @brief UsersDialog::closeEvent save window state on close.
177
 * @param event
178
 */
179
void UsersDialog::closeEvent(QCloseEvent *event) {
×
180
  QtPassSettings::setDialogGeometry("usersDialog", saveGeometry());
×
181
  if (!isMaximized()) {
×
182
    QtPassSettings::setDialogPos("usersDialog", pos());
×
183
    QtPassSettings::setDialogSize("usersDialog", size());
×
184
  }
185
  QtPassSettings::setDialogMaximized("usersDialog", isMaximized());
×
186
  event->accept();
187
}
×
188

189
/**
190
 * @brief UsersDialog::keyPressEvent clear the lineEdit when escape is pressed.
191
 * No action for Enter currently.
192
 * @param event
193
 */
194
void UsersDialog::keyPressEvent(QKeyEvent *event) {
×
195
  switch (event->key()) {
×
196
  case Qt::Key_Escape:
×
197
    ui->lineEdit->clear();
×
198
    break;
×
199
  default:
200
    break;
201
  }
202
}
×
203

204
/**
205
 * @brief UsersDialog::itemChange update the item information.
206
 * @param item
207
 */
208
void UsersDialog::itemChange(QListWidgetItem *item) {
×
209
  if (!item) {
×
210
    return;
×
211
  }
212
  bool ok = false;
×
213
  const int index = item->data(Qt::UserRole).toInt(&ok);
×
214
  if (!ok) {
×
215
#ifdef QT_DEBUG
216
    qWarning() << "UsersDialog::itemChange: invalid user index data for item";
217
#endif
218
    return;
219
  }
220
  if (index < 0 || index >= m_userList.size()) {
×
221
#ifdef QT_DEBUG
222
    qWarning() << "UsersDialog::itemChange: user index out of range:" << index
223
               << "valid range is [0," << (m_userList.size() - 1) << "]";
224
#endif
225
    return;
226
  }
227
  m_userList[index].enabled = item->checkState() == Qt::Checked;
×
228
}
229

230
/**
231
 * @brief UsersDialog::populateList update the view based on filter options
232
 * (such as searching).
233
 * @param filter
234
 */
235
void UsersDialog::populateList(const QString &filter) {
×
236
  // Invalidate cached datetime so expiry checks use fresh current time
237
  m_cachedDateTimeValid = false;
×
238

239
  QString patternString = "*" + filter + "*";
×
240
  if (m_cachedPatternString != patternString) {
×
241
    QRegularExpression re(
242
        QRegularExpression::wildcardToRegularExpression(patternString),
×
243
        QRegularExpression::CaseInsensitiveOption);
×
244
    if (re.isValid()) {
×
245
      m_cachedNameFilter = re;
×
246
      m_cachedPatternString = patternString;
×
247
    }
248
  }
×
249
  const QRegularExpression &nameFilter = m_cachedNameFilter;
×
250
  ui->listWidget->clear();
×
251

252
  for (int i = 0; i < m_userList.size(); ++i) {
×
253
    const auto &user = m_userList.at(i);
254
    if (!passesFilter(user, filter, nameFilter)) {
×
255
      continue;
×
256
    }
257

258
    auto *item = new QListWidgetItem(buildUserText(user), ui->listWidget);
×
259
    applyUserStyling(item, user);
×
260
    item->setCheckState(user.enabled ? Qt::Checked : Qt::Unchecked);
×
261
    item->setData(Qt::UserRole, QVariant::fromValue(i));
×
262
    ui->listWidget->addItem(item);
×
263
  }
264
}
×
265

266
/**
267
 * @brief Checks if a user passes the filter criteria.
268
 * @param user User to check
269
 * @param filter Filter string
270
 * @param nameFilter Compiled name filter regex
271
 * @return true if user passes filter
272
 */
273
bool UsersDialog::passesFilter(const UserInfo &user, const QString &filter,
×
274
                               const QRegularExpression &nameFilter) const {
275
  if (!filter.isEmpty() && !nameFilter.match(user.name).hasMatch()) {
×
276
    return false;
277
  }
278
  if (!user.isValid() && !ui->checkBox->isChecked()) {
×
279
    return false;
280
  }
281
  const bool expired = isUserExpired(user);
×
282
  return !(expired && !ui->checkBox->isChecked());
×
283
}
284

285
/**
286
 * @brief Checks if a user's key has expired.
287
 * @param user User to check
288
 * @return true if user's key is expired
289
 */
290
auto UsersDialog::isUserExpired(const UserInfo &user) const -> bool {
×
291
  if (!m_cachedDateTimeValid) {
×
292
    m_cachedCurrentDateTime = QDateTime::currentDateTime();
×
293
    m_cachedDateTimeValid = true;
×
294
  }
295
  return user.expiry.toSecsSinceEpoch() > 0 &&
×
296
         m_cachedCurrentDateTime > user.expiry;
×
297
}
298

299
/**
300
 * @brief Builds display text for a user.
301
 * @param user User to format
302
 * @return Formatted user text
303
 */
304
QString UsersDialog::buildUserText(const UserInfo &user) const {
×
305
  QString text = user.name + "\n" + user.key_id;
×
306
  if (user.created.toSecsSinceEpoch() > 0) {
×
307
    text += " " + tr("created") + " " +
×
308
            QLocale::system().toString(user.created, QLocale::ShortFormat);
×
309
  }
310
  if (user.expiry.toSecsSinceEpoch() > 0) {
×
311
    text += " " + tr("expires") + " " +
×
312
            QLocale::system().toString(user.expiry, QLocale::ShortFormat);
×
313
  }
314
  return text;
×
315
}
316

317
/**
318
 * @brief Applies visual styling to a user list item based on key status.
319
 * @param item List widget item to style
320
 * @param user User whose status determines styling
321
 */
322
void UsersDialog::applyUserStyling(QListWidgetItem *item,
×
323
                                   const UserInfo &user) const {
324
  const QString originalText = item->text();
×
325
  if (user.have_secret) {
×
326
    const QPalette palette = QApplication::palette();
×
327
    item->setForeground(palette.color(QPalette::Link));
×
328
    QFont font = item->font();
×
329
    font.setBold(true);
330
    item->setFont(font);
×
331
  } else if (!user.isValid()) {
×
332
    item->setBackground(Qt::darkRed);
×
333
    item->setForeground(Qt::white);
×
334
    item->setText(tr("[INVALID] ") + originalText);
×
335
  } else if (isUserExpired(user)) {
×
336
    item->setForeground(Qt::darkRed);
×
337
    item->setText(tr("[EXPIRED] ") + originalText);
×
338
  } else if (!user.fullyValid()) {
×
339
    item->setBackground(Qt::darkYellow);
×
340
    item->setForeground(Qt::white);
×
341
    item->setText(tr("[PARTIAL] ") + originalText);
×
342
  } else {
343
    item->setText(originalText);
×
344
  }
345
}
×
346

347
/**
348
 * @brief UsersDialog::on_lineEdit_textChanged typing in the searchbox.
349
 * @param filter
350
 */
351
void UsersDialog::on_lineEdit_textChanged(const QString &filter) {
×
352
  populateList(filter);
×
353
}
×
354

355
/**
356
 * @brief UsersDialog::on_checkBox_clicked filtering.
357
 */
358
void UsersDialog::on_checkBox_clicked() { populateList(ui->lineEdit->text()); }
×
359

NEW
360
void UsersDialog::on_importKeyButton_clicked() {
×
NEW
361
  ImportKeyDialog dialog(this);
×
NEW
362
  if (dialog.exec() != QDialog::Accepted) {
×
363
    return;
364
  }
365

NEW
366
  QString fingerprint = dialog.importedKeyFingerprint();
×
NEW
367
  if (fingerprint.isEmpty()) {
×
NEW
368
    QMessageBox::warning(
×
NEW
369
        this, tr("Import Key"),
×
NEW
370
        tr("No key was imported. Please use the Import button to import a key."));
×
NEW
371
    return;
×
372
  }
373

NEW
374
  if (!loadGpgKeys()) {
×
375
    return;
376
  }
377

NEW
378
  QString currentFilter = ui->lineEdit->text();
×
NEW
379
  ui->lineEdit->clear();
×
NEW
380
  populateList(QString());
×
381

NEW
382
  for (int i = 0; i < ui->listWidget->count(); ++i) {
×
NEW
383
    QListWidgetItem *item = ui->listWidget->item(i);
×
NEW
384
    if (item && item->text().contains(fingerprint)) {
×
NEW
385
      ui->listWidget->setCurrentItem(item);
×
386
      break;
387
    }
388
  }
389

NEW
390
  if (!currentFilter.isEmpty()) {
×
NEW
391
    ui->lineEdit->setText(currentFilter);
×
392
  }
NEW
393
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc