2
* Copyright (C) 2017 Weslly Honorato <weslly@protonmail.com>
3
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
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.
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.
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/>.
19
#include "TotpDialog.h"
20
#include "ui_TotpDialog.h"
22
#include "core/Clock.h"
24
#include "gui/Clipboard.h"
25
#include "gui/MainWindow.h"
30
TotpDialog::TotpDialog(QWidget* parent, Entry* entry)
32
, m_ui(new Ui::TotpDialog())
35
setAttribute(Qt::WA_DeleteOnClose);
39
m_step = m_entry->totpSettings()->step;
43
connect(&m_totpUpdateTimer, SIGNAL(timeout()), this, SLOT(updateProgressBar()));
44
connect(&m_totpUpdateTimer, SIGNAL(timeout()), this, SLOT(updateSeconds()));
45
m_totpUpdateTimer.start(m_step * 10);
48
new QShortcut(QKeySequence(QKeySequence::Copy), this, SLOT(copyToClipboard()));
50
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Copy"));
52
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(close()));
53
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(copyToClipboard()));
56
TotpDialog::~TotpDialog() = default;
58
void TotpDialog::copyToClipboard()
60
clipboard()->setText(m_entry->totp());
61
if (config()->get(Config::HideWindowOnCopy).toBool()) {
62
if (config()->get(Config::MinimizeOnCopy).toBool()) {
63
getMainWindow()->minimizeOrHide();
64
} else if (config()->get(Config::DropToBackgroundOnCopy).toBool()) {
65
getMainWindow()->lower();
71
void TotpDialog::updateProgressBar()
73
if (m_counter < 100) {
74
m_ui->progressBar->setValue(100 - m_counter);
75
m_ui->progressBar->update();
83
void TotpDialog::updateSeconds()
85
uint epoch = Clock::currentSecondsSinceEpoch() - 1;
86
m_ui->timerLabel->setText(tr("Expires in <b>%n</b> second(s)", "", m_step - (epoch % m_step)));
89
void TotpDialog::updateTotp()
91
QString totpCode = m_entry->totp();
92
QString firstHalf = totpCode.left(totpCode.size() / 2);
93
QString secondHalf = totpCode.mid(totpCode.size() / 2);
94
m_ui->totpLabel->setText(firstHalf + " " + secondHalf);
97
void TotpDialog::resetCounter()
99
uint epoch = Clock::currentSecondsSinceEpoch();
100
m_counter = static_cast<int>(static_cast<double>(epoch % m_step) / m_step * 100);