From 9bfadc71a4fb694ae3946711d04acdfe009264c0 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 10 Mar 2016 11:57:08 +0100 Subject: A factory to load account plugins. --- framework/domain/CMakeLists.txt | 5 +-- framework/domain/accountfactory.cpp | 72 +++++++++++++++++++++++++++++++++++++ framework/domain/accountfactory.h | 48 +++++++++++++++++++++++++ framework/domain/mailplugin.cpp | 2 ++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 framework/domain/accountfactory.cpp create mode 100644 framework/domain/accountfactory.h (limited to 'framework/domain') diff --git a/framework/domain/CMakeLists.txt b/framework/domain/CMakeLists.txt index 361b816b..10a8c065 100644 --- a/framework/domain/CMakeLists.txt +++ b/framework/domain/CMakeLists.txt @@ -12,18 +12,19 @@ set(mailplugin_SRCS mailtransport.cpp mailtemplates.cpp retriever.cpp + accountfactory.cpp ) add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") find_package(CURL 7.20.0 REQUIRED) +find_package(KF5 REQUIRED COMPONENTS Package) include_directories(${CURL_INCLUDE_DIRS}) add_library(mailplugin SHARED ${mailplugin_SRCS}) qt5_use_modules(mailplugin Core Quick Qml WebKitWidgets) - -target_link_libraries(mailplugin actionplugin settingsplugin sink KF5::Otp KF5::Codecs ${CURL_LIBRARIES}) +target_link_libraries(mailplugin actionplugin settingsplugin sink KF5::Otp KF5::Codecs KF5::Package ${CURL_LIBRARIES}) install(TARGETS mailplugin DESTINATION ${QML_INSTALL_DIR}/org/kube/framework/domain) install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kube/framework/domain) diff --git a/framework/domain/accountfactory.cpp b/framework/domain/accountfactory.cpp new file mode 100644 index 00000000..d54f70a2 --- /dev/null +++ b/framework/domain/accountfactory.cpp @@ -0,0 +1,72 @@ +/* + 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 "accountfactory.h" + +#include +#include +#include + +#include + +AccountFactory::AccountFactory(QObject *parent) + : QObject(parent) +{ + +} + +QString AccountFactory::name() const +{ + return "Maildir"; +} + +QString AccountFactory::icon() const +{ + return "icon"; +} + +QVariant AccountFactory::ui() const +{ + return createComponent(getAccountType()); +} + +QByteArray AccountFactory::getAccountType() const +{ + return "maildir"; +} + +QString AccountFactory::uiPath() const +{ + auto accountType = getAccountType(); + auto package = KPackage::PackageLoader::self()->loadPackage("KPackage/GenericQML", "org.kube.accounts." + accountType); + Q_ASSERT(package.isValid()); + return package.filePath("mainscript"); +} + +QVariant AccountFactory::createComponent(const QByteArray &accountType) const const +{ + qWarning() << "Trying to load accounts package " << accountType << mAccountId; + auto engine = qmlEngine(this); + Q_ASSERT(engine); + auto component = new QQmlComponent(engine, QUrl::fromLocalFile(uiPath()), QQmlComponent::PreferSynchronous); + for (const auto &error : component->errors()) { + qWarning() << error.toString(); + } + Q_ASSERT(component->isReady()); + return QVariant::fromValue(component); +} diff --git a/framework/domain/accountfactory.h b/framework/domain/accountfactory.h new file mode 100644 index 00000000..0c6afe50 --- /dev/null +++ b/framework/domain/accountfactory.h @@ -0,0 +1,48 @@ +/* + 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 + +/** + * A factory to instantiate accountp plugins. + */ +class AccountFactory : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString accountId MEMBER mAccountId); + Q_PROPERTY(QString name READ name); + Q_PROPERTY(QString icon READ icon); + Q_PROPERTY(QVariant ui READ ui); + Q_PROPERTY(QString uiPath READ uiPath); +public: + explicit AccountFactory(QObject *parent = Q_NULLPTR); + + QString name() const; + QString icon() const; + QVariant ui() const; + QString uiPath() const; + + Q_INVOKABLE QVariant createComponent(const QByteArray &accountType) const; +private: + QByteArray getAccountType() const; + QString mAccountId; +}; diff --git a/framework/domain/mailplugin.cpp b/framework/domain/mailplugin.cpp index c45666c1..dd438114 100644 --- a/framework/domain/mailplugin.cpp +++ b/framework/domain/mailplugin.cpp @@ -25,6 +25,7 @@ #include "composercontroller.h" #include "messageparser.h" #include "retriever.h" +#include "accountfactory.h" #include @@ -37,4 +38,5 @@ void MailPlugin::registerTypes (const char *uri) qmlRegisterType(uri, 1, 0, "ComposerController"); qmlRegisterType(uri, 1, 0, "MessageParser"); qmlRegisterType(uri, 1, 0, "Retriever"); + qmlRegisterType(uri, 1, 0, "AccountFactory"); } -- cgit v1.2.3