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);
55 if (CFStringCompare(CFBundleGetIdentifier(bundle), CFSTR("com.Growl.GrowlHelperApp"), kCFCompareCaseInsensitive | kCFCompareBackwards) == kCFCompareEqualTo) {
56 if (CFStringHasSuffix(CFURLGetString(cfurl), CFSTR("/Growl.app/")))
67 Notificator::~Notificator()
76 // Loosely based on http://www.qtcentre.org/archive/index.php/t-25879.html
77 class FreedesktopImage
81 FreedesktopImage(const QImage &img);
83 static int metaType();
85 // Image to variant that can be marshalled over DBus
86 static QVariant toVariant(const QImage &img);
89 int width, height, stride;
95 friend QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i);
96 friend const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i);
99 Q_DECLARE_METATYPE(FreedesktopImage);
101 // Image configuration settings
102 const int CHANNELS = 4;
103 const int BYTES_PER_PIXEL = 4;
104 const int BITS_PER_SAMPLE = 8;
106 FreedesktopImage::FreedesktopImage(const QImage &img):
108 height(img.height()),
109 stride(img.width() * BYTES_PER_PIXEL),
112 bitsPerSample(BITS_PER_SAMPLE)
114 // Convert 00xAARRGGBB to RGBA bytewise (endian-independent) format
115 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
116 const uint32_t *data = reinterpret_cast<const uint32_t*>(tmp.bits());
118 unsigned int num_pixels = width * height;
119 image.resize(num_pixels * BYTES_PER_PIXEL);
121 for(unsigned int ptr = 0; ptr < num_pixels; ++ptr)
123 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16; // R
124 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8; // G
125 image[ptr*BYTES_PER_PIXEL+2] = data[ptr]; // B
126 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24; // A
130 QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i)
133 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
138 const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i)
141 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
146 int FreedesktopImage::metaType()
148 return qDBusRegisterMetaType<FreedesktopImage>();
151 QVariant FreedesktopImage::toVariant(const QImage &img)
153 FreedesktopImage fimg(img);
154 return QVariant(FreedesktopImage::metaType(), &fimg);
157 void Notificator::notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
160 // Arguments for DBus call:
161 QList<QVariant> args;
164 args.append(programName);
166 // Unique ID of this notification type:
169 // Application Icon, empty string
170 args.append(QString());
178 // Actions (none, actions are deprecated)
180 args.append(actions);
185 // If no icon specified, set icon based on class
189 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
192 case Information: sicon = QStyle::SP_MessageBoxInformation; break;
193 case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
194 case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
197 tmpicon = QApplication::style()->standardIcon(sicon);
203 hints["icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
207 args.append(millisTimeout);
210 interface->callWithArgumentList(QDBus::NoBlock, "Notify", args);
214 void Notificator::notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
217 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
218 switch(cls) // Set icon based on class
220 case Information: sicon = QSystemTrayIcon::Information; break;
221 case Warning: sicon = QSystemTrayIcon::Warning; break;
222 case Critical: sicon = QSystemTrayIcon::Critical; break;
224 trayIcon->showMessage(title, text, sicon, millisTimeout);
227 // Based on Qt's tray icon implementation
229 void Notificator::notifyGrowl(Class cls, const QString &title, const QString &text, const QIcon &icon)
231 const QString script(
232 "tell application \"%5\"\n"
233 " set the allNotificationsList to {\"Notification\"}\n" // -- Make a list of all the notification types (all)
234 " set the enabledNotificationsList to {\"Notification\"}\n" // -- Make a list of the notifications (enabled)
235 " register as application \"%1\" all notifications allNotificationsList default notifications enabledNotificationsList\n" // -- Register our script with Growl
236 " notify with name \"Notification\" title \"%2\" description \"%3\" application name \"%1\"%4\n" // -- Send a Notification
240 QString notificationApp(QApplication::applicationName());
241 if (notificationApp.isEmpty())
242 notificationApp = "Application";
244 QPixmap notificationIconPixmap;
245 if (icon.isNull()) { // If no icon specified, set icon based on class
246 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
249 case Information: sicon = QStyle::SP_MessageBoxInformation; break;
250 case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
251 case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
253 notificationIconPixmap = QApplication::style()->standardPixmap(sicon);
256 QSize size = icon.actualSize(QSize(48, 48));
257 notificationIconPixmap = icon.pixmap(size);
260 QString notificationIcon;
261 QTemporaryFile notificationIconFile;
262 if (!notificationIconPixmap.isNull() && notificationIconFile.open()) {
263 QImageWriter writer(¬ificationIconFile, "PNG");
264 if (writer.write(notificationIconPixmap.toImage()))
265 notificationIcon = QString(" image from location \"file://%1\"").arg(notificationIconFile.fileName());
268 QString quotedTitle(title), quotedText(text);
269 quotedTitle.replace("\\", "\\\\").replace("\"", "\\");
270 quotedText.replace("\\", "\\\\").replace("\"", "\\");
271 QString growlApp(this->mode == Notificator::Growl13 ? "Growl" : "GrowlHelperApp");
272 qt_mac_execute_apple_script(script.arg(notificationApp, quotedTitle, quotedText, notificationIcon, growlApp), 0);
276 void Notificator::notify(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
282 notifyDBus(cls, title, text, icon, millisTimeout);
286 notifySystray(cls, title, text, icon, millisTimeout);
291 notifyGrowl(cls, title, text, icon);
297 // Fall back to old fashioned pop-up dialog if critical and no other notification available
298 QMessageBox::critical(parent, title, text, QMessageBox::Ok, QMessageBox::Ok);