OS X: Send notification natively

For #2728
This commit is contained in:
Markus Goetz 2015-02-07 18:23:09 +01:00
parent b8e9dd587d
commit d4132072d8
4 changed files with 39 additions and 1 deletions

View file

@ -84,8 +84,8 @@ set(updater_SRCS
IF( APPLE )
list(APPEND client_SRCS cocoainitializer_mac.mm)
list(APPEND client_SRCS settingsdialogmac.cpp)
list(APPEND client_SRCS systray.mm)
if(SPARKLE_FOUND)
# Define this, we need to check in updater.cpp

View file

@ -14,6 +14,7 @@
#include "systray.h"
#include "theme.h"
#include <QDebug>
#ifdef USE_FDO_NOTIFICATIONS
#include <QDBusConnection>
@ -38,6 +39,11 @@ void Systray::showMessage(const QString & title, const QString & message, Messag
method.setArguments(args);
QDBusConnection::sessionBus().asyncCall(method);
} else
#endif
#ifdef Q_OS_OSX
if (canOsXSendUserNotification()) {
sendOsXUserNotification(title, message);
} else
#endif
{
QSystemTrayIcon::showMessage(title, message, icon, millisecondsTimeoutHint);

View file

@ -21,6 +21,11 @@ class QIcon;
namespace OCC {
#ifdef Q_OS_OSX
bool canOsXSendUserNotification();
void sendOsXUserNotification(const QString &title, const QString &message);
#endif
class Systray : public QSystemTrayIcon
{
Q_OBJECT

27
src/gui/systray.mm Normal file
View file

@ -0,0 +1,27 @@
#include <QString>
#include <QDebug>
#import <Cocoa/Cocoa.h>
namespace OCC {
bool canOsXSendUserNotification()
{
return NSClassFromString(@"NSUserNotificationCenter") != nil;
}
void sendOsXUserNotification(const QString &title, const QString &message)
{
qDebug() << Q_FUNC_INFO << title << message;
Class cuserNotificationCenter = NSClassFromString(@"NSUserNotificationCenter");
id userNotificationCenter = [cuserNotificationCenter defaultUserNotificationCenter];
Class cuserNotification = NSClassFromString(@"NSUserNotification");
id notification = [[cuserNotification alloc] init];
[notification setTitle:[NSString stringWithUTF8String:title.toUtf8().data()]];
[notification setInformativeText:[NSString stringWithUTF8String:message.toUtf8().data()]];
[userNotificationCenter deliverNotification:notification];
[notification release];
}
}