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

IJHack / QtPass / 24854023917

23 Apr 2026 07:14PM UTC coverage: 26.908% (-0.09%) from 26.998%
24854023917

Pull #1143

github

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

0 of 21 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

1632 of 6065 relevant lines covered (26.91%)

29.22 hits per line

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

44.53
/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; }
24✔
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
  templateLines.clear();
7✔
198

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

215
/**
216
 * @brief PasswordDialog::setLength
217
 * PasswordDialog::setLength password length.
218
 * @param length
219
 */
220
void PasswordDialog::setLength(int length) {
8✔
221
  ui->spinBox_pwdLength->setValue(length);
8✔
222
}
8✔
223

224
/**
225
 * @brief PasswordDialog::setPasswordCharTemplate
226
 * PasswordDialog::setPasswordCharTemplate chose the template style.
227
 * @param templateIndex
228
 */
229
void PasswordDialog::setPasswordCharTemplate(int templateIndex) {
8✔
230
  ui->passwordTemplateSwitch->setCurrentIndex(templateIndex);
8✔
231
}
8✔
232

233
/**
234
 * @brief PasswordDialog::usePwgen
235
 * PasswordDialog::usePwgen don't use own password generator.
236
 * @param usePwgen
237
 */
238
void PasswordDialog::usePwgen(bool usePwgen) {
×
239
  ui->passwordTemplateSwitch->setDisabled(usePwgen);
×
240
  ui->label_characterset->setDisabled(usePwgen);
×
241
}
×
242

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

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

276
/**
277
 * @brief Cycle to next template (Ctrl+T).
278
 */
NEW
279
void PasswordDialog::cycleTemplate() {
×
280
  if (m_availableTemplates.isEmpty()) {
NEW
281
    return;
×
282
  }
NEW
283
  QStringList names;
×
NEW
284
  names.reserve(m_availableTemplates.size());
×
NEW
285
  for (auto it = m_availableTemplates.constKeyValueBegin();
×
286
       it != m_availableTemplates.constKeyValueEnd(); ++it) {
287
    names.append(it->first);
288
  }
NEW
289
  std::sort(names.begin(), names.end());
×
290

NEW
291
  QString currentName;
×
NEW
292
  for (const QString &field : std::as_const(m_fields)) {
×
NEW
293
    if (!field.isEmpty()) {
×
NEW
294
      currentName = field;
×
NEW
295
      break;
×
296
    }
297
  }
298

NEW
299
  int currentIdx = names.indexOf(currentName);
×
NEW
300
  int nextIdx = (currentIdx + 1) % names.size();
×
UNCOV
301
  applyTemplate(names.at(nextIdx));
×
302
}
303

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