File indexing completed on 2024-05-12 03:59:50

0001 /*
0002     This file is part of the KDE project
0003 
0004     SPDX-FileCopyrightText: 2010-2012 Martin Sandsmark <martin.sandsmark@kde.org>
0005     SPDX-FileCopyrightText: 2018 Olivier Churlaud <olivier@churlaud.com>
0006 
0007     SPDX-License-Identifier: LGPL-3.0-or-later
0008 */
0009 
0010 #include "dialog.h"
0011 
0012 #include <KWallet>
0013 #include <QMap>
0014 #include <QPushButton>
0015 #include <QVBoxLayout>
0016 
0017 Dialog::Dialog(QWidget *parent)
0018     : QDialog(parent)
0019 {
0020     // Create the object
0021     m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), winId(), KWallet::Wallet::Asynchronous);
0022 
0023     QLabel *explanation =
0024         new QLabel(QStringLiteral("<b>HELLO!</b><br/>"
0025                                   "Please type in something to save in the wallet!<br/>"
0026                                   "It will be saved in the form data folder, under <br/>"
0027                                   "the entry <i>http://test.com/#form</i>."));
0028     m_statusLabel = new QLabel(QStringLiteral("Opening wallet..."), this);
0029     m_statusLabel->setAlignment(Qt::AlignCenter);
0030     m_keyInput = new QLineEdit(this);
0031     m_valueInput = new QLineEdit(this);
0032     m_launchButton = new QPushButton(QStringLiteral("Save!"), this);
0033     m_launchButton->setDisabled(true);
0034 
0035     QVBoxLayout *layout = new QVBoxLayout(this);
0036     layout->addWidget(explanation);
0037     layout->addStretch();
0038     layout->addWidget(m_statusLabel);
0039     layout->addWidget(new QLabel(QStringLiteral("Key:"), this));
0040     layout->addWidget(m_keyInput);
0041     layout->addWidget(new QLabel(QStringLiteral("Value:"), this));
0042     layout->addWidget(m_valueInput);
0043     layout->addWidget(m_launchButton);
0044 
0045     connect(m_launchButton, &QPushButton::clicked, this, &Dialog::doSave);
0046 
0047     // As we work asynchronously we need to use a signal/slot architecture
0048     connect(m_wallet, &KWallet::Wallet::walletOpened, this, &Dialog::walletOpened);
0049     setMinimumSize(500, 200);
0050 }
0051 
0052 void Dialog::walletOpened(bool ok)
0053 {
0054     if (ok && (m_wallet->hasFolder(KWallet::Wallet::FormDataFolder()) || m_wallet->createFolder(KWallet::Wallet::FormDataFolder()))
0055         && m_wallet->setFolder(KWallet::Wallet::FormDataFolder())) {
0056         m_launchButton->setDisabled(false);
0057         m_statusLabel->setText(QStringLiteral("Idle."));
0058     } else {
0059         m_statusLabel->setText(QStringLiteral("Error opening wallet!"));
0060     }
0061 }
0062 
0063 void Dialog::doSave()
0064 {
0065     if (m_keyInput->text().isEmpty() || m_valueInput->text().isEmpty()) {
0066         m_statusLabel->setText(QStringLiteral("Empty field!"));
0067         return;
0068     }
0069 
0070     m_launchButton->setDisabled(true);
0071 
0072     m_statusLabel->setText(QStringLiteral("Saving ..."));
0073 
0074     QMap<QString, QString> map;
0075     map[m_keyInput->text()] = m_valueInput->text();
0076 
0077     // Write in the map "http://test.com/#form" key/value contained in map
0078     if (m_wallet->writeMap(QStringLiteral("http://test.com/#form"), map)) {
0079         m_statusLabel->setText(QStringLiteral("Something went wrong!"));
0080     } else {
0081         m_statusLabel->setText(QStringLiteral("Saved!"));
0082         m_keyInput->clear();
0083         m_valueInput->clear();
0084     }
0085     m_launchButton->setDisabled(false);
0086 }
0087 
0088 #include "moc_dialog.cpp"