< Development | Tutorials
Difference between revisions of "Development/Tutorials/KWallet"
(Mark for updating) |
|||
Line 1: | Line 1: | ||
− | + | {{Review| | |
+ | * Port to KF5 | ||
+ | * Improve content | ||
+ | }} | ||
== Abstract == | == Abstract == |
Latest revision as of 14:45, 31 May 2019
Warning
This page needs a review and probably holds information that needs to be fixed.
Parts to be reviewed:
- Port to KF5
- Improve content
Abstract
A simple introduction for using the KWallet API, a secure storage framework.
Prerequisites: Basic KDE knowledge, C++.
Example
The API is fun and easy to use, and will guarantee you a long and stable life.
In KWallet, entries are organized in Folders (or think of it as categories), and in them there are binary data, maps, passwords and a catch-all unknown. Binary entries are application-specific arrays of bytes, maps should be known from QMaps, where a series of values are accessed by keys. Passwords are simply strings, and Unknown is for everything else.
dialog.cpp
1 #include "dialog.h"
2
3 #include <QLayout>
4 #include <QLabel>
5 #include <QPushButton>
6 #include <QMap>
7 #include <QVBoxLayout>
8 #include <QSpacerItem>
9
10 Dialog::Dialog(QWidget *parent) :
11 QDialog(parent)
12 {
13 setLayout(new QVBoxLayout);
14 m_wallet = Wallet::openWallet(Wallet::NetworkWallet(),
15 winId(),
16 Wallet::Asynchronous);
17
18 QLabel *explanation = new QLabel("<b>HELLO!</b><br/>"
19 "Please type in something to save in the wallet!<br/>"
20 "It will be saved in the form data folder, under <br/>"
21 "the entry <i>http://test.com/#form</i>.");
22 m_statusLabel = new QLabel("Opening wallet...", this);
23 m_statusLabel->setAlignment(Qt::AlignCenter);
24 m_keyInput = new QLineEdit(this);
25 m_valueInput = new QLineEdit(this);
26 m_launchButton = new QPushButton("Save!", this);
27 m_launchButton->setDisabled(true);
28
29 layout()->addWidget(explanation);
30 qobject_cast<QVBoxLayout*>(layout())->addStretch();
31 layout()->addWidget(m_statusLabel);
32 layout()->addWidget(new QLabel("Key:", this));
33 layout()->addWidget(m_keyInput);
34 layout()->addWidget(new QLabel("Value:", this));
35 layout()->addWidget(m_valueInput);
36 layout()->addWidget(m_launchButton);
37
38 connect(m_launchButton, SIGNAL(clicked()), SLOT(doSave()));
39 connect(m_wallet, SIGNAL(walletOpened(bool)), SLOT(walletOpened(bool)));
40 setMinimumSize(500, 200);
41 }
42
43 void Dialog::walletOpened(bool ok)
44 {
45
46 if (ok &&
47 (m_wallet->hasFolder(KWallet::Wallet::FormDataFolder()) ||
48 m_wallet->createFolder(KWallet::Wallet::FormDataFolder())) &&
49 m_wallet->setFolder(KWallet::Wallet::FormDataFolder())) {
50 m_launchButton->setDisabled(false);
51 m_statusLabel->setText("Idle.");
52 } else
53 m_statusLabel->setText("Error opening wallet!");
54
55 }
56
57 void Dialog::doSave()
58 {
59 if (m_keyInput->text().isEmpty() || m_valueInput->text().isEmpty()) {
60 m_statusLabel->setText("Empty field!");
61 return;
62 }
63
64 m_launchButton->setDisabled(true);
65
66 m_statusLabel->setText("Saving ...");
67
68 QMap<QString, QString> map;
69 map[m_keyInput->text()] = m_valueInput->text();
70 if (m_wallet->writeMap("http://test.com/#form", map)) m_statusLabel->setText("Something went wrong!");
71 else {
72 m_statusLabel->setText("Saved!");
73 m_keyInput->clear();
74 m_valueInput->clear();
75 }
76 m_launchButton->setDisabled(false);
77 }
dialog.h
1 #ifndef DIALOG_H
2 #define DIALOG_H
3
4 #include <QDialog>
5 #include <QLabel>
6 #include <QLineEdit>
7 #include <KWallet/Wallet>
8
9 using KWallet::Wallet;
10
11 class Dialog : public QDialog
12 {
13 Q_OBJECT
14 public:
15 Dialog(QWidget *parent = 0);
16
17 private slots:
18 void doSave();
19 void walletOpened(bool ok);
20
21 private:
22 Wallet *m_wallet;
23 QLineEdit *m_keyInput;
24 QLineEdit *m_valueInput;
25 QLabel *m_statusLabel;
26 QPushButton *m_launchButton;
27 };
28
29 #endif // DIALOG_H
main.cpp
1 #include "dialog.h"
2
3 #include <KAboutData>
4 #include <KApplication>
5 #include <KCmdLineArgs>
6
7 static KAboutData about(
8 "KWalletDemoApplication",
9 "",
10 ki18n("KWalletDemoApplication"),
11 "1.0",
12 ki18n("Demonstrates basic KWallet usage."),
13 KAboutData::License_LGPL,
14 ki18n("(C) 2010 Martin Sandsmark"),
15 KLocalizedString(),
16 "http://www.mts.ms/");
17
18 int main(int argc, char *argv[])
19 {
20 about.addAuthor(ki18n("Martin Sandsmark"), ki18n("Maintainer"), "[email protected]", "http://iskrembilen.com/");
21
22 KCmdLineArgs::init(argc, argv, &about);
23 KApplication app;
24
25 Dialog dialog;
26 dialog.show();
27 return app.exec();
28 }
CMakeLists.txt
1 project(Importer)
2
3 find_package(KDE4 REQUIRED)
4 add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
5 include(KDE4Defaults)
6 include(MacroLibrary)
7 include_directories(${KDE4_INCLUDES} ${KDE4_INCLUDE_DIR} ${QT_INCLUDES})
8
9 set(SRCS main.cpp dialog.cpp)
10
11 kde4_add_executable(kwallet-demo ${SRCS})
12 target_link_libraries(kwallet-demo ${KDE4_KDECORE_LIBS} ${KDE4_KPARTS_LIBS} ${KDE4_KDEUI_LIBS})
13 install(TARGETS kwallet-demo ${INSTALL_TARGETS_DEFAULT_ARGS})
This page was last edited on 31 May 2019, at 14:45. Content is available under Creative Commons License SA 4.0 unless otherwise noted.