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

IJHack / QtPass / 27694030650

17 Jun 2026 01:52PM UTC coverage: 57.238% (-0.2%) from 57.47%
27694030650

push

github

web-flow
refactor(#1511): inject Pass*/AppSettings into dialog layer (#1553)

* refactor(#1511): inject Pass*/AppSettings into dialog layer

- ImportKeyDialog: constructor now takes const QString &gpgExe;
  importFromString() uses m_gpgExe instead of QtPassSettings::getGpgExecutable();
  removes qtpasssettings.h dependency. Call site in usersdialog.cpp updated.

- PasswordDisplayPanel: displayFields() and appendField() now take
  const AppSettings &s; addField() receives it transitively. Replaces
  four QtPassSettings:: reads (clipBoardType, useQrencode, useMonospace,
  hidePassword) with struct fields; removes qtpasssettings.h dependency.
  Tests updated with AppSettings{} default.

- PasswordDialog (second constructor): signature changed to
  (Pass*, const AppSettings&, QString, bool, QWidget*). Settings
  injected at construction time; m_pass stored as member so
  on_createPasswordButton_clicked() and on_accepted() also use it.
  First constructor still resolves Pass* via QtPassSettings::getPass()
  for test compatibility.

Part of the #1511 AppSettings injection series (PR C).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(review): single-snapshot passShowHandler; document m_pass lifetime

- passShowHandler: replace 7 separate QtPassSettings singleton reads
  with one AppSettings s = QtPassSettings::load() at the top of the
  handler; guard conditions (s.hideContent, s.displayAsIs) and the
  AppSettings passed to displayFields() now come from the same snapshot,
  eliminating the mixed-read inconsistency flagged in PR review.

- PasswordDialog first constructor: add a comment explaining that
  m_pass is captured once from the singleton and that this constructor
  is test-only; production always uses the injected-Pass* overload.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

11 of 36 new or added lines in 5 files covered. (30.56%)

20 existing lines in 4 files now uncovered.

3966 of 6929 relevant lines covered (57.24%)

23.31 hits per line

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

69.63
/src/passworddialog.cpp
1
// SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "passworddialog.h"
4
#include "filecontent.h"
5
#include "pass.h"
6
#include "passwordconfiguration.h"
7
#include "qtpasssettings.h"
8
#include "ui_passworddialog.h"
9
#include "util.h"
10
#include <algorithm>
11

12
#include <QHash>
13
#include <QLabel>
14
#include <QLineEdit>
15
#include <QShortcut>
16
#include <utility>
17

18
#ifdef QT_DEBUG
19
#include "debughelper.h"
20
#endif
21

22
/**
23
 * @brief PasswordDialog::PasswordDialog basic constructor.
24
 * @param passConfig configuration constant
25
 * @param parent
26
 */
27
PasswordDialog::PasswordDialog(PasswordConfiguration passConfig,
17✔
28
                               QWidget *parent)
17✔
29
    : QDialog(parent), ui(new Ui::PasswordDialog),
34✔
30
      m_passConfig(std::move(passConfig)), m_pass(QtPassSettings::getPass()) {
17✔
31
  // m_pass is captured once from the singleton here. This constructor is
32
  // only reached from tests; production always uses the two-arg overload
33
  // that receives an explicit Pass* with a stable, caller-controlled lifetime.
34
  m_templating = false;
35
  m_isNew = false;
36

37
  ui->setupUi(this);
17✔
38
  setLength(m_passConfig.length);
17✔
39
  setPasswordCharTemplate(m_passConfig.selected);
17✔
40

41
  connect(m_pass, &Pass::finishedShow, this, &PasswordDialog::setPass);
17✔
42
}
17✔
43

44
/**
45
 * @brief PasswordDialog::PasswordDialog complete constructor.
46
 * @param file
47
 * @param isNew
48
 * @param parent pointer
49
 */
NEW
50
PasswordDialog::PasswordDialog(Pass *pass, const AppSettings &s, QString file,
×
NEW
51
                               const bool &isNew, QWidget *parent)
×
NEW
52
    : QDialog(parent), ui(new Ui::PasswordDialog), m_pass(pass),
×
NEW
53
      m_file(std::move(file)), m_isNew(isNew) {
×
54

55
  if (!isNew) {
×
NEW
56
    m_pass->Show(m_file);
×
57
  }
58

59
  ui->setupUi(this);
×
60

61
  setWindowTitle(this->windowTitle() + " " + m_file);
×
NEW
62
  m_passConfig = s.passwordConfiguration;
×
NEW
63
  usePwgen(s.usePwgen);
×
NEW
64
  setTemplate(s.passTemplate, s.useTemplate);
×
65

66
  setLength(m_passConfig.length);
×
67
  setPasswordCharTemplate(m_passConfig.selected);
×
68

NEW
69
  connect(m_pass, &Pass::finishedShow, this, &PasswordDialog::setPass);
×
NEW
70
  connect(m_pass, &Pass::processErrorExit, this, &PasswordDialog::close);
×
71
  connect(this, &PasswordDialog::accepted, this, &PasswordDialog::on_accepted);
×
72
  connect(this, &PasswordDialog::rejected, this, &PasswordDialog::on_rejected);
×
73
}
×
74

75
/**
76
 * @brief PasswordDialog::~PasswordDialog basic destructor.
77
 */
78
PasswordDialog::~PasswordDialog() { delete ui; }
68✔
79

80
/**
81
 * @brief PasswordDialog::on_checkBoxShow_stateChanged hide or show passwords.
82
 * @param arg1
83
 */
84
void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
2✔
85
  if (arg1) {
2✔
86
    ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
1✔
87
  } else {
88
    ui->lineEditPassword->setEchoMode(QLineEdit::Password);
1✔
89
  }
90
}
2✔
91

92
/**
93
 * @brief PasswordDialog::on_createPasswordButton_clicked generate a random
94
 * password.
95
 */
96
void PasswordDialog::on_createPasswordButton_clicked() {
×
97
  ui->widget->setEnabled(false);
×
98
  const int currentIndex = ui->passwordTemplateSwitch->currentIndex();
×
99
  if (currentIndex < 0 ||
×
100
      currentIndex >= static_cast<int>(PasswordConfiguration::CHARSETS_COUNT)) {
101
    ui->widget->setEnabled(true);
×
102
    return;
×
103
  }
104

NEW
105
  QString newPass = m_pass->generatePassword(
×
106
      static_cast<unsigned int>(ui->spinBox_pwdLength->value()),
×
107
      m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(
108
          currentIndex)]);
×
109
  if (!newPass.isEmpty()) {
×
110
    ui->lineEditPassword->setText(newPass);
×
111
  }
112
  ui->widget->setEnabled(true);
×
113
}
114

