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

IJHack / QtPass / 24855696817

23 Apr 2026 07:53PM UTC coverage: 26.946% (-0.05%) from 26.998%
24855696817

Pull #1143

github

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

3 of 19 new or added lines in 2 files covered. (15.79%)

15 existing lines in 2 files now uncovered.

1634 of 6064 relevant lines covered (26.95%)

29.23 hits per line

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

46.67
/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->removeRow(line);
×
200
  }
201
  templateLines.clear();
7✔
202

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

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

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

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

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

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

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

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

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