2 #include "notificationsinterface.h" 4 #include <QCoreApplication> 5 #include <QDBusConnection> 6 #include <QDBusPendingReply> 31 static std::map<uint, DBusNotification *> pendingNotifications;
33 OrgFreedesktopNotificationsInterface *DBusNotification::m_dbusInterface =
nullptr;
39 DBusNotification::DBusNotification(
const QString &title,
NotificationIcon icon,
int timeout, QObject *parent) :
67 void DBusNotification::initInterface()
69 if(!m_dbusInterface) {
70 m_dbusInterface =
new OrgFreedesktopNotificationsInterface(QStringLiteral(
"org.freedesktop.Notifications"), QStringLiteral(
"/org/freedesktop/Notifications"), QDBusConnection::sessionBus());
72 connect(m_dbusInterface, &OrgFreedesktopNotificationsInterface::NotificationClosed, &DBusNotification::handleNotificationClosed);
81 auto i = pendingNotifications.find(m_id);
82 if(i != pendingNotifications.end()) {
83 pendingNotifications.erase(i);
94 return m_dbusInterface->isValid();
104 m_icon = QStringLiteral(
"dialog-information");
break;
106 m_icon = QStringLiteral(
"dialog-warning");
break;
108 m_icon = QStringLiteral(
"dialog-critical");
break;
130 if(!m_dbusInterface->isValid()) {
136 m_watcher =
new QDBusPendingCallWatcher(m_dbusInterface->Notify(QCoreApplication::applicationName(), m_id, m_icon, m_title, m_msg, m_actions, m_hints, m_timeout),
this);
137 connect(m_watcher, &QDBusPendingCallWatcher::finished,
this, &DBusNotification::handleNotifyResult);
166 if(!m_msg.startsWith(QStringLiteral(
"•"))) {
167 m_msg.insert(0, QStringLiteral(
"• "));
169 m_msg.append(QStringLiteral(
"\n• "));
182 m_dbusInterface->CloseNotification(m_id);
189 void DBusNotification::handleNotifyResult(QDBusPendingCallWatcher *watcher)
191 if(watcher != m_watcher) {
195 watcher->deleteLater();
198 QDBusPendingReply<uint> returnValue = *watcher;
199 if (returnValue.isError()) {
203 pendingNotifications[m_id = returnValue.argumentAt<0>()] =
this;
211 void DBusNotification::handleNotificationClosed(uint
id, uint reason)
213 auto i = pendingNotifications.find(
id);
214 if(i != pendingNotifications.end()) {
216 notification->m_id = 0;
218 pendingNotifications.erase(i);
225 void DBusNotification::handleActionInvoked(uint
id,
const QString &action)
227 auto i = pendingNotifications.find(
id);
228 if(i != pendingNotifications.end()) {
234 notification->m_id = 0;
235 pendingNotifications.erase(i);
238 m_dbusInterface->CloseNotification(i->first);
void setIcon(const QString &icon)
Sets the icon name.
void closed(NotificationCloseReason reason)
Emitted when the notification has been closed.
const QString & title() const
bool update(const QString &line)
Updates the message and shows/updates the notification.
void hide()
Hides the notification (if still visible).
void shown()
Emitted when the notification could be shown successful.
static bool isAvailable()
Returns whether the notification D-Bus daemon is running.
bool isVisible() const
Returns whether the notification is (still) visible.
DBusNotification(const QString &title, NotificationIcon icon=NotificationIcon::Information, int timeout=10000, QObject *parent=nullptr)
Creates a new notification (which is not shown instantly).
bool show()
Shows the notification.
void error()
Emitted when the notification couldn't be shown.
void actionInvoked(const QString &action)
Emitted when action has been invoked.
~DBusNotification()
Closes the notification if still shown and delete the object.
The DBusNotification class emits D-Bus notifications.
void deleteOnCloseOrError()
Makes the notification object delete itself when the notification has been closed or an error occured...
const QString & icon() const
const QString & message() const