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

IJHack / QtPass / 24852791986

23 Apr 2026 06:47PM UTC coverage: 27.038% (-0.3%) from 27.334%
24852791986

push

github

web-flow
Merge pull request #1141 from IJHack/feat/multi-template-support

feat: multi-template support via .templates files

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

1 existing line in 1 file now uncovered.

1632 of 6036 relevant lines covered (27.04%)

29.36 hits per line

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

51.26
/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 <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,
8✔
28
                               QWidget *parent)
8✔
29
    : QDialog(parent), ui(new Ui::PasswordDialog),
16✔
30
      m_passConfig(std::move(passConfig)) {
8✔
31
  m_templating = false;
32
  m_isNew = false;
8✔
33

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

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

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

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

56
  ui->setupUi(this);
×
57

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

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

67
  connect(QtPassSettings::getPass(), &Pass::finishedShow, this,
×
68
          &PasswordDialog::setPass);
×
69
  connect(QtPassSettings::getPass(), &Pass::processErrorExit, this,
×
70
          &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; }
24✔
79

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

105
  QString newPass = QtPassSettings::getPass()->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

128
  QtPassSettings::getPass()->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) {
8✔
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);
8✔
145
  ui->lineEditPassword->setText(fileContent.getPassword());
8✔
146

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

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

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

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

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

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

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

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

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

266
/**
267
 * @brief Sets the password from pass show output.
268
 * @param output Output from pass show command
269
 */
270
void PasswordDialog::setPass(const QString &output) {
8✔
271
  setPassword(output);
8✔
272
  // UI is enabled by default when password is set - no additional action needed
273
}
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