Difference between revisions of "Development/Tutorials/KWallet"

Jump to: navigation, search
(first version)
 
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/KWallet}}
+
 
  
 
== Abstract ==
 
== Abstract ==
Line 12: Line 12:
  
 
===dialog.cpp===
 
===dialog.cpp===
 +
<syntaxhighlight lang="cpp-qt" line>
 
#include "dialog.h"
 
#include "dialog.h"
  
Line 89: Line 90:
 
     m_launchButton->setDisabled(false);
 
     m_launchButton->setDisabled(false);
 
}
 
}
 
+
</syntaxhighlight>
  
 
===dialog.h===
 
===dialog.h===
 +
<syntaxhighlight lang="cpp-qt" line>
 
#ifndef DIALOG_H
 
#ifndef DIALOG_H
 
#define DIALOG_H
 
#define DIALOG_H
Line 121: Line 123:
  
 
#endif // DIALOG_H
 
#endif // DIALOG_H
 +
</syntaxhighlight>
  
 
===main.cpp===
 
===main.cpp===
 +
<syntaxhighlight lang="cpp-qt" line>
 
#include "dialog.h"
 
#include "dialog.h"
  
Line 142: Line 146:
 
int main(int argc, char *argv[])
 
int main(int argc, char *argv[])
 
{
 
{
     about.addAuthor(ki18n("Martin Sandsmark"), ki18n("Maintainer"), "sandsmark@iskrembilen.com", "http://iskrembilen.com/");
+
     about.addAuthor(ki18n("Martin Sandsmark"), ki18n("Maintainer"), "martin.sandsmark@kde.org", "http://iskrembilen.com/");
  
 
     KCmdLineArgs::init(argc, argv, &about);
 
     KCmdLineArgs::init(argc, argv, &about);
Line 151: Line 155:
 
     return app.exec();
 
     return app.exec();
 
}
 
}
 +
</syntaxhighlight>
  
 
===CMakeLists.txt===
 
===CMakeLists.txt===
 +
<syntaxhighlight lang="make" line>
 
project(Importer)
 
project(Importer)
  
Line 166: Line 172:
 
target_link_libraries(kwallet-demo ${KDE4_KDECORE_LIBS} ${KDE4_KPARTS_LIBS} ${KDE4_KDEUI_LIBS})
 
target_link_libraries(kwallet-demo ${KDE4_KDECORE_LIBS} ${KDE4_KPARTS_LIBS} ${KDE4_KDEUI_LIBS})
 
install(TARGETS kwallet-demo ${INSTALL_TARGETS_DEFAULT_ARGS})
 
install(TARGETS kwallet-demo ${INSTALL_TARGETS_DEFAULT_ARGS})
 +
</syntaxhighlight>

Latest revision as of 17:01, 19 July 2012


Contents

[edit] Abstract

A simple introduction for using the KWallet API, a secure storage framework.

Prerequisites: Basic KDE knowledge, C++.

[edit] 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.

[edit] 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. }

[edit] 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

[edit] 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"), "martin.sandsmark@kde.org", "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. }

[edit] 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 modified on 19 July 2012, at 17:01. This page has been accessed 6,314 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal