From 0c881f0c1b77cf8876094e3647d1732210b954d1 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 21 Mar 2018 17:34:05 +0100 Subject: An extension mechanism load qml files at generic extension points. and forward the email via an extension api. --- CMakeLists.txt | 5 ++ components/kube/qml/Kube.qml | 1 + extensions/CMakeLists.txt | 7 +++ extensions/api/CMakeLists.txt | 12 +++++ extensions/api/qmldir | 4 ++ extensions/api/src/CMakeLists.txt | 25 +++++++++ extensions/api/src/extensionapi.cpp | 89 +++++++++++++++++++++++++++++++ extensions/api/src/extensionapi.h | 30 +++++++++++ extensions/api/src/extensionapiplugin.cpp | 42 +++++++++++++++ extensions/api/src/extensionapiplugin.h | 33 ++++++++++++ extensions/fileasexpense/qml/main.qml | 32 +++++++++++ framework/qml/ExtensionPoint.qml | 37 +++++++++++++ framework/qml/MailViewer.qml | 11 +++- framework/qmldir | 1 + framework/src/extensionmodel.cpp | 24 +++++++-- framework/src/extensionmodel.h | 5 ++ views/conversation/qml/View.qml | 1 + 17 files changed, 353 insertions(+), 6 deletions(-) create mode 100644 extensions/CMakeLists.txt create mode 100644 extensions/api/CMakeLists.txt create mode 100644 extensions/api/qmldir create mode 100644 extensions/api/src/CMakeLists.txt create mode 100644 extensions/api/src/extensionapi.cpp create mode 100644 extensions/api/src/extensionapi.h create mode 100644 extensions/api/src/extensionapiplugin.cpp create mode 100644 extensions/api/src/extensionapiplugin.h create mode 100644 extensions/fileasexpense/qml/main.qml create mode 100644 framework/qml/ExtensionPoint.qml diff --git a/CMakeLists.txt b/CMakeLists.txt index 350dfea2..321cacf7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,8 @@ cmake_policy(SET CMP0053 NEW) cmake_policy(SET CMP0063 NEW) option(EXPERIMENTAL_VIEWS "Install experimental views" OFF) +#Do not enable this unless you actually distribute a custom extension. +option(ENABLE_EXTENSION "Enable custom kube extensions" OFF) include(CPack) include(FeatureSummary) @@ -41,3 +43,6 @@ add_subdirectory(applications) add_subdirectory(views) add_subdirectory(accounts) add_subdirectory(tests) +if (${ENABLE_EXTENSION}) + add_subdirectory(extensions) +endif() diff --git a/components/kube/qml/Kube.qml b/components/kube/qml/Kube.qml index bb161d8e..61933908 100644 --- a/components/kube/qml/Kube.qml +++ b/components/kube/qml/Kube.qml @@ -160,6 +160,7 @@ Controls2.ApplicationWindow { Repeater { model: Kube.ExtensionModel { id: extensionModel + extensionPoint: "views" sortOrder: ["composer", "conversation", "people"] } Kube.IconButton { diff --git a/extensions/CMakeLists.txt b/extensions/CMakeLists.txt new file mode 100644 index 00000000..b8412096 --- /dev/null +++ b/extensions/CMakeLists.txt @@ -0,0 +1,7 @@ +macro(install_extension name extensionpoint) + install(DIRECTORY ${name}/qml/ DESTINATION ${QML_INSTALL_DIR}/org/kube/extensions/${extensionpoint}/${name}) + #install(FILES ${name}/metadata.json DESTINATION ${QML_INSTALL_DIR}/org/kube/extensions/${name}) +endmacro() + +add_subdirectory(api) +install_extension(fileasexpense mailview) diff --git a/extensions/api/CMakeLists.txt b/extensions/api/CMakeLists.txt new file mode 100644 index 00000000..82a5c7b0 --- /dev/null +++ b/extensions/api/CMakeLists.txt @@ -0,0 +1,12 @@ +include(GenerateExportHeader) +include(ECMGenerateHeaders) +include(CMakePackageConfigHelpers) + +set(EXTENSIONAPI_INSTALL_DIR ${QML_INSTALL_DIR}/org/kube/extensionapi) + +install(DIRECTORY qml/ DESTINATION ${EXTENSIONAPI_INSTALL_DIR}) +install(FILES qmldir DESTINATION ${EXTENSIONAPI_INSTALL_DIR}) + +add_subdirectory(src) + +feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/extensions/api/qmldir b/extensions/api/qmldir new file mode 100644 index 00000000..405785e5 --- /dev/null +++ b/extensions/api/qmldir @@ -0,0 +1,4 @@ +module org.kube.extensionapi +depends org.kube.framework 1.0 + +plugin extensionapiplugin diff --git a/extensions/api/src/CMakeLists.txt b/extensions/api/src/CMakeLists.txt new file mode 100644 index 00000000..f7e543dc --- /dev/null +++ b/extensions/api/src/CMakeLists.txt @@ -0,0 +1,25 @@ +add_definitions("-Wall -std=c++14 -g") +set(CMAKE_CXX_VISIBILITY_PRESET default) + +find_package(Qt5 COMPONENTS REQUIRED Core Concurrent Quick Qml WebEngineWidgets Test WebEngine Gui) +find_package(KF5Mime 4.87.0 CONFIG REQUIRED) +find_package(Sink 0.6.0 CONFIG REQUIRED) + +include_directories(../../../framework/src/domain/mime ${KMIME_INCLUDES}) + +add_library(extensionapiplugin SHARED extensionapiplugin.cpp extensionapi.cpp) +target_link_libraries(extensionapiplugin + kubeframework + KF5::Mime + sink + Qt5::Core + Qt5::Quick + Qt5::Qml + Qt5::WebEngineWidgets + Qt5::Test + Qt5::WebEngine + Qt5::Gui +) +install(TARGETS extensionapiplugin DESTINATION ${EXTENSIONAPI_INSTALL_DIR}) + +feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/extensions/api/src/extensionapi.cpp b/extensions/api/src/extensionapi.cpp new file mode 100644 index 00000000..3e6689b9 --- /dev/null +++ b/extensions/api/src/extensionapi.cpp @@ -0,0 +1,89 @@ +/* + Copyright (c) 2018 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 "extensionapi.h" + +#include +#include +#include +#include + +#include + +static void send(const QByteArray &message, const QByteArray &accountId) +{ + using namespace Sink; + using namespace Sink::ApplicationDomain; + + Q_ASSERT(!accountId.isEmpty()); + Query query; + query.containsFilter(ResourceCapabilities::Mail::transport); + query.filter(accountId); + auto job = Store::fetchAll(query) + .then([=](const QList &resources) { + if (!resources.isEmpty()) { + auto resourceId = resources[0]->identifier(); + SinkLog() << "Sending message via resource: " << resourceId; + Mail mail(resourceId); + mail.setMimeMessage(message); + return Store::create(mail) + .then([=] { + //Trigger a sync, but don't wait for it. + Store::synchronize(Sink::SyncScope{}.resourceFilter(resourceId)).exec(); + }); + } + SinkWarning() << "Failed to find a mailtransport resource"; + return KAsync::error(0, "Failed to find a MailTransport resource."); + }) + .then([&] (const KAsync::Error &) { + SinkLog() << "Message was sent: "; + }); + job.exec(); +} + +static QStringList toStringList(const QVariantList &list) +{ + QStringList s; + for (const auto &e : list) { + s << e.toString(); + } + return s; +} + +Q_INVOKABLE void ExtensionApi::forwardMail(const QVariantMap &map) +{ + SinkLog() << "Forwarding mail " << map; + auto mailObject = map.value("mail").value(); + Q_ASSERT(mailObject); + KMime::Message::Ptr msg(new KMime::Message); + msg->setContent(KMime::CRLFtoLF(mailObject->getMimeMessage())); + msg->parse(); + + MailTemplates::forward(msg, [map] (const KMime::Message::Ptr &fwdMessage) { + auto msg = fwdMessage; + msg->subject()->fromUnicodeString(map.value("subject").toString(), "utf8"); + auto list = toStringList(map.value("to").toList()); + for (const auto &address : list) { + KMime::Types::Mailbox mb; + mb.fromUnicodeString(address); + msg->to()->addAddress(mb); + } + msg->assemble(); + send(msg->encodedContent(true), map.value("accountId").toByteArray()); + }); +} diff --git a/extensions/api/src/extensionapi.h b/extensions/api/src/extensionapi.h new file mode 100644 index 00000000..305d1206 --- /dev/null +++ b/extensions/api/src/extensionapi.h @@ -0,0 +1,30 @@ +/* + Copyright (c) 2018 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 + +class ExtensionApi : public QObject +{ + Q_OBJECT + +public: + Q_INVOKABLE void forwardMail(const QVariantMap &map); +}; diff --git a/extensions/api/src/extensionapiplugin.cpp b/extensions/api/src/extensionapiplugin.cpp new file mode 100644 index 00000000..3f399e36 --- /dev/null +++ b/extensions/api/src/extensionapiplugin.cpp @@ -0,0 +1,42 @@ +/* + Copyright (c) 2018 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 "extensionapiplugin.h" + +#include "extensionapi.h" + +#include + +void ExtensionApiPlugin::initializeEngine(QQmlEngine *engine, const char *uri) +{ + Q_UNUSED(uri); + Q_UNUSED(engine); +} + +static QObject *extensionApiSingleton(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + return new ExtensionApi; +} + +void ExtensionApiPlugin::registerTypes (const char *uri) +{ + qmlRegisterSingletonType(uri, 1, 0, "ExtensionApi", extensionApiSingleton); +} diff --git a/extensions/api/src/extensionapiplugin.h b/extensions/api/src/extensionapiplugin.h new file mode 100644 index 00000000..e23e5cc4 --- /dev/null +++ b/extensions/api/src/extensionapiplugin.h @@ -0,0 +1,33 @@ +/* + Copyright (c) 2018 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 + +class ExtensionApiPlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") + +public: + void registerTypes(const char *uri) Q_DECL_OVERRIDE; + void initializeEngine(QQmlEngine *engine, const char *uri) Q_DECL_OVERRIDE; +}; diff --git a/extensions/fileasexpense/qml/main.qml b/extensions/fileasexpense/qml/main.qml new file mode 100644 index 00000000..b53951f2 --- /dev/null +++ b/extensions/fileasexpense/qml/main.qml @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2017 Christian Mollekopf, + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import QtQuick 2.7 +import org.kube.framework 1.0 as Kube +import org.kube.extensionapi 1.0 + +Kube.Button { + property variant context: {} + visible: true + activeFocusOnTab: false + + text: qsTr("File as Expense") + onClicked: { + ExtensionApi.forwardMail({mail: context.mail, to: ["test1@kolab.org"], subject: "Expense: " + context.subject, accountId: context.accountId}) + } +} diff --git a/framework/qml/ExtensionPoint.qml b/framework/qml/ExtensionPoint.qml new file mode 100644 index 00000000..4f66b20e --- /dev/null +++ b/framework/qml/ExtensionPoint.qml @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2017 Christian Mollekopf, + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import QtQuick 2.7 + +import org.kube.framework 1.0 as Kube + +Repeater { + id: root + property alias extensionPoint: extensionModel.extensionPoint + property variant context: {} + + model: Kube.ExtensionModel { + id: extensionModel + } + Loader { + source: root.model.findSource(model.name, "main.qml") + onLoaded: { + item.context = root.context + } + } +} diff --git a/framework/qml/MailViewer.qml b/framework/qml/MailViewer.qml index 2a1af3a6..e9759d72 100644 --- a/framework/qml/MailViewer.qml +++ b/framework/qml/MailViewer.qml @@ -348,6 +348,8 @@ Rectangle { } Item { id: footer + property var mail: model.mail + property string subject: model.subject anchors.bottom: parent.bottom @@ -373,13 +375,12 @@ Rectangle { } } - Grid { + Row { anchors { verticalCenter: parent.verticalCenter right: parent.right rightMargin: Kube.Units.largeSpacing } - columns: 2 spacing: Kube.Units.smallSpacing Kube.Button { @@ -405,6 +406,12 @@ Rectangle { } } } + Row { + Kube.ExtensionPoint { + extensionPoint: "extensions/mailview" + context: {"mail": footer.mail, "subject": footer.subject, "accountId": currentAccount} + } + } } } diff --git a/framework/qmldir b/framework/qmldir index 80146b8e..4a6f40ef 100644 --- a/framework/qmldir +++ b/framework/qmldir @@ -45,6 +45,7 @@ TreeView 1.0 TreeView.qml GridView 1.0 GridView.qml ScrollHelper 1.0 ScrollHelper.qml ModelIndexRetriever 1.0 ModelIndexRetriever.qml +ExtensionPoint 1.0 ExtensionPoint.qml singleton Messages 1.0 Messages.qml singleton Notifications 1.0 Notifications.qml singleton Colors 1.0 Colors.qml diff --git a/framework/src/extensionmodel.cpp b/framework/src/extensionmodel.cpp index e3fab7d8..5f42aa7a 100644 --- a/framework/src/extensionmodel.cpp +++ b/framework/src/extensionmodel.cpp @@ -47,12 +47,17 @@ QHash ExtensionModel::roleNames() const void ExtensionModel::load() { - auto model = new QStandardItemModel(this); - + if (auto m = sourceModel()) { + setSourceModel(nullptr); + delete m; + } auto engine = qmlEngine(this); - Q_ASSERT(engine); + if (!engine) { + return; + } + auto model = new QStandardItemModel(this); for (const auto &path : engine->importPathList()) { - QDir dir{path + "/org/kube/views"}; + QDir dir{path + "/org/kube/" + mExtensionPoint}; for (const auto &pluginName : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { const auto pluginPath = dir.path() + "/" + pluginName; mPaths.insert(pluginName, pluginPath); @@ -101,6 +106,17 @@ QVariantList ExtensionModel::sortOrder() const return {}; } +void ExtensionModel::setExtensionPoint(const QString &extensionPoint) +{ + mExtensionPoint = extensionPoint; + QTimer::singleShot(0, this, &ExtensionModel::load); +} + +QString ExtensionModel::extensionPoint() const +{ + return mExtensionPoint; +} + QVariant ExtensionModel::data(const QModelIndex &idx, int role) const { return QSortFilterProxyModel::data(idx, role); diff --git a/framework/src/extensionmodel.h b/framework/src/extensionmodel.h index 5360cc2f..07601e57 100644 --- a/framework/src/extensionmodel.h +++ b/framework/src/extensionmodel.h @@ -29,6 +29,7 @@ class ExtensionModel : public QSortFilterProxyModel Q_OBJECT Q_PROPERTY(QVariantList sortOrder WRITE setSortOrder READ sortOrder) + Q_PROPERTY(QString extensionPoint WRITE setExtensionPoint READ extensionPoint) public: ExtensionModel(QObject *parent = Q_NULLPTR); @@ -49,6 +50,9 @@ public: void setSortOrder(const QVariantList &order); QVariantList sortOrder() const; + void setExtensionPoint(const QString &order); + QString extensionPoint() const; + Q_INVOKABLE QString findSource(const QString &extensionName, const QString &sourceName); private slots: @@ -57,6 +61,7 @@ private slots: private: QStringList mSortOrder; QHash mPaths; + QString mExtensionPoint; }; } diff --git a/views/conversation/qml/View.qml b/views/conversation/qml/View.qml index 5f0d362d..c0e44144 100644 --- a/views/conversation/qml/View.qml +++ b/views/conversation/qml/View.qml @@ -26,6 +26,7 @@ import QtQuick.Layouts 1.1 import org.kube.framework 1.0 as Kube FocusScope { + property alias currentAccount: accountFolderview.currentAccount SplitView { anchors.fill: parent Rectangle { -- cgit v1.2.3