-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
TotpKeyDisplayDialog.cpp
98 lines (87 loc) · 3.16 KB
/
TotpKeyDisplayDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TotpKeyDisplayDialog.h"
#include "core/Config.h"
#include "core/Entry.h"
#include "qrcodegen/QrCode.h"
#include "gui/Clipboard.h"
#include "gui/DatabaseWidget.h"
#include "totp/totp.h"
#include "ui_TotpKeyDisplayDialog.h"
#include <QPushButton>
#include <QMessageBox>
using qrcodegen::QrCode;
TotpKeyDisplayDialog::TotpKeyDisplayDialog(DatabaseWidget* parent, Entry* entry)
: QDialog(parent)
, m_ui(new Ui::TotpKeyDisplayDialog())
, m_secTillClose(30)
, m_entry(entry)
, m_parent(parent)
, m_timer(new QTimer(this))
{
m_ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
const QUrl keyUri = totpKeyUri(entry);
const QVariant maybeQRC = QrCode::encodeText(keyUri.toEncoded());
if (!maybeQRC.isNull()) {
const QrCode qrc = maybeQRC.value<QrCode>();
m_ui->totpKeyLabel->setPixmap(QPixmap::fromImage(qrc.toQImage(200, 4)));
} else {
auto errorBox = new QMessageBox(m_parent);
errorBox->setIcon(QMessageBox::Warning);
errorBox->setText(tr("There was an error creating the QR code."));
errorBox->exec();
close();
}
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Copy"));
m_ui->countDown->setText(tr("Closing in %n seconds.", "", m_secTillClose));
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(close()));
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(copyToClipboard()));
connect(m_timer, SIGNAL(timeout()), this, SLOT(autoClose()));
m_timer->start(1000);
}
void TotpKeyDisplayDialog::copyToClipboard()
{
const QUrl keyUri = totpKeyUri(m_entry);
clipboard()->setText(keyUri.toEncoded().data());
if (config()->get("MinimizeOnCopy").toBool()) {
m_parent->window()->showMinimized();
}
}
void TotpKeyDisplayDialog::autoClose()
{
if (--m_secTillClose > 0)
{
m_ui->countDown->setText(tr("Closing in %n seconds.", "", m_secTillClose));
} else {
m_timer->stop();
close();
}
}
QUrl TotpKeyDisplayDialog::totpKeyUri(const Entry* entry) const
{
return Totp::generateOtpString(entry->totpSeed(),
QString("totp"),
QString(entry->title()),
QString(entry->username()),
QString("SHA1"),
entry->totpDigits(),
entry->totpStep());
}
TotpKeyDisplayDialog::~TotpKeyDisplayDialog()
{
}