Keep DBUS from blocking

This commit is contained in:
Loren Burkholder 2021-01-19 18:30:04 -05:00
parent b51bf6748b
commit 70a4e1e265

View file

@ -2,9 +2,11 @@
#include <QDebug> #include <QDebug>
#include <QImage> #include <QImage>
#include <QtDBus/QDBusConnection> #include <QDBusConnection>
#include <QtDBus/QDBusMessage> #include <QDBusMessage>
#include <QtDBus/QDBusMetaType> #include <QDBusMetaType>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
NotificationsManager::NotificationsManager(QObject *parent) NotificationsManager::NotificationsManager(QObject *parent)
: QObject(parent) : QObject(parent)
@ -36,6 +38,12 @@ NotificationsManager::NotificationsManager(QObject *parent)
SLOT(notificationReplied(uint, QString))); SLOT(notificationReplied(uint, QString)));
} }
/**
* This function is based on code from
* https://github.com/rohieb/StratumsphereTrayIcon
* Copyright (C) 2012 Roland Hieber <rohieb@rohieb.name>
* Licensed under the GNU General Public License, version 3
*/
void void
NotificationsManager::postNotification(const QString &roomid, NotificationsManager::postNotification(const QString &roomid,
const QString &eventid, const QString &eventid,
@ -44,28 +52,16 @@ NotificationsManager::postNotification(const QString &roomid,
const QString &text, const QString &text,
const QImage &icon) const QImage &icon)
{ {
uint id = showNotification(roomname, sender + ": " + text, icon); Q_UNUSED(icon)
notificationIds[id] = roomEventId{roomid, eventid};
}
/**
* This function is based on code from
* https://github.com/rohieb/StratumsphereTrayIcon
* Copyright (C) 2012 Roland Hieber <rohieb@rohieb.name>
* Licensed under the GNU General Public License, version 3
*/
uint
NotificationsManager::showNotification(const QString summary,
const QString text,
const QImage image)
{
QVariantMap hints; QVariantMap hints;
hints["image-data"] = image; hints["image-data"] = sender + ": " + text;
hints["sound-name"] = "message-new-instant"; hints["sound-name"] = "message-new-instant";
QList<QVariant> argumentList; QList<QVariant> argumentList;
argumentList << "nheko"; // app_name argumentList << "nheko"; // app_name
argumentList << (uint)0; // replace_id argumentList << (uint)0; // replace_id
argumentList << ""; // app_icon argumentList << ""; // app_icon
argumentList << summary; // summary argumentList << roomname; // summary
argumentList << text; // body argumentList << text; // body
// The list of actions has always the action name and then a localized version of that // 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. // action. Currently we just use an empty string for that.
@ -79,15 +75,27 @@ NotificationsManager::showNotification(const QString summary,
static QDBusInterface notifyApp("org.freedesktop.Notifications", static QDBusInterface notifyApp("org.freedesktop.Notifications",
"/org/freedesktop/Notifications", "/org/freedesktop/Notifications",
"org.freedesktop.Notifications"); "org.freedesktop.Notifications");
QDBusMessage reply = auto call =
notifyApp.callWithArgumentList(QDBus::AutoDetect, "Notify", argumentList); notifyApp.callWithArgumentList(QDBus::AutoDetect, "Notify", argumentList);
if (reply.type() == QDBusMessage::ErrorMessage) { QDBusPendingCallWatcher watcher{QDBusPendingReply{call}};
qDebug() << "D-Bus Error:" << reply.errorMessage(); connect(&watcher, &QDBusPendingCallWatcher::finished, this, [&watcher, this, &roomid, &eventid]() {
if (watcher.reply().type() == QDBusMessage::ErrorMessage)
qDebug() << "D-Bus Error:" << watcher.reply().errorMessage();
else
notificationIds[watcher.reply().arguments().first().toUInt()] = roomEventId{roomid, eventid};
});
}
uint
NotificationsManager::showNotification(const QString summary,
const QString text,
const QImage image)
{
Q_UNUSED(summary)
Q_UNUSED(text)
Q_UNUSED(image)
return 0; return 0;
} else {
return reply.arguments().first().toUInt();
}
return true;
} }
void void