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

IJHack / QtPass / 24284297576

11 Apr 2026 02:15PM UTC coverage: 20.88%. First build
24284297576

Pull #968

github

web-flow
Merge 8a4eaa84e into 71fc52f07
Pull Request #968: refactor: split UsersDialog constructor into helper methods

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

1106 of 5297 relevant lines covered (20.88%)

7.75 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
  loadGpgKeys();
×
29

NEW
30
  populateList();
×
31

NEW
32
  connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
×
NEW
33
          &UsersDialog::accept);
×
NEW
34
  connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
×
NEW
35
  connect(ui->listWidget, &QListWidget::itemChanged, this,
×
NEW
36
          &UsersDialog::itemChange);
×
37

NEW
38
  ui->lineEdit->setClearButtonEnabled(true);
×
NEW
39
}
×
40

NEW
41
void UsersDialog::restoreDialogState() {
×
42
  QByteArray savedGeometry = QtPassSettings::getDialogGeometry("usersDialog");
×
43
  bool hasSavedGeometry = !savedGeometry.isEmpty();
44
  if (hasSavedGeometry) {
×
45
    restoreGeometry(savedGeometry);
×
46
  }
47
  if (QtPassSettings::isDialogMaximized("usersDialog")) {
×
48
    showMaximized();
×
49
  } else if (hasSavedGeometry) {
×
50
    move(QtPassSettings::getDialogPos("usersDialog"));
×
51
    resize(QtPassSettings::getDialogSize("usersDialog"));
×
52
  }
NEW
53
}
×
54

NEW
55
void UsersDialog::loadGpgKeys() {
×
56
  QList<UserInfo> users = QtPassSettings::getPass()->listKeys();
×
57
  if (users.isEmpty()) {
×
NEW
58
    QMessageBox::critical(parentWidget(), tr("Keylist missing"),
×
59
                          tr("Could not fetch list of available GPG keys"));
×
60
    reject();
×
61
    return;
62
  }
63

64
  QList<UserInfo> secret_keys = QtPassSettings::getPass()->listKeys("", true);
×
65
  QSet<QString> secretKeyIds;
66
  for (const UserInfo &sec : secret_keys) {
×
67
    secretKeyIds.insert(sec.key_id);
×
68
  }
69
  for (auto &user : users) {
×
70
    if (secretKeyIds.contains(user.key_id)) {
×
71
      user.have_secret = true;
×
72
    }
73
  }
74

75
  m_userList = users;
NEW
76
  loadRecipients();
×
77
}
78

NEW
79
void UsersDialog::loadRecipients() {
×
80
  QList<UserInfo> selectedUsers = QtPassSettings::getPass()->listKeys(
NEW
81
      QtPassSettings::getPass()->getRecipientString(
×
NEW
82
          m_dir.isEmpty() ? "" : m_dir, " "));
×
83
  QSet<QString> selectedKeyIds;
NEW
84
  for (const UserInfo &sel : selectedUsers) {
×
85
    selectedKeyIds.insert(sel.key_id);
×
86
  }
NEW
87
  for (auto &user : m_userList) {
×
88
    if (selectedKeyIds.contains(user.key_id)) {
×
89
      user.enabled = true;
×
90
    }
91
  }
92

NEW
93
  int count = 0;
×
NEW
94
  QStringList recipients = QtPassSettings::getPass()->getRecipientString(
×
NEW
95
      m_dir.isEmpty() ? "" : m_dir, " ", &count);
×
96

NEW
97
  if (count > static_cast<int>(m_userList.size())) {
×
98
    QStringList allRecipients = QtPassSettings::getPass()->getRecipientList(
×
99
        m_dir.isEmpty() ? "" : m_dir);
×
100
    QSet<QString> missingKeyRecipients;
101
    for (const QString &recipient : allRecipients) {
×
NEW
102
      if (!missingKeyRecipients.contains(recipient) &&
×
103
          QtPassSettings::getPass()->listKeys(recipient).empty()) {
×
104
        missingKeyRecipients.insert(recipient);
×
105
        UserInfo i;
×
106
        i.enabled = true;
×
107
        i.key_id = recipient;
×
108
        i.name = " ?? " + tr("Key not found in keyring");
×
109
        m_userList.append(i);
110
      }
×
111
    }
112
  }
113
}
×
114

