1 #include "notificator.h"
6 #include <QApplication>
9 #include <QSystemTrayIcon>
10 #include <QMessageBox>
11 #include <QTemporaryFile>
12 #include <QImageWriter>
15 #include <QtDBus/QtDBus>
20 #include <ApplicationServices/ApplicationServices.h>
21 extern bool qt_mac_execute_apple_script(const QString &script, AEDesc *ret);
24 // https://wiki.ubuntu.com/NotificationDevelopmentGuidelines recommends at least 128
25 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
27 Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, QWidget *parent):
30 programName(programName),
37 if(trayicon && trayicon->supportsMessages())
42 interface = new QDBusInterface("org.freedesktop.Notifications",
43 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
44 if(interface->isValid())
50 // Check if Growl is installed (based on Qt's tray icon implementation)
52 OSStatus status = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, CFSTR("growlTicket"), kLSRolesAll, 0, &cfurl);
53 if (status != kLSApplicationNotFoundErr) {
54 CFBundleRef bundle = CFBundleCreate(0, cfurl);
56 if (CFStringCompare(CFBundleGetIdentifier(bundle), CFSTR("com.Growl.GrowlHelperApp"), kCFCompareCaseInsensitive | kCFCompareBackwards) == kCFCompareEqualTo) {
64 Notificator::~Notificator()
73 // Loosely based on http://www.qtcentre.org/archive/index.php/t-25879.html
74 class FreedesktopImage
78 FreedesktopImage(const QImage &img);
80 static int metaType();
82 // Image to variant that can be marshaled over DBus
83 static QVariant toVariant(const QImage &img);
86 int width, height, stride;
92 friend QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i);
93 friend const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i);
96 Q_DECLARE_METATYPE(FreedesktopImage);
98 // Image configuration settings
99 const int CHANNELS = 4;
100 const int BYTES_PER_PIXEL = 4;
101 const int BITS_PER_SAMPLE = 8;
103 FreedesktopImage::FreedesktopImage(const QImage &img):
105 height(img.height()),
106 stride(img.width() * BYTES_PER_PIXEL),
109 bitsPerSample(BITS_PER_SAMPLE)
111 // Convert 00xAARRGGBB to RGBA bytewise (endian-independent) format
112 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
113 const uint32_t *data = reinterpret_cast<const uint32_t*>(tmp.constBits());
115 unsigned int num_pixels = width * height;
116 image.resize(num_pixels * BYTES_PER_PIXEL);
118 for(unsigned int ptr = 0; ptr < num_pixels; ++ptr)
120 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16; // R
121 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8; // G
122 image[ptr*BYTES_PER_PIXEL+2] = data[ptr]; // B
123 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24; // A
127 QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i)
130 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
135 const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i)
138 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
143 int FreedesktopImage::metaType()
145 return qDBusRegisterMetaType<FreedesktopImage>();
148 QVariant FreedesktopImage::toVariant(const QImage &img)
150 FreedesktopImage fimg(img);
151 return QVariant(FreedesktopImage::metaType(), &fimg);
154 void Notificator::notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
157 // Arguments for DBus call:
158 QList<QVariant> args;
161 args.append(programName);
163 // Unique ID of this notification type:
166 // Application Icon, empty string
167 args.append(QString());
175 // Actions (none, actions are deprecated)
177 args.append(actions);
182 // If no icon specified, set icon based on class
186 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
189 case Information: sicon = QStyle::SP_MessageBoxInformation; break;
190 case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
191 case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
194 tmpicon = QApplication::style()->standardIcon(sicon);
200 hints["icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
204 args.append(millisTimeout);
207 interface->callWithArgumentList(QDBus::NoBlock, "Notify", args);
211 void Notificator::notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
214 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
215 switch(cls) // Set icon based on class
217 case Information: sicon = QSystemTrayIcon::Information; break;
218 case Warning: sicon = QSystemTrayIcon::Warning; break;
219 case Critical: sicon = QSystemTrayIcon::Critical; break;
221 trayIcon->showMessage(title, text, sicon, millisTimeout);
224 // Based on Qt's tray icon implementation
226 void Notificator::notifyGrowl(Class cls, const QString &title, const QString &text, const QIcon &icon)
228 const QString script(
229 "tell application \"GrowlHelperApp\"\n"
230 " set the allNotificationsList to {\"Notification\"}\n" // -- Make a list of all the notification types (all)
231 " set the enabledNotificationsList to {\"Notification\"}\n" // -- Make a list of the notifications (enabled)
232 " register as application \"%1\" all notifications allNotificationsList default notifications enabledNotificationsList\n" // -- Register our script with Growl
233 " notify with name \"Notification\" title \"%2\" description \"%3\" application name \"%1\"%4\n" // -- Send a Notification
237 QString notificationApp(QApplication::applicationName());
238 if (notificationApp.isEmpty())
239 notificationApp = "Application";
241 QPixmap notificationIconPixmap;
242 if (icon.isNull()) { // If no icon specified, set icon based on class
243 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
246 case Information: sicon = QStyle::SP_MessageBoxInformation; break;
247 case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
248 case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
250 notificationIconPixmap = QApplication::style()->standardPixmap(sicon);
253 QSize size = icon.actualSize(QSize(48, 48));
254 notificationIconPixmap = icon.pixmap(size);
257 QString notificationIcon;
258 QTemporaryFile notificationIconFile;
259 if (!notificationIconPixmap.isNull() && notificationIconFile.open()) {
260 QImageWriter writer(¬ificationIconFile, "PNG");
261 if (writer.write(notificationIconPixmap.toImage()))
262 notificationIcon = QString(" image from location \"file://%1\"").arg(notificationIconFile.fileName());
265 QString quotedTitle(title), quotedText(text);
266 quotedTitle.replace("\\", "\\\\").replace("\"", "\\");
267 quotedText.replace("\\", "\\\\").replace("\"", "\\");
268 qt_mac_execute_apple_script(script.arg(notificationApp, quotedTitle, quotedText, notificationIcon), 0);
272 void Notificator::notify(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
278 notifyDBus(cls, title, text, icon, millisTimeout);
282 notifySystray(cls, title, text, icon, millisTimeout);
286 notifyGrowl(cls, title, text, icon);
292 // Fall back to old fashioned popup dialog if critical and no other notification available
293 QMessageBox::critical(parent, title, text, QMessageBox::Ok, QMessageBox::Ok);