2013-08-20 12:55:03 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notificationsdbusinterface.h"
|
2014-09-21 23:45:06 +01:00
|
|
|
#include "notification_debug.h"
|
2017-04-13 20:31:46 +01:00
|
|
|
#include "notification.h"
|
2013-08-20 12:55:03 +01:00
|
|
|
|
|
|
|
#include <QDBusConnection>
|
2017-05-31 14:33:21 +01:00
|
|
|
|
2014-07-01 00:23:21 +01:00
|
|
|
#include <core/device.h>
|
|
|
|
#include <core/kdeconnectplugin.h>
|
|
|
|
|
2013-11-06 21:16:55 +00:00
|
|
|
#include "notificationsplugin.h"
|
2017-05-31 14:33:21 +01:00
|
|
|
#include "sendreplydialog.h"
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2014-06-14 19:35:00 +01:00
|
|
|
NotificationsDbusInterface::NotificationsDbusInterface(KdeConnectPlugin* plugin)
|
2014-07-01 00:23:21 +01:00
|
|
|
: QDBusAbstractAdaptor(const_cast<Device*>(plugin->device()))
|
2014-06-14 19:35:00 +01:00
|
|
|
, mDevice(plugin->device())
|
|
|
|
, mPlugin(plugin)
|
2013-08-20 12:55:03 +01:00
|
|
|
, mLastId(0)
|
|
|
|
{
|
2017-04-13 20:31:46 +01:00
|
|
|
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationsDbusInterface::~NotificationsDbusInterface()
|
2015-06-15 02:59:53 +01:00
|
|
|
{
|
2015-09-10 15:36:03 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Destroying NotificationsDbusInterface";
|
2015-06-15 02:59:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsDbusInterface::clearNotifications()
|
2013-08-20 12:55:03 +01:00
|
|
|
{
|
|
|
|
qDeleteAll(mNotifications);
|
2016-03-31 22:37:35 +01:00
|
|
|
mNotifications.clear();
|
2016-04-05 23:25:12 +01:00
|
|
|
Q_EMIT allNotificationsRemoved();
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList NotificationsDbusInterface::activeNotifications()
|
|
|
|
{
|
|
|
|
return mNotifications.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsDbusInterface::processPackage(const NetworkPackage& np)
|
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.get<bool>(QStringLiteral("isCancel"))) {
|
|
|
|
QString id = np.get<QString>(QStringLiteral("id"));
|
2015-09-10 15:36:03 +01:00
|
|
|
// cut off kdeconnect-android's prefix if there:
|
2016-11-26 14:38:08 +00:00
|
|
|
if (id.startsWith(QLatin1String("org.kde.kdeconnect_tp::")))
|
|
|
|
id = id.mid(id.indexOf(QLatin1String("::")) + 2);
|
2015-09-10 15:36:03 +01:00
|
|
|
removeNotification(id);
|
2016-11-26 14:38:08 +00:00
|
|
|
} else if (np.get<bool>(QStringLiteral("isRequest"))) {
|
2017-07-20 15:14:07 +01:00
|
|
|
for (const auto& n : qAsConst(mNotifications)) {
|
2016-06-21 19:07:12 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_NOTIFICATION_REQUEST, {
|
|
|
|
{"id", n->internalId()},
|
|
|
|
{"appName", n->appName()},
|
|
|
|
{"ticker", n->ticker()},
|
|
|
|
{"isClearable", n->dismissable()},
|
|
|
|
{"requestAnswer", true}
|
|
|
|
});
|
2015-09-10 15:36:03 +01:00
|
|
|
mPlugin->sendPackage(np);
|
|
|
|
}
|
2017-04-13 20:31:46 +01:00
|
|
|
} else if(np.get<bool>(QStringLiteral("requestAnswer"), false)) {
|
2013-08-22 03:39:04 +01:00
|
|
|
|
2017-04-13 20:31:46 +01:00
|
|
|
} else {
|
|
|
|
QString id = np.get<QString>(QStringLiteral("id"));
|
2013-09-20 14:54:30 +01:00
|
|
|
|
2017-04-13 20:31:46 +01:00
|
|
|
if (!mInternalIdToPublicId.contains(id)) {
|
|
|
|
Notification* noti = new Notification(np, this);
|
|
|
|
addNotification(noti);
|
|
|
|
} else {
|
|
|
|
QString pubId = mInternalIdToPublicId[id];
|
|
|
|
mNotifications[pubId]->update(np);
|
2013-08-22 03:39:04 +01:00
|
|
|
}
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsDbusInterface::addNotification(Notification* noti)
|
|
|
|
{
|
|
|
|
const QString& internalId = noti->internalId();
|
|
|
|
|
|
|
|
if (mInternalIdToPublicId.contains(internalId)) {
|
|
|
|
removeNotification(internalId);
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:33:58 +01:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "addNotification" << internalId;
|
2016-03-08 00:01:44 +00:00
|
|
|
|
2015-10-17 23:32:13 +01:00
|
|
|
connect(noti, &Notification::dismissRequested,
|
|
|
|
this, &NotificationsDbusInterface::dismissRequested);
|
2017-05-31 14:33:21 +01:00
|
|
|
|
|
|
|
connect(noti, &Notification::replyRequested, this, [this,noti]{
|
|
|
|
replyRequested(noti);
|
|
|
|
});
|
2013-08-20 12:55:03 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-05-25 21:33:58 +01:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "removeNotification" << internalId;
|
2013-08-20 12:55:03 +01:00
|
|
|
|
|
|
|
if (!mInternalIdToPublicId.contains(internalId)) {
|
2016-05-25 21:33:58 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Not found noti by internal Id: " << internalId;
|
2013-08-20 12:55:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-11 14:19:23 +01:00
|
|
|
QString publicId = mInternalIdToPublicId.take(internalId);
|
2013-08-20 12:55:03 +01:00
|
|
|
|
2013-10-11 14:19:23 +01:00
|
|
|
Notification* noti = mNotifications.take(publicId);
|
|
|
|
if (!noti) {
|
2016-05-25 21:33:58 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Not found noti by public Id: " << publicId;
|
2013-08-20 12:55:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-20 13:05:22 +01:00
|
|
|
//Deleting the notification will unregister it automatically
|
2013-08-20 12:55:03 +01:00
|
|
|
//QDBusConnection::sessionBus().unregisterObject(mDevice->dbusPath()+"/notifications/"+publicId);
|
|
|
|
noti->deleteLater();
|
|
|
|
|
|
|
|
Q_EMIT notificationRemoved(publicId);
|
|
|
|
}
|
|
|
|
|
2015-10-17 23:32:13 +01:00
|
|
|
void NotificationsDbusInterface::dismissRequested(const QString& internalId)
|
2013-08-20 12:55:03 +01:00
|
|
|
{
|
2016-05-31 16:16:01 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_NOTIFICATION_REQUEST);
|
2016-11-26 14:38:08 +00:00
|
|
|
np.set<QString>(QStringLiteral("cancel"), internalId);
|
2014-06-14 19:35:00 +01:00
|
|
|
mPlugin->sendPackage(np);
|
2013-08-20 12:55:03 +01:00
|
|
|
|
2014-06-15 20:39:23 +01:00
|
|
|
//Workaround: we erase notifications without waiting a repsonse from the
|
|
|
|
//phone because we won't receive a response if we are out of sync and this
|
|
|
|
//notification no longer exists. Ideally, each time we reach the phone
|
|
|
|
//after some time disconnected we should re-sync all the notifications.
|
|
|
|
removeNotification(internalId);
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 14:33:21 +01:00
|
|
|
void NotificationsDbusInterface::replyRequested(Notification* noti)
|
|
|
|
{
|
|
|
|
QString replyId = noti->replyId();
|
|
|
|
QString appName = noti->appName();
|
|
|
|
QString originalMessage = noti->ticker();
|
|
|
|
SendReplyDialog* dialog = new SendReplyDialog(originalMessage, replyId, appName);
|
|
|
|
connect(dialog, &SendReplyDialog::sendReply, this, &NotificationsDbusInterface::sendReply);
|
|
|
|
dialog->show();
|
2017-08-03 16:23:12 +01:00
|
|
|
dialog->raise();
|
2017-05-31 14:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsDbusInterface::sendReply(const QString& replyId, const QString& message)
|
|
|
|
{
|
|
|
|
NetworkPackage np(PACKAGE_TYPE_NOTIFICATION_REPLY);
|
|
|
|
np.set<QString>(QStringLiteral("requestReplyId"), replyId);
|
|
|
|
np.set<QString>(QStringLiteral("message"), message);
|
|
|
|
mPlugin->sendPackage(np);
|
|
|
|
}
|
|
|
|
|
2013-08-20 12:55:03 +01:00
|
|
|
QString NotificationsDbusInterface::newId()
|
|
|
|
{
|
|
|
|
return QString::number(++mLastId);
|
|
|
|
}
|