keepassxc

Форк
0
/
TotpDialog.cpp 
101 строка · 3.0 Кб
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/Totp.h"
24
#include "gui/Clipboard.h"
25
#include "gui/MainWindow.h"
26

27
#include <QPushButton>
28
#include <QShortcut>
29

30
TotpDialog::TotpDialog(QWidget* parent, Entry* entry)
31
    : QDialog(parent)
32
    , m_ui(new Ui::TotpDialog())
33
    , m_entry(entry)
34
{
35
    setAttribute(Qt::WA_DeleteOnClose);
36

37
    m_ui->setupUi(this);
38

39
    m_step = m_entry->totpSettings()->step;
40
    resetCounter();
41
    updateProgressBar();
42

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);
46
    updateTotp();
47

48
    new QShortcut(QKeySequence(QKeySequence::Copy), this, SLOT(copyToClipboard()));
49

50
    m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Copy"));
51

52
    connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(close()));
53
    connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(copyToClipboard()));
54
}
55

56
TotpDialog::~TotpDialog() = default;
57

58
void TotpDialog::copyToClipboard()
59
{
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();
66
            window()->lower();
67
        }
68
    }
69
}
70

71
void TotpDialog::updateProgressBar()
72
{
73
    if (m_counter < 100) {
74
        m_ui->progressBar->setValue(100 - m_counter);
75
        m_ui->progressBar->update();
76
        ++m_counter;
77
    } else {
78
        updateTotp();
79
        resetCounter();
80
    }
81
}
82

83
void TotpDialog::updateSeconds()
84
{
85
    uint epoch = Clock::currentSecondsSinceEpoch() - 1;
86
    m_ui->timerLabel->setText(tr("Expires in <b>%n</b> second(s)", "", m_step - (epoch % m_step)));
87
}
88

89
void TotpDialog::updateTotp()
90
{
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);
95
}
96

97
void TotpDialog::resetCounter()
98
{
99
    uint epoch = Clock::currentSecondsSinceEpoch();
100
    m_counter = static_cast<int>(static_cast<double>(epoch % m_step) / m_step * 100);
101
}
102

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.