keepassxc/src/gui/TotpDialog.cpp

103 lines
3.0 KiB
C++
Raw Normal View History

2017-04-13 06:05:36 -04: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"
#include "core/Clock.h"
2017-04-13 06:05:36 -04:00
#include "core/Config.h"
#include "gui/Clipboard.h"
TotpDialog::TotpDialog(QWidget* parent, Entry* entry)
2017-04-13 06:05:36 -04:00
: QDialog(parent)
, m_ui(new Ui::TotpDialog())
, m_entry(entry)
2017-04-13 06:05:36 -04:00
{
if (!m_entry->hasTotp()) {
close();
return;
}
2017-04-13 06:05:36 -04:00
m_ui->setupUi(this);
m_step = m_entry->totpSettings()->step;
resetCounter();
2017-04-13 06:05:36 -04:00
updateProgressBar();
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);
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()));
}
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()) {
qobject_cast<DatabaseWidget*>(parent())->window()->showMinimized();
} else if (config()->get("DropToBackgroundOnCopy").toBool()) {
qobject_cast<DatabaseWidget*>(parent())->window()->lower();
window()->lower();
}
2017-04-13 06:05:36 -04:00
}
}
void TotpDialog::updateProgressBar()
{
if (m_counter < 100) {
m_ui->progressBar->setValue(100 - m_counter);
2017-04-13 06:05:36 -04:00
m_ui->progressBar->update();
++m_counter;
2017-04-13 06:05:36 -04:00
} else {
updateTotp();
resetCounter();
2017-04-13 06:05:36 -04:00
}
}
void TotpDialog::updateSeconds()
{
uint epoch = Clock::currentSecondsSinceEpoch() - 1;
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();
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
}
void TotpDialog::resetCounter()
2017-04-13 06:05:36 -04:00
{
uint epoch = Clock::currentSecondsSinceEpoch();
m_counter = static_cast<int>(static_cast<double>(epoch % m_step) / m_step * 100);
2017-04-13 06:05:36 -04:00
}