2 #include "notificationsinterface.h" 4 #include <QCoreApplication> 5 #include <QDBusConnection> 6 #include <QDBusPendingReply> 36 static std::map<uint, DBusNotification *> pendingNotifications;
38 OrgFreedesktopNotificationsInterface *DBusNotification::m_dbusInterface =
nullptr;
44 DBusNotification::DBusNotification(
const QString &title,
NotificationIcon icon,
int timeout, QObject *parent)
72 void DBusNotification::initInterface()
74 if (!m_dbusInterface) {
75 m_dbusInterface =
new OrgFreedesktopNotificationsInterface(
76 QStringLiteral(
"org.freedesktop.Notifications"), QStringLiteral(
"/org/freedesktop/Notifications"), QDBusConnection::sessionBus());
78 connect(m_dbusInterface, &OrgFreedesktopNotificationsInterface::NotificationClosed, &DBusNotification::handleNotificationClosed);
87 auto i = pendingNotifications.find(m_id);
88 if (i != pendingNotifications.end()) {
89 pendingNotifications.erase(i);
100 return m_dbusInterface->isValid();
110 m_icon = QStringLiteral(
"dialog-information");
113 m_icon = QStringLiteral(
"dialog-warning");
116 m_icon = QStringLiteral(
"dialog-critical");
141 if (!m_dbusInterface->isValid()) {
147 m_watcher =
new QDBusPendingCallWatcher(
148 m_dbusInterface->Notify(QCoreApplication::applicationName(), m_id, m_icon, m_title, m_msg, m_actions, m_hints, m_timeout),
this);
149 connect(m_watcher, &QDBusPendingCallWatcher::finished,
this, &DBusNotification::handleNotifyResult);
183 if (!m_msg.startsWith(QStringLiteral(
"•"))) {
184 m_msg.insert(0, QStringLiteral(
"• "));
186 m_msg.append(QStringLiteral(
"\n• "));
200 m_dbusInterface->CloseNotification(m_id);
207 void DBusNotification::handleNotifyResult(QDBusPendingCallWatcher *watcher)
209 if (watcher != m_watcher) {
213 watcher->deleteLater();
216 QDBusPendingReply<uint> returnValue = *watcher;
217 if (returnValue.isError()) {
221 pendingNotifications[m_id = returnValue.argumentAt<0>()] =
this;
229 void DBusNotification::handleNotificationClosed(uint
id, uint reason)
231 auto i = pendingNotifications.find(
id);
232 if (i != pendingNotifications.end()) {
234 notification->m_id = 0;
236 pendingNotifications.erase(i);
243 void DBusNotification::handleActionInvoked(uint
id,
const QString &action)
245 auto i = pendingNotifications.find(
id);
246 if (i != pendingNotifications.end()) {
248 emit notification->actionInvoked(action);
253 notification->m_id = 0;
254 pendingNotifications.erase(i);
257 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.
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.
~DBusNotification()
Closes the notification if still shown and delete the object.
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