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

IJHack / QtPass / 23688200227

28 Mar 2026 03:22PM UTC coverage: 18.397% (-0.007%) from 18.404%
23688200227

push

github

web-flow
Merge pull request #822 from IJHack/ai-findings-autofix/src-usersdialog.cpp

Potential fixes for 3 code quality findings

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

925 of 5028 relevant lines covered (18.4%)

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"));
×
NEW
30
    reject();
×
31
  }
32

33
  QList<UserInfo> secret_keys = QtPassSettings::getPass()->listKeys("", true);
×
34
  QSet<QString> secretKeyIds;
35
  for (const UserInfo &sec : secret_keys) {
×
36
    secretKeyIds.insert(sec.key_id);
×
37
  }
38
  for (auto &user : users) {
×
39
    if (secretKeyIds.contains(user.key_id)) {
×
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;
53
  for (const UserInfo &sel : selected_users) {
×
54
    selectedKeyIds.insert(sel.key_id);
×
55
  }
56
  for (auto &user : users) {
×
57
    if (selectedKeyIds.contains(user.key_id)) {
×
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 allRecipients = QtPassSettings::getPass()->getRecipientList(
×
65
        m_dir.isEmpty() ? "" : m_dir);
×
66
    for (const QString &recipient : allRecipients) {
×
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 *)
×
NEW
95
Q_DECLARE_METATYPE(const UserInfo *)
×
96

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

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

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

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

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

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

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

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

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

182
bool UsersDialog::isUserExpired(const UserInfo &user) const {
×
183
  return user.expiry.toSecsSinceEpoch() > 0 &&
×
184
         QDateTime::currentDateTime() > user.expiry;
×
185
}
186

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

200
void UsersDialog::applyUserStyling(QListWidgetItem *item,
×
201
                                   const UserInfo &user) const {
202
  if (user.have_secret) {
×
203
    item->setForeground(Qt::blue);
×
NEW
204
    QFont font = item->font();
×
205
    font.setBold(true);
206
    item->setFont(font);
×
207
  } else if (!user.isValid()) {
×
208
    item->setBackground(Qt::darkRed);
×
209
    item->setForeground(Qt::white);
×
210
  } else if (isUserExpired(user)) {
×
211
    item->setForeground(Qt::darkRed);
×
212
  } else if (!user.fullyValid()) {
×
213
    item->setBackground(Qt::darkYellow);
×
214
    item->setForeground(Qt::white);
×
215
  }
216
}
×
217

218
/**
219
 * @brief UsersDialog::on_lineEdit_textChanged typing in the searchbox.
220
 * @param filter
221
 */
222
void UsersDialog::on_lineEdit_textChanged(const QString &filter) {
×
223
  populateList(filter);
×
224
}
×
225

226
/**
227
 * @brief UsersDialog::on_checkBox_clicked filtering.
228
 */
229
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