diff --git a/CMakeLists.txt b/CMakeLists.txt index 36ae6ad..4d755c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,53 +1,54 @@ cmake_minimum_required(VERSION 3.0) set(KF5_VERSION "5.50.0") # handled by release scripts set(KF5_DEP_VERSION "5.49.0") # handled by release scripts project(KWallet VERSION ${KF5_VERSION}) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) include(FeatureSummary) find_package(ECM 5.49.0 NO_MODULE) set_package_properties(ECM PROPERTIES TYPE REQUIRED DESCRIPTION "Extra CMake Modules." URL "https://projects.kde.org/projects/kdesupport/extra-cmake-modules") feature_summary(WHAT REQUIRED_PACKAGES_NOT_FOUND FATAL_ON_MISSING_REQUIRED_PACKAGES) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) set(REQUIRED_QT_VERSION 5.8.0) find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Widgets DBus) include(KDEInstallDirs) include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE) include(KDECMakeSettings) include(ECMAddQch) include(ECMSetupVersion) include(ECMQtDeclareLoggingCategory) option(BUILD_KWALLETD "Build the kwallet daemon" ON) option(BUILD_KWALLET_QUERY "Build kwallet-query tool" ON) option(BUILD_QCH "Build API documentation in QCH format (for e.g. Qt Assistant, Qt Creator & KDevelop)" OFF) add_feature_info(QCH ${BUILD_QCH} "API documentation in QCH format (for e.g. Qt Assistant, Qt Creator & KDevelop)") find_package(KF5CoreAddons ${KF5_DEP_VERSION} REQUIRED) find_package(KF5Config ${KF5_DEP_VERSION} REQUIRED) find_package(KF5WindowSystem ${KF5_DEP_VERSION} REQUIRED) find_package(KF5I18n ${KF5_DEP_VERSION} REQUIRED) find_package(KF5DocTools ${KF5_DEP_VERSION}) add_definitions(-DTRANSLATION_DOMAIN=\"kwalletd5\") if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/po") ki18n_install(po) endif() add_subdirectory(src) if (BUILD_TESTING) add_subdirectory(autotests) add_subdirectory(tests) + add_subdirectory(examples) endif() if (KF5DocTools_FOUND) add_subdirectory(docs) endif() # contains list of debug categories, for kdebugsettings install(FILES kwallet.categories DESTINATION ${KDE_INSTALL_CONFDIR}) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..47d6b62 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(asynchronous_app) diff --git a/examples/asynchronous_app/CMakeLists.txt b/examples/asynchronous_app/CMakeLists.txt new file mode 100644 index 0000000..8164571 --- /dev/null +++ b/examples/asynchronous_app/CMakeLists.txt @@ -0,0 +1,7 @@ +set(SRCS main.cpp dialog.cpp) + +add_executable(kwallet-example-asynchronous ${SRCS}) +target_link_libraries(kwallet-example-asynchronous KF5::Wallet Qt5::Widgets) + + + diff --git a/examples/asynchronous_app/dialog.cpp b/examples/asynchronous_app/dialog.cpp new file mode 100644 index 0000000..4c9f5fe --- /dev/null +++ b/examples/asynchronous_app/dialog.cpp @@ -0,0 +1,106 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2010-2012 Martin Sandsmark + * Copyright (C) 2018 Olivier Churlaud + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "dialog.h" + +#include +#include +#include +#include +#include +#include +#include + +Dialog::Dialog(QWidget *parent) : + QDialog(parent) +{ + // Create the object + m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), + winId(), + KWallet::Wallet::Asynchronous); + + QLabel *explanation = new QLabel(QStringLiteral("HELLO!
" + "Please type in something to save in the wallet!
" + "It will be saved in the form data folder, under
" + "the entry http://test.com/#form.")); + m_statusLabel = new QLabel(QStringLiteral("Opening wallet..."), this); + m_statusLabel->setAlignment(Qt::AlignCenter); + m_keyInput = new QLineEdit(this); + m_valueInput = new QLineEdit(this); + m_launchButton = new QPushButton(QStringLiteral("Save!"), this); + m_launchButton->setDisabled(true); + + QVBoxLayout * layout = new QVBoxLayout(this); + layout->addWidget(explanation); + layout->addStretch(); + layout->addWidget(m_statusLabel); + layout->addWidget(new QLabel(QStringLiteral("Key:"), this)); + layout->addWidget(m_keyInput); + layout->addWidget(new QLabel(QStringLiteral("Value:"), this)); + layout->addWidget(m_valueInput); + layout->addWidget(m_launchButton); + + connect(m_launchButton, &QPushButton::clicked, this, &Dialog::doSave); + + // As we work asynchronously we need to use a signal/slot architecture + connect(m_wallet, &KWallet::Wallet::walletOpened, this, &Dialog::walletOpened); + 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(QStringLiteral("Idle.")); + } else + m_statusLabel->setText(QStringLiteral("Error opening wallet!")); + +} + +void Dialog::doSave() +{ + if (m_keyInput->text().isEmpty() || m_valueInput->text().isEmpty()) { + m_statusLabel->setText(QStringLiteral("Empty field!")); + return; + } + + m_launchButton->setDisabled(true); + + m_statusLabel->setText(QStringLiteral("Saving ...")); + + QMap map; + map[m_keyInput->text()] = m_valueInput->text(); + + // Write in the map "http://test.com/#form" key/value contained in map + if (m_wallet->writeMap(QStringLiteral("http://test.com/#form"), map)) { + m_statusLabel->setText(QStringLiteral("Something went wrong!")); + } + else { + m_statusLabel->setText(QStringLiteral("Saved!")); + m_keyInput->clear(); + m_valueInput->clear(); + } + m_launchButton->setDisabled(false); +} diff --git a/examples/asynchronous_app/dialog.h b/examples/asynchronous_app/dialog.h new file mode 100644 index 0000000..544b07f --- /dev/null +++ b/examples/asynchronous_app/dialog.h @@ -0,0 +1,51 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2010-2012 Martin Sandsmark + * Copyright (C) 2018 Olivier Churlaud + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef DIALOG_H +#define DIALOG_H + +#include +#include +#include + +namespace KWallet { + class Wallet; +} + +class Dialog : public QDialog +{ +Q_OBJECT +public: + Dialog(QWidget *parent = nullptr); + +private Q_SLOTS: + void doSave(); + void walletOpened(bool ok); + +private: + KWallet::Wallet *m_wallet; + QLineEdit *m_keyInput; + QLineEdit *m_valueInput; + QLabel *m_statusLabel; + QPushButton *m_launchButton; +}; + +#endif // DIALOG_H diff --git a/examples/asynchronous_app/main.cpp b/examples/asynchronous_app/main.cpp new file mode 100644 index 0000000..5ba2fd1 --- /dev/null +++ b/examples/asynchronous_app/main.cpp @@ -0,0 +1,33 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2010-2012 Martin Sandsmark + * Copyright (C) 2018 Olivier Churlaud + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "dialog.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + Dialog dialog; + dialog.show(); + return app.exec(); +}