2 #include "notificationsinterface.h" 4 #include <QCoreApplication> 5 #include <QDBusConnection> 6 #include <QDBusPendingReply> 37 static std::map<uint, DBusNotification *> pendingNotifications;
39 OrgFreedesktopNotificationsInterface *DBusNotification::m_dbusInterface =
nullptr;
49 inline SwappedImage::SwappedImage(
const QImage &image)
50 : QImage(image.rgbSwapped())
81 argument.beginStructure();
82 argument << img.
width;
89 argument.endStructure();
95 argument.beginStructure();
96 argument >> img.
width;
102 argument >> img.
data;
103 argument.endStructure();
113 : isValid(imageData.canConvert<QDBusArgument>())
116 imageData.value<QDBusArgument>() >> *
this;
126 : width(image.width())
127 , height(image.height())
128 , rowstride(image.bytesPerLine())
129 , hasAlpha(image.hasAlphaChannel())
130 , channels(image.isGrayscale() ? 1 : hasAlpha ? 4 : 3)
131 , bitsPerSample(image.depth() / channels)
132 , data(reinterpret_cast<const char *>(image.bits()), image.byteCount())
133 , isValid(!image.isNull())
139 return isValid ? QImage(reinterpret_cast<const uchar *>(
data.constData()),
width,
height,
hasAlpha ? QImage::Format_ARGB32 : QImage::Format_RGB32)
147 return QVariant::fromValue(
isValid ? arg << *
this : arg);
187 void DBusNotification::initInterface()
189 if (!m_dbusInterface) {
190 m_dbusInterface =
new OrgFreedesktopNotificationsInterface(
191 QStringLiteral(
"org.freedesktop.Notifications"), QStringLiteral(
"/org/freedesktop/Notifications"), QDBusConnection::sessionBus());
193 connect(m_dbusInterface, &OrgFreedesktopNotificationsInterface::NotificationClosed, &DBusNotification::handleNotificationClosed);
202 auto i = pendingNotifications.find(m_id);
203 if (i != pendingNotifications.end()) {
204 pendingNotifications.erase(i);
215 return m_dbusInterface->isValid();
225 m_icon = QStringLiteral(
"dialog-information");
228 m_icon = QStringLiteral(
"dialog-warning");
231 m_icon = QStringLiteral(
"dialog-critical");
276 if (!m_dbusInterface->isValid()) {
282 m_watcher =
new QDBusPendingCallWatcher(
283 m_dbusInterface->Notify(QCoreApplication::applicationName(), m_id, m_icon, m_title, m_msg, m_actions, m_hints, m_timeout),
this);
284 connect(m_watcher, &QDBusPendingCallWatcher::finished,
this, &DBusNotification::handleNotifyResult);
318 if (!m_msg.startsWith(QStringLiteral(
"•"))) {
319 m_msg.insert(0, QStringLiteral(
"• "));
321 m_msg.append(QStringLiteral(
"\n• "));
335 m_dbusInterface->CloseNotification(m_id);
342 void DBusNotification::handleNotifyResult(QDBusPendingCallWatcher *watcher)
344 if (watcher != m_watcher) {
348 watcher->deleteLater();
351 QDBusPendingReply<uint> returnValue = *watcher;
352 if (returnValue.isError()) {
356 pendingNotifications[m_id = returnValue.argumentAt<0>()] =
this;
364 void DBusNotification::handleNotificationClosed(uint
id, uint reason)
366 auto i = pendingNotifications.find(
id);
367 if (i != pendingNotifications.end()) {
369 notification->m_id = 0;
371 pendingNotifications.erase(i);
378 void DBusNotification::handleActionInvoked(uint
id,
const QString &action)
380 auto i = pendingNotifications.find(
id);
381 if (i != pendingNotifications.end()) {
383 emit notification->actionInvoked(action);
388 notification->m_id = 0;
389 pendingNotifications.erase(i);
392 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.
const QImage image() const
Returns the image.
void hide()
Hides the notification (if still visible).
QVariant hint(const QString &name) const
Returns the hint with the specified name.
Q_DECLARE_METATYPE(MiscUtils::NotificationImage)
void shown()
Emitted when the notification could be shown successful.
static bool isAvailable()
Returns whether the notification D-Bus daemon is running.
QDBusArgument & operator<<(QDBusArgument &argument, const NotificationImage &img)
void setImage(const QImage &image)
Sets the image.
QVariant toDBusArgument() const
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.
const QDBusArgument & operator>>(const QDBusArgument &argument, NotificationImage &img)
~DBusNotification()
Closes the notification if still shown and delete the object.
The SwappedImage struct represents RGB-interved version of the image specified on construction...
void deleteOnCloseOrError()
Makes the notification object delete itself when the notification has been closed or an error occured...
The ImageData struct is a raw data image format.
const QString & icon() const
const QString & message() const