QtPass 1.6.0
Multi-platform GUI for pass, the standard unix password manager.
Loading...
Searching...
No Matches
keygendialog.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
3#include "keygendialog.h"
4#include "configdialog.h"
5#include "pass.h"
7#include "qtpasssettings.h"
8#include "ui_keygendialog.h"
9#include "util.h"
10#include <QMessageBox>
11#include <QRegularExpression>
12
13#ifdef QT_DEBUG
14#include "debughelper.h"
15#endif
16
22 : QDialog(parent), ui(new Ui::KeygenDialog) {
23 ui->setupUi(this);
24 dialog = parent;
25
26 // Restore dialog state
27 QByteArray savedGeometry = QtPassSettings::getDialogGeometry("keygenDialog");
28 bool hasSavedGeometry = !savedGeometry.isEmpty();
29 if (hasSavedGeometry) {
30 restoreGeometry(savedGeometry);
31 }
32 if (QtPassSettings::isDialogMaximized("keygenDialog")) {
33 showMaximized();
34 } else if (hasSavedGeometry) {
35 move(QtPassSettings::getDialogPos("keygenDialog"));
36 resize(QtPassSettings::getDialogSize("keygenDialog"));
37 } else {
38 // Let window manager handle positioning for first launch
39 }
40
41 ui->plainTextEdit->setPlainText(Pass::getDefaultKeyTemplate());
42}
43
48
54void KeygenDialog::on_passphrase1_textChanged(const QString &arg1) {
55 bool state = ui->passphrase1->text() == ui->passphrase2->text();
56 if (state) {
57 replace("Passphrase", arg1);
58 no_protection(arg1.isEmpty());
59 }
60
61 ui->buttonBox->setEnabled(state);
62}
63
69void KeygenDialog::on_passphrase2_textChanged(const QString &arg1) {
70 on_passphrase1_textChanged(arg1);
71}
72
77void KeygenDialog::on_checkBox_stateChanged(int arg1) {
78 ui->plainTextEdit->setReadOnly(!arg1);
79 ui->plainTextEdit->setEnabled(arg1);
80}
81
87void KeygenDialog::on_email_textChanged(const QString &arg1) {
88 replace("Name-Email", arg1);
89}
90
96void KeygenDialog::on_name_textChanged(const QString &arg1) {
97 replace("Name-Real", arg1);
98}
99
106void KeygenDialog::replace(const QString &key, const QString &value) {
107 QStringList clear;
108 QString expert = ui->plainTextEdit->toPlainText();
109#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
110 const QStringList lines =
111 expert.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
112#else
113 const QStringList lines =
114 expert.split(Util::newLinesRegex(), QString::SkipEmptyParts);
115#endif
116 for (QString line : lines) {
117 line.replace(QRegularExpression(key + ":.*"), key + ": " + value);
118 if (key == "Passphrase") {
119 line.replace("%no-protection", "Passphrase: " + value);
120 }
121 clear.append(line);
122 }
123 ui->plainTextEdit->setPlainText(clear.join("\n"));
124}
125
131void KeygenDialog::no_protection(bool enable) {
132 QStringList clear;
133 QString expert = ui->plainTextEdit->toPlainText();
134#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
135 const QStringList lines =
136 expert.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
137#else
138 const QStringList lines =
139 expert.split(Util::newLinesRegex(), QString::SkipEmptyParts);
140#endif
141 for (QString line : lines) {
142 bool remove = false;
143 if (!enable) {
144 if (line.indexOf("%no-protection") == 0) {
145 remove = true;
146 }
147 } else {
148 if (line.indexOf("Passphrase") == 0) {
149 line = "%no-protection";
150 }
151 }
152 if (!remove) {
153 clear.append(line);
154 }
155 }
156 ui->plainTextEdit->setPlainText(clear.join("\n"));
157}
158
164void KeygenDialog::done(int r) {
165 if (QDialog::Accepted == r) { // ok was pressed
166 // check name
167 if (ui->name->text().length() < 5) {
168 QMessageBox::critical(this, tr("Invalid name"),
169 tr("Name must be at least 5 characters long."));
170 return;
171 }
172
173 // check email
174 static const QRegularExpression mailre(
175 QRegularExpression::anchoredPattern(
176 R"(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b)"),
177 QRegularExpression::CaseInsensitiveOption);
178 if (!mailre.match(ui->email->text()).hasMatch()) {
179 QMessageBox::critical(
180 this, tr("Invalid email"),
181 tr("The email address you typed is not a valid email address."));
182 return;
183 }
184
185 ui->widget->setEnabled(false);
186 ui->buttonBox->setEnabled(false);
187 ui->checkBox->setEnabled(false);
188 ui->plainTextEdit->setEnabled(false);
189
190 auto *pi = new QProgressIndicator();
191 pi->startAnimation();
192 pi->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
193
194 ui->frame->hide();
195 ui->label->setText(
196 tr("This operation can take some minutes.<br />"
197 "We need to generate a lot of random bytes. It is a good idea to "
198 "perform some other action (type on the keyboard, move the mouse, "
199 "utilize the disks) during the prime generation; this gives the "
200 "random number generator a better chance to gain enough entropy."));
201
202 this->layout()->addWidget(pi);
203
204 this->show();
205 dialog->genKey(ui->plainTextEdit->toPlainText(), this);
206 } else { // cancel, close or exc was pressed
207 QDialog::done(r);
208 return;
209 }
210}
211
216void KeygenDialog::closeEvent(QCloseEvent *event) {
217 QtPassSettings::setDialogGeometry("keygenDialog", saveGeometry());
218 if (!isMaximized()) {
219 QtPassSettings::setDialogPos("keygenDialog", pos());
220 QtPassSettings::setDialogSize("keygenDialog", size());
221 }
222 QtPassSettings::setDialogMaximized("keygenDialog", isMaximized());
223 event->accept();
224}
The ConfigDialog handles the configuration interface.
void closeEvent(QCloseEvent *event) override
KeygenDialog::closeEvent we are done here.
KeygenDialog(ConfigDialog *parent=nullptr)
KeygenDialog::KeygenDialog basic constructor.
~KeygenDialog() override
KeygenDialog::~KeygenDialog even more basic destructor.
static QString getDefaultKeyTemplate()
Get default key template for new GPG keys.
Definition pass.cpp:178
static auto getDialogPos(const QString &key, const QPoint &defaultValue=QVariant().toPoint()) -> QPoint
Get saved dialog position.
static void setDialogPos(const QString &key, const QPoint &pos)
Save dialog position.
static auto getDialogSize(const QString &key, const QSize &defaultValue=QVariant().toSize()) -> QSize
Get saved dialog size.
static void setDialogSize(const QString &key, const QSize &size)
Save dialog size.
static void setDialogMaximized(const QString &key, const bool &maximized)
Save dialog maximized state.
static auto isDialogMaximized(const QString &key, const bool &defaultValue=QVariant().toBool()) -> bool
Get dialog maximized state.
static void setDialogGeometry(const QString &key, const QByteArray &geometry)
Save dialog geometry.
static auto getDialogGeometry(const QString &key, const QByteArray &defaultValue=QVariant().toByteArray()) -> QByteArray
Get saved dialog geometry.
static auto newLinesRegex() -> const QRegularExpression &
Returns a regex to match newline characters.
Definition util.cpp:272
Debug utilities for QtPass.