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

IJHack / QtPass / 44816049215

20 Nov 2026 08:57PM UTC coverage: 94.65% (-0.003%) from 94.653%
44816049215

push

github

web-flow
Merge pull request #2278 from swarajnandedkar/fix/v5-v6-openpgp-fingerprints

Use full fingerprints for v5/v6 recipients

26 of 28 new or added lines in 2 files covered. (92.86%)

9377 of 9907 relevant lines covered (94.65%)

1248.15 hits per line

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

97.54
/src/usersdialog.cpp
1
// SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
/**
4
 * @file
5
 * @brief Implements UsersDialog; selectRecipients() matches the `.gpg-id`
6
 * lines against the keyring and lists the ones no key answers to.
7
 */
8
#include "usersdialog.h"
9
#include "importkeydialog.h"
10
#include "pass.h"
11
#include "qtpasssettings.h"
12
#include "ui_usersdialog.h"
13
#include "util.h"
14
#include "windowstatestore.h"
15
#include <QApplication>
16
#include <QDateTime>
17
#include <QDialogButtonBox>
18
#include <QDir>
19
#include <QFileInfo>
20
#include <QFont>
21
#include <QHash>
22
#include <QKeyEvent>
23
#include <QLabel>
24
#include <QLineEdit>
25
#include <QListWidget>
26
#include <QMessageBox>
27
#include <QPalette>
28
#include <QPushButton>
29
#include <QRegularExpression>
30
#include <QSet>
31
#include <QSignalBlocker>
32
#include <QWidget>
33
#include <algorithm>
34
#include <utility>
35

36
#include "qtpasslogging.h"
37
UsersDialog::UsersDialog(Pass *pass, const AppSettings &s, QString dir,
51 ✔
38
                         QWidget *parent)
51 ✔
39
    : UsersDialog(pass, s, std::move(dir), Preselect::FromList, parent) {}
102 ✔
40

41
UsersDialog::UsersDialog(Pass *pass, const AppSettings &s, QString dir,
55 ✔
42
                         Preselect preselect, QWidget *parent)
55 ✔
43
    : QDialog(parent), ui(new Ui::UsersDialog), m_pass(pass),
55 ✔
44
      m_passStore(s.passStore), m_gpgExe(s.gpgExecutable),
45
      m_dir(std::move(dir)), m_preselect(preselect) {
110 ✔
46
  Q_ASSERT(pass);
47
  ui->setupUi(this);
55 ✔
48

49
  restoreDialogState();
55 ✔
50
  if (!loadGpgKeys()) {
55 ✔
51
    connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
1 ✔
52
    return;
1 ✔
53
  }
54

55
  loadRecipients();
54 ✔
56
  showNestedLists();
54 ✔
57
  populateList();
54 ✔
58

59
  connectSignals();
54 ✔
60
}
×
61

62
void UsersDialog::connectSignals() {
54 ✔
63
  connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
54 ✔
64
          &UsersDialog::accept);
54 ✔
65
  connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
54 ✔
66
  connect(ui->listWidget, &QListWidget::itemChanged, this,
54 ✔
67
          &UsersDialog::itemChange);
108 ✔
68
  // importKeyButton is wired via Qt's automatic on_<name>_<signal> mechanism
69
  // already triggered by setupUi().
70

71
  ui->lineEdit->setClearButtonEnabled(true);
54 ✔
72
}
54 ✔
73

74
void UsersDialog::restoreDialogState() {
55 ✔
75
  WindowStateStore::attach(*this, QStringLiteral("usersDialog"));
110 ✔
76
}
55 ✔
77

78
auto UsersDialog::loadGpgKeys() -> bool {
57 ✔
79
  QList<UserInfo> users = m_pass->listKeys();
57 ✔
80
  if (users.isEmpty()) {
57 ✔
81
    QMessageBox::critical(parentWidget(), tr("Keylist missing"),
3 ✔
82
                          tr("Could not fetch list of available GPG keys"));
×
83
    reject();
1 ✔
84
    return false;
85
  }
86

87
  markSecretKeys(users);
56 ✔
88

89
  m_userList = users;
90
  return true;
56 ✔
91
}
92

