1 #include "notificator.h"
6 #include <QApplication>
9 #include <QSystemTrayIcon>
10 #include <QMessageBox>
13 #include <QtDBus/QtDBus>
17 // https://wiki.ubuntu.com/NotificationDevelopmentGuidelines recommends at least 128
18 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
20 Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, QWidget *parent):
23 programName(programName),
30 if(trayicon && trayicon->supportsMessages())
35 interface = new QDBusInterface("org.freedesktop.Notifications",
36 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
37 if(interface->isValid())
44 Notificator::~Notificator()
53 // Loosely based on http://www.qtcentre.org/archive/index.php/t-25879.html
54 class FreedesktopImage
58 FreedesktopImage(const QImage &img);
60 static int metaType();
62 // Image to variant that can be marshaled over DBus
63 static QVariant toVariant(const QImage &img);
66 int width, height, stride;
72 friend QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i);
73 friend const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i);
76 Q_DECLARE_METATYPE(FreedesktopImage);
78 // Image configuration settings
79 const int CHANNELS = 4;
80 const int BYTES_PER_PIXEL = 4;
81 const int BITS_PER_SAMPLE = 8;
83 FreedesktopImage::FreedesktopImage(const QImage &img):
86 stride(img.width() * BYTES_PER_PIXEL),
89 bitsPerSample(BITS_PER_SAMPLE)
91 // Convert 00xAARRGGBB to RGBA bytewise (endian-independent) format
92 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
93 const uint32_t *data = reinterpret_cast<const uint32_t*>(tmp.constBits());
95 unsigned int num_pixels = width * height;
96 image.resize(num_pixels * BYTES_PER_PIXEL);
98 for(unsigned int ptr = 0; ptr < num_pixels; ++ptr)
100 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16; // R
101 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8; // G
102 image[ptr*BYTES_PER_PIXEL+2] = data[ptr]; // B
103 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24; // A
107 QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i)
110 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
115 const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i)
118 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
123 int FreedesktopImage::metaType()
125 return qDBusRegisterMetaType<FreedesktopImage>();
128 QVariant FreedesktopImage::toVariant(const QImage &img)
130 FreedesktopImage fimg(img);
131 return QVariant(FreedesktopImage::metaType(), &fimg);
134 void Notificator::notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
137 // Arguments for DBus call:
138 QList<QVariant> args;
141 args.append(programName);
143 // Unique ID of this notification type:
146 // Application Icon, empty string
147 args.append(QString());
155 // Actions (none, actions are deprecated)
157 args.append(actions);
162 // If no icon specified, set icon based on class
166 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
169 case Information: sicon = QStyle::SP_MessageBoxInformation; break;
170 case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
171 case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
174 tmpicon = QApplication::style()->standardIcon(sicon);
180 hints["icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
184 args.append(millisTimeout);
187 interface->callWithArgumentList(QDBus::NoBlock, "Notify", args);
191 void Notificator::notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
194 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
195 switch(cls) // Set icon based on class
197 case Information: sicon = QSystemTrayIcon::Information; break;
198 case Warning: sicon = QSystemTrayIcon::Warning; break;
199 case Critical: sicon = QSystemTrayIcon::Critical; break;
201 trayIcon->showMessage(title, text, sicon, millisTimeout);
204 void Notificator::notify(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
210 notifyDBus(cls, title, text, icon, millisTimeout);
214 notifySystray(cls, title, text, icon, millisTimeout);
219 // Fall back to old fashioned popup dialog if critical and no other notification available
220 QMessageBox::critical(parent, title, text, QMessageBox::Ok, QMessageBox::Ok);