+// Copyright (c) 2011-2013 The Bitcoin developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
#include "notificator.h"
-#include <QMetaType>
-#include <QVariant>
-#include <QIcon>
#include <QApplication>
-#include <QStyle>
#include <QByteArray>
-#include <QSystemTrayIcon>
+#include <QIcon>
+#include <QImageWriter>
#include <QMessageBox>
-
+#include <QMetaType>
+#include <QStyle>
+#include <QSystemTrayIcon>
+#include <QTemporaryFile>
+#include <QVariant>
#ifdef USE_DBUS
-#include <QtDBus/QtDBus>
#include <stdint.h>
+#include <QtDBus>
+#endif
+// Include ApplicationServices.h after QtDbus to avoid redefinition of check().
+// This affects at least OSX 10.6. See /usr/include/AssertMacros.h for details.
+// Note: This could also be worked around using:
+// #define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
+#ifdef Q_OS_MAC
+#include <ApplicationServices/ApplicationServices.h>
+#include "macnotificationhandler.h"
#endif
+
+#ifdef USE_DBUS
// https://wiki.ubuntu.com/NotificationDevelopmentGuidelines recommends at least 128
const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
+#endif
-Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, QWidget *parent):
+Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, QWidget *parent) :
QObject(parent),
parent(parent),
programName(programName),
}
#ifdef USE_DBUS
interface = new QDBusInterface("org.freedesktop.Notifications",
- "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
+ "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
if(interface->isValid())
{
mode = Freedesktop;
}
#endif
+#ifdef Q_OS_MAC
+ // check if users OS has support for NSUserNotification
+ if( MacNotificationHandler::instance()->hasUserNotificationCenterSupport()) {
+ mode = UserNotificationCenter;
+ }
+ else {
+ // Check if Growl is installed (based on Qt's tray icon implementation)
+ CFURLRef cfurl;
+ OSStatus status = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, CFSTR("growlTicket"), kLSRolesAll, 0, &cfurl);
+ if (status != kLSApplicationNotFoundErr) {
+ CFBundleRef bundle = CFBundleCreate(0, cfurl);
+ if (CFStringCompare(CFBundleGetIdentifier(bundle), CFSTR("com.Growl.GrowlHelperApp"), kCFCompareCaseInsensitive | kCFCompareBackwards) == kCFCompareEqualTo) {
+ if (CFStringHasSuffix(CFURLGetString(cfurl), CFSTR("/Growl.app/")))
+ mode = Growl13;
+ else
+ mode = Growl12;
+ }
+ CFRelease(cfurl);
+ CFRelease(bundle);
+ }
+ }
+#endif
}
Notificator::~Notificator()
static int metaType();
- // Image to variant that can be marshaled over DBus
+ // Image to variant that can be marshalled over DBus
static QVariant toVariant(const QImage &img);
private:
{
// Convert 00xAARRGGBB to RGBA bytewise (endian-independent) format
QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
- const uint32_t *data = reinterpret_cast<const uint32_t*>(tmp.constBits());
+ const uint32_t *data = reinterpret_cast<const uint32_t*>(tmp.bits());
unsigned int num_pixels = width * height;
image.resize(num_pixels * BYTES_PER_PIXEL);
trayIcon->showMessage(title, text, sicon, millisTimeout);
}
+// Based on Qt's tray icon implementation
+#ifdef Q_OS_MAC
+void Notificator::notifyGrowl(Class cls, const QString &title, const QString &text, const QIcon &icon)
+{
+ const QString script(
+ "tell application \"%5\"\n"
+ " set the allNotificationsList to {\"Notification\"}\n" // -- Make a list of all the notification types (all)
+ " set the enabledNotificationsList to {\"Notification\"}\n" // -- Make a list of the notifications (enabled)
+ " register as application \"%1\" all notifications allNotificationsList default notifications enabledNotificationsList\n" // -- Register our script with Growl
+ " notify with name \"Notification\" title \"%2\" description \"%3\" application name \"%1\"%4\n" // -- Send a Notification
+ "end tell"
+ );
+
+ QString notificationApp(QApplication::applicationName());
+ if (notificationApp.isEmpty())
+ notificationApp = "Application";
+
+ QPixmap notificationIconPixmap;
+ if (icon.isNull()) { // If no icon specified, set icon based on class
+ QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
+ switch (cls)
+ {
+ case Information: sicon = QStyle::SP_MessageBoxInformation; break;
+ case Warning: sicon = QStyle::SP_MessageBoxWarning; break;
+ case Critical: sicon = QStyle::SP_MessageBoxCritical; break;
+ }
+ notificationIconPixmap = QApplication::style()->standardPixmap(sicon);
+ }
+ else {
+ QSize size = icon.actualSize(QSize(48, 48));
+ notificationIconPixmap = icon.pixmap(size);
+ }
+
+ QString notificationIcon;
+ QTemporaryFile notificationIconFile;
+ if (!notificationIconPixmap.isNull() && notificationIconFile.open()) {
+ QImageWriter writer(¬ificationIconFile, "PNG");
+ if (writer.write(notificationIconPixmap.toImage()))
+ notificationIcon = QString(" image from location \"file://%1\"").arg(notificationIconFile.fileName());
+ }
+
+ QString quotedTitle(title), quotedText(text);
+ quotedTitle.replace("\\", "\\\\").replace("\"", "\\");
+ quotedText.replace("\\", "\\\\").replace("\"", "\\");
+ QString growlApp(this->mode == Notificator::Growl13 ? "Growl" : "GrowlHelperApp");
+ MacNotificationHandler::instance()->sendAppleScript(script.arg(notificationApp, quotedTitle, quotedText, notificationIcon, growlApp));
+}
+
+void Notificator::notifyMacUserNotificationCenter(Class cls, const QString &title, const QString &text, const QIcon &icon) {
+ // icon is not supported by the user notification center yet. OSX will use the app icon.
+ MacNotificationHandler::instance()->showNotification(title, text);
+}
+
+#endif
+
void Notificator::notify(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
{
switch(mode)
case QSystemTray:
notifySystray(cls, title, text, icon, millisTimeout);
break;
+#ifdef Q_OS_MAC
+ case UserNotificationCenter:
+ notifyMacUserNotificationCenter(cls, title, text, icon);
+ break;
+ case Growl12:
+ case Growl13:
+ notifyGrowl(cls, title, text, icon);
+ break;
+#endif
default:
if(cls == Critical)
{
- // Fall back to old fashioned popup dialog if critical and no other notification available
+ // Fall back to old fashioned pop-up dialog if critical and no other notification available
QMessageBox::critical(parent, title, text, QMessageBox::Ok, QMessageBox::Ok);
}
break;