93
void UsersDialog::markSecretKeys(QList<UserInfo> &users) {
56 ✔
94
  QList<UserInfo> secret_keys = m_pass->listKeys("", true);
112 ✔
95
  QSet<QString> secretKeyIds;
96
  for (const UserInfo &sec : secret_keys) {
61 ✔
97
    secretKeyIds.insert(sec.key_id);
5 ✔
98
  }
99
  for (auto &user : users) {
162 ✔
100
    if (secretKeyIds.contains(user.key_id)) {
106 ✔
101
      user.have_secret = true;
5 ✔
102
    }
103
  }
104
}
56 ✔
105

106
void UsersDialog::showRecipientWarning(const QString &text,
11 ✔
107
                                       const QString &name) {
108
  // Plain text before the text is set: it can quote the store's recipient
109
  // list, which as rich text could make Qt load a file (<img src=...>).
110
  auto *banner = new QLabel(this);
11 ✔
111
  banner->setTextFormat(Qt::PlainText);
11 ✔
112
  // And with nothing in it that changes how the rest reads: a folder name
113
  // with a bidi override mirrored the warning after it.
114
  banner->setText(Util::displayable(text));
11 ✔
115
  banner->setObjectName(name);
11 ✔
116
  banner->setWordWrap(true);
11 ✔
117
  // Palette, not a stylesheet colour, so it reads on dark themes too; red as
118
  // ProcessOutputPanel paints error lines.
119
  QFont bold = banner->font();
11 ✔
120
  bold.setBold(true);
121
  banner->setFont(bold);
11 ✔
122
  QPalette palette = banner->palette();
11 ✔
123
  palette.setColor(QPalette::WindowText, QColor(Qt::red));
11 ✔
124
  banner->setPalette(palette);
11 ✔
125
  banner->setContentsMargins(4, 4, 4, 4);
11 ✔
126
  ui->verticalLayout->insertWidget(1, banner);
11 ✔
127
}
11 ✔
128

129
void UsersDialog::showNestedLists() {
54 ✔
130
  // The walk re-encryption takes: hidden folders and links are not entered.
131
  const QString dir = QDir::cleanPath(m_dir);
54 ✔
132
  if (!QFileInfo(dir).isDir()) {
54 ✔
133
    return;
134
  }
135
  QStringList unlisted;
54 ✔
136
  const QStringList lists = Util::regularFilesUnder(
162 ✔
137
      dir, {QStringLiteral(".gpg-id")}, nullptr, true, &unlisted);
108 ✔
138
  const auto relative = [&dir](const QString &folder) {
5 ✔
139
    return QDir::toNativeSeparators(QDir(dir).relativeFilePath(folder));
10 ✔
140
  };
54 ✔
141
  QStringList nested;
54 ✔
142
  for (const QString &list : lists) {
92 ✔
143
    const QString folder = QFileInfo(list).path();
38 ✔
144
    if (QDir::cleanPath(folder) != dir) {
76 ✔
145
      nested << relative(folder);
8 ✔
146
    }
147
  }
148
  QStringList unread;
54 ✔
149
  for (const QString &folder : std::as_const(unlisted)) {
55 ✔
150
    unread << relative(folder);
2 ✔
151
  }
152
  constexpr qsizetype kMaxNamed = 5;
153
  const auto named = [](const QStringList &folders) {
4 ✔
154
    QString names = folders.mid(0, kMaxNamed).join(QStringLiteral(", "));
8 ✔
155
    if (folders.size() > kMaxNamed) {
4 ✔
156
      names += QStringLiteral(", …");
×
157
    }
158
    return names;
4 ✔
159
  };
160
  QStringList text;
54 ✔
161
  if (!nested.isEmpty()) {
54 ✔
162
    //: %1 lists folders, such as "work, work/bank". Shown when changing the
163
    //: recipients of a folder that has folders with their own list below it.
164
    text << tr("%n folder(s) below this one have their own recipients, which "
3 ✔
165
               "saving here does not change: %1",
166
               nullptr, static_cast<int>(nested.size()))
167
                .arg(named(nested));
6 ✔
168
  }
169
  if (!unread.isEmpty()) {
54 ✔
170
    //: %1 lists folders, such as "work, work/bank", that QtPass could not
171
    //: read: no permission, or a name in them that is not valid UTF-8.
172
    text << tr("%n folder(s) below this one could not be read, so whether "
1 ✔
173
               "they have their own recipients is unknown: %1",
174
               nullptr, static_cast<int>(unread.size()))
175
                .arg(named(unread));
2 ✔
176
  }
177
  if (!text.isEmpty()) {
54 ✔
178
    showRecipientWarning(text.join(QLatin1Char('\n')),
8 ✔
179
                         QStringLiteral("nestedListsWarning"));
4 ✔
180
  }
181
}
182

