From 74703d12ef6f72a057f11957181b6cf6f4730e2d Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 24 Apr 2017 15:34:31 +0200 Subject: Added the Fabric as an in application message bus --- framework/src/CMakeLists.txt | 1 + framework/src/fabric.cpp | 83 +++++++++++++++++++++++++++++++++++++++ framework/src/fabric.h | 70 +++++++++++++++++++++++++++++++++ framework/src/frameworkplugin.cpp | 11 ++++++ 4 files changed, 165 insertions(+) create mode 100644 framework/src/fabric.cpp create mode 100644 framework/src/fabric.h (limited to 'framework/src') 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 accounts/accountfactory.cpp accounts/accountsmodel.cpp notifications/notificationhandler.cpp + fabric.cpp ) 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 @@ +/* + 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 "fabric.h" + +#include + +namespace Kube { +namespace Fabric { + +class Bus { +public: + Bus() = default; + ~Bus() = default; + + static Bus &instance() + { + static Bus bus; + return bus; + } + + void registerListener(Listener *listener) + { + mListener << listener; + } + + void unregisterListener(Listener *listener) + { + mListener.removeAll(listener); + } + + void postMessage(const QString &id, const QVariantMap &message) + { + for (const auto &l : mListener) { + l->notify(id, message); + } + } + +private: + QVector mListener; +}; + +void Fabric::postMessage(const QString &id, const QVariantMap &msg) +{ + Bus::instance().postMessage(id, msg); +} + +Listener::Listener(QObject *parent) + : QObject(parent) +{ + Bus::instance().registerListener(this); +} + +Listener::~Listener() +{ + Bus::instance().unregisterListener(this); +} + +void Listener::notify(const QString &messageId, const QVariantMap &msg) +{ + if (messageId == mFilter) { + emit messageReceived(msg); + } +} + +} +} 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 @@ +/* + 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 +#include + +namespace Kube { +/** + * The Fabric is a publish/subscribe message bus to interconnect components in kube. + * + * It provides a background mesh ("the fabric"), that interconnects various parts of kube. + * This is useful as it allows us to decouple functionality from the UI components, and keeps us from writing unnecessary API + * for visual components to pass through all necessary information for interaction. + */ +namespace Fabric { + +class Fabric : public QObject { + Q_OBJECT +public: + Q_INVOKABLE void postMessage(const QString &id, const QVariantMap &); +}; + +/** + * A message handler. + * + * Can beinstantiated from QML like so: + * Listener { + * function onMessageReceived(msg) { + * ...do something + * } + * } + */ +class Listener : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString filter MEMBER mFilter) + +public: + Listener(QObject *parent = 0); + virtual ~Listener(); + + virtual void notify(const QString &id, const QVariantMap ¬ification); + +signals: + void messageReceived(const QVariantMap &message); + +private: + QString mFilter; +}; + +} +} 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 @@ #include "actions/context.h" #include "actions/actionhandler.h" #include "actions/actionresult.h" +#include "fabric.h" #include +static QObject *example_qobject_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + return new Kube::Fabric::Fabric; +} + void FrameworkPlugin::registerTypes (const char *uri) { qmlRegisterType(uri, 1, 0, "FolderListModel"); @@ -69,4 +77,7 @@ void FrameworkPlugin::registerTypes (const char *uri) qmlRegisterType(uri, 1, 0, "Action"); qmlRegisterType(uri, 1, 0, "ActionHandler"); qmlRegisterType(uri, 1, 0, "ActionResult"); + + qmlRegisterType(uri, 1, 0, "Listener"); + qmlRegisterSingletonType(uri, 1, 0, "Fabric", example_qobject_singletontype_provider); } -- cgit v1.2.3