summaryrefslogtreecommitdiffstats
path: root/framework/src/fabric.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-04-24 15:34:31 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-04-24 15:34:31 +0200
commit74703d12ef6f72a057f11957181b6cf6f4730e2d (patch)
treea9126fd02863243e26fdc2881b3910b163d59c87 /framework/src/fabric.cpp
parent68ed477e34756beb5b152f8077c2e2527bba4708 (diff)
downloadkube-74703d12ef6f72a057f11957181b6cf6f4730e2d.tar.gz
kube-74703d12ef6f72a057f11957181b6cf6f4730e2d.zip
Added the Fabric as an in application message bus
Diffstat (limited to 'framework/src/fabric.cpp')
-rw-r--r--framework/src/fabric.cpp83
1 files changed, 83 insertions, 0 deletions
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
24namespace Kube {
25namespace Fabric {
26
27class Bus {
28public:
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
55private:
56 QVector<Listener*> mListener;
57};
58
59void Fabric::postMessage(const QString &id, const QVariantMap &msg)
60{
61 Bus::instance().postMessage(id, msg);
62}
63
64Listener::Listener(QObject *parent)
65 : QObject(parent)
66{
67 Bus::instance().registerListener(this);
68}
69
70Listener::~Listener()
71{
72 Bus::instance().unregisterListener(this);
73}
74
75void Listener::notify(const QString &messageId, const QVariantMap &msg)
76{
77 if (messageId == mFilter) {
78 emit messageReceived(msg);
79 }
80}
81
82}
83}