From fed67ae13d4b9c109449f6077cea328913a8548e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 25 Feb 2016 16:40:54 +0100 Subject: An overly basic settings framework. and a settings view to mess around. --- framework/CMakeLists.txt | 1 + framework/mail/CMakeLists.txt | 2 +- framework/mail/composer.cpp | 15 +++- framework/settings/CMakeLists.txt | 13 +-- framework/settings/qmldir | 2 +- framework/settings/settings.cpp | 147 ++++++++++++++++++++++++++++++++++ framework/settings/settings.h | 89 ++++++++++++++++++++ framework/settings/settingsplugin.cpp | 16 ++-- 8 files changed, 266 insertions(+), 19 deletions(-) create mode 100644 framework/settings/settings.cpp create mode 100644 framework/settings/settings.h (limited to 'framework') diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index d8d45af9..1955d709 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -36,5 +36,6 @@ set(SINK_RESOURCE_PLUGINS_PATH ${QT_PLUGIN_INSTALL_DIR}/sink/resources) add_subdirectory(mail) add_subdirectory(actions) +add_subdirectory(settings) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/framework/mail/CMakeLists.txt b/framework/mail/CMakeLists.txt index 7215427b..54a031cc 100644 --- a/framework/mail/CMakeLists.txt +++ b/framework/mail/CMakeLists.txt @@ -24,7 +24,7 @@ add_library(mailplugin SHARED ${mailplugin_SRCS}) qt5_use_modules(mailplugin Core Quick Qml) -target_link_libraries(mailplugin actionplugin sink KF5::Otp KF5::Codecs ${CURL_LIBRARIES}) +target_link_libraries(mailplugin actionplugin settingsplugin sink KF5::Otp KF5::Codecs ${CURL_LIBRARIES}) install(TARGETS mailplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/mail) install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/mail) diff --git a/framework/mail/composer.cpp b/framework/mail/composer.cpp index 1ec56347..4ef112fa 100644 --- a/framework/mail/composer.cpp +++ b/framework/mail/composer.cpp @@ -21,6 +21,7 @@ #include "composer.h" #include #include +#include #include #include #include @@ -127,12 +128,18 @@ void Composer::send() mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); mail->setBody(m_body.toUtf8()); mail->assemble(); + + Kube::ApplicationContext settings; + auto account = settings.currentAccount(); + auto identity = account.primaryIdentity(); + auto transport = identity.transport(); + Kube::Context context; context.setProperty("message", QVariant::fromValue(mail)); - //TODO get from somewhere - context.setProperty("username", QVariant::fromValue(QByteArray("test@test.com"))); - context.setProperty("password", QVariant::fromValue(QByteArray("pass"))); - context.setProperty("server", QVariant::fromValue(QByteArray("smtp://smtp.gmail.com:587"))); + + context.setProperty("username", transport.username()); + context.setProperty("password", transport.password()); + context.setProperty("server", transport.server()); Kube::Action("org.kde.kube.actions.sendmail", context).execute(); clear(); diff --git a/framework/settings/CMakeLists.txt b/framework/settings/CMakeLists.txt index 77268ab9..15ab7584 100644 --- a/framework/settings/CMakeLists.txt +++ b/framework/settings/CMakeLists.txt @@ -1,15 +1,16 @@ set(settingsplugin_SRCS settingsplugin.cpp - maildir_resource.cpp - resourcelistmodel.cpp - resourcescontroller.cpp + # maildir_resource.cpp + # resourcelistmodel.cpp + # resourcescontroller.cpp + settings.cpp ) add_library(settingsplugin SHARED ${settingsplugin_SRCS}) qt5_use_modules(settingsplugin Core Quick Qml) -target_link_libraries(settingsplugin sink) +target_link_libraries(settingsplugin) -install(TARGETS settingsplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/sink/settings) -install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/sink/settings) +install(TARGETS settingsplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/settings) +install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/settings) diff --git a/framework/settings/qmldir b/framework/settings/qmldir index 1740f29a..7b756aea 100644 --- a/framework/settings/qmldir +++ b/framework/settings/qmldir @@ -1,3 +1,3 @@ -module org.kde.sink.settings +module org.kde.kube.settings plugin settingsplugin diff --git a/framework/settings/settings.cpp b/framework/settings/settings.cpp new file mode 100644 index 00000000..a4e28190 --- /dev/null +++ b/framework/settings/settings.cpp @@ -0,0 +1,147 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + 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 2 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 "settings.h" + +#include +#include +#include +#include + +using namespace Kube; + +Settings::Settings(QObject *parent) + : QObject(parent) +{ + +} + +Settings::Settings(const QByteArray &id, QObject *parent) + : QObject(parent), + mIdentifier(id) +{ + load(); +} + +Settings::Settings(const Settings &other) + : QObject(other.parent()), + mIdentifier(other.mIdentifier) +{ + load(); +} + +Settings::~Settings() +{ + // save(); +} + +QSharedPointer Settings::getSettings() +{ + return QSharedPointer::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QString("/kube/%1.ini").arg(QString::fromLatin1(mIdentifier)), QSettings::IniFormat); +} + +void Settings::save() +{ + qWarning() << "Saving" << mIdentifier; + auto settings = getSettings(); + for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) { + const auto p = metaObject()->property(i).name(); + qWarning() << "setting " << p << property(p); + settings->setValue(p, property(p)); + } + settings->sync(); +} + +void Settings::load() +{ + qWarning() << "loading" << mIdentifier; + for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) { + auto p = metaObject()->property(i).name(); + setProperty(p, QVariant()); + } + auto settings = getSettings(); + for (const auto &p : settings->allKeys()) { + qWarning() << "loading " << p << settings->value(p); + setProperty(p.toLatin1(), settings->value(p)); + } +} + +void Settings::setIdentifier(const QByteArray &id) +{ + mIdentifier = id; + load(); +} + +QByteArray Settings::identifier() const +{ + return mIdentifier; +} + +ApplicationContext::ApplicationContext() + : Settings("applicationcontext") +{ + +} + +Account ApplicationContext::currentAccount() const +{ + return Account(property("currentAccountId").toByteArray()); +} + +Account::Account(const QByteArray &identifier) + : Settings("account." + identifier) +{ + +} + +Identity Account::primaryIdentity() const +{ + return Identity(property("primaryIdentityId").toByteArray()); +} + +Identity::Identity(const QByteArray &identifier) + : Settings("identity." + identifier) +{ + +} + +Transport Identity::transport() const +{ + return Transport(property("transportId").toByteArray()); +} + +Transport::Transport(const QByteArray &identifier) + : Settings("transport." + identifier) +{ + +} + +QByteArray Transport::username() const +{ + return property("username").toByteArray(); +} + +QByteArray Transport::password() const +{ + return property("password").toByteArray(); +} + +QByteArray Transport::server() const +{ + return property("server").toByteArray(); +} diff --git a/framework/settings/settings.h b/framework/settings/settings.h new file mode 100644 index 00000000..bfee55cb --- /dev/null +++ b/framework/settings/settings.h @@ -0,0 +1,89 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + 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 2 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. +*/ +#pragma once + +#include +#include +#include +#include + +namespace Kube { + +class Settings : public QObject { + Q_OBJECT + Q_PROPERTY(QByteArray identifier READ identifier WRITE setIdentifier) +public: + Settings(QObject *parent = 0); + Settings(const QByteArray &id, QObject *parent = 0); + virtual ~Settings(); + Settings(const Settings&); + + void setIdentifier(const QByteArray &id); + QByteArray identifier() const; + + Q_INVOKABLE void save(); +private: + void load(); + QSharedPointer getSettings(); + QByteArray mIdentifier; +}; + +class Account; +class Identity; +class Transport; + +class ApplicationContext : public Settings +{ + Q_OBJECT +public: + ApplicationContext(); + Account currentAccount() const; + +}; + +class Account : public Settings +{ + Q_OBJECT +public: + Account(const QByteArray &identifier); + Identity primaryIdentity() const; +}; + +class Identity : public Settings +{ + Q_OBJECT +public: + Identity(const QByteArray &identifier); + Transport transport() const; +}; + +class Transport : public Settings +{ + Q_OBJECT +public: + Transport(const QByteArray &identifier); + QByteArray username() const; + QByteArray password() const; + QByteArray server() const; +}; + +} + +Q_DECLARE_METATYPE(Kube::Settings*); + diff --git a/framework/settings/settingsplugin.cpp b/framework/settings/settingsplugin.cpp index ca670583..a1888669 100644 --- a/framework/settings/settingsplugin.cpp +++ b/framework/settings/settingsplugin.cpp @@ -1,16 +1,18 @@ #include "settingsplugin.h" -#include "resourcescontroller.h" -#include "resourcelistmodel.h" -#include "maildir_resource.h" +// #include "resourcescontroller.h" +// #include "resourcelistmodel.h" +// #include "maildir_resource.h" +#include "settings.h" #include void SettingsPlugin::registerTypes (const char *uri) { - Q_ASSERT(uri == QLatin1String("org.kde.sink.settings")); + Q_ASSERT(uri == QLatin1String("org.kde.kube.settings")); - qmlRegisterType(); - qmlRegisterType(uri, 1, 0, "Resources"); - qmlRegisterType(uri, 1, 0, "Maildir"); + // qmlRegisterType(); + // qmlRegisterType(uri, 1, 0, "Resources"); + // qmlRegisterType(uri, 1, 0, "Maildir"); + qmlRegisterType(uri, 1, 0, "Settings"); } -- cgit v1.2.3