115
/**
116
 * @brief PasswordDialog::on_accepted handle Ok click for QDialog
117
 */
118
void PasswordDialog::on_accepted() {
×
119
  QString newValue = getPassword();
×
120
  if (newValue.isEmpty()) {
×
121
    return;
122
  }
123

124
  if (newValue.right(1) != "\n") {
×
125
    newValue += "\n";
×
126
  }
127

NEW
128
  m_pass->Insert(m_file, newValue, !m_isNew);
×
129
}
130

131
/**
132
 * @brief PasswordDialog::on_rejected handle Cancel click for QDialog
133
 */
134
void PasswordDialog::on_rejected() { setPassword(QString()); }
×
135

136
/**
137
 * @brief PasswordDialog::setPassword populate the (templated) fields.
138
 * @param password
139
 */
140
void PasswordDialog::setPassword(const QString &password) {
9✔
141
  // Always parse all fields as editable so users can edit any field in the
142
  // password file. This fixes issue #132 where users couldn't edit
143
  // fields without toggling the "Show all fields templated" setting.
144
  FileContent fileContent = FileContent::parse(password, m_fields, true);
9✔
145
  ui->lineEditPassword->setText(fileContent.getPassword());
9✔
146

147
  QWidget *previous = ui->checkBoxShow;
9✔
148
  // first set templated values
149
  NamedValues namedValues = fileContent.getNamedValues();
9✔
150
  for (QLineEdit *line : std::as_const(m_templateLines)) {
10✔
151
    line->setText(namedValues.takeValue(line->objectName()));
2✔
152
    previous = line;
153
  }
154
  // show remaining values (if there are)
155
  // Remove previously created dynamic widgets to prevent duplicates and leaks
156
  for (QLineEdit *line : std::as_const(m_otherLines)) {
9✔
157
    ui->formLayout->removeRow(line);
×
158
  }
159
  m_otherLines.clear();
9✔
160
  for (const NamedValue &nv : std::as_const(namedValues)) {
12✔
161
    auto *line = new QLineEdit();
3✔
162
    line->setObjectName(nv.name);
3✔
163
    line->setText(nv.value);
3✔
164
    ui->formLayout->addRow(new QLabel(nv.name), line);
3✔
165
    setTabOrder(previous, line);
3✔
166
    m_otherLines.append(line);
3✔
167
    previous = line;
168
  }
169

170
  ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
18✔
171
}
9✔
172

173
/**
174
 * @brief PasswordDialog::getPassword  join the (templated) fields to a QString
175
 * for writing back.
176
 * @return collapsed password.
177
 */
178
auto PasswordDialog::getPassword() -> QString {
9✔
179
  QString passFile = ui->lineEditPassword->text() + "\n";
18✔
180
  QList<QLineEdit *> allLines(m_templateLines);
181
  allLines.append(m_otherLines);
9✔
182
  for (QLineEdit *line : std::as_const(allLines)) {
13✔
183
    QString text = line->text();
4✔
184
    if (text.isEmpty()) {
4✔
185
      continue;
186
    }
187
    passFile += line->objectName() + ": " + text + "\n";
8✔
188
  }
189
  passFile += ui->plainTextEdit->toPlainText();
18✔
190
  return passFile;
9✔
191
}
192

