summaryrefslogtreecommitdiffstats
path: root/debian/pinentry-tqt/pinentry-tqt-1.2.1/qt/pinentryconfirm.cpp
blob: a97eaa6e78ed0711554599dc63b6cd85d8890e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* pinentryconfirm.cpp - A QMessageBox with a timeout
 *
 * Copyright (C) 2011 Ben Kibbey <bjk@luxsci.net>
 * Copyright (C) 2022 g10 Code GmbH
 *
 * Software engineering by Ingo Klöcker <dev@ingo-kloecker.de>
 *
 * 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 of the
 * License, or (at your option) any later version.
 *
 * 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 <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: GPL-2.0+
 */

#include "pinentryconfirm.h"

#include "accessibility.h"
#include "pinentrydialog.h"

#include <QApplication>
#include <QAbstractButton>
#include <QGridLayout>
#include <QLabel>
#include <QSpacerItem>
#include <QFontMetrics>

namespace
{
QLabel *messageBoxLabel(QMessageBox *messageBox)
{
    return messageBox->findChild<QLabel *>(QStringLiteral("qt_msgbox_label"));
}
}

PinentryConfirm::PinentryConfirm(Icon icon, const QString &title, const QString &text,
                                 StandardButtons buttons, QWidget *parent, Qt::WindowFlags flags)
    : QMessageBox{icon, title, text, buttons, parent, flags}
{
    _timer.callOnTimeout(this, &PinentryConfirm::slotTimeout);
    Accessibility::setDescription(this, text);
    Accessibility::setName(this, title);

#ifndef QT_NO_ACCESSIBILITY
    QAccessible::installActivationObserver(this);
    accessibilityActiveChanged(QAccessible::isActive());
#endif

#if QT_VERSION >= 0x050000
    /* This is in line with PinentryDialog ctor to have a maximizing
     * animation when opening. */
    if (qApp->platformName() != QLatin1String("wayland")) {
        setWindowState(Qt::WindowMinimized);
        QTimer::singleShot(0, this, [this] () {
            raiseWindow(this);
        });
    }
#else
    activateWindow();
    raise();
#endif
}

PinentryConfirm::~PinentryConfirm()
{
#ifndef QT_NO_ACCESSIBILITY
    QAccessible::removeActivationObserver(this);
#endif
}

void PinentryConfirm::setTimeout(std::chrono::seconds timeout)
{
    _timer.setInterval(timeout);
}

std::chrono::seconds PinentryConfirm::timeout() const
{
    return std::chrono::duration_cast<std::chrono::seconds>(_timer.intervalAsDuration());
}

bool PinentryConfirm::timedOut() const
{
    return _timed_out;
}

void PinentryConfirm::showEvent(QShowEvent *event)
{
    static bool resized;
    if (!resized) {
        QGridLayout* lay = dynamic_cast<QGridLayout*> (layout());
        if (lay) {
            QSize textSize = fontMetrics().size(Qt::TextExpandTabs, text(), fontMetrics().maxWidth());
            QSpacerItem* horizontalSpacer = new QSpacerItem(textSize.width() + iconPixmap().width(),
                                                            0, QSizePolicy::Minimum, QSizePolicy::Expanding);
            lay->addItem(horizontalSpacer, lay->rowCount(), 1, 1, lay->columnCount() - 1);
        }
        resized = true;
    }

    QMessageBox::showEvent(event);

    if (timeout() > std::chrono::milliseconds::zero()) {
        _timer.setSingleShot(true);
        _timer.start();
    }
}

bool PinentryConfirm::focusNextPrevChild(bool next)
{
    auto ret = QMessageBox::focusNextPrevChild(next);
    if (ret && (focusWidget() == messageBoxLabel(this))) {
        Accessibility::selectLabelText(messageBoxLabel(this));
    }
    return ret;
}

void PinentryConfirm::slotTimeout()
{
    QAbstractButton *b = button(QMessageBox::Cancel);
    _timed_out = true;

    if (b) {
        b->animateClick(0);
    }
}

#ifndef QT_NO_ACCESSIBILITY
void PinentryConfirm::accessibilityActiveChanged(bool active)
{
    // Allow text label to get focus if accessibility is active
    const auto focusPolicy = active ? Qt::StrongFocus : Qt::ClickFocus;
    if (auto label = messageBoxLabel(this)) {
        label->setFocusPolicy(focusPolicy);
    }
}
#endif

#include "pinentryconfirm.moc"