115
/**
116
 * @brief UsersDialog::~UsersDialog basic destructor.
117
 */
118
UsersDialog::~UsersDialog() { delete ui; }
×
119

120
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
121
Q_DECLARE_METATYPE(UserInfo *)
122
Q_DECLARE_METATYPE(const UserInfo *)
123
#endif
124

125
/**
126
 * @brief UsersDialog::accept
127
 */
128
void UsersDialog::accept() {
×
129
  QtPassSettings::getPass()->Init(m_dir, m_userList);
×
130

131
  QDialog::accept();
×
132
}
×
133

134
/**
135
 * @brief UsersDialog::closeEvent save window state on close.
136
 * @param event
137
 */
138
void UsersDialog::closeEvent(QCloseEvent *event) {
×
139
  QtPassSettings::setDialogGeometry("usersDialog", saveGeometry());
×
140
  if (!isMaximized()) {
×
141
    QtPassSettings::setDialogPos("usersDialog", pos());
×
142
    QtPassSettings::setDialogSize("usersDialog", size());
×
143
  }
144
  QtPassSettings::setDialogMaximized("usersDialog", isMaximized());
×
145
  event->accept();
146
}
×
147

148
/**
149
 * @brief UsersDialog::keyPressEvent clear the lineEdit when escape is pressed.
150
 * No action for Enter currently.
151
 * @param event
152
 */
153
void UsersDialog::keyPressEvent(QKeyEvent *event) {
×
154
  switch (event->key()) {
×
155
  case Qt::Key_Escape:
×
156
    ui->lineEdit->clear();
×
157
    break;
×
158
  default:
159
    break;
160
  }
161
}
×
162

163
/**
164
 * @brief UsersDialog::itemChange update the item information.
165
 * @param item
166
 */
167
void UsersDialog::itemChange(QListWidgetItem *item) {
×
168
  if (!item) {
×
169
    return;
×
170
  }
171
  bool ok = false;
×
172
  const int index = item->data(Qt::UserRole).toInt(&ok);
×
173
  if (!ok) {
×
174
#ifdef QT_DEBUG
175
    qWarning() << "UsersDialog::itemChange: invalid user index data for item";
176
#endif
177
    return;
178
  }
179
  if (index < 0 || index >= m_userList.size()) {
×
180
#ifdef QT_DEBUG
181
    qWarning() << "UsersDialog::itemChange: user index out of range:" << index
182
               << "valid range is [0," << (m_userList.size() - 1) << "]";
183
#endif
184
    return;
185
  }
186
  m_userList[index].enabled = item->checkState() == Qt::Checked;
×
187
}
188

189
/**
190
 * @brief UsersDialog::populateList update the view based on filter options
191
 * (such as searching).
192
 * @param filter
193
 */
194
void UsersDialog::populateList(const QString &filter) {
×
195
  static QString lastFilter;
×
196
  static QRegularExpression cachedNameFilter;
×
197
  if (filter != lastFilter) {
×
198
    lastFilter = filter;
×
199
    cachedNameFilter = QRegularExpression(
×
200
        QRegularExpression::wildcardToRegularExpression("*" + filter + "*"),
×
201
        QRegularExpression::CaseInsensitiveOption);
202
  }
203
  const QRegularExpression &nameFilter = cachedNameFilter;
204
  ui->listWidget->clear();
×
205

206
  for (int i = 0; i < m_userList.size(); ++i) {
×
207
    const auto &user = m_userList.at(i);
208
    if (!passesFilter(user, filter, nameFilter)) {
×
209
      continue;
×
210
    }
211

212
    auto *item = new QListWidgetItem(buildUserText(user), ui->listWidget);
×
213
    applyUserStyling(item, user);
×
214
    item->setCheckState(user.enabled ? Qt::Checked : Qt::Unchecked);
×
215
    item->setData(Qt::UserRole, QVariant::fromValue(i));
×
216
    ui->listWidget->addItem(item);
×
217
  }
218
}
×
219

