diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-24 15:34:31 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-24 15:34:31 +0200 |
commit | 74703d12ef6f72a057f11957181b6cf6f4730e2d (patch) | |
tree | a9126fd02863243e26fdc2881b3910b163d59c87 /framework/src | |
parent | 68ed477e34756beb5b152f8077c2e2527bba4708 (diff) | |
download | kube-74703d12ef6f72a057f11957181b6cf6f4730e2d.tar.gz kube-74703d12ef6f72a057f11957181b6cf6f4730e2d.zip |
Added the Fabric as an in application message bus
Diffstat (limited to 'framework/src')
-rw-r--r-- | framework/src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | framework/src/fabric.cpp | 83 | ||||
-rw-r--r-- | framework/src/fabric.h | 70 | ||||
-rw-r--r-- | framework/src/frameworkplugin.cpp | 11 |
4 files changed, 165 insertions, 0 deletions
diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt index b773748b..39f17c32 100644 --- a/framework/src/CMakeLists.txt +++ b/framework/src/CMakeLists.txt | |||
@@ -49,6 +49,7 @@ set(SRCS | |||
49 | accounts/accountfactory.cpp | 49 | accounts/accountfactory.cpp |
50 | accounts/accountsmodel.cpp | 50 | accounts/accountsmodel.cpp |
51 | notifications/notificationhandler.cpp | 51 | notifications/notificationhandler.cpp |
52 | fabric.cpp | ||
52 | ) | 53 | ) |
53 | 54 | ||
54 | add_library(frameworkplugin SHARED ${SRCS}) | 55 | add_library(frameworkplugin SHARED ${SRCS}) |
diff --git a/framework/src/fabric.cpp b/framework/src/fabric.cpp new file mode 100644 index 00000000..b14ed55d --- /dev/null +++ b/framework/src/fabric.cpp | |||
@@ -0,0 +1,83 @@ | |||
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 "fabric.h" | ||
21 | |||
22 | #include <QDebug> | ||
23 | |||
24 | namespace Kube { | ||
25 | namespace Fabric { | ||
26 | |||
27 | class Bus { | ||
28 | public: | ||
29 | Bus() = default; | ||
30 | ~Bus() = default; | ||
31 | |||
32 | static Bus &instance() | ||
33 | { | ||
34 | static Bus bus; | ||
35 | return bus; | ||
36 | } | ||
37 | |||
38 | void registerListener(Listener *listener) | ||
39 | { | ||
40 | mListener << listener; | ||
41 | } | ||
42 | |||
43 | void unregisterListener(Listener *listener) | ||
44 | { | ||
45 | mListener.removeAll(listener); | ||
46 | } | ||
47 | |||
48 | void postMessage(const QString &id, const QVariantMap &message) | ||
49 | { | ||
50 | for (const auto &l : mListener) { | ||
51 | l->notify(id, message); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | private: | ||
56 | QVector<Listener*> mListener; | ||
57 | }; | ||
58 | |||
59 | void Fabric::postMessage(const QString &id, const QVariantMap &msg) | ||
60 | { | ||
61 | Bus::instance().postMessage(id, msg); | ||
62 | } | ||
63 | |||
64 | Listener::Listener(QObject *parent) | ||
65 | : QObject(parent) | ||
66 | { | ||
67 | Bus::instance().registerListener(this); | ||
68 | } | ||
69 | |||
70 | Listener::~Listener() | ||
71 | { | ||
72 | Bus::instance().unregisterListener(this); | ||
73 | } | ||
74 | |||
75 | void Listener::notify(const QString &messageId, const QVariantMap &msg) | ||
76 | { | ||
77 | if (messageId == mFilter) { | ||
78 | emit messageReceived(msg); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | } | ||
83 | } | ||
diff --git a/framework/src/fabric.h b/framework/src/fabric.h new file mode 100644 index 00000000..764c42b1 --- /dev/null +++ b/framework/src/fabric.h | |||
@@ -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 | #pragma once | ||
20 | |||
21 | #include <QObject> | ||
22 | #include <QVariant> | ||
23 | #include <QVector> | ||
24 | |||
25 | namespace Kube { | ||
26 | /** | ||
27 | * The Fabric is a publish/subscribe message bus to interconnect components in kube. | ||
28 | * | ||
29 | * It provides a background mesh ("the fabric"), that interconnects various parts of kube. | ||
30 | * This is useful as it allows us to decouple functionality from the UI components, and keeps us from writing unnecessary API | ||
31 | * for visual components to pass through all necessary information for interaction. | ||
32 | */ | ||
33 | namespace Fabric { | ||
34 | |||
35 | class Fabric : public QObject { | ||
36 | Q_OBJECT | ||
37 | public: | ||
38 | Q_INVOKABLE void postMessage(const QString &id, const QVariantMap &); | ||
39 | }; | ||
40 | |||
41 | /** | ||
42 | * A message handler. | ||
43 | * | ||
44 | * Can beinstantiated from QML like so: | ||
45 | * Listener { | ||
46 | * function onMessageReceived(msg) { | ||
47 | * ...do something | ||
48 | * } | ||
49 | * } | ||
50 | */ | ||
51 | class Listener : public QObject | ||
52 | { | ||
53 | Q_OBJECT | ||
54 | Q_PROPERTY(QString filter MEMBER mFilter) | ||
55 | |||
56 | public: | ||
57 | Listener(QObject *parent = 0); | ||
58 | virtual ~Listener(); | ||
59 | |||
60 | virtual void notify(const QString &id, const QVariantMap ¬ification); | ||
61 | |||
62 | signals: | ||
63 | void messageReceived(const QVariantMap &message); | ||
64 | |||
65 | private: | ||
66 | QString mFilter; | ||
67 | }; | ||
68 | |||
69 | } | ||
70 | } | ||
diff --git a/framework/src/frameworkplugin.cpp b/framework/src/frameworkplugin.cpp index f491bae7..4c17242f 100644 --- a/framework/src/frameworkplugin.cpp +++ b/framework/src/frameworkplugin.cpp | |||
@@ -40,9 +40,17 @@ | |||
40 | #include "actions/context.h" | 40 | #include "actions/context.h" |
41 | #include "actions/actionhandler.h" | 41 | #include "actions/actionhandler.h" |
42 | #include "actions/actionresult.h" | 42 | #include "actions/actionresult.h" |
43 | #include "fabric.h" | ||
43 | 44 | ||
44 | #include <QtQml> | 45 | #include <QtQml> |
45 | 46 | ||
47 | static QObject *example_qobject_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) | ||
48 | { | ||
49 | Q_UNUSED(engine) | ||
50 | Q_UNUSED(scriptEngine) | ||
51 | return new Kube::Fabric::Fabric; | ||
52 | } | ||
53 | |||
46 | void FrameworkPlugin::registerTypes (const char *uri) | 54 | void FrameworkPlugin::registerTypes (const char *uri) |
47 | { | 55 | { |
48 | qmlRegisterType<FolderListModel>(uri, 1, 0, "FolderListModel"); | 56 | qmlRegisterType<FolderListModel>(uri, 1, 0, "FolderListModel"); |
@@ -69,4 +77,7 @@ void FrameworkPlugin::registerTypes (const char *uri) | |||
69 | qmlRegisterType<Kube::Action>(uri, 1, 0, "Action"); | 77 | qmlRegisterType<Kube::Action>(uri, 1, 0, "Action"); |
70 | qmlRegisterType<Kube::ActionHandler>(uri, 1, 0, "ActionHandler"); | 78 | qmlRegisterType<Kube::ActionHandler>(uri, 1, 0, "ActionHandler"); |
71 | qmlRegisterType<Kube::ActionResult>(uri, 1, 0, "ActionResult"); | 79 | qmlRegisterType<Kube::ActionResult>(uri, 1, 0, "ActionResult"); |
80 | |||
81 | qmlRegisterType<Kube::Fabric::Listener>(uri, 1, 0, "Listener"); | ||
82 | qmlRegisterSingletonType<Kube::Fabric::Fabric>(uri, 1, 0, "Fabric", example_qobject_singletontype_provider); | ||
72 | } | 83 | } |