2017-04-13 06:05:36 -04:00
|
|
|
/*
|
2019-12-31 12:39:50 -05:00
|
|
|
* Copyright (C) 2017 Weslly Honorato <weslly@protonmail.com>
|
2017-06-09 17:40:36 -04:00
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2017-04-13 06:05:36 -04:00
|
|
|
*
|
|
|
|
* 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 "TotpDialog.h"
|
|
|
|
#include "ui_TotpDialog.h"
|
|
|
|
|
2018-09-30 08:45:06 -04:00
|
|
|
#include "core/Clock.h"
|
2017-04-13 06:05:36 -04:00
|
|
|
#include "core/Config.h"
|
|
|
|
#include "gui/Clipboard.h"
|
2019-10-14 09:25:45 -04:00
|
|
|
#include "gui/MainWindow.h"
|
|
|
|
|
|
|
|
#include <QShortcut>
|
2017-04-13 06:05:36 -04:00
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
TotpDialog::TotpDialog(QWidget* parent, Entry* entry)
|
2017-04-13 06:05:36 -04:00
|
|
|
: QDialog(parent)
|
|
|
|
, m_ui(new Ui::TotpDialog())
|
2018-01-22 15:42:22 -05:00
|
|
|
, m_entry(entry)
|
2017-04-13 06:05:36 -04:00
|
|
|
{
|
2018-09-05 16:20:57 -04:00
|
|
|
if (!m_entry->hasTotp()) {
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-13 06:05:36 -04:00
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
m_step = m_entry->totpSettings()->step;
|
|
|
|
resetCounter();
|
2017-04-13 06:05:36 -04:00
|
|
|
updateProgressBar();
|
|
|
|
|
2019-12-31 12:39:50 -05:00
|
|
|
connect(parent, SIGNAL(databaseLocked()), SLOT(close()));
|
2018-09-05 16:20:57 -04:00
|
|
|
connect(&m_totpUpdateTimer, SIGNAL(timeout()), this, SLOT(updateProgressBar()));
|
|
|
|
connect(&m_totpUpdateTimer, SIGNAL(timeout()), this, SLOT(updateSeconds()));
|
|
|
|
m_totpUpdateTimer.start(m_step * 10);
|
2017-04-13 06:05:36 -04:00
|
|
|
updateTotp();
|
|
|
|
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
2019-10-14 09:25:45 -04:00
|
|
|
new QShortcut(QKeySequence(QKeySequence::Copy), this, SLOT(copyToClipboard()));
|
|
|
|
|
2017-04-13 06:05:36 -04:00
|
|
|
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Copy"));
|
|
|
|
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(close()));
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(copyToClipboard()));
|
|
|
|
}
|
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
TotpDialog::~TotpDialog()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-13 06:05:36 -04:00
|
|
|
void TotpDialog::copyToClipboard()
|
|
|
|
{
|
|
|
|
clipboard()->setText(m_entry->totp());
|
2019-06-09 14:15:02 -04:00
|
|
|
if (config()->get("HideWindowOnCopy").toBool()) {
|
|
|
|
if (config()->get("MinimizeOnCopy").toBool()) {
|
2020-02-24 06:33:43 -05:00
|
|
|
getMainWindow()->minimizeOrHide();
|
2019-06-09 14:15:02 -04:00
|
|
|
} else if (config()->get("DropToBackgroundOnCopy").toBool()) {
|
2019-10-14 09:25:45 -04:00
|
|
|
getMainWindow()->lower();
|
2019-06-09 14:15:02 -04:00
|
|
|
window()->lower();
|
|
|
|
}
|
2017-04-13 06:05:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TotpDialog::updateProgressBar()
|
|
|
|
{
|
2018-09-05 16:20:57 -04:00
|
|
|
if (m_counter < 100) {
|
|
|
|
m_ui->progressBar->setValue(100 - m_counter);
|
2017-04-13 06:05:36 -04:00
|
|
|
m_ui->progressBar->update();
|
2018-09-05 16:20:57 -04:00
|
|
|
++m_counter;
|
2017-04-13 06:05:36 -04:00
|
|
|
} else {
|
|
|
|
updateTotp();
|
2018-09-05 16:20:57 -04:00
|
|
|
resetCounter();
|
2017-04-13 06:05:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TotpDialog::updateSeconds()
|
|
|
|
{
|
2018-09-30 08:45:06 -04:00
|
|
|
uint epoch = Clock::currentSecondsSinceEpoch() - 1;
|
2018-03-13 15:59:08 -04:00
|
|
|
m_ui->timerLabel->setText(tr("Expires in <b>%n</b> second(s)", "", m_step - (epoch % m_step)));
|
2017-04-13 06:05:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TotpDialog::updateTotp()
|
|
|
|
{
|
2017-05-03 20:26:08 -04:00
|
|
|
QString totpCode = m_entry->totp();
|
2017-11-20 13:01:22 -05:00
|
|
|
QString firstHalf = totpCode.left(totpCode.size() / 2);
|
|
|
|
QString secondHalf = totpCode.mid(totpCode.size() / 2);
|
2017-05-03 20:26:08 -04:00
|
|
|
m_ui->totpLabel->setText(firstHalf + " " + secondHalf);
|
2017-04-13 06:05:36 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
void TotpDialog::resetCounter()
|
2017-04-13 06:05:36 -04:00
|
|
|
{
|
2018-09-30 08:45:06 -04:00
|
|
|
uint epoch = Clock::currentSecondsSinceEpoch();
|
2018-09-05 16:20:57 -04:00
|
|
|
m_counter = static_cast<int>(static_cast<double>(epoch % m_step) / m_step * 100);
|
2017-04-13 06:05:36 -04:00
|
|
|
}
|