193
/**
194
 * @brief PasswordDialog::setTemplate set the template and create the fields.
195
 * @param rawFields
196
 */
197
void PasswordDialog::setTemplate(const QString &rawFields, bool useTemplate) {
12✔
198
  m_fields = rawFields.split('\n');
12✔
199
  m_templating = useTemplate;
12✔
200

201
  for (QLineEdit *line : std::as_const(m_templateLines)) {
14✔
202
    ui->formLayout->removeRow(line);
2✔
203
  }
204
  m_templateLines.clear();
12✔
205

206
  // Defensively remove all rows tracked in m_otherLines to prevent accumulation
207
  // when cycling templates or applying new templates after setPassword
208
  for (QLineEdit *line : std::as_const(m_otherLines)) {
12✔
209
    ui->formLayout->removeRow(line);
×
210
  }
211
  m_otherLines.clear();
12✔
212

213
  if (m_templating) {
12✔
214
    QWidget *previous = ui->checkBoxShow;
6✔
215
    for (const QString &field : std::as_const(m_fields)) {
13✔
216
      if (field.isEmpty()) {
7✔
217
        continue;
×
218
      }
219
      auto *line = new QLineEdit();
7✔
220
      auto *label = new QLabel(field);
7✔
221
      line->setObjectName(field);
7✔
222
      ui->formLayout->addRow(label, line);
7✔
223
      setTabOrder(previous, line);
7✔
224
      m_templateLines.append(line);
7✔
225
      previous = line;
226
    }
227
  }
228
}
12✔
229

230
/**
231
 * @brief PasswordDialog::setLength
232
 * PasswordDialog::setLength password length.
233
 * @param length
234
 */
235
void PasswordDialog::setLength(int length) {
19✔
236
  ui->spinBox_pwdLength->setValue(length);
19✔
237
}
19✔
238

239
/**
240
 * @brief PasswordDialog::setPasswordCharTemplate
241
 * PasswordDialog::setPasswordCharTemplate chose the template style.
242
 * @param templateIndex
243
 */
244
void PasswordDialog::setPasswordCharTemplate(int templateIndex) {
19✔
245
  ui->passwordTemplateSwitch->setCurrentIndex(templateIndex);
19✔
246
}
19✔
247

248
/**
249
 * @brief PasswordDialog::usePwgen
250
 * PasswordDialog::usePwgen don't use own password generator.
251
 * @param usePwgen
252
 */
253
void PasswordDialog::usePwgen(bool usePwgen) {
2✔
254
  ui->passwordTemplateSwitch->setDisabled(usePwgen);
2✔
255
  ui->label_characterset->setDisabled(usePwgen);
2✔
256
}
2✔
257

258
/**
259
 * @brief Set available templates from .templates file and apply default.
260
 * @param templates Hash of template name to field list.
261
 * @param defaultTemplate Name of default template to select.
262
 */
263
void PasswordDialog::setAvailableTemplates(
3✔
264
    const QHash<QString, QStringList> &templates,
265
    const QString &defaultTemplate) {
266
  m_availableTemplates = templates;
3✔
267
  QStringList templateNames = templates.keys();
3✔
268
  if (templateNames.isEmpty()) {
3✔
269
    return;
270
  }
271
  std::sort(templateNames.begin(), templateNames.end());
6✔
272
  QString selected = defaultTemplate;
273
  if (!templateNames.contains(selected)) {
3✔
274
    selected = templateNames.first();
×
275
  }
276
  applyTemplate(selected);
3✔
277
}
278

279
/**
280
 * @brief Apply a template by name.
281
 * @param templateName Name of template to apply.
282
 */
283
void PasswordDialog::applyTemplate(const QString &templateName) {
5✔
284
  auto it = m_availableTemplates.constFind(templateName);
5✔
285
  if (it != m_availableTemplates.constEnd()) {
286
    m_currentTemplateName = templateName;
5✔
287
    QString fields = it.value().join("\n");
5✔
288
    setTemplate(fields, true);
5✔
289
  }
290
}
5✔
291

292
/**
293
 * @brief Cycle to next template (Ctrl+T).
294
 */
295
void PasswordDialog::cycleTemplate() {
4✔
296
  if (m_availableTemplates.isEmpty()) {
297
    return;
2✔
298
  }
299
  QStringList names = m_availableTemplates.keys();
2✔
300
  std::sort(names.begin(), names.end());
4✔
301

302
  qsizetype currentIdx = names.indexOf(m_currentTemplateName);
303
  qsizetype nextIdx;
304
  if (currentIdx < 0) {
2✔
305
    nextIdx = 0;
306
  } else {
307
    nextIdx = (currentIdx + 1) % names.size();
2✔
308
  }
309
  applyTemplate(names.at(nextIdx));
2✔
310
}
311

312
/**
313
 * @brief Sets the password from pass show output.
314
 * @param output Output from pass show command
315
 */
316
void PasswordDialog::setPass(const QString &output) {
9✔
317
  setPassword(output);
9✔
318
  // UI is enabled by default when password is set - no additional action needed
319
}
9✔
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