summaryrefslogtreecommitdiffstats
path: root/framework
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-04 23:24:43 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-05 13:41:40 +0100
commit021dde561b5ce86820013c681ff554b07590b052 (patch)
treeb9d0b3b08efff6bd011544ef124c00ddec10012c /framework
parentdd09ca9ef4bb9780b953d6dd2999dbefe50bd1ff (diff)
downloadkube-021dde561b5ce86820013c681ff554b07590b052.tar.gz
kube-021dde561b5ce86820013c681ff554b07590b052.zip
Prototype of the action system
Diffstat (limited to 'framework')
-rw-r--r--framework/CMakeLists.txt1
-rw-r--r--framework/actions/CMakeLists.txt14
-rw-r--r--framework/actions/action.cpp84
-rw-r--r--framework/actions/action.h58
-rw-r--r--framework/actions/actionbroker.cpp74
-rw-r--r--framework/actions/actionbroker.h47
-rw-r--r--framework/actions/actionhandler.cpp70
-rw-r--r--framework/actions/actionhandler.h48
-rw-r--r--framework/actions/actionplugin.cpp15
-rw-r--r--framework/actions/actionplugin.h13
-rw-r--r--framework/actions/context.cpp29
-rw-r--r--framework/actions/context.h35
-rw-r--r--framework/actions/qmldir3
13 files changed, 491 insertions, 0 deletions
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()
27set(AKONADI2_RESOURCE_PLUGINS_PATH ${QT_PLUGIN_INSTALL_DIR}/akonadi2/resources) 27set(AKONADI2_RESOURCE_PLUGINS_PATH ${QT_PLUGIN_INSTALL_DIR}/akonadi2/resources)
28 28
29add_subdirectory(mail) 29add_subdirectory(mail)
30add_subdirectory(actions)
30 31
31feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) 32feature_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 @@
1set(SRCS
2 actionplugin.cpp
3 action.cpp
4 actionhandler.cpp
5 actionbroker.cpp
6 context.cpp
7)
8
9add_library(actionplugin SHARED ${SRCS})
10
11qt5_use_modules(actionplugin Core Quick Qml)
12
13install(TARGETS actionplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/actions)
14install(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 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#include "action.h"
20
21#include <QDebug>
22#include <QEvent>
23#include <QDynamicPropertyChangeEvent>
24#include <QMetaObject>
25#include <QMetaProperty>
26
27#include "actionbroker.h"
28#include "context.h"
29
30using namespace Kube;
31
32Action::Action(QObject *parent)
33 : QObject(parent),
34 mContext(nullptr)
35{
36}
37
38void Action::setContext(Context *context)
39{
40 //Get notified when any property changes
41 for (int i = 0; i < context->metaObject()->propertyCount(); i++) {
42 auto property = context->metaObject()->property(i) ;
43 qWarning() << "Property " << property.name() << property.hasNotifySignal() << property.notifySignal().name();
44 if (QString(property.name()) != "objectName") {
45 //We do what SIGNAL does to connect to the changed signal automatically
46 QObject::connect(context, "2"+property.notifySignal().name()+"()", this, SLOT(contextChanged()));
47 }
48 }
49 mContext = context;
50 mContext->installEventFilter(this);
51 emit readyChanged();
52}
53
54void Action::contextChanged()
55{
56 emit readyChanged();
57}
58
59Context *Action::context() const
60{
61 return mContext;
62}
63
64void Action::setActionId(const QByteArray &actionId)
65{
66 mActionId = actionId;
67 emit readyChanged();
68}
69
70QByteArray Action::actionId() const
71{
72 return mActionId;
73}
74
75bool Action::ready() const
76{
77 return ActionBroker::instance().isActionReady(mActionId, mContext);
78}
79
80void Action::execute()
81{
82 ActionBroker::instance().executeAction(mActionId, mContext);
83}
84
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 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#pragma once
20
21#include <QObject>
22#include "context.h"
23
24namespace Kube {
25
26class Action : public QObject
27{
28 Q_OBJECT
29 Q_PROPERTY(QByteArray actionId READ actionId WRITE setActionId)
30 //FIXME if I set the property to Context* qml fails to assign the registered type which is calle Kube::Context_QML_90 in QML...
31 Q_PROPERTY(Context* context READ context WRITE setContext)
32 Q_PROPERTY(bool ready READ ready NOTIFY readyChanged)
33
34public:
35 Action(QObject *parent = 0);
36
37 void setContext(Context *);
38 Context *context() const;
39
40 void setActionId(const QByteArray &);
41 QByteArray actionId() const;
42
43 bool ready() const;
44
45 Q_INVOKABLE void execute();
46
47Q_SIGNALS:
48 void readyChanged();
49
50private Q_SLOTS:
51 void contextChanged();
52
53private:
54 Context *mContext;
55 QByteArray mActionId;
56};
57
58}
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 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "actionbroker.h"
21
22#include "context.h"
23#include "actionhandler.h"
24
25#include <QDebug>
26
27using namespace Kube;
28
29ActionBroker::ActionBroker(QObject *parent)
30 : QObject(parent)
31{
32
33}
34
35ActionBroker &ActionBroker::instance()
36{
37 static ActionBroker instance;
38 return instance;
39}
40
41bool ActionBroker::isActionReady(const QByteArray &actionId, Context *context)
42{
43 if (!context) {
44 return false;
45 }
46
47 //TODO This should return true if all handlers together promise to gather all necessary data, but not otherwise
48 for (const auto handler : mHandler.values(actionId)) {
49 if (handler && handler->isActionReady(context)) {
50 return true;
51 }
52 }
53
54 return false;
55}
56
57void ActionBroker::executeAction(const QByteArray &actionId, Context *context)
58{
59 if (context) {
60 for (const auto handler : mHandler.values(actionId)) {
61 if (handler) {
62 handler->execute(context);
63 }
64 }
65 } else {
66 qWarning() << "Can't execute without context";
67 }
68}
69
70void ActionBroker::registerHandler(const QByteArray &actionId, ActionHandler *handler)
71{
72 //TODO get notified on destruction via QPointer
73 mHandler.insert(actionId, handler);
74}
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 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#pragma once
20
21#include <QObject>
22#include <QMultiMap>
23
24namespace Kube {
25class Context;
26class ActionHandler;
27
28class ActionBroker : public QObject
29{
30 Q_OBJECT
31public:
32 static ActionBroker &instance();
33
34 bool isActionReady(const QByteArray &actionId, Context *context);
35 void executeAction(const QByteArray &actionId, Context *context);
36
37 void registerHandler(const QByteArray &actionId, ActionHandler *handler);
38
39Q_SIGNALS:
40 void readyChanged();
41
42private:
43 ActionBroker(QObject *parent = 0);
44 QMultiMap<QByteArray, ActionHandler*> mHandler;
45};
46
47}
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 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "actionhandler.h"
21
22#include "context.h"
23#include "actionbroker.h"
24#include <QDebug>
25
26using namespace Kube;
27
28ActionHandler::ActionHandler(QObject *parent)
29 : QObject(parent)
30{
31
32}
33
34bool ActionHandler::isActionReady(Context *context)
35{
36 if (context) {
37 QVariant returnedValue;
38 QMetaObject::invokeMethod(this, "isReady",
39 Q_RETURN_ARG(QVariant, returnedValue),
40 Q_ARG(QVariant, QVariant::fromValue(context)));
41 return returnedValue.toBool();
42 } else {
43 qWarning() << "The handler didn't get a context";
44 }
45 return false;
46}
47
48void ActionHandler::execute(Context *context)
49{
50 QVariant returnedValue;
51 qWarning() << "Executing the handler";
52 if (context) {
53 QMetaObject::invokeMethod(this, "handler",
54 Q_RETURN_ARG(QVariant, returnedValue),
55 Q_ARG(QVariant, QVariant::fromValue(context)));
56 } else {
57 qWarning() << "The handler didn't get a context";
58 }
59}
60
61void ActionHandler::setActionId(const QByteArray &actionId)
62{
63 mActionId = actionId;
64 ActionBroker::instance().registerHandler(actionId, this);
65}
66
67QByteArray ActionHandler::actionId() const
68{
69 return mActionId;
70}
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 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#pragma once
20
21#include <QObject>
22#include <QMultiMap>
23
24namespace Kube {
25class Context;
26
27class ActionHandler : public QObject
28{
29 Q_OBJECT
30 Q_PROPERTY(QByteArray actionId READ actionId WRITE setActionId)
31
32public:
33 ActionHandler(QObject *parent = 0);
34
35 virtual bool isActionReady(Context *context);
36
37 // void pre(Context *context);
38 virtual void execute(Context *context);
39 // void post(Context *context);
40
41 void setActionId(const QByteArray &);
42 QByteArray actionId() const;
43
44private:
45 QByteArray mActionId;
46};
47
48}
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 @@
1#include "actionplugin.h"
2
3#include "action.h"
4#include "context.h"
5#include "actionhandler.h"
6
7#include <QtQml>
8
9void KubePlugin::registerTypes (const char *uri)
10{
11 Q_ASSERT(uri == QLatin1String("org.kde.kube.actions"));
12 qmlRegisterType<Kube::Context>(uri, 1, 0, "Context");
13 qmlRegisterType<Kube::Action>(uri, 1, 0, "Action");
14 qmlRegisterType<Kube::ActionHandler>(uri, 1, 0, "ActionHandler");
15}
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 @@
1#pragma once
2
3#include <QQmlEngine>
4#include <QQmlExtensionPlugin>
5
6class KubePlugin : public QQmlExtensionPlugin
7{
8 Q_OBJECT
9 Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
10
11public:
12 virtual void registerTypes(const char *uri);
13};
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 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#include "context.h"
20
21#include <QDebug>
22
23using namespace Kube;
24
25Context::Context(QObject *parent)
26 : QObject(parent)
27{
28
29}
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 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#pragma once
20
21#include <QObject>
22
23namespace Kube {
24
25class Context : public QObject {
26 Q_OBJECT
27public:
28 Context(QObject *parent = 0);
29 virtual ~Context(){};
30};
31
32}
33
34Q_DECLARE_METATYPE(Kube::Context*);
35
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 @@
1module org.kde.kube.actions
2
3plugin actionplugin