From 021dde561b5ce86820013c681ff554b07590b052 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 4 Jan 2016 23:24:43 +0100 Subject: Prototype of the action system --- .../kube-mail/package/contents/ui/main.qml | 29 +++++++- framework/CMakeLists.txt | 1 + framework/actions/CMakeLists.txt | 14 ++++ framework/actions/action.cpp | 84 ++++++++++++++++++++++ framework/actions/action.h | 58 +++++++++++++++ framework/actions/actionbroker.cpp | 74 +++++++++++++++++++ framework/actions/actionbroker.h | 47 ++++++++++++ framework/actions/actionhandler.cpp | 70 ++++++++++++++++++ framework/actions/actionhandler.h | 48 +++++++++++++ framework/actions/actionplugin.cpp | 15 ++++ framework/actions/actionplugin.h | 13 ++++ framework/actions/context.cpp | 29 ++++++++ framework/actions/context.h | 35 +++++++++ framework/actions/qmldir | 3 + 14 files changed, 519 insertions(+), 1 deletion(-) create mode 100644 framework/actions/CMakeLists.txt create mode 100644 framework/actions/action.cpp create mode 100644 framework/actions/action.h create mode 100644 framework/actions/actionbroker.cpp create mode 100644 framework/actions/actionbroker.h create mode 100644 framework/actions/actionhandler.cpp create mode 100644 framework/actions/actionhandler.h create mode 100644 framework/actions/actionplugin.cpp create mode 100644 framework/actions/actionplugin.h create mode 100644 framework/actions/context.cpp create mode 100644 framework/actions/context.h create mode 100644 framework/actions/qmldir diff --git a/applications/kube-mail/package/contents/ui/main.qml b/applications/kube-mail/package/contents/ui/main.qml index 200328f3..33744157 100644 --- a/applications/kube-mail/package/contents/ui/main.qml +++ b/applications/kube-mail/package/contents/ui/main.qml @@ -20,6 +20,8 @@ import QtQuick.Controls 1.3 import QtQuick.Layouts 1.1 import org.kde.plasma.components 2.0 as PlasmaComponents +import org.kde.kube.actions 1.0 as Action + ApplicationWindow { id: app @@ -30,6 +32,29 @@ ApplicationWindow { visible: true + Action.ActionHandler { + actionId: "org.kde.kube.actions.mark-as-read" + function isReady(context) { + return context.mail ? true : false; + } + + function handler(context) { + console.warn("Got message:", context.mail) + } + } + + Action.Context { + id: "maillistcontext" + property variant mail + mail: mailListView.currentMail + } + + Action.Action { + id: "markAsReadAction" + actionId: "org.kde.kube.actions.mark-as-read" + context: maillistcontext + } + //UI toolBar: ToolBar { @@ -51,9 +76,10 @@ ApplicationWindow { iconName: "mail-mark-unread" text: "Mark Unread" + enabled: markAsReadAction.ready onClicked: { - mailList.markMailUnread(true) + markAsReadAction.execute() } } @@ -109,6 +135,7 @@ ApplicationWindow { Layout.fillWidth: true } + } //TODO find a better way to scale UI diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 1e0dd958..3a441f99 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -27,5 +27,6 @@ enable_testing() set(AKONADI2_RESOURCE_PLUGINS_PATH ${QT_PLUGIN_INSTALL_DIR}/akonadi2/resources) add_subdirectory(mail) +add_subdirectory(actions) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/framework/actions/CMakeLists.txt b/framework/actions/CMakeLists.txt new file mode 100644 index 00000000..1151878c --- /dev/null +++ b/framework/actions/CMakeLists.txt @@ -0,0 +1,14 @@ +set(SRCS + actionplugin.cpp + action.cpp + actionhandler.cpp + actionbroker.cpp + context.cpp +) + +add_library(actionplugin SHARED ${SRCS}) + +qt5_use_modules(actionplugin Core Quick Qml) + +install(TARGETS actionplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/actions) +install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/actions) diff --git a/framework/actions/action.cpp b/framework/actions/action.cpp new file mode 100644 index 00000000..1f94ae81 --- /dev/null +++ b/framework/actions/action.cpp @@ -0,0 +1,84 @@ +/* + 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 "action.h" + +#include +#include +#include +#include +#include + +#include "actionbroker.h" +#include "context.h" + +using namespace Kube; + +Action::Action(QObject *parent) + : QObject(parent), + mContext(nullptr) +{ +} + +void Action::setContext(Context *context) +{ + //Get notified when any property changes + for (int i = 0; i < context->metaObject()->propertyCount(); i++) { + auto property = context->metaObject()->property(i) ; + qWarning() << "Property " << property.name() << property.hasNotifySignal() << property.notifySignal().name(); + if (QString(property.name()) != "objectName") { + //We do what SIGNAL does to connect to the changed signal automatically + QObject::connect(context, "2"+property.notifySignal().name()+"()", this, SLOT(contextChanged())); + } + } + mContext = context; + mContext->installEventFilter(this); + emit readyChanged(); +} + +void Action::contextChanged() +{ + emit readyChanged(); +} + +Context *Action::context() const +{ + return mContext; +} + +void Action::setActionId(const QByteArray &actionId) +{ + mActionId = actionId; + emit readyChanged(); +} + +QByteArray Action::actionId() const +{ + return mActionId; +} + +bool Action::ready() const +{ + return ActionBroker::instance().isActionReady(mActionId, mContext); +} + +void Action::execute() +{ + ActionBroker::instance().executeAction(mActionId, mContext); +} + diff --git a/framework/actions/action.h b/framework/actions/action.h new file mode 100644 index 00000000..067c3c37 --- /dev/null +++ b/framework/actions/action.h @@ -0,0 +1,58 @@ +/* + 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 "context.h" + +namespace Kube { + +class Action : public QObject +{ + Q_OBJECT + Q_PROPERTY(QByteArray actionId READ actionId WRITE setActionId) + //FIXME if I set the property to Context* qml fails to assign the registered type which is calle Kube::Context_QML_90 in QML... + Q_PROPERTY(Context* context READ context WRITE setContext) + Q_PROPERTY(bool ready READ ready NOTIFY readyChanged) + +public: + Action(QObject *parent = 0); + + void setContext(Context *); + Context *context() const; + + void setActionId(const QByteArray &); + QByteArray actionId() const; + + bool ready() const; + + Q_INVOKABLE void execute(); + +Q_SIGNALS: + void readyChanged(); + +private Q_SLOTS: + void contextChanged(); + +private: + Context *mContext; + QByteArray mActionId; +}; + +} diff --git a/framework/actions/actionbroker.cpp b/framework/actions/actionbroker.cpp new file mode 100644 index 00000000..43a535a1 --- /dev/null +++ b/framework/actions/actionbroker.cpp @@ -0,0 +1,74 @@ +/* + 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 "actionbroker.h" + +#include "context.h" +#include "actionhandler.h" + +#include + +using namespace Kube; + +ActionBroker::ActionBroker(QObject *parent) + : QObject(parent) +{ + +} + +ActionBroker &ActionBroker::instance() +{ + static ActionBroker instance; + return instance; +} + +bool ActionBroker::isActionReady(const QByteArray &actionId, Context *context) +{ + if (!context) { + return false; + } + + //TODO This should return true if all handlers together promise to gather all necessary data, but not otherwise + for (const auto handler : mHandler.values(actionId)) { + if (handler && handler->isActionReady(context)) { + return true; + } + } + + return false; +} + +void ActionBroker::executeAction(const QByteArray &actionId, Context *context) +{ + if (context) { + for (const auto handler : mHandler.values(actionId)) { + if (handler) { + handler->execute(context); + } + } + } else { + qWarning() << "Can't execute without context"; + } +} + +void ActionBroker::registerHandler(const QByteArray &actionId, ActionHandler *handler) +{ + //TODO get notified on destruction via QPointer + mHandler.insert(actionId, handler); +} diff --git a/framework/actions/actionbroker.h b/framework/actions/actionbroker.h new file mode 100644 index 00000000..08eac742 --- /dev/null +++ b/framework/actions/actionbroker.h @@ -0,0 +1,47 @@ +/* + 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 + +namespace Kube { +class Context; +class ActionHandler; + +class ActionBroker : public QObject +{ + Q_OBJECT +public: + static ActionBroker &instance(); + + bool isActionReady(const QByteArray &actionId, Context *context); + void executeAction(const QByteArray &actionId, Context *context); + + void registerHandler(const QByteArray &actionId, ActionHandler *handler); + +Q_SIGNALS: + void readyChanged(); + +private: + ActionBroker(QObject *parent = 0); + QMultiMap mHandler; +}; + +} diff --git a/framework/actions/actionhandler.cpp b/framework/actions/actionhandler.cpp new file mode 100644 index 00000000..748c2303 --- /dev/null +++ b/framework/actions/actionhandler.cpp @@ -0,0 +1,70 @@ +/* + 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 "actionhandler.h" + +#include "context.h" +#include "actionbroker.h" +#include + +using namespace Kube; + +ActionHandler::ActionHandler(QObject *parent) + : QObject(parent) +{ + +} + +bool ActionHandler::isActionReady(Context *context) +{ + if (context) { + QVariant returnedValue; + QMetaObject::invokeMethod(this, "isReady", + Q_RETURN_ARG(QVariant, returnedValue), + Q_ARG(QVariant, QVariant::fromValue(context))); + return returnedValue.toBool(); + } else { + qWarning() << "The handler didn't get a context"; + } + return false; +} + +void ActionHandler::execute(Context *context) +{ + QVariant returnedValue; + qWarning() << "Executing the handler"; + if (context) { + QMetaObject::invokeMethod(this, "handler", + Q_RETURN_ARG(QVariant, returnedValue), + Q_ARG(QVariant, QVariant::fromValue(context))); + } else { + qWarning() << "The handler didn't get a context"; + } +} + +void ActionHandler::setActionId(const QByteArray &actionId) +{ + mActionId = actionId; + ActionBroker::instance().registerHandler(actionId, this); +} + +QByteArray ActionHandler::actionId() const +{ + return mActionId; +} diff --git a/framework/actions/actionhandler.h b/framework/actions/actionhandler.h new file mode 100644 index 00000000..cfcfddd7 --- /dev/null +++ b/framework/actions/actionhandler.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 + +namespace Kube { +class Context; + +class ActionHandler : public QObject +{ + Q_OBJECT + Q_PROPERTY(QByteArray actionId READ actionId WRITE setActionId) + +public: + ActionHandler(QObject *parent = 0); + + virtual bool isActionReady(Context *context); + + // void pre(Context *context); + virtual void execute(Context *context); + // void post(Context *context); + + void setActionId(const QByteArray &); + QByteArray actionId() const; + +private: + QByteArray mActionId; +}; + +} diff --git a/framework/actions/actionplugin.cpp b/framework/actions/actionplugin.cpp new file mode 100644 index 00000000..710ba08a --- /dev/null +++ b/framework/actions/actionplugin.cpp @@ -0,0 +1,15 @@ +#include "actionplugin.h" + +#include "action.h" +#include "context.h" +#include "actionhandler.h" + +#include + +void KubePlugin::registerTypes (const char *uri) +{ + Q_ASSERT(uri == QLatin1String("org.kde.kube.actions")); + qmlRegisterType(uri, 1, 0, "Context"); + qmlRegisterType(uri, 1, 0, "Action"); + qmlRegisterType(uri, 1, 0, "ActionHandler"); +} diff --git a/framework/actions/actionplugin.h b/framework/actions/actionplugin.h new file mode 100644 index 00000000..e276da3b --- /dev/null +++ b/framework/actions/actionplugin.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +class KubePlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") + +public: + virtual void registerTypes(const char *uri); +}; diff --git a/framework/actions/context.cpp b/framework/actions/context.cpp new file mode 100644 index 00000000..a7f87d16 --- /dev/null +++ b/framework/actions/context.cpp @@ -0,0 +1,29 @@ +/* + 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 "context.h" + +#include + +using namespace Kube; + +Context::Context(QObject *parent) + : QObject(parent) +{ + +} diff --git a/framework/actions/context.h b/framework/actions/context.h new file mode 100644 index 00000000..7289f850 --- /dev/null +++ b/framework/actions/context.h @@ -0,0 +1,35 @@ +/* + 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 + +namespace Kube { + +class Context : public QObject { + Q_OBJECT +public: + Context(QObject *parent = 0); + virtual ~Context(){}; +}; + +} + +Q_DECLARE_METATYPE(Kube::Context*); + diff --git a/framework/actions/qmldir b/framework/actions/qmldir new file mode 100644 index 00000000..de2e4b46 --- /dev/null +++ b/framework/actions/qmldir @@ -0,0 +1,3 @@ +module org.kde.kube.actions + +plugin actionplugin -- cgit v1.2.3