diff --git a/daemon/plugins/notifications/notification.cpp b/daemon/plugins/notifications/notification.cpp new file mode 100644 index 000000000..501fa042a --- /dev/null +++ b/daemon/plugins/notifications/notification.cpp @@ -0,0 +1,47 @@ +/* + * + * Copyright 2013 + * + * 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) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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, see . + * + */ + +#include "notification.h" + +#include +#include + +Notification::Notification(const NetworkPackage& np, QObject* parent) + : QObject(parent) +{ + mId = np.get("id"); + mAppName = np.get("appName"); + mTicker = np.get("ticker"); + mDismissable = np.get("isClearable"); +} + +Notification::~Notification() +{ + qDebug() << "Destroying notification" << mId; +} + +void Notification::dismiss() +{ + if (mDismissable) { + Q_EMIT dismissRequested(this); + } +} diff --git a/daemon/plugins/notifications/notification.h b/daemon/plugins/notifications/notification.h new file mode 100644 index 000000000..50898e741 --- /dev/null +++ b/daemon/plugins/notifications/notification.h @@ -0,0 +1,65 @@ +/* + * + * Copyright 2013 + * + * 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) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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, see . + * + */ + +#ifndef NOTIFICATION_H +#define NOTIFICATION_H + +#include +#include + +#include "../../networkpackage.h" + +class Notification + : public QObject +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.notifications.notification") + Q_PROPERTY(QString internalId READ internalId) + Q_PROPERTY(QString appName READ appName) + Q_PROPERTY(QString ticker READ ticker) + Q_PROPERTY(QString dismissable READ dismissable) + +public: + Notification(const NetworkPackage& np, QObject* parent); + virtual ~Notification(); + + QString internalId() { return mId; } + QString appName() { return mAppName; } + QString ticker() { return mTicker; } + bool dismissable() { return mDismissable; } + +public Q_SLOTS: + Q_SCRIPTABLE void dismiss(); + +Q_SIGNALS: + void dismissRequested(Notification* self); + +private: + QString mId; + QString mAppName; + QString mTicker; + //QIcon mIcon; + bool mDismissable; + +}; + +#endif diff --git a/daemon/plugins/notifications/notificationsdbusinterface.cpp b/daemon/plugins/notifications/notificationsdbusinterface.cpp new file mode 100644 index 000000000..e3a578d05 --- /dev/null +++ b/daemon/plugins/notifications/notificationsdbusinterface.cpp @@ -0,0 +1,118 @@ +/** + * Copyright 2013 Albert Vaca + * + * 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) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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, see . + */ + +#include "notificationsdbusinterface.h" + +#include +#include + +NotificationsDbusInterface::NotificationsDbusInterface(Device* device, QObject *parent) + : QDBusAbstractAdaptor(parent) + , mDevice(device) + , mLastId(0) +{ + +} + +NotificationsDbusInterface::~NotificationsDbusInterface() +{ + qDeleteAll(mNotifications); +} + +QStringList NotificationsDbusInterface::activeNotifications() +{ + return mNotifications.keys(); +} + +void NotificationsDbusInterface::processPackage(const NetworkPackage& np) +{ + if (np.get("isCancel")) { + removeNotification(np.get("id")); + } else { + Notification* noti = new Notification(np, this); + addNotification(noti); + } +} + +void NotificationsDbusInterface::addNotification(Notification* noti) +{ + const QString& internalId = noti->internalId(); + + if (mInternalIdToPublicId.contains(internalId)) { + removeNotification(internalId); + } + + connect(noti, SIGNAL(dismissRequested(Notification*)), + this, SLOT(dismissRequested(Notification*))); + + const QString& publicId = newId(); + mNotifications[publicId] = noti; + mInternalIdToPublicId[internalId] = publicId; + + QDBusConnection::sessionBus().registerObject(mDevice->dbusPath()+"/notifications/"+publicId, noti, QDBusConnection::ExportScriptableContents); + Q_EMIT notificationPosted(publicId); +} + +void NotificationsDbusInterface::removeNotification(const QString& internalId) +{ + qDebug() << "removeNotification" << internalId; + + if (!mInternalIdToPublicId.contains(internalId)) { + qDebug() << "Not found"; + return; + } + + QString publicId = mInternalIdToPublicId[internalId]; + mInternalIdToPublicId.remove(internalId); + + if (!mNotifications.contains(publicId)) { + qDebug() << "Not found"; + return; + } + + Notification* noti = mNotifications[publicId]; + mNotifications.remove(publicId); + + //Deleting the notification will unregister it automatically? + //QDBusConnection::sessionBus().unregisterObject(mDevice->dbusPath()+"/notifications/"+publicId); + noti->deleteLater(); + + Q_EMIT notificationRemoved(publicId); + +} + +void NotificationsDbusInterface::dismissRequested(Notification* notification) +{ + const QString& internalId = notification->internalId(); + + NetworkPackage np(PACKAGE_TYPE_NOTIFICATION); + np.set("cancel", internalId); + mDevice->sendPackage(np); + + //This should be called automatically back from server + //removeNotification(internalId); +} + +QString NotificationsDbusInterface::newId() +{ + return QString::number(++mLastId); +} + + diff --git a/daemon/plugins/notifications/notificationsdbusinterface.h b/daemon/plugins/notifications/notificationsdbusinterface.h new file mode 100644 index 000000000..cd722ab65 --- /dev/null +++ b/daemon/plugins/notifications/notificationsdbusinterface.h @@ -0,0 +1,65 @@ +/** + * Copyright 2013 Albert Vaca + * + * 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) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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, see . + */ + +#ifndef NOTIFICATIONSDBUSINTERFACE_H +#define NOTIFICATIONSDBUSINTERFACE_H + +#include +#include +#include +#include + +#include "../../device.h" +#include "notification.h" + +class NotificationsDbusInterface + : public QDBusAbstractAdaptor +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.notifications") + +public: + explicit NotificationsDbusInterface(Device* device, QObject *parent); + virtual ~NotificationsDbusInterface(); + + void processPackage(const NetworkPackage& np); + +public Q_SLOTS: + Q_SCRIPTABLE QStringList activeNotifications(); + void dismissRequested(Notification* notification); + +Q_SIGNALS: + void notificationPosted(const QString& publicId); + void notificationRemoved(const QString& publicId); + +private /*methods*/: + void addNotification(Notification* noti); + void removeNotification(const QString& internalId); + QString newId(); //Generates successive identifitiers to use as public ids + +private /*attributes*/: + Device* mDevice; + QHash mNotifications; + QHash mInternalIdToPublicId; + int mLastId; + +}; + +#endif