kdeconnect-kde/plugins/notifications/notificationsdbusinterface.cpp

193 lines
6.4 KiB
C++
Raw Normal View History

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
2019-03-23 16:29:26 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2013-08-20 12:55:03 +01:00
*/
#include "notificationsdbusinterface.h"
#include "notification_debug.h"
#include "notification.h"
2013-08-20 12:55:03 +01:00
#include <core/device.h>
#include <core/kdeconnectplugin.h>
2019-06-09 16:28:49 +01:00
#include <dbushelper.h>
#include "notificationsplugin.h"
#include "sendreplydialog.h"
Build kdeconnect on sailfish and port some simple plugins Summary: Below is a lost of the commits, but, in summary Port the build system for Sailfish, which means selectively building only the bits we need/can, and only against the KF5 libs that are available. Allow to build on Qt 5.6 Switch from knotification to nemo notification (not complete!) Add a very simple example sailfish app. Note, there is still much missing functionality. Notifications dont work, pairing sort of works but not really, but when it is paired you can send a ping to the desktop client Dont build kio for Sailfish Port core build system Port daemon buld system Require CoreAddons on Sailfish Port plugins build for sailfish and include the ping plugin for now Final build changes for sailfish. Disable tests and other not needed parts Add includes for QCA Fix build errors on sailfish Get core/ to build on sailfish Get interfaces/ to build on sailfish Build daemon on sailfish On sailfish, dont install the kcm file Start port plugin to sailfish Fixup installed files Add sfos app Hack declarative plugin to give a public interface Build sfos app Compile declarativeplugin into the sfos app for now Redefine qAsConst for qt 5.6 Packaging fixes Use official icon Package .desktop Reviewers: #kde_connect, apol, nicolasfella, albertvaka Reviewed By: #kde_connect, apol, nicolasfella, albertvaka Subscribers: kdeconnect, andyholmes, albertvaka, kossebau, mtijink, vonreth, apol, #kde_connect, nicolasfella Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D10703
2018-08-02 20:10:59 +01:00
//In older Qt released, qAsConst isnt available
#include "qtcompat_p.h"
NotificationsDbusInterface::NotificationsDbusInterface(KdeConnectPlugin* plugin)
: QDBusAbstractAdaptor(const_cast<Device*>(plugin->device()))
, m_device(plugin->device())
, m_plugin(plugin)
, m_lastId(0)
2013-08-20 12:55:03 +01:00
{
2013-08-20 12:55:03 +01:00
}
NotificationsDbusInterface::~NotificationsDbusInterface()
{
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Destroying NotificationsDbusInterface";
}
void NotificationsDbusInterface::clearNotifications()
2013-08-20 12:55:03 +01:00
{
qDeleteAll(m_notifications);
m_notifications.clear();
Q_EMIT allNotificationsRemoved();
2013-08-20 12:55:03 +01:00
}
QStringList NotificationsDbusInterface::activeNotifications()
{
return m_notifications.keys();
2013-08-20 12:55:03 +01:00
}
void NotificationsDbusInterface::notificationReady()
{
Notification* noti = static_cast<Notification*>(sender());
disconnect(noti, &Notification::ready, this, &NotificationsDbusInterface::notificationReady);
addNotification(noti);
}
void NotificationsDbusInterface::processPacket(const NetworkPacket& np)
2013-08-20 12:55:03 +01:00
{
if (np.get<bool>(QStringLiteral("isCancel"))) {
QString id = np.get<QString>(QStringLiteral("id"));
// cut off kdeconnect-android's prefix if there:
if (id.startsWith(QLatin1String("org.kde.kdeconnect_tp::")))
id = id.mid(id.indexOf(QLatin1String("::")) + 2);
removeNotification(id);
return;
}
QString id = np.get<QString>(QStringLiteral("id"));
Notification* noti = nullptr;
if (!m_internalIdToPublicId.contains(id)) {
noti = new Notification(np, m_plugin->device(), this);
if (noti->isReady()) {
addNotification(noti);
} else {
connect(noti, &Notification::ready, this, &NotificationsDbusInterface::notificationReady);
}
} else {
QString pubId = m_internalIdToPublicId.value(id);
noti = m_notifications.value(pubId);
2013-08-20 12:55:03 +01:00
}
noti->update(np);
2013-08-20 12:55:03 +01:00
}
void NotificationsDbusInterface::addNotification(Notification* noti)
{
const QString& internalId = noti->internalId();
if (m_internalIdToPublicId.contains(internalId)) {
2013-08-20 12:55:03 +01:00
removeNotification(internalId);
}
2016-05-25 21:33:58 +01:00
//qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "addNotification" << internalId;
2016-03-08 00:01:44 +00:00
connect(noti, &Notification::dismissRequested,
this, &NotificationsDbusInterface::dismissRequested);
Build kdeconnect on sailfish and port some simple plugins Summary: Below is a lost of the commits, but, in summary Port the build system for Sailfish, which means selectively building only the bits we need/can, and only against the KF5 libs that are available. Allow to build on Qt 5.6 Switch from knotification to nemo notification (not complete!) Add a very simple example sailfish app. Note, there is still much missing functionality. Notifications dont work, pairing sort of works but not really, but when it is paired you can send a ping to the desktop client Dont build kio for Sailfish Port core build system Port daemon buld system Require CoreAddons on Sailfish Port plugins build for sailfish and include the ping plugin for now Final build changes for sailfish. Disable tests and other not needed parts Add includes for QCA Fix build errors on sailfish Get core/ to build on sailfish Get interfaces/ to build on sailfish Build daemon on sailfish On sailfish, dont install the kcm file Start port plugin to sailfish Fixup installed files Add sfos app Hack declarative plugin to give a public interface Build sfos app Compile declarativeplugin into the sfos app for now Redefine qAsConst for qt 5.6 Packaging fixes Use official icon Package .desktop Reviewers: #kde_connect, apol, nicolasfella, albertvaka Reviewed By: #kde_connect, apol, nicolasfella, albertvaka Subscribers: kdeconnect, andyholmes, albertvaka, kossebau, mtijink, vonreth, apol, #kde_connect, nicolasfella Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D10703
2018-08-02 20:10:59 +01:00
connect(noti, &Notification::replyRequested, this, [this,noti]{
replyRequested(noti);
});
2013-08-20 12:55:03 +01:00
connect(noti, &Notification::actionTriggered, this, &NotificationsDbusInterface::sendAction);
2013-08-20 12:55:03 +01:00
const QString& publicId = newId();
m_notifications[publicId] = noti;
m_internalIdToPublicId[internalId] = publicId;
2013-08-20 12:55:03 +01:00
DbusHelper::sessionBus().registerObject(m_device->dbusPath() + QStringLiteral("/notifications/") + publicId, noti, QDBusConnection::ExportScriptableContents);
2013-08-20 12:55:03 +01:00
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 (!m_internalIdToPublicId.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;
}
QString publicId = m_internalIdToPublicId.take(internalId);
2013-08-20 12:55:03 +01:00
Notification* noti = m_notifications.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;
}
//Deleting the notification will unregister it automatically
2019-06-09 16:28:49 +01:00
//DbusHelper::sessionBus().unregisterObject(mDevice->dbusPath()+"/notifications/"+publicId);
2013-08-20 12:55:03 +01:00
noti->deleteLater();
Q_EMIT notificationRemoved(publicId);
}
void NotificationsDbusInterface::dismissRequested(const QString& internalId)
2013-08-20 12:55:03 +01:00
{
NetworkPacket np(PACKET_TYPE_NOTIFICATION_REQUEST);
np.set<QString>(QStringLiteral("cancel"), internalId);
m_plugin->sendPacket(np);
2013-08-20 12:55:03 +01:00
2018-10-07 19:23:20 +01:00
//Workaround: we erase notifications without waiting a response 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
}
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();
dialog->raise();
}
void NotificationsDbusInterface::sendReply(const QString& replyId, const QString& message)
{
NetworkPacket np(PACKET_TYPE_NOTIFICATION_REPLY);
np.set<QString>(QStringLiteral("requestReplyId"), replyId);
np.set<QString>(QStringLiteral("message"), message);
m_plugin->sendPacket(np);
}
void NotificationsDbusInterface::sendAction(const QString& key, const QString& action)
{
NetworkPacket np(PACKET_TYPE_NOTIFICATION_ACTION);
np.set<QString>(QStringLiteral("key"), key);
np.set<QString>(QStringLiteral("action"), action);
m_plugin->sendPacket(np);
}
2013-08-20 12:55:03 +01:00
QString NotificationsDbusInterface::newId()
{
return QString::number(++m_lastId);
2013-08-20 12:55:03 +01:00
}