1 /*
2 * Copyright (C) 2017 Weslly Honorato <weslly@protonmail.com>
3 * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 or (at your option)
8 * version 3 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "TotpDialog.h"
20 #include "ui_TotpDialog.h"
21
22 #include "core/Clock.h"
23 #include "core/Config.h"
24 #include "gui/Clipboard.h"
25 #include "gui/MainWindow.h"
26
27 #include <QShortcut>
28
TotpDialog(QWidget * parent,Entry * entry)29 TotpDialog::TotpDialog(QWidget* parent, Entry* entry)
30 : QDialog(parent)
31 , m_ui(new Ui::TotpDialog())
32 , m_entry(entry)
33 {
34 setAttribute(Qt::WA_DeleteOnClose);
35
36 m_ui->setupUi(this);
37
38 m_step = m_entry->totpSettings()->step;
39 resetCounter();
40 updateProgressBar();
41
42 connect(&m_totpUpdateTimer, SIGNAL(timeout()), this, SLOT(updateProgressBar()));
43 connect(&m_totpUpdateTimer, SIGNAL(timeout()), this, SLOT(updateSeconds()));
44 m_totpUpdateTimer.start(m_step * 10);
45 updateTotp();
46
47 new QShortcut(QKeySequence(QKeySequence::Copy), this, SLOT(copyToClipboard()));
48
49 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Copy"));
50
51 connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(close()));
52 connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(copyToClipboard()));
53 }
54
~TotpDialog()55 TotpDialog::~TotpDialog()
56 {
57 }
58
copyToClipboard()59 void TotpDialog::copyToClipboard()
60 {
61 clipboard()->setText(m_entry->totp());
62 if (config()->get(Config::HideWindowOnCopy).toBool()) {
63 if (config()->get(Config::MinimizeOnCopy).toBool()) {
64 getMainWindow()->minimizeOrHide();
65 } else if (config()->get(Config::DropToBackgroundOnCopy).toBool()) {
66 getMainWindow()->lower();
67 window()->lower();
68 }
69 }
70 }
71
updateProgressBar()72 void TotpDialog::updateProgressBar()
73 {
74 if (m_counter < 100) {
75 m_ui->progressBar->setValue(100 - m_counter);
76 m_ui->progressBar->update();
77 ++m_counter;
78 } else {
79 updateTotp();
80 resetCounter();
81 }
82 }
83
updateSeconds()84 void TotpDialog::updateSeconds()
85 {
86 uint epoch = Clock::currentSecondsSinceEpoch() - 1;
87 m_ui->timerLabel->setText(tr("Expires in <b>%n</b> second(s)", "", m_step - (epoch % m_step)));
88 }
89
updateTotp()90 void TotpDialog::updateTotp()
91 {
92 QString totpCode = m_entry->totp();
93 QString firstHalf = totpCode.left(totpCode.size() / 2);
94 QString secondHalf = totpCode.mid(totpCode.size() / 2);
95 m_ui->totpLabel->setText(firstHalf + " " + secondHalf);
96 }
97
resetCounter()98 void TotpDialog::resetCounter()
99 {
100 uint epoch = Clock::currentSecondsSinceEpoch();
101 m_counter = static_cast<int>(static_cast<double>(epoch % m_step) / m_step * 100);
102 }
103