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

IJHack / QtPass / 23685886070

28 Mar 2026 01:07PM UTC coverage: 18.408% (-0.007%) from 18.415%
23685886070

push

github

web-flow
Merge pull request #817 from IJHack/fix/usersdialog-perf

refactor: replace deprecated foreach with range-based for loops

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

925 of 5025 relevant lines covered (18.41%)

7.53 hits per line

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

0.0
/src/usersdialog.cpp
1
// SPDX-FileCopyrightText: 2016 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 <QCloseEvent>
7
#include <QKeyEvent>
8
#include <QMessageBox>
9
#include <QRegularExpression>
10
#include <QSet>
11
#include <QWidget>
12
#include <utility>
13

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

24
  ui->setupUi(this);
×
25

26
  QList<UserInfo> users = QtPassSettings::getPass()->listKeys();
×
27
  if (users.isEmpty()) {
×
28
    QMessageBox::critical(parent, tr("Keylist missing"),
×
29
                          tr("Could not fetch list of available GPG keys"));
×
30
    return;
31
  }
32

33
  QList<UserInfo> secret_keys = QtPassSettings::getPass()->listKeys("", true);
×
34
  QSet<QString> secretKeyIds;
NEW
35
  for (const UserInfo &sec : secret_keys) {
×
NEW
36
    secretKeyIds.insert(sec.key_id);
×
37
  }
NEW
38
  for (auto &user : users) {
×
NEW
39
    if (secretKeyIds.contains(user.key_id)) {
×
NEW
40
      user.have_secret = true;
×
41
    }
42
  }
43

44
  QList<UserInfo> selected_users;
×
45
  int count = 0;
×
46

47
  QStringList recipients = QtPassSettings::getPass()->getRecipientString(
×
48
      m_dir.isEmpty() ? "" : m_dir, " ", &count);
×
49
  if (!recipients.isEmpty()) {
×
50
    selected_users = QtPassSettings::getPass()->listKeys(recipients);
×
51
  }
52
  QSet<QString> selectedKeyIds;
NEW
53
  for (const UserInfo &sel : selected_users) {
×
NEW
54
    selectedKeyIds.insert(sel.key_id);
×
55
  }
NEW
56
  for (auto &user : users) {
×
NEW
57
    if (selectedKeyIds.contains(user.key_id)) {
×
NEW
58
      user.enabled = true;
×
59
    }
60
  }
61

62
  if (count > selected_users.size()) {
×
63
    // Some keys seem missing from keyring, add them separately
64
    QStringList recipients = QtPassSettings::getPass()->getRecipientList(
×
65
        m_dir.isEmpty() ? "" : m_dir);
×
NEW
66
    for (const QString &recipient : recipients) {
×
67
      if (QtPassSettings::getPass()->listKeys(recipient).empty()) {
×
68
        UserInfo i;
×
69
        i.enabled = true;
×
70
        i.key_id = recipient;
×
71
        i.name = " ?? " + tr("Key not found in keyring");
×
72
        users.append(i);
73
      }
×
74
    }
75
  }
76

77
  m_userList = users;
78
  populateList();
×
79

80
  connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
×
81
          &UsersDialog::accept);
×
82
  connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
×
83
  connect(ui->listWidget, &QListWidget::itemChanged, this,
×
84
          &UsersDialog::itemChange);
×
85

86
  ui->lineEdit->setClearButtonEnabled(true);
×
87
}
×
88

89
/**
90
 * @brief UsersDialog::~UsersDialog basic destructor.
91
 */
92
UsersDialog::~UsersDialog() { delete ui; }
×
93

94
Q_DECLARE_METATYPE(UserInfo *)
×
95

96
/**
97
 * @brief UsersDialog::accept
98
 */
99
void UsersDialog::accept() {
×
100
  QtPassSettings::getPass()->Init(m_dir, m_userList);
×
101

102
  QDialog::accept();
×
103
}
×
104

105
/**
106
 * @brief UsersDialog::closeEvent might have to store size and location if that
107
 * is wanted.
108
 * @param event
109
 */
110
void UsersDialog::closeEvent(QCloseEvent *event) {
×
111
  // TODO(annejan): save window size or something
112
  event->accept();
113
}
×
114

115
/**
116
 * @brief UsersDialog::keyPressEvent clear the lineEdit when escape is pressed.
117
 * No action for Enter currently.
118
 * @param event
119
 */
120
void UsersDialog::keyPressEvent(QKeyEvent *event) {
×
121
  switch (event->key()) {
×
122
  case Qt::Key_Escape:
×
123
    ui->lineEdit->clear();
×
124
    break;
×
125
  default:
126
    break;
127
  }
128
}
×
129

130
/**
131
 * @brief UsersDialog::itemChange update the item information.
132
 * @param item
133
 */
134
void UsersDialog::itemChange(QListWidgetItem *item) {
×
135
  if (!item) {
×
136
    return;
137
  }
138
  auto *info = item->data(Qt::UserRole).value<UserInfo *>();
×
139
  if (!info) {
×
140
    return;
141
  }
142
  info->enabled = item->checkState() == Qt::Checked;
×
143
}
144

145
/**
146
 * @brief UsersDialog::populateList update the view based on filter options
147
 * (such as searching).
148
 * @param filter
149
 */
150
void UsersDialog::populateList(const QString &filter) {
×
151
  QRegularExpression nameFilter(
152
      QRegularExpression::wildcardToRegularExpression("*" + filter + "*"),
×
153
      QRegularExpression::CaseInsensitiveOption);
×
154
  ui->listWidget->clear();
×
155

156
  for (auto &user : m_userList) {
×
157
    if (!passesFilter(user, filter, nameFilter)) {
×
158
      continue;
×
159
    }
160

161
    auto *item = new QListWidgetItem(buildUserText(user), ui->listWidget);
×
162
    applyUserStyling(item, user);
×
163
    item->setCheckState(user.enabled ? Qt::Checked : Qt::Unchecked);
×
164
    item->setData(Qt::UserRole, QVariant::fromValue(&user));
×
165
    ui->listWidget->addItem(item);
×
166
  }
167
}
×
168

169
bool UsersDialog::passesFilter(const UserInfo &user, const QString &filter,
×
170
                               const QRegularExpression &nameFilter) const {
171
  if (!filter.isEmpty() && !nameFilter.match(user.name).hasMatch()) {
×
172
    return false;
173
  }
174
  if (!user.isValid() && !ui->checkBox->isChecked()) {
×
175
    return false;
176
  }
177
  bool expired = user.expiry.toSecsSinceEpoch() > 0 &&
×
178
                 user.expiry.daysTo(QDateTime::currentDateTime()) > 0;
×
179
  return !(expired && !ui->checkBox->isChecked());
×
180
}
181

182
QString UsersDialog::buildUserText(const UserInfo &user) const {
×
183
  QString text = user.name + "\n" + user.key_id;
×
184
  if (user.created.toSecsSinceEpoch() > 0) {
×
185
    text += " " + tr("created") + " " +
×
186
            QLocale::system().toString(user.created, QLocale::ShortFormat);
×
187
  }
188
  if (user.expiry.toSecsSinceEpoch() > 0) {
×
189
    text += " " + tr("expires") + " " +
×
190
            QLocale::system().toString(user.expiry, QLocale::ShortFormat);
×
191
  }
192
  return text;
×
193
}
194

195
void UsersDialog::applyUserStyling(QListWidgetItem *item,
×
196
                                   const UserInfo &user) const {
197
  if (user.have_secret) {
×
198
    item->setForeground(Qt::blue);
×
199
    QFont font;
×
200
    font.setBold(true);
201
    item->setFont(font);
×
202
  } else if (!user.isValid()) {
×
203
    item->setBackground(Qt::darkRed);
×
204
    item->setForeground(Qt::white);
×
205
  } else if (user.expiry.toSecsSinceEpoch() > 0 &&
×
206
             user.expiry.daysTo(QDateTime::currentDateTime()) > 0) {
×
207
    item->setForeground(Qt::darkRed);
×
208
  } else if (!user.fullyValid()) {
×
209
    item->setBackground(Qt::darkYellow);
×
210
    item->setForeground(Qt::white);
×
211
  }
212
}
×
213

214
/**
215
 * @brief UsersDialog::on_lineEdit_textChanged typing in the searchbox.
216
 * @param filter
217
 */
218
void UsersDialog::on_lineEdit_textChanged(const QString &filter) {
×
219
  populateList(filter);
×
220
}
×
221

222
/**
223
 * @brief UsersDialog::on_checkBox_clicked filtering.
224
 */
225
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