1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "notificator.h"
7 #include <QApplication>
13 #include <QSystemTrayIcon>
14 #include <QMessageBox>
15 #include <QTemporaryFile>
16 #include <QImageWriter>
24 #include <ApplicationServices/ApplicationServices.h>
25 #include "macnotificationhandler.h"
28 // https://wiki.ubuntu.com/NotificationDevelopmentGuidelines recommends at least 128
29 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
31 Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, QWidget *parent):
34 programName(programName),
41 if(trayicon && trayicon->supportsMessages())
46 interface = new QDBusInterface("org.freedesktop.Notifications",
47 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
48 if(interface->isValid())
54 // check if users OS has support for NSUserNotification
55 if( MacNotificationHandler::instance()->hasUserNotificationCenterSupport()) {
56 mode = UserNotificationCenter;
59 // Check if Growl is installed (based on Qt's tray icon implementation)
61 OSStatus status = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, CFSTR("growlTicket"), kLSRolesAll, 0, &cfurl);
62 if (status != kLSApplicationNotFoundErr) {
63 CFBundleRef bundle = CFBundleCreate(0, cfurl);
64 if (CFStringCompare(CFBundleGetIdentifier(bundle), CFSTR("com.Growl.GrowlHelperApp"), kCFCompareCaseInsensitive | kCFCompareBackwards) == kCFCompareEqualTo) {
65 if (CFStringHasSuffix(CFURLGetString(cfurl), CFSTR("/Growl.app/")))
77 Notificator::~Notificator()
86 // Loosely based on http://www.qtcentre.org/archive/index.php/t-25879.html
87 class FreedesktopImage
91 FreedesktopImage(const QImage &img);
93 static int metaType();
95 // Image to variant that can be marshalled over DBus
96 static QVariant toVariant(const QImage &img);
99 int width, height, stride;
105 friend QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i);
106 friend const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i);
109 Q_DECLARE_METATYPE(FreedesktopImage);
111 // Image configuration settings
112 const int CHANNELS = 4;
113 const int BYTES_PER_PIXEL = 4;
114 const int BITS_PER_SAMPLE = 8;
116 FreedesktopImage::FreedesktopImage(const QImage &img):
118 height(img.height()),
119 stride(img.width() * BYTES_PER_PIXEL),
122 bitsPerSample(BITS_PER_SAMPLE)
124 // Convert 00xAARRGGBB to RGBA bytewise (endian-independent) format
125 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
126 const uint32_t *data = reinterpret_cast<const uint32_t*>(tmp.bits());
128 unsigned int num_pixels = width * height;
129 image.resize(num_pixels * BYTES_PER_PIXEL);
131 for(unsigned int ptr = 0; ptr < num_pixels; ++ptr)
133 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16; // R
134 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8; // G
135 image[ptr*BYTES_PER_PIXEL+2] = data[ptr]; // B
136 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24; // A
140 QDBusArgument &operator<<(QDBusArgument &a, const FreedesktopImage &i)
143 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
148 const QDBusArgument &operator>>(const QDBusArgument &a, FreedesktopImage &i)
151 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
156 int FreedesktopImage::metaType()
158 return qDBusRegisterMetaType<FreedesktopImage>();
161 QVariant FreedesktopImage::toVariant(const QImage &img)
163 FreedesktopImage fimg(img);
164 return QVariant(FreedesktopImage::metaType(), &fimg);
167 void Notificator::notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
170 // Arguments for DBus call:
171 QList<QVariant> args;
174 args.append(programName);
176 // Unique ID of this notification type:
179 // Application Icon, empty string
180 args.append(QString());
188 // Actions (none, actions are deprecated)
190 args.append(actions);
195 // If no icon specified, set icon based on class
199 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
202 case Information: sicon = QStyle::SP_MessageBoxInformation; break;
203 case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
204 case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
207 tmpicon = QApplication::style()->standardIcon(sicon);
213 hints["icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
217 args.append(millisTimeout);
220 interface->callWithArgumentList(QDBus::NoBlock, "Notify", args);
224 void Notificator::notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
227 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
228 switch(cls) // Set icon based on class
230 case Information: sicon = QSystemTrayIcon::Information; break;
231 case Warning: sicon = QSystemTrayIcon::Warning; break;
232 case Critical: sicon = QSystemTrayIcon::Critical; break;
234 trayIcon->showMessage(title, text, sicon, millisTimeout);
237 // Based on Qt's tray icon implementation
239 void Notificator::notifyGrowl(Class cls, const QString &title, const QString &text, const QIcon &icon)
241 const QString script(
242 "tell application \"%5\"\n"
243 " set the allNotificationsList to {\"Notification\"}\n" // -- Make a list of all the notification types (all)
244 " set the enabledNotificationsList to {\"Notification\"}\n" // -- Make a list of the notifications (enabled)
245 " register as application \"%1\" all notifications allNotificationsList default notifications enabledNotificationsList\n" // -- Register our script with Growl
246 " notify with name \"Notification\" title \"%2\" description \"%3\" application name \"%1\"%4\n" // -- Send a Notification
250 QString notificationApp(QApplication::applicationName());
251 if (notificationApp.isEmpty())
252 notificationApp = "Application";
254 QPixmap notificationIconPixmap;
255 if (icon.isNull()) { // If no icon specified, set icon based on class
256 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
259 case Information: sicon = QStyle::SP_MessageBoxInformation; break;
260 case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
261 case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
263 notificationIconPixmap = QApplication::style()->standardPixmap(sicon);
266 QSize size = icon.actualSize(QSize(48, 48));
267 notificationIconPixmap = icon.pixmap(size);
270 QString notificationIcon;
271 QTemporaryFile notificationIconFile;
272 if (!notificationIconPixmap.isNull() && notificationIconFile.open()) {
273 QImageWriter writer(¬ificationIconFile, "PNG");
274 if (writer.write(notificationIconPixmap.toImage()))
275 notificationIcon = QString(" image from location \"file://%1\"").arg(notificationIconFile.fileName());
278 QString quotedTitle(title), quotedText(text);
279 quotedTitle.replace("\\", "\\\\").replace("\"", "\\");
280 quotedText.replace("\\", "\\\\").replace("\"", "\\");
281 QString growlApp(this->mode == Notificator::Growl13 ? "Growl" : "GrowlHelperApp");
282 MacNotificationHandler::instance()->sendAppleScript(script.arg(notificationApp, quotedTitle, quotedText, notificationIcon, growlApp));
285 void Notificator::notifyMacUserNotificationCenter(Class cls, const QString &title, const QString &text, const QIcon &icon) {
286 // icon is not supported by the user notification center yet. OSX will use the app icon.
287 MacNotificationHandler::instance()->showNotification(title, text);
292 void Notificator::notify(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
298 notifyDBus(cls, title, text, icon, millisTimeout);
302 notifySystray(cls, title, text, icon, millisTimeout);
305 case UserNotificationCenter:
306 notifyMacUserNotificationCenter(cls, title, text, icon);
310 notifyGrowl(cls, title, text, icon);
316 // Fall back to old fashioned pop-up dialog if critical and no other notification available
317 QMessageBox::critical(parent, title, text, QMessageBox::Ok, QMessageBox::Ok);