183
namespace {
184

185
/// A key ID as written anywhere: without a 0x prefix, upper case.
186
auto normalisedKeyId(const QString &id) -> QString {
126 ✔
187
  const QString trimmed = id.trimmed();
188
  return (trimmed.startsWith(QLatin1String("0x"), Qt::CaseInsensitive)
126 ✔
189
              ? trimmed.mid(2)
252 ✔
190
              : trimmed)
191
      .toUpper();
126 ✔
192
}
193

194
/// Whether two hex identifiers name the same key. v4 key IDs are fingerprint
195
/// suffixes; v5/v6 key IDs are the first 16 digits, with the short ID in the
196
/// following eight. Do not accept a v5/v6 fingerprint's tail as its key ID.
197
auto keyIdsMatch(const QString &first, const QString &second) -> bool {
63 ✔
198
  static const QRegularExpression hexId(QStringLiteral("^[0-9A-F]{8,}$"));
69 ✔
199
  const QString a = normalisedKeyId(first);
63 ✔
200
  const QString b = normalisedKeyId(second);
63 ✔
201
  if (!hexId.match(a).hasMatch() || !hexId.match(b).hasMatch()) {
126 ✔
202
    return false;
203
  }
204
  if (a.size() == 64) {
63 ✔
205
    return b == a || (b.size() == 16 && a.startsWith(b)) ||
16 ✔
206
           (b.size() == 8 && a.mid(8, 8) == b);
19 ✔
207
  }
208
  if (b.size() == 64) {
47 ✔
NEW
209
    return a == b || (a.size() == 16 && b.startsWith(a)) ||
×
NEW
210
           (a.size() == 8 && b.mid(8, 8) == a);
×
211
  }
212
  return a.endsWith(b) || b.endsWith(a);
47 ✔
213
}
214

215
/// Whether @p recipient, as a .gpg-id lists it, names @p key the way gpg's
216
/// -r resolves it: a key ID in any length gpg accepts (the key carries its
217
/// fingerprint, the list may carry its v4 suffix or v5/v6 head ID, or the
218
/// fingerprint itself; eight characters is the shortest), or else a name,
219
/// matched case-insensitively as a substring of the UID after gpg's "=",
220
/// "<...>", "@" and "*" markers.
221
auto answersTo(const UserInfo &key, const QString &recipient) -> bool {
64 ✔
222
  static const QRegularExpression hexId(
223
      QStringLiteral("^(0x)?[0-9A-Fa-f]{8,}$"));
70 ✔
224
  if (hexId.match(recipient.trimmed()).hasMatch()) {
128 ✔
225
    return keyIdsMatch(key.key_id, recipient);
59 ✔
226
  }
227
  QString term = recipient.trimmed();
228
  if (term.startsWith(u'=') || term.startsWith(u'@') || term.startsWith(u'*')) {
5 ✔
229
    term.remove(0, 1);
×
230
  } else if (term.startsWith(u'<') && term.endsWith(u'>')) {
5 ✔
231
    term = term.mid(1, term.size() - 2);
2 ✔
232
  }
233
  return !term.isEmpty() && key.name.contains(term, Qt::CaseInsensitive);
5 ✔
234
}
235

236
} // namespace
237

238
void UsersDialog::selectRecipients(const QStringList &recipients) {
31 ✔
239
  // Each per-line gpg lookup is a blocking call on the dialog's way up, so a
240
  // store with many keys missing from the keyring (a fresh clone, a team
241
  // store) is capped at this many across both the fallback below and the
242
  // missing-key check; the rest resolve by the batched call or stay unchecked.
243
  constexpr int kMaxLookups = 10;
244
  int lookups = 0;
245
  // Remembers the per-line lookups the fallback already ran, so the
246
  // missing-key loop below reuses them instead of asking gpg a second time.
247
  QHash<QString, bool> perLineHasKey;
31 ✔
248

249
  // One gpg call resolves every recipient, by key ID or by email/UID.
250
  QList<UserInfo> resolved = m_pass->listKeys(recipients);
62 ✔
251
  if (resolved.isEmpty() && recipients.size() > 1) {
31 ✔
252
    // One line gpg cannot read as a key search ("0x", a fingerprint one
253
    // digit short) aborts that whole call with exit code 2, so nothing is
254
    // listed for the other lines, whose keys are in the keyring all the same.
255
    // Asked for one by one they still resolve; only the unreadable line is
256
    // reported missing by the loop below, as an unknown key already is.
257
    for (const QString &recipient : recipients) {
25 ✔
258
      if (lookups++ >= kMaxLookups) {
22 ✔
259
        break;
260
      }
261
      const QList<UserInfo> keys = m_pass->listKeys(recipient);
21 ✔
262
      perLineHasKey.insert(recipient, !keys.isEmpty());
21 ✔
263
      resolved += keys;
264
    }
265
  }
266
  for (UserInfo &user : m_userList) {
89 ✔
267
    if (std::any_of(resolved.cbegin(), resolved.cend(),
58 ✔
268
                    [&user](const UserInfo &key) {
269
                      return key.key_id == user.key_id;
68 ✔
270
                    })) {
271
      user.enabled = true;
40 ✔
272
    }
273
  }
274
  // Only what gpg itself cannot find is reported missing. The fallback's
275
  // per-line answer, when it has one, is gpg's own verdict for that line and
276
  // decides it outright. Otherwise the heuristic match is a fast path (a
277
  // secondary UID or a subkey ID resolves in gpg without showing in what it
278
  // lists), and a per-line gpg call settles anything it does not catch. Taking
279
  // the cached answer first also keeps a genuinely unreadable line ("0x")
280
  // reported missing even when a resolved key's UID happens to contain it, so
281
  // the heuristic answersTo() would otherwise match.
282
  for (const QString &recipient : recipients) {
106 ✔
283
    bool found = false;
284
    const auto cached = perLineHasKey.constFind(recipient);
75 ✔
285
    if (cached != perLineHasKey.constEnd()) {
286
      found = cached.value();
21 ✔
287
    } else {
288
      found = std::any_of(resolved.cbegin(), resolved.cend(),
54 ✔
289
                          [&recipient](const UserInfo &key) {
290
                            return answersTo(key, recipient);
64 ✔
291
                          });
292
      if (!found && lookups++ < kMaxLookups) {
54 ✔
293
        found = !m_pass->listKeys(recipient).isEmpty();
20 ✔
294
      }
295
    }
296
    if (!found) {
75 ✔
297
      UserInfo missing;
39 ✔
298
      missing.enabled = true;
39 ✔
299
      missing.key_id = recipient;
39 ✔
300
      missing.name = " ?? " + tr("Key not found in keyring");
78 ✔
301
      m_userList.append(missing);
302
    }
39 ✔
303
  }
304
}
31 ✔
305

306
void UsersDialog::loadRecipients() {
54 ✔
307
  // A store that had no list when it was chosen (a new profile): a list
308
  // that appeared since is nobody's choice, and was read under the active
309
  // backend's signing policy, not the new profile's.
310
  if (m_preselect == Preselect::Nothing) {
54 ✔
311
    return;
4 ✔
312
  }
313
  // Through the backend: with a signing key only a verified list may be
314
  // preselected, since OK signs whatever is selected.
315
  const Pass::RecipientsForEditing loaded =
316
      m_pass->recipientsForEditing(m_dir, m_passStore);
50 ✔
317
  if (!loaded.warning.isEmpty()) {
50 ✔
318
    showRecipientWarning(loaded.warning);
14 ✔
319
  }
320
  // A folder without .gpg-id (new store, new profile) has no recipients yet;
321
  // listKeys() with no filter would return the whole keyring, preselected.
322
  if (loaded.state != Pass::RecipientsForEditing::State::Rejected &&
50 ✔
323
      !loaded.recipients.isEmpty()) {
324
    selectRecipients(loaded.recipients);
31 ✔
325
  }
326
}
327

328
UsersDialog::~UsersDialog() = default;
110 ✔
329

330
void UsersDialog::accept() {
9 ✔
331
  if (!hasSelection()) {
9 ✔
332
    // Nothing to encrypt to: an empty .gpg-id would make every insert fail
333
    // and the wizard never offers this dialog again once the file exists.
334
    return;
335
  }
336
  if (m_initOnAccept) {
7 ✔
337
    m_pass->Init(m_dir, m_userList);
8 ✔
338
  }
339

340
  QDialog::accept();
7 ✔
341
}
342

343
auto UsersDialog::hasSelection() const -> bool {
152 ✔
344
  return std::any_of(m_userList.cbegin(), m_userList.cend(),
345
                     [](const UserInfo &user) { return user.enabled; });
227 ✔
346
}
347

348
void UsersDialog::updateOkButton() {
140 ✔
349
  if (auto *ok = ui->buttonBox->button(QDialogButtonBox::Ok)) {
140 ✔
350
    ok->setEnabled(hasSelection());
140 ✔
351
  }
352
}
140 ✔
353

354
void UsersDialog::keyPressEvent(QKeyEvent *event) {
1 ✔
355
  switch (event->key()) {
1 ✔
356
  case Qt::Key_Escape:
1 ✔
357
    ui->lineEdit->clear();
1 ✔
358
    break;
1 ✔
359
  default:
360
    break;
361
  }
362
}
1 ✔
363

364
void UsersDialog::itemChange(QListWidgetItem *item) {
142 ✔
365
  if (!item) {
142 ✔
366
    return;
82 ✔
367
  }
368
  bool ok = false;
142 ✔
369
  const int index = item->data(Qt::UserRole).toInt(&ok);
142 ✔
370
  if (!ok) {
142 ✔
371
    qCWarning(lcQtPass)
162 ✔
372
        << "UsersDialog::itemChange: invalid user index data for item";
81 ✔
373
    return;
81 ✔
374
  }
375
  if (index < 0 || index >= m_userList.size()) {
61 ✔
376
    qCWarning(lcQtPass) << "UsersDialog::itemChange: user index out of range:"
2 ✔
377
                        << index << "valid range is [0,"
1 ✔
378
                        << (m_userList.size() - 1) << "]";
1 ✔
379
    return;
1 ✔
380
  }
381
  m_userList[index].enabled = item->checkState() == Qt::Checked;
60 ✔
382
  updateOkButton();
60 ✔
383
}
384

385
void UsersDialog::populateList(const QString &filter) {
80 ✔
386
  // Invalidate cached datetime so expiry checks use fresh current time
387
  m_cachedDateTimeValid = false;
80 ✔
388

389
  QString patternString = "*" + filter + "*";
80 ✔
390
  if (m_cachedPatternString != patternString) {
80 ✔
391
    // A user ID is not a path: "*" and "?" must match a "/" as well, or a
392
    // key named "ACME A/S" or "(dev/ops)" drops out of the search and stays
393
    // ticked unseen while the listed rows are unticked.
394
    QRegularExpression re(
395
        QRegularExpression::wildcardToRegularExpression(
140 ✔
396
            patternString, QRegularExpression::NonPathWildcardConversion),
397
        QRegularExpression::CaseInsensitiveOption);
70 ✔
398
    if (re.isValid()) {
70 ✔
399
      m_cachedNameFilter = re;
70 ✔
400
      m_cachedPatternString = patternString;
70 ✔
401
    }
402
  }
70 ✔
403
  const QRegularExpression &nameFilter = m_cachedNameFilter;
80 ✔
404
  ui->listWidget->clear();
80 ✔
405

406
  for (int i = 0; i < m_userList.size(); ++i) {
287 ✔
407
    const auto &user = m_userList.at(i);
408
    if (!passesFilter(user, filter, nameFilter)) {
207 ✔
409
      continue;
27 ✔
410
    }
411

412
    auto *item = new QListWidgetItem(buildUserText(user), ui->listWidget);
180 ✔
413
    applyUserStyling(item, user);
180 ✔
414
    item->setCheckState(user.enabled ? Qt::Checked : Qt::Unchecked);
257 ✔
415
    item->setData(Qt::UserRole, QVariant::fromValue(i));
180 ✔
416
    ui->listWidget->addItem(item);
180 ✔
417
  }
418
  updateOkButton();
80 ✔
419
}
80 ✔
420

421
bool UsersDialog::passesFilter(const UserInfo &user, const QString &filter,
207 ✔
422
                               const QRegularExpression &nameFilter) const {
423
  if (!filter.isEmpty() && !nameFilter.match(user.name).hasMatch()) {
221 ✔
424
    return false;
425
  }
426
  // A ticked key is one the folder is encrypted to: it is shown whatever
427
  // its state, or an expired or missing recipient stays in .gpg-id unseen and
428
  // cannot be unticked. "Show unusable keys" is about keys to add.
429
  if (user.enabled || ui->checkBox->isChecked()) {
193 ✔
430
    return true;
112 ✔
431
  }
432
  return user.isValid() && !isUserExpired(user);
73 ✔
433
}
434

435
auto UsersDialog::isUserExpired(const UserInfo &user) const -> bool {
253 ✔
436
  if (!m_cachedDateTimeValid) {
253 ✔
437
    m_cachedCurrentDateTime = QDateTime::currentDateTime();
77 ✔
438
    m_cachedDateTimeValid = true;
77 ✔
439
  }
440
  // gpg marks a key it already judged expired with validity 'e'.
441
  return user.validity == 'e' ||
504 ✔
442
         (user.expiry.isValid() && m_cachedCurrentDateTime > user.expiry);
260 ✔
443
}
444

445
QString UsersDialog::buildUserText(const UserInfo &user) const {
180 ✔
446
  // One line for the user ID: a line separator in it (gpg escapes only
447
  // ASCII) drew a line that looked like the key's fingerprint.
448
  // The key ID too: a recipient no key answers to is the .gpg-id line as
449
  // written, which may hold the same separators.
450
  QString text = Util::displayable(user.name, false) + "\n" +
360 ✔
451
                 Util::displayable(user.key_id, false);
360 ✔
452
  if (user.created.isValid()) {
180 ✔
453
    text += " " + tr("created") + " " +
280 ✔
454
            QLocale::system().toString(user.created, QLocale::ShortFormat);
420 ✔
455
  }
456
  if (user.expiry.isValid()) {
180 ✔
457
    text += " " + tr("expires") + " " +
12 ✔
458
            QLocale::system().toString(user.expiry, QLocale::ShortFormat);
18 ✔
459
  }
460
  return text;
180 ✔
461
}
462

463
void UsersDialog::applyUserStyling(QListWidgetItem *item,
180 ✔
464
                                   const UserInfo &user) const {
465
  const QString originalText = item->text();
180 ✔
466
  // Status badge first: an own key can be expired too, and then the marker
467
  // matters more than the "you can decrypt with this" colour.
468
  bool badged = true;
469
  // Expired before invalid: gpg gives an expired key validity 'e', which is
470
  // not a valid one, and "expired" says what to do about it.
471
  if (isUserExpired(user)) {
180 ✔
472
    // A lone dark-red foreground is unreadable on dark themes, and gpg
473
    // refuses to encrypt to expired keys anyway.
474
    item->setBackground(Qt::darkRed);
6 ✔
475
    item->setForeground(Qt::white);
6 ✔
476
    item->setText(tr("[EXPIRED] ") + originalText);
12 ✔
477
  } else if (!user.isValid()) {
478
    item->setBackground(Qt::darkRed);
43 ✔
479
    item->setForeground(Qt::white);
43 ✔
480
    item->setText(tr("[INVALID] ") + originalText);
86 ✔
481
  } else if (!user.fullyValid()) {
131 ✔
482
    item->setBackground(Qt::darkYellow);
7 ✔
483
    item->setForeground(Qt::white);
7 ✔
484
    item->setText(tr("[PARTIAL] ") + originalText);
14 ✔
485
  } else {
486
    item->setText(originalText);
124 ✔
487
    badged = false;
488
  }
489
  if (user.have_secret) {
180 ✔
490
    if (!badged) {
7 ✔
491
      item->setForeground(QApplication::palette().color(QPalette::Link));
14 ✔
492
    }
493
    QFont font = item->font();
7 ✔
494
    font.setBold(true);
495
    item->setFont(font);
7 ✔
496
  }
7 ✔
497
}
180 ✔
498

499
void UsersDialog::on_lineEdit_textChanged(const QString &filter) {
14 ✔
500
  populateList(filter);
14 ✔
501
}
14 ✔
502

503
void UsersDialog::on_checkBox_clicked() { populateList(ui->lineEdit->text()); }
20 ✔
504

505
void UsersDialog::selectImportedKey(const QString &importedKey) {
2 ✔
506
  // The stored key_id, not the item text.
507
  for (int i = 0; i < ui->listWidget->count(); ++i) {
4 ✔
508
    QListWidgetItem *item = ui->listWidget->item(i);
4 ✔
509
    bool ok = false;
4 ✔
510
    const int idx = item != nullptr ? item->data(Qt::UserRole).toInt(&ok) : -1;
4 ✔
511
    if (ok && idx >= 0 && idx < m_userList.size() &&
8 ✔
512
        !m_userList[idx].key_id.isEmpty() &&
8 ✔
513
        keyIdsMatch(m_userList[idx].key_id, importedKey)) {
4 ✔
514
      ui->listWidget->setCurrentItem(item);
2 ✔
515
      return;
2 ✔
516
    }
517
  }
518
}
519

520
void UsersDialog::on_importKeyButton_clicked() {
3 ✔
521
  ImportKeyDialog dialog(m_gpgExe, this);
3 ✔
522
  if (dialog.exec() != QDialog::Accepted) {
3 ✔
523
    return;
524
  }
525

526
  // Non-empty after Accepted, but an empty id would make the endsWith()
527
  // below match the first listed key.
528
  const QString importedKey = dialog.importedKeyId();
2 ✔
529
  if (importedKey.isEmpty()) {
2 ✔
530
    return;
531
  }
532

533
  // loadGpgKeys() rebuilds the key list from the keyring and drops every tick.
534
  // Remember which recipients were selected (the .gpg-id list and the user's
535
  // own ticks, including any "key not found" rows) and re-apply them after, so
536
  // importing a key does not silently remove the other recipients.
537
  QStringList selected;
2 ✔
538
  for (const UserInfo &user : std::as_const(m_userList)) {
4 ✔
539
    if (user.enabled) {
2 ✔
540
      selected.append(user.key_id);
2 ✔
541
    }
542
  }
543

544
  if (!loadGpgKeys()) {
2 ✔
545
    return;
546
  }
547

548
  // Re-apply the remembered selection. Most saved identifiers are a key ID that
549
  // is in the rebuilt list, so match those exactly first (no gpg lookup). An
550
  // identifier that does not match a row is one that was listed as "key not
551
  // found" - a short ID or email that gpg could not resolve, or whose key has
552
  // just been imported and now shows under its fingerprint. Hand those to
553
  // selectRecipients(), which resolves each against the refreshed keyring,
554
  // ticks the matching row, and re-adds a "key not found" row for any that
555
  // still does not resolve.
556
  QStringList unresolved;
2 ✔
557
  for (const QString &keyId : std::as_const(selected)) {
4 ✔
558
    bool matched = false;
559
    for (UserInfo &user : m_userList) {
6 ✔
560
      if (user.key_id == keyId) {
4 ✔
561
        user.enabled = true;
2 ✔
562
        matched = true;
563
      }
564
    }
565
    if (!matched) {
2 ✔
566
      unresolved.append(keyId);
567
    }
568
  }
569
  if (!unresolved.isEmpty()) {
2 ✔
570
    selectRecipients(unresolved);
×
571
  }
572

573
  // Clear the filter so the just-imported key is visible. setText("") still
574
  // emits textChanged once, so don't double-populate.
575
  {
576
    const QSignalBlocker blocker(ui->lineEdit);
2 ✔
577
    ui->lineEdit->clear();
2 ✔
578
  }
2 ✔
579
  populateList(QString());
2 ✔
580

581
  selectImportedKey(importedKey);
2 ✔
582
}
3 ✔
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