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

IJHack / QtPass / 24854486928

23 Apr 2026 07:25PM UTC coverage: 26.941% (-0.06%) from 26.998%
24854486928

Pull #1143

github

web-flow
Merge 5b1331c13 into 52106b058
Pull Request #1143: feat: add Ctrl+T to cycle between templates

3 of 20 new or added lines in 2 files covered. (15.0%)

15 existing lines in 2 files now uncovered.

1634 of 6065 relevant lines covered (26.94%)

29.22 hits per line

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

46.32
/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 "helpers.h"
6
#include "pass.h"
7
#include "passwordconfiguration.h"
8
#include "qtpasssettings.h"
9
#include "ui_passworddialog.h"
10
#include "util.h"
11
#include <algorithm>
12

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

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

23
/**
24
 * @brief PasswordDialog::PasswordDialog basic constructor.
25
 * @param passConfig configuration constant
26
 * @param parent
27
 */
28
PasswordDialog::PasswordDialog(PasswordConfiguration passConfig,
8✔
29
                               QWidget *parent)
8✔
30
    : QDialog(parent), ui(new Ui::PasswordDialog),
16✔
31
      m_passConfig(std::move(passConfig)) {
8✔
32
  m_templating = false;
33
  m_isNew = false;
8✔
34

35
  ui->setupUi(this);
8✔
36
  setLength(m_passConfig.length);
8✔
37
  setPasswordCharTemplate(m_passConfig.selected);
8✔
38

39
  connect(QtPassSettings::getPass(), &Pass::finishedShow, this,
8✔
40
          &PasswordDialog::setPass);
8✔
41
}
8✔
42

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

53
  if (!isNew) {
×
54
    QtPassSettings::getPass()->Show(m_file);
×
55
  }
56

57
  ui->setupUi(this);
×
58

59
  setWindowTitle(this->windowTitle() + " " + m_file);
×
60
  m_passConfig = QtPassSettings::getPasswordConfiguration();
×
61
  usePwgen(QtPassSettings::isUsePwgen());
×
62
  setTemplate(QtPassSettings::getPassTemplate(),
×
63
              QtPassSettings::isUseTemplate());
×
64

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

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

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

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

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

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

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

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

129
  QtPassSettings::getPass()->Insert(m_file, newValue, !m_isNew);
×
130
}
131

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

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

148
  QWidget *previous = ui->checkBoxShow;
8✔
149
  // first set templated values
150
  NamedValues namedValues = fileContent.getNamedValues();
8✔
151
  for (QLineEdit *line : AS_CONST(templateLines)) {
9✔
152
    line->setText(namedValues.takeValue(line->objectName()));
2✔
153
    previous = line;
154
  }
155
  // show remaining values (if there are)
156
  otherLines.clear();
8✔
157
  for (const NamedValue &nv : AS_CONST(namedValues)) {
11✔
158
    auto *line = new QLineEdit();
3✔
159
    line->setObjectName(nv.name);
3✔
160
    line->setText(nv.value);
3✔
161
    ui->formLayout->addRow(new QLabel(nv.name), line);
3✔
162
    setTabOrder(previous, line);
3✔
163
    otherLines.append(line);
3✔
164
    previous = line;
165
  }
166

167
  ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
16✔
168
}
8✔
169

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

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

198
  for (QLineEdit *line : std::as_const(templateLines)) {
7✔
NEW
199
    ui->formLayout->removeWidget(line);
×
NEW
200
    line->deleteLater();
×
201
  }
202
  templateLines.clear();
7✔
203

204
  if (m_templating) {
7✔
205
    QWidget *previous = ui->checkBoxShow;
1✔
206
    for (const QString &field : std::as_const(m_fields)) {
2✔
207
      if (field.isEmpty()) {
1✔
208
        continue;
×
209
      }
210
      auto *line = new QLineEdit();
1✔
211
      auto *label = new QLabel(field);
1✔
212
      line->setObjectName(field);
1✔
213
      ui->formLayout->addRow(label, line);
1✔
214
      setTabOrder(previous, line);
1✔
215
      templateLines.append(line);
1✔
216
      previous = line;
217
    }
218
  }
219
}
7✔
220

221
/**
222
 * @brief PasswordDialog::setLength
223
 * PasswordDialog::setLength password length.
224
 * @param length
225
 */
226
void PasswordDialog::setLength(int length) {
8✔
227
  ui->spinBox_pwdLength->setValue(length);
8✔
228
}
8✔
229

230
/**
231
 * @brief PasswordDialog::setPasswordCharTemplate
232
 * PasswordDialog::setPasswordCharTemplate chose the template style.
233
 * @param templateIndex
234
 */
235
void PasswordDialog::setPasswordCharTemplate(int templateIndex) {
8✔
236
  ui->passwordTemplateSwitch->setCurrentIndex(templateIndex);
8✔
237
}
8✔
238

239
/**
240
 * @brief PasswordDialog::usePwgen
241
 * PasswordDialog::usePwgen don't use own password generator.
242
 * @param usePwgen
243
 */
244
void PasswordDialog::usePwgen(bool usePwgen) {
×
245
  ui->passwordTemplateSwitch->setDisabled(usePwgen);
×
246
  ui->label_characterset->setDisabled(usePwgen);
×
247
}
×
248

249
/**
250
 * @brief Set available templates from .templates file and apply default.
251
 * @param templates Hash of template name to field list.
252
 * @param defaultTemplate Name of default template to select.
253
 */
254
void PasswordDialog::setAvailableTemplates(
×
255
    const QHash<QString, QStringList> &templates,
256
    const QString &defaultTemplate) {
NEW
257
  m_availableTemplates = templates;
×
258
  QStringList templateNames = templates.keys();
×
259
  if (templateNames.isEmpty()) {
×
260
    return;
261
  }
262
  std::sort(templateNames.begin(), templateNames.end());
×
263
  QString selected = defaultTemplate;
264
  if (!templateNames.contains(selected)) {
×
265
    selected = templateNames.first();
×
266
  }
NEW
267
  applyTemplate(selected);
×
268
}
269

270
/**
271
 * @brief Apply a template by name.
272
 * @param templateName Name of template to apply.
273
 */
NEW
274
void PasswordDialog::applyTemplate(const QString &templateName) {
×
NEW
275
  m_currentTemplateName = templateName;
×
NEW
276
  auto it = m_availableTemplates.constFind(templateName);
×
277
  if (it != m_availableTemplates.constEnd()) {
278
    QString fields = it.value().join("\n");
×
279
    setTemplate(fields, true);
×
280
  }
UNCOV
281
}
×
282

283
/**
284
 * @brief Cycle to next template (Ctrl+T).
285
 */
NEW
286
void PasswordDialog::cycleTemplate() {
×
287
  if (m_availableTemplates.isEmpty()) {
NEW
288
    return;
×
289
  }
NEW
290
  QStringList names = m_availableTemplates.keys();
×
NEW
291
  std::sort(names.begin(), names.end());
×
292

NEW
293
  int currentIdx = names.indexOf(m_currentTemplateName);
×
NEW
294
  if (currentIdx < 0) {
×
295
    currentIdx = 0;
296
  }
NEW
297
  int nextIdx = (currentIdx + 1) % names.size();
×
NEW
298
  applyTemplate(names.at(nextIdx));
×
299
}
300

301
/**
302
 * @brief Sets the password from pass show output.
303
 * @param output Output from pass show command
304
 */
305
void PasswordDialog::setPass(const QString &output) {
8✔
306
  setPassword(output);
8✔
307
  // UI is enabled by default when password is set - no additional action needed
308
}
8✔
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