File: dialog.cpp

package info (click to toggle)
kf6-kwallet 6.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,084 kB
  • sloc: cpp: 12,536; xml: 743; sh: 16; makefile: 6
file content (88 lines) | stat: -rw-r--r-- 3,100 bytes parent folder | download | duplicates (3)
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
/*
    This file is part of the KDE project

    SPDX-FileCopyrightText: 2010-2012 Martin Sandsmark <martin.sandsmark@kde.org>
    SPDX-FileCopyrightText: 2018 Olivier Churlaud <olivier@churlaud.com>

    SPDX-License-Identifier: LGPL-3.0-or-later
*/

#include "dialog.h"

#include <KWallet>
#include <QMap>
#include <QPushButton>
#include <QVBoxLayout>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    // Create the object
    m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), winId(), KWallet::Wallet::Asynchronous);

    QLabel *explanation =
        new QLabel(QStringLiteral("<b>HELLO!</b><br/>"
                                  "Please type in something to save in the wallet!<br/>"
                                  "It will be saved in the form data folder, under <br/>"
                                  "the entry <i>http://test.com/#form</i>."));
    m_statusLabel = new QLabel(QStringLiteral("Opening wallet..."), this);
    m_statusLabel->setAlignment(Qt::AlignCenter);
    m_keyInput = new QLineEdit(this);
    m_valueInput = new QLineEdit(this);
    m_launchButton = new QPushButton(QStringLiteral("Save!"), this);
    m_launchButton->setDisabled(true);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(explanation);
    layout->addStretch();
    layout->addWidget(m_statusLabel);
    layout->addWidget(new QLabel(QStringLiteral("Key:"), this));
    layout->addWidget(m_keyInput);
    layout->addWidget(new QLabel(QStringLiteral("Value:"), this));
    layout->addWidget(m_valueInput);
    layout->addWidget(m_launchButton);

    connect(m_launchButton, &QPushButton::clicked, this, &Dialog::doSave);

    // As we work asynchronously we need to use a signal/slot architecture
    connect(m_wallet, &KWallet::Wallet::walletOpened, this, &Dialog::walletOpened);
    setMinimumSize(500, 200);
}

void Dialog::walletOpened(bool ok)
{
    if (ok && (m_wallet->hasFolder(KWallet::Wallet::FormDataFolder()) || m_wallet->createFolder(KWallet::Wallet::FormDataFolder()))
        && m_wallet->setFolder(KWallet::Wallet::FormDataFolder())) {
        m_launchButton->setDisabled(false);
        m_statusLabel->setText(QStringLiteral("Idle."));
    } else {
        m_statusLabel->setText(QStringLiteral("Error opening wallet!"));
    }
}

void Dialog::doSave()
{
    if (m_keyInput->text().isEmpty() || m_valueInput->text().isEmpty()) {
        m_statusLabel->setText(QStringLiteral("Empty field!"));
        return;
    }

    m_launchButton->setDisabled(true);

    m_statusLabel->setText(QStringLiteral("Saving ..."));

    QMap<QString, QString> map;
    map[m_keyInput->text()] = m_valueInput->text();

    // Write in the map "http://test.com/#form" key/value contained in map
    if (m_wallet->writeMap(QStringLiteral("http://test.com/#form"), map)) {
        m_statusLabel->setText(QStringLiteral("Something went wrong!"));
    } else {
        m_statusLabel->setText(QStringLiteral("Saved!"));
        m_keyInput->clear();
        m_valueInput->clear();
    }
    m_launchButton->setDisabled(false);
}

#include "moc_dialog.cpp"