220
/**
221
 * @brief Checks if a user passes the filter criteria.
222
 * @param user User to check
223
 * @param filter Filter string
224
 * @param nameFilter Compiled name filter regex
225
 * @return true if user passes filter
226
 */
227
bool UsersDialog::passesFilter(const UserInfo &user, const QString &filter,
×
228
                               const QRegularExpression &nameFilter) const {
229
  if (!filter.isEmpty() && !nameFilter.match(user.name).hasMatch()) {
×
230
    return false;
231
  }
232
  if (!user.isValid() && !ui->checkBox->isChecked()) {
×
233
    return false;
234
  }
235
  const bool expired = isUserExpired(user);
×
236
  return !(expired && !ui->checkBox->isChecked());
×
237
}
238

239
/**
240
 * @brief Checks if a user's key has expired.
241
 * @param user User to check
242
 * @return true if user's key is expired
243
 */
244
auto UsersDialog::isUserExpired(const UserInfo &user) const -> bool {
×
245
  return user.expiry.toSecsSinceEpoch() > 0 &&
×
246
         QDateTime::currentDateTime() > user.expiry;
×
247
}
248

249
/**
250
 * @brief Builds display text for a user.
251
 * @param user User to format
252
 * @return Formatted user text
253
 */
254
QString UsersDialog::buildUserText(const UserInfo &user) const {
×
255
  QString text = user.name + "\n" + user.key_id;
×
256
  if (user.created.toSecsSinceEpoch() > 0) {
×
257
    text += " " + tr("created") + " " +
×
258
            QLocale::system().toString(user.created, QLocale::ShortFormat);
×
259
  }
260
  if (user.expiry.toSecsSinceEpoch() > 0) {
×
261
    text += " " + tr("expires") + " " +
×
262
            QLocale::system().toString(user.expiry, QLocale::ShortFormat);
×
263
  }
264
  return text;
×
265
}
266

267
/**
268
 * @brief Applies visual styling to a user list item based on key status.
269
 * @param item List widget item to style
270
 * @param user User whose status determines styling
271
 */
272
void UsersDialog::applyUserStyling(QListWidgetItem *item,
×
273
                                   const UserInfo &user) const {
274
  const QString originalText = item->text();
×
275
  if (user.have_secret) {
×
276
    const QPalette palette = QApplication::palette();
×
277
    item->setForeground(palette.color(QPalette::Link));
×
278
    QFont font = item->font();
×
279
    font.setBold(true);
280
    item->setFont(font);
×
281
  } else if (!user.isValid()) {
×
282
    item->setBackground(Qt::darkRed);
×
283
    item->setForeground(Qt::white);
×
284
    item->setText(tr("[INVALID] ") + originalText);
×
285
  } else if (isUserExpired(user)) {
×
286
    item->setForeground(Qt::darkRed);
×
287
    item->setText(tr("[EXPIRED] ") + originalText);
×
288
  } else if (!user.fullyValid()) {
×
289
    item->setBackground(Qt::darkYellow);
×
290
    item->setForeground(Qt::white);
×
291
    item->setText(tr("[PARTIAL] ") + originalText);
×
292
  } else {
293
    item->setText(originalText);
×
294
  }
295
}
×
296

297
/**
298
 * @brief UsersDialog::on_lineEdit_textChanged typing in the searchbox.
299
 * @param filter
300
 */
301
void UsersDialog::on_lineEdit_textChanged(const QString &filter) {
×
302
  populateList(filter);
×
303
}
×
304

305
/**
306
 * @brief UsersDialog::on_checkBox_clicked filtering.
307
 */
308
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