Development/Tutorials/KWallet

< Development‎ | Tutorials
Revision as of 20:32, 29 June 2011 by Neverendingo (Talk | contribs) (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>")

Jump to: navigation, search


Development/Tutorials/KWallet


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 }
 78 </code>
 79 
 80 ===dialog.h===
 81 <syntaxhighlight lang="cpp-qt" line>
 82 #ifndef DIALOG_H
 83 #define DIALOG_H
 84 
 85 #include <QDialog>
 86 #include <QLabel>
 87 #include <QLineEdit>
 88 #include <KWallet/Wallet>
 89 
 90 using KWallet::Wallet;
 91 
 92 class Dialog : public QDialog
 93 {
 94 Q_OBJECT
 95 public:
 96     Dialog(QWidget *parent = 0);
 97 
 98 private slots:
 99     void doSave();
100     void walletOpened(bool ok);
101 
102 private:
103     Wallet *m_wallet;
104     QLineEdit *m_keyInput;
105     QLineEdit *m_valueInput;
106     QLabel *m_statusLabel;
107     QPushButton *m_launchButton;
108 };
109 
110 #endif // DIALOG_H
111 </code>
112 
113 ===main.cpp===
114 <syntaxhighlight lang="cpp-qt" line>
115 #include "dialog.h"
116 
117 #include <KAboutData>
118 #include <KApplication>
119 #include <KCmdLineArgs>
120 
121 static KAboutData about(
122     "KWalletDemoApplication",
123     "",
124     ki18n("KWalletDemoApplication"),
125     "1.0",
126     ki18n("Demonstrates basic KWallet usage."),
127     KAboutData::License_LGPL,
128     ki18n("(C) 2010 Martin Sandsmark"),
129     KLocalizedString(),
130     "http://www.mts.ms/");
131 
132 int main(int argc, char *argv[])
133 {
134     about.addAuthor(ki18n("Martin Sandsmark"), ki18n("Maintainer"), "sandsmark@iskrembilen.com", "http://iskrembilen.com/");
135 
136     KCmdLineArgs::init(argc, argv, &about);
137     KApplication app;
138 
139     Dialog dialog;
140     dialog.show();
141     return app.exec();
142 }
143 </code>
144 
145 ===CMakeLists.txt===
146 <code cmake n>
147 project(Importer)
148 
149 find_package(KDE4 REQUIRED)
150 add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
151 include(KDE4Defaults)
152 include(MacroLibrary)
153 include_directories(${KDE4_INCLUDES} ${KDE4_INCLUDE_DIR} ${QT_INCLUDES})
154 
155 set(SRCS main.cpp dialog.cpp)
156 
157 kde4_add_executable(kwallet-demo ${SRCS})
158 target_link_libraries(kwallet-demo ${KDE4_KDECORE_LIBS} ${KDE4_KPARTS_LIBS} ${KDE4_KDEUI_LIBS})
159 install(TARGETS kwallet-demo ${INSTALL_TARGETS_DEFAULT_ARGS})
160 </code>