Development/Tutorials/KWallet
< Development | Tutorials
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
#include "dialog.h"
#include <QLayout>
#include <QLabel>
#include <QPushButton>
#include <QMap>
#include <QVBoxLayout>
#include <QSpacerItem>
Dialog::Dialog(QWidget *parent) :
QDialog(parent)
{
setLayout(new QVBoxLayout);
m_wallet = Wallet::openWallet(Wallet::NetworkWallet(),
winId(),
Wallet::Asynchronous);
QLabel *explanation = new QLabel("<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("Opening wallet...", this);
m_statusLabel->setAlignment(Qt::AlignCenter);
m_keyInput = new QLineEdit(this);
m_valueInput = new QLineEdit(this);
m_launchButton = new QPushButton("Save!", this);
m_launchButton->setDisabled(true);
layout()->addWidget(explanation);
qobject_cast<QVBoxLayout*>(layout())->addStretch();
layout()->addWidget(m_statusLabel);
layout()->addWidget(new QLabel("Key:", this));
layout()->addWidget(m_keyInput);
layout()->addWidget(new QLabel("Value:", this));
layout()->addWidget(m_valueInput);
layout()->addWidget(m_launchButton);
connect(m_launchButton, SIGNAL(clicked()), SLOT(doSave()));
connect(m_wallet, SIGNAL(walletOpened(bool)), SLOT(walletOpened(bool)));
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("Idle.");
} else
m_statusLabel->setText("Error opening wallet!");
}
void Dialog::doSave()
{
if (m_keyInput->text().isEmpty() || m_valueInput->text().isEmpty()) {
m_statusLabel->setText("Empty field!");
return;
}
m_launchButton->setDisabled(true);
m_statusLabel->setText("Saving ...");
QMap<QString, QString> map;
map[m_keyInput->text()] = m_valueInput->text();
if (m_wallet->writeMap("http://test.com/#form", map)) m_statusLabel->setText("Something went wrong!");
else {
m_statusLabel->setText("Saved!");
m_keyInput->clear();
m_valueInput->clear();
}
m_launchButton->setDisabled(false);
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <KWallet/Wallet>
using KWallet::Wallet;
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
private slots:
void doSave();
void walletOpened(bool ok);
private:
Wallet *m_wallet;
QLineEdit *m_keyInput;
QLineEdit *m_valueInput;
QLabel *m_statusLabel;
QPushButton *m_launchButton;
};
#endif // DIALOG_H
main.cpp
#include "dialog.h"
#include <KAboutData>
#include <KApplication>
#include <KCmdLineArgs>
static KAboutData about(
"KWalletDemoApplication",
"",
ki18n("KWalletDemoApplication"),
"1.0",
ki18n("Demonstrates basic KWallet usage."),
KAboutData::License_LGPL,
ki18n("(C) 2010 Martin Sandsmark"),
KLocalizedString(),
"http://www.mts.ms/");
int main(int argc, char *argv[])
{
about.addAuthor(ki18n("Martin Sandsmark"), ki18n("Maintainer"), "[email protected]", "http://iskrembilen.com/");
KCmdLineArgs::init(argc, argv, &about);
KApplication app;
Dialog dialog;
dialog.show();
return app.exec();
}
CMakeLists.txt
project(Importer)
find_package(KDE4 REQUIRED)
add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
include(KDE4Defaults)
include(MacroLibrary)
include_directories(${KDE4_INCLUDES} ${KDE4_INCLUDE_DIR} ${QT_INCLUDES})
set(SRCS main.cpp dialog.cpp)
kde4_add_executable(kwallet-demo ${SRCS})
target_link_libraries(kwallet-demo ${KDE4_KDECORE_LIBS} ${KDE4_KPARTS_LIBS} ${KDE4_KDEUI_LIBS})
install(TARGETS kwallet-demo ${INSTALL_TARGETS_DEFAULT_ARGS})