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/qml/ConversationView.qml | 16 +++++++ framework/qml/FolderListView.qml | 16 +++---- framework/qml/InlineAccountSwitcher.qml | 6 --- framework/qml/MailListView.qml | 32 +++++++------ framework/qml/Messages.qml | 31 ++++++++++++ framework/qmldir | 1 + framework/src/CMakeLists.txt | 1 + framework/src/fabric.cpp | 83 +++++++++++++++++++++++++++++++++ framework/src/fabric.h | 70 +++++++++++++++++++++++++++ framework/src/frameworkplugin.cpp | 11 +++++ 10 files changed, 240 insertions(+), 27 deletions(-) create mode 100644 framework/qml/Messages.qml create mode 100644 framework/src/fabric.cpp create mode 100644 framework/src/fabric.h (limited to 'framework') diff --git a/framework/qml/ConversationView.qml b/framework/qml/ConversationView.qml index bdcd0aa0..85712a5a 100644 --- a/framework/qml/ConversationView.qml +++ b/framework/qml/ConversationView.qml @@ -37,6 +37,22 @@ Rectangle { property bool hideTrash: true; property bool hideNonTrash: false; + + Kube.Listener { + filter: Kube.Messages.mailSelection + onMessageReceived: { + root.mail = message.mail + } + } + + Kube.Listener { + filter: Kube.Messages.folderSelection + onMessageReceived: { + root.hideTrash = !message.trash + root.hideNonTrash = message.trash + } + } + onCurrentIndexChanged: { markAsReadTimer.restart(); } diff --git a/framework/qml/FolderListView.qml b/framework/qml/FolderListView.qml index 4082e08d..78d3c5d0 100644 --- a/framework/qml/FolderListView.qml +++ b/framework/qml/FolderListView.qml @@ -26,18 +26,18 @@ import org.kube.framework 1.0 as Kube Rectangle { id: root - property variant currentFolder: null property variant accountId - property bool isTrashFolder: false color: Kube.Colors.textColor Kube.FolderController { id: folderController - Binding on folder { - //!! checks for the availability of the type - when: !!root.currentFolder - value: root.currentFolder + } + Kube.Listener { + id: controllerListener + filter: Kube.Messages.folderSelection + onMessageReceived: { + folderController.folder = message.folder } } @@ -64,8 +64,8 @@ Rectangle { onCurrentIndexChanged: { model.fetchMore(currentIndex) - root.currentFolder = model.data(currentIndex, Kube.FolderListModel.DomainObject) - root.isTrashFolder = model.data(currentIndex, Kube.FolderListModel.Trash) + Kube.Fabric.postMessage(Kube.Messages.folderSelection, {"folder":model.data(currentIndex, Kube.FolderListModel.DomainObject), + "trash":model.data(currentIndex, Kube.FolderListModel.Trash)}) folderController.synchronizeAction.execute() console.error(model.data) } diff --git a/framework/qml/InlineAccountSwitcher.qml b/framework/qml/InlineAccountSwitcher.qml index b7e70746..b5b4bb9f 100644 --- a/framework/qml/InlineAccountSwitcher.qml +++ b/framework/qml/InlineAccountSwitcher.qml @@ -24,8 +24,6 @@ Rectangle { id: root property string currentAccount: null - property var currentFolder: null - property bool isTrashFolder: false Kube.AccountsModel { id: accountsModel @@ -109,10 +107,6 @@ Rectangle { accountId: model.accountId visible: model.accountId == root.currentAccount - onCurrentFolderChanged: { - root.currentFolder = currentFolder - root.isTrashFolder = isTrashFolder - } } } } diff --git a/framework/qml/MailListView.qml b/framework/qml/MailListView.qml index 96e266bb..5adc3f98 100644 --- a/framework/qml/MailListView.qml +++ b/framework/qml/MailListView.qml @@ -25,26 +25,25 @@ import org.kube.framework 1.0 as Kube Item { id: root - property variant parentFolder - property variant currentMail: null + //InterfaceProperties + property string filterString + //Private properties + property variant parentFolder: null property bool isDraft : false property bool isImportant : false property bool isTrash : false property bool isUnread : false - property int currentIndex - property string filterString - onParentFolderChanged: { - currentMail = null + Kube.Listener { + filter: Kube.Messages.folderSelection + onMessageReceived: { + parentFolder = message.folder + Kube.Fabric.postMessage(Kube.Messages.mailSelection, {"mail":null}) + } } Kube.MailController { id: mailController - Binding on mail { - //!! checks for the availability of the type - when: !!root.currentMail - value: root.currentMail - } unread: root.isUnread trash: root.isTrash important: root.isImportant @@ -52,6 +51,14 @@ Item { operateOnThreads: mailListModel.isThreaded } + Kube.Listener { + id: controllerListener + filter: Kube.Messages.mailSelection + onMessageReceived: { + mailController.mail = message.mail + } + } + Shortcut { sequence: StandardKey.Delete onActivated: mailController.moveToTrashAction.execute() @@ -93,9 +100,8 @@ Item { } //END keyboard nav - currentIndex: root.currentIndex onCurrentItemChanged: { - root.currentMail = currentItem.currentData.domainObject; + Kube.Fabric.postMessage(Kube.Messages.mailSelection, {"mail":currentItem.currentData.mail}) root.isDraft = currentItem.currentData.draft; root.isTrash = currentItem.currentData.trash; root.isImportant = currentItem.currentData.important; diff --git a/framework/qml/Messages.qml b/framework/qml/Messages.qml new file mode 100644 index 00000000..3241af0b --- /dev/null +++ b/framework/qml/Messages.qml @@ -0,0 +1,31 @@ +/* + Copyright (C) 2017 Michael Bohlender, + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +pragma Singleton + +import QtQuick 2.7 + +Item { + //Selections + property string folderSelection: "currentFolder" + property string mailSelection: "currentMail" + + //Actions + property string moveToTrash: "moveToTrash" +} + diff --git a/framework/qmldir b/framework/qmldir index 35d1cb62..8e969c96 100644 --- a/framework/qmldir +++ b/framework/qmldir @@ -20,6 +20,7 @@ ComboBox 1.0 ComboBox.qml PositiveButton 1.0 PositiveButton.qml TextField 1.0 TextField.qml Label 1.0 Label.qml +singleton Messages 1.0 Messages.qml singleton Colors 1.0 Colors.qml singleton Icons 1.0 Icons.qml singleton Units 1.0 Units.qml 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