Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : :
4 : : /**
5 : : * @file
6 : : * @brief Implements OtpCodeWidget: builds the code label, copy button and
7 : : * countdown bar, and owns the one-second refresh timer.
8 : : *
9 : : * @see otpcodewidget.h
10 : : */
11 : :
12 : : #include "otpcodewidget.h"
13 : : #include "qpushbuttonwithclipboard.h"
14 : :
15 : : #include <QHBoxLayout>
16 : : #include <QLabel>
17 : : #include <QProgressBar>
18 : : #include <QTimer>
19 : : #include <utility>
20 : :
21 : : namespace {
22 : :
23 : : /// Refresh cadence: the countdown is shown in whole seconds.
24 : : constexpr int kRefreshIntervalMs = 1000;
25 : : /// Matches the action-button visual height used elsewhere in the field rows.
26 : : constexpr int kProgressHeight = 18;
27 : : /// Enough for a three-digit countdown without the bar dominating the row.
28 : : constexpr int kProgressWidth = 52;
29 : :
30 : : } // namespace
31 : :
32 : 19 : OtpCodeWidget::OtpCodeWidget(Totp::Settings settings, bool withCopyButton,
33 : 19 : bool monospace, QWidget *parent)
34 : 19 : : QWidget(parent), m_settings(std::move(settings)) {
35 : : // Same borderless look as the action buttons PasswordDisplayPanel builds.
36 : : const QString buttonStyle =
37 : : "QPushButton { border-style: none; background: transparent; padding: 0; "
38 : 19 : "margin: 0; icon-size: 16px; color: inherit; }";
39 : :
40 : 19 : auto *layout = new QHBoxLayout(this);
41 : 19 : layout->setContentsMargins(0, 0, 0, 0);
42 : 19 : layout->setSpacing(4);
43 : :
44 [ + + ]: 19 : if (withCopyButton) {
45 : 10 : m_copyButton = new QPushButtonWithClipboard(QString(), this);
46 : 10 : m_copyButton->setStyleSheet(buttonStyle);
47 : 10 : connect(m_copyButton, &QPushButtonWithClipboard::textClicked, this,
48 : 10 : &OtpCodeWidget::copyRequested);
49 : 10 : layout->addWidget(m_copyButton);
50 : : }
51 : :
52 : 19 : m_codeLabel = new QLabel(this);
53 : 38 : m_codeLabel->setObjectName(QStringLiteral("OTP code"));
54 : : // Not selectable: the label's own Copy and the X11 selection put the code
55 : : // out without the secret hint or the autoclear, and past "never copy".
56 : : // The copy button copies it the tracked way.
57 : 19 : m_codeLabel->setTextInteractionFlags(Qt::NoTextInteraction);
58 [ - + ]: 19 : if (monospace) {
59 : 0 : m_codeLabel->setStyleSheet("QLabel { font-family: monospace; }");
60 : : }
61 : 19 : layout->addWidget(m_codeLabel);
62 : 19 : layout->addStretch();
63 : :
64 : 19 : m_progress = new QProgressBar(this);
65 : 38 : m_progress->setObjectName(QStringLiteral("otpCountdown"));
66 : 19 : m_progress->setRange(0, static_cast<int>(m_settings.step));
67 : : // Number-only format: no translatable string, and no unit to mistranslate.
68 : 38 : m_progress->setFormat(QStringLiteral("%v"));
69 : 19 : m_progress->setFixedWidth(kProgressWidth);
70 : 19 : m_progress->setMaximumHeight(kProgressHeight);
71 : 19 : m_progress->setToolTip(tr("Seconds until the OTP code changes"));
72 : 19 : layout->addWidget(m_progress);
73 : :
74 : : // The timer is a child of this widget: deleting the row stops it, so a
75 : : // queued timeout can never reach a destroyed label.
76 : 19 : auto *timer = new QTimer(this);
77 : 19 : timer->setInterval(kRefreshIntervalMs);
78 : 19 : connect(timer, &QTimer::timeout, this,
79 : 19 : [this]() { refresh(Totp::currentUnixTime()); });
80 : 19 : timer->start();
81 : :
82 : 19 : refresh(Totp::currentUnixTime());
83 : 19 : }
84 : :
85 : 24 : void OtpCodeWidget::refresh(quint64 unixTime) {
86 : 24 : const quint64 counter = Totp::counter(m_settings, unixTime);
87 [ + + + - ]: 24 : if (!m_hasCode || counter != m_counter) {
88 : 24 : m_counter = counter;
89 : 24 : m_hasCode = true;
90 : 24 : m_code = Totp::generate(m_settings, unixTime);
91 : 24 : m_codeLabel->setText(m_code);
92 [ + + ]: 24 : if (m_copyButton != nullptr) {
93 : 11 : m_copyButton->setTextToCopy(m_code);
94 : : }
95 : : }
96 : :
97 : 24 : m_secondsRemaining = Totp::secondsRemaining(m_settings, unixTime);
98 : 24 : m_progress->setValue(static_cast<int>(m_secondsRemaining));
99 : 24 : }
100 : :
101 : 10 : auto OtpCodeWidget::code() const -> QString { return m_code; }
102 : :
103 : 2 : auto OtpCodeWidget::secondsRemaining() const -> uint {
104 : 2 : return m_secondsRemaining;
105 : : }
|