From 64cfb8daa16a49cd5cd4b2d2344d85db31739c0e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 15 Mar 2017 18:42:51 +0100 Subject: Start of a notifications framework --- framework/CMakeLists.txt | 2 + framework/notifications/CMakeLists.txt | 12 ++++ framework/notifications/notificationhandler.cpp | 63 +++++++++++++++++++ framework/notifications/notificationhandler.h | 80 +++++++++++++++++++++++++ framework/notifications/notificationplugin.cpp | 30 ++++++++++ framework/notifications/notificationplugin.h | 31 ++++++++++ framework/notifications/qmldir | 3 + 7 files changed, 221 insertions(+) create mode 100644 framework/notifications/CMakeLists.txt create mode 100644 framework/notifications/notificationhandler.cpp create mode 100644 framework/notifications/notificationhandler.h create mode 100644 framework/notifications/notificationplugin.cpp create mode 100644 framework/notifications/notificationplugin.h create mode 100644 framework/notifications/qmldir (limited to 'framework') diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index aa2362f1..d14360b4 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -41,5 +41,7 @@ add_subdirectory(settings) add_subdirectory(domain) # The accounts framework add_subdirectory(accounts) +# The notifications framework +add_subdirectory(notifications) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/framework/notifications/CMakeLists.txt b/framework/notifications/CMakeLists.txt new file mode 100644 index 00000000..ec2f52c2 --- /dev/null +++ b/framework/notifications/CMakeLists.txt @@ -0,0 +1,12 @@ +set(SRCS + notificationplugin.cpp + notificationhandler.cpp +) + +add_library(notificationplugin SHARED ${SRCS}) + +target_link_libraries(notificationplugin sink) +qt5_use_modules(notificationplugin Core Quick Qml) + +install(TARGETS notificationplugin DESTINATION ${QML_INSTALL_DIR}/org/kube/framework/notifications) +install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kube/framework/notifications) diff --git a/framework/notifications/notificationhandler.cpp b/framework/notifications/notificationhandler.cpp new file mode 100644 index 00000000..23aa3da1 --- /dev/null +++ b/framework/notifications/notificationhandler.cpp @@ -0,0 +1,63 @@ +/* + 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 "notificationhandler.h" + +#include + +#include +#include +#include + +using namespace Kube; + +NotificationHandler::NotificationHandler(QObject *parent) + : QObject(parent) +{ + Sink::Query query{Sink::Query::LiveQuery}; + mNotifier.reset(new Sink::Notifier{query}); + mNotifier->registerHandler([this] (const Sink::Notification ¬ification) { + notify(notification); + }); + +} + +void NotificationHandler::notify(const Sink::Notification ¬ification) +{ + Notification n; + qWarning() << "Received notification: " << notification; + if (notification.type == Sink::Notification::Warning) { + n.mType = Notification::Warning; + n.mMessage = notification.message; + } else if (notification.type == Sink::Notification::Status) { + if (notification.code == Sink::ApplicationDomain::ErrorStatus) { + //A resource entered error status + n.mType = Notification::Warning; + n.mMessage = notification.message; + } + } else if (notification.type == Sink::Notification::Info) { + n.mType = Notification::Info; + n.mMessage = notification.message; + } else { + return; + } + //The base implementation to call the handler in QML + QMetaObject::invokeMethod(this, "handler", Q_ARG(QVariant, QVariant::fromValue(&n))); +} + diff --git a/framework/notifications/notificationhandler.h b/framework/notifications/notificationhandler.h new file mode 100644 index 00000000..fa992e87 --- /dev/null +++ b/framework/notifications/notificationhandler.h @@ -0,0 +1,80 @@ +/* + 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 +#include + +namespace Sink { + class Notification; +} + +namespace Kube { + +class Notification : public QObject +{ + Q_OBJECT + +public: + enum Type { + Info, + Warning + }; + Q_ENUMS(Type) + + Q_PROPERTY(Type type MEMBER mType CONSTANT) + Q_PROPERTY(QString message MEMBER mMessage CONSTANT) + + Notification() = default; + ~Notification() = default; + + Type mType; + QString mMessage; +}; + +/** + * A notification handler. + * + * Can beinstantiated from QML like so: + * NotificationHandler { + * function handler(notification) { + * ...do something + * } + * } + * + * The handler will listen for all notifications by default. + */ +class NotificationHandler : public QObject +{ + Q_OBJECT + +public: + NotificationHandler(QObject *parent = 0); + + virtual void notify(const Sink::Notification ¬ification); + +private: + QScopedPointer mNotifier; +}; + +} + +Q_DECLARE_METATYPE(Kube::Notification*); diff --git a/framework/notifications/notificationplugin.cpp b/framework/notifications/notificationplugin.cpp new file mode 100644 index 00000000..a7a9aa5b --- /dev/null +++ b/framework/notifications/notificationplugin.cpp @@ -0,0 +1,30 @@ +/* + 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 "notificationplugin.h" + +#include "notificationhandler.h" + +#include + +void NotificationPlugin::registerTypes (const char *uri) +{ + Q_ASSERT(uri == QLatin1String("org.kube.framework.notifications")); + qmlRegisterType(uri, 1, 0, "NotificationHandler"); + qmlRegisterType(uri, 1, 0, "Notification"); +} diff --git a/framework/notifications/notificationplugin.h b/framework/notifications/notificationplugin.h new file mode 100644 index 00000000..9aa0e033 --- /dev/null +++ b/framework/notifications/notificationplugin.h @@ -0,0 +1,31 @@ +/* + 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 + +class NotificationPlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") + +public: + virtual void registerTypes(const char *uri); +}; diff --git a/framework/notifications/qmldir b/framework/notifications/qmldir new file mode 100644 index 00000000..4eaad10c --- /dev/null +++ b/framework/notifications/qmldir @@ -0,0 +1,3 @@ +module org.kube.framework.notifications + +plugin notificationplugin -- cgit v1.2.3