2018-05-05 21:40:24 +02:00
|
|
|
#include "notifications/Manager.h"
|
|
|
|
|
2021-01-20 00:30:04 +01:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusMessage>
|
|
|
|
#include <QDBusMetaType>
|
|
|
|
#include <QDBusPendingCallWatcher>
|
|
|
|
#include <QDBusPendingReply>
|
2021-01-20 00:47:44 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QImage>
|
2021-02-14 00:59:17 +01:00
|
|
|
#include <QTextDocumentFragment>
|
2018-07-11 16:33:02 +02:00
|
|
|
|
2021-02-13 18:10:49 +01:00
|
|
|
#include "Cache.h"
|
|
|
|
#include "EventAccessors.h"
|
|
|
|
#include "MatrixClient.h"
|
|
|
|
#include "Utils.h"
|
2021-02-13 18:59:50 +01:00
|
|
|
#include <mtx/responses/notifications.hpp>
|
2021-02-13 19:48:37 +01:00
|
|
|
#include <cmark.h>
|
2021-02-13 18:10:49 +01:00
|
|
|
|
2018-07-14 15:27:51 +02:00
|
|
|
NotificationsManager::NotificationsManager(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, dbus("org.freedesktop.Notifications",
|
|
|
|
"/org/freedesktop/Notifications",
|
|
|
|
"org.freedesktop.Notifications",
|
|
|
|
QDBusConnection::sessionBus(),
|
|
|
|
this)
|
2018-07-11 16:33:02 +02:00
|
|
|
{
|
|
|
|
qDBusRegisterMetaType<QImage>();
|
|
|
|
|
2018-07-14 15:27:51 +02:00
|
|
|
QDBusConnection::sessionBus().connect("org.freedesktop.Notifications",
|
|
|
|
"/org/freedesktop/Notifications",
|
|
|
|
"org.freedesktop.Notifications",
|
|
|
|
"ActionInvoked",
|
|
|
|
this,
|
|
|
|
SLOT(actionInvoked(uint, QString)));
|
|
|
|
QDBusConnection::sessionBus().connect("org.freedesktop.Notifications",
|
|
|
|
"/org/freedesktop/Notifications",
|
|
|
|
"org.freedesktop.Notifications",
|
|
|
|
"NotificationClosed",
|
|
|
|
this,
|
|
|
|
SLOT(notificationClosed(uint, uint)));
|
2021-01-07 10:44:59 +01:00
|
|
|
QDBusConnection::sessionBus().connect("org.freedesktop.Notifications",
|
|
|
|
"/org/freedesktop/Notifications",
|
|
|
|
"org.freedesktop.Notifications",
|
|
|
|
"NotificationReplied",
|
|
|
|
this,
|
|
|
|
SLOT(notificationReplied(uint, QString)));
|
2018-07-11 16:33:02 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 00:35:15 +01:00
|
|
|
// SPDX-FileCopyrightText: 2012 Roland Hieber <rohieb@rohieb.name>
|
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2018-07-11 16:33:02 +02:00
|
|
|
void
|
2021-02-13 18:10:49 +01:00
|
|
|
NotificationsManager::postNotification(const mtx::responses::Notification ¬ification,
|
|
|
|
const QImage &icon)
|
2018-07-11 16:33:02 +02:00
|
|
|
{
|
2021-02-13 18:10:49 +01:00
|
|
|
const auto room_id = QString::fromStdString(notification.room_id);
|
|
|
|
const auto event_id = QString::fromStdString(mtx::accessors::event_id(notification.event));
|
|
|
|
const auto sender = cache::displayName(
|
|
|
|
room_id, QString::fromStdString(mtx::accessors::sender(notification.event)));
|
|
|
|
const auto text = utils::event_body(notification.event);
|
2021-02-14 00:59:17 +01:00
|
|
|
auto formattedText = utils::markdownToHtml(text);
|
|
|
|
|
2021-02-15 22:52:19 +01:00
|
|
|
auto capabilites = dbus.call("GetCapabilites");
|
2021-02-14 00:59:17 +01:00
|
|
|
if (!capabilites.arguments().contains("body-markup"))
|
|
|
|
formattedText = QTextDocumentFragment::fromHtml(formattedText).toPlainText();
|
2021-02-13 18:10:49 +01:00
|
|
|
|
2021-01-20 00:47:44 +01:00
|
|
|
QVariantMap hints;
|
2021-01-20 22:15:14 +01:00
|
|
|
hints["image-data"] = icon;
|
2021-01-20 00:47:44 +01:00
|
|
|
hints["sound-name"] = "message-new-instant";
|
|
|
|
QList<QVariant> argumentList;
|
2021-02-13 18:10:49 +01:00
|
|
|
argumentList << "nheko"; // app_name
|
|
|
|
argumentList << (uint)0; // replace_id
|
|
|
|
argumentList << ""; // app_icon
|
|
|
|
argumentList << QString::fromStdString(
|
|
|
|
cache::singleRoomInfo(notification.room_id).name); // summary
|
2021-02-12 17:28:41 +01:00
|
|
|
|
|
|
|
// body
|
2021-02-13 18:10:49 +01:00
|
|
|
if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
|
2021-02-13 19:48:37 +01:00
|
|
|
argumentList << "* " + sender + " " + formattedText;
|
2021-02-12 17:28:41 +01:00
|
|
|
else
|
2021-02-13 19:48:37 +01:00
|
|
|
argumentList << sender + ": " + formattedText;
|
2021-02-13 18:10:49 +01:00
|
|
|
|
2021-01-20 00:47:44 +01:00
|
|
|
// The list of actions has always the action name and then a localized version of that
|
|
|
|
// action. Currently we just use an empty string for that.
|
|
|
|
// TODO(Nico): Look into what to actually put there.
|
|
|
|
argumentList << (QStringList("default") << ""
|
|
|
|
<< "inline-reply"
|
|
|
|
<< ""); // actions
|
|
|
|
argumentList << hints; // hints
|
|
|
|
argumentList << (int)-1; // timeout in ms
|
2021-01-20 00:30:04 +01:00
|
|
|
|
2021-02-15 22:52:19 +01:00
|
|
|
QDBusPendingCall call = dbus.asyncCallWithArgumentList("Notify", argumentList);
|
2021-01-20 23:59:27 +01:00
|
|
|
auto watcher = new QDBusPendingCallWatcher{call, this};
|
2021-01-20 22:09:25 +01:00
|
|
|
connect(
|
2021-02-13 18:10:49 +01:00
|
|
|
watcher, &QDBusPendingCallWatcher::finished, this, [watcher, this, room_id, event_id]() {
|
2021-01-20 22:09:25 +01:00
|
|
|
if (watcher->reply().type() == QDBusMessage::ErrorMessage)
|
|
|
|
qDebug() << "D-Bus Error:" << watcher->reply().errorMessage();
|
|
|
|
else
|
|
|
|
notificationIds[watcher->reply().arguments().first().toUInt()] =
|
2021-02-13 18:10:49 +01:00
|
|
|
roomEventId{room_id, event_id};
|
2021-01-20 22:13:21 +01:00
|
|
|
watcher->deleteLater();
|
2021-01-20 22:09:25 +01:00
|
|
|
});
|
2018-07-11 16:33:02 +02:00
|
|
|
}
|
2021-01-20 00:30:04 +01:00
|
|
|
|
2020-04-09 20:52:50 +02:00
|
|
|
void
|
|
|
|
NotificationsManager::closeNotification(uint id)
|
|
|
|
{
|
2021-02-15 22:52:19 +01:00
|
|
|
auto call = dbus.asyncCall("CloseNotification", (uint)id); // replace_id
|
2021-01-20 23:59:27 +01:00
|
|
|
auto watcher = new QDBusPendingCallWatcher{call, this};
|
2021-02-23 12:42:57 +01:00
|
|
|
connect(watcher, &QDBusPendingCallWatcher::finished, this, [watcher]() {
|
2021-01-20 22:09:25 +01:00
|
|
|
if (watcher->reply().type() == QDBusMessage::ErrorMessage) {
|
|
|
|
qDebug() << "D-Bus Error:" << watcher->reply().errorMessage();
|
|
|
|
};
|
2021-01-20 22:13:21 +01:00
|
|
|
watcher->deleteLater();
|
2021-01-20 22:09:25 +01:00
|
|
|
});
|
2020-04-09 20:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NotificationsManager::removeNotification(const QString &roomId, const QString &eventId)
|
2020-04-13 16:22:30 +02:00
|
|
|
{
|
|
|
|
roomEventId reId = {roomId, eventId};
|
|
|
|
for (auto elem = notificationIds.begin(); elem != notificationIds.end(); ++elem) {
|
|
|
|
if (elem.value().roomId != roomId)
|
|
|
|
continue;
|
2020-04-09 20:52:50 +02:00
|
|
|
|
2020-04-13 16:22:30 +02:00
|
|
|
// close all notifications matching the eventId or having a lower
|
|
|
|
// notificationId
|
|
|
|
// This relies on the notificationId not wrapping around. This allows for
|
|
|
|
// approximately 2,147,483,647 notifications, so it is a bit unlikely.
|
|
|
|
// Otherwise we would need to store a 64bit counter instead.
|
|
|
|
closeNotification(elem.key());
|
2020-04-09 20:52:50 +02:00
|
|
|
|
2020-04-13 16:22:30 +02:00
|
|
|
// FIXME: compare index of event id of the read receipt and the notification instead
|
|
|
|
// of just the id to prevent read receipts of events without notification clearing
|
|
|
|
// all notifications in that room!
|
|
|
|
if (elem.value() == reId)
|
|
|
|
break;
|
2020-04-09 20:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 21:40:24 +02:00
|
|
|
void
|
2018-07-11 16:33:02 +02:00
|
|
|
NotificationsManager::actionInvoked(uint id, QString action)
|
2018-05-05 21:40:24 +02:00
|
|
|
{
|
2021-01-07 10:44:59 +01:00
|
|
|
if (notificationIds.contains(id)) {
|
|
|
|
roomEventId idEntry = notificationIds[id];
|
|
|
|
if (action == "default") {
|
|
|
|
emit notificationClicked(idEntry.roomId, idEntry.eventId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NotificationsManager::notificationReplied(uint id, QString reply)
|
|
|
|
{
|
|
|
|
if (notificationIds.contains(id)) {
|
2018-07-11 16:33:02 +02:00
|
|
|
roomEventId idEntry = notificationIds[id];
|
2021-01-07 10:44:59 +01:00
|
|
|
emit sendNotificationReply(idEntry.roomId, idEntry.eventId, reply);
|
2018-07-11 16:33:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NotificationsManager::notificationClosed(uint id, uint reason)
|
|
|
|
{
|
|
|
|
Q_UNUSED(reason);
|
2020-04-11 23:19:03 +02:00
|
|
|
notificationIds.remove(id);
|
2018-07-11 16:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatic marshaling of a QImage for org.freedesktop.Notifications.Notify
|
|
|
|
*
|
|
|
|
* This function is from the Clementine project (see
|
|
|
|
* http://www.clementine-player.org) and licensed under the GNU General Public
|
|
|
|
* License, version 3 or later.
|
|
|
|
*
|
2021-03-05 00:35:15 +01:00
|
|
|
* SPDX-FileCopyrightText: 2010 David Sansome <me@davidsansome.com>
|
2018-07-11 16:33:02 +02:00
|
|
|
*/
|
2018-07-14 15:27:51 +02:00
|
|
|
QDBusArgument &
|
|
|
|
operator<<(QDBusArgument &arg, const QImage &image)
|
|
|
|
{
|
|
|
|
if (image.isNull()) {
|
|
|
|
arg.beginStructure();
|
|
|
|
arg << 0 << 0 << 0 << false << 0 << 0 << QByteArray();
|
|
|
|
arg.endStructure();
|
|
|
|
return arg;
|
|
|
|
}
|
2018-07-11 16:33:02 +02:00
|
|
|
|
2018-07-14 15:27:51 +02:00
|
|
|
QImage scaled = image.scaledToHeight(100, Qt::SmoothTransformation);
|
|
|
|
scaled = scaled.convertToFormat(QImage::Format_ARGB32);
|
2018-07-11 16:33:02 +02:00
|
|
|
|
|
|
|
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
|
2018-07-14 15:27:51 +02:00
|
|
|
// ABGR -> ARGB
|
|
|
|
QImage i = scaled.rgbSwapped();
|
2018-07-11 16:33:02 +02:00
|
|
|
#else
|
2018-07-14 15:27:51 +02:00
|
|
|
// ABGR -> GBAR
|
|
|
|
QImage i(scaled.size(), scaled.format());
|
|
|
|
for (int y = 0; y < i.height(); ++y) {
|
|
|
|
QRgb *p = (QRgb *)scaled.scanLine(y);
|
|
|
|
QRgb *q = (QRgb *)i.scanLine(y);
|
|
|
|
QRgb *end = p + scaled.width();
|
|
|
|
while (p < end) {
|
|
|
|
*q = qRgba(qGreen(*p), qBlue(*p), qAlpha(*p), qRed(*p));
|
|
|
|
p++;
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 16:33:02 +02:00
|
|
|
#endif
|
|
|
|
|
2018-07-14 15:27:51 +02:00
|
|
|
arg.beginStructure();
|
|
|
|
arg << i.width();
|
|
|
|
arg << i.height();
|
|
|
|
arg << i.bytesPerLine();
|
|
|
|
arg << i.hasAlphaChannel();
|
|
|
|
int channels = i.isGrayscale() ? 1 : (i.hasAlphaChannel() ? 4 : 3);
|
|
|
|
arg << i.depth() / channels;
|
|
|
|
arg << channels;
|
2019-07-05 22:31:01 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
arg << QByteArray(reinterpret_cast<const char *>(i.bits()), i.byteCount());
|
|
|
|
#else
|
2019-07-05 03:20:19 +02:00
|
|
|
arg << QByteArray(reinterpret_cast<const char *>(i.bits()), i.sizeInBytes());
|
2019-07-05 22:31:01 +02:00
|
|
|
#endif
|
2018-07-14 15:27:51 +02:00
|
|
|
arg.endStructure();
|
|
|
|
return arg;
|
2018-07-11 16:33:02 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 15:27:51 +02:00
|
|
|
const QDBusArgument &
|
|
|
|
operator>>(const QDBusArgument &arg, QImage &)
|
|
|
|
{
|
|
|
|
// This is needed to link but shouldn't be called.
|
|
|
|
Q_ASSERT(0);
|
|
|
|
return arg;
|
2018-05-05 21:40:24 +02:00
|
|
|
}
|