File: verificationdialog.cpp

package info (click to toggle)
quaternion 0.0.97.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,800 kB
  • sloc: cpp: 8,380; xml: 172; sh: 5; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 2,685 bytes parent folder | download
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
#include "verificationdialog.h"

#include <Quotient/keyverificationsession.h>

#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>

#include <QtCore/QStringBuilder>

using namespace Qt::StringLiterals;

VerificationDialog::VerificationDialog(Session* session, QWidget* parent)
    : Dialog(tr("Verifying device %1").arg(session->remoteDeviceId()),
             QDialogButtonBox::Ok | QDialogButtonBox::Discard, parent)
    , session(session)
{
    // The same check as in Session::handleEvent() in the KeyVerificationKeyEvent branch
    QUO_CHECK(session->state() == Session::WAITINGFORKEY || session->state() == Session::ACCEPTED);

    addWidget(new QLabel(
      tr("Confirm that the same icons, in the same order are displayed on the other side")));
    const auto emojis = session->sasEmojis();
    constexpr auto rowsCount = 2;
    const auto rowSize = (emojis.size() + 1) / rowsCount;
    auto emojiGrid = addLayout<QHBoxLayout>();
    for (int i = 0; i < emojis.size(); ++i) {
        auto emojiLayout = new QVBoxLayout();
        auto emoji = new QLabel(emojis[i].emoji);
        emoji->setFont({ u"emoji"_s, emoji->font().pointSize() * 4 });
        for (auto* const l : { emoji, new QLabel(emojis[i].description) }) {
            emojiLayout->addWidget(l);
            emojiLayout->setAlignment(l, Qt::AlignCenter);
        }
        emojiGrid->addLayout(emojiLayout);
        if (i % rowSize == rowSize - 1)
            emojiGrid = addLayout<QHBoxLayout>(); // Start new line
    }
    button(QDialogButtonBox::Ok)->setText(tr("They match"));
    button(QDialogButtonBox::Discard)->setText(tr("They DON'T match"));

    // Pin lifecycles of the dialog and the session, avoiding recursion (by the time
    // QObject::destroyed is emitted, KeyVerificationSession signals are disconnected)
    connect(session, &QObject::destroyed, this, &QDialog::reject);
    // NB: this is only triggered when a dialog is closed using a window close button;
    //     QDialogButtonBox::Discard doesn't trigger QDialog::rejected as it has DestructiveRole
    connect(this, &QDialog::rejected, session, [session] {
        if (session->state() != Session::CANCELED)
            session->cancelVerification(Session::USER);
    });
}

VerificationDialog::~VerificationDialog() = default;

void VerificationDialog::buttonClicked(QAbstractButton* button)
{
    if (button == this->button(QDialogButtonBox::Ok)) {
        session->sendMac();
        accept();
    } else if (button == this->button(QDialogButtonBox::Discard)) {
        session->cancelVerification(Session::MISMATCHED_SAS);
        reject();
    } else
        QUO_ALARM_X(false, "Unknown button: " % button->text());
}