Cocoa: QSystemTrayIcon showing native messages on Mountain Lion.

This patch enables usage of the new NSUserNotificationCenter as part of
Mountain Lion. On earlier versions, or if compiled on earlier versions,
Growl will be used, if installed.

Change-Id: I676f9c63aa3c1ada19d36b6310ae90915be63011
Reviewed-by: Jake Petroules <jake.petroules@petroules.com>
Reviewed-by: Richard J. Moore <rich@kde.org>
bb10
Christoph Schleifenbaum 2013-02-13 17:21:48 +01:00 committed by The Qt Project
parent b26c9da892
commit 8de8800de1
2 changed files with 47 additions and 2 deletions

View File

@ -93,7 +93,11 @@ QT_USE_NAMESPACE
@class QT_MANGLE_NAMESPACE(QNSMenu);
@class QT_MANGLE_NAMESPACE(QNSImageView);
@interface QT_MANGLE_NAMESPACE(QNSStatusItem) : NSObject {
@interface QT_MANGLE_NAMESPACE(QNSStatusItem) : NSObject
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
<NSUserNotificationCenterDelegate>
#endif
{
@public
QCocoaSystemTrayIcon *systray;
NSStatusItem *item;
@ -108,6 +112,11 @@ QT_USE_NAMESPACE
-(QRectF)geometry;
- (void)triggerSelector:(id)sender button:(Qt::MouseButton)mouseButton;
- (void)doubleClickSelector:(id)sender;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification;
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification;
#endif
@end
@interface QT_MANGLE_NAMESPACE(QNSImageView) : NSImageView {
@ -132,9 +141,19 @@ class QSystemTrayIconSys
public:
QSystemTrayIconSys(QCocoaSystemTrayIcon *sys) {
item = [[QT_MANGLE_NAMESPACE(QNSStatusItem) alloc] initWithSysTray:sys];
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_8) {
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:item];
}
#endif
}
~QSystemTrayIconSys() {
[[[item item] view] setHidden: YES];
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_8) {
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:nil];
}
#endif
[item release];
}
QT_MANGLE_NAMESPACE(QNSStatusItem) *item;
@ -223,6 +242,18 @@ void QCocoaSystemTrayIcon::showMessage(const QString &title, const QString &mess
if (!m_sys)
return;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_8) {
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = [NSString stringWithUTF8String:title.toUtf8().data()];
notification.informativeText = [NSString stringWithUTF8String:message.toUtf8().data()];
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
return;
}
#endif
#ifdef QT_MAC_SYSTEMTRAY_USE_GROWL
// Make sure that we have Growl installed on the machine we are running on.
QCFType<CFURLRef> cfurl;
@ -439,6 +470,20 @@ QT_END_NAMESPACE
emit systray->activated(QPlatformSystemTrayIcon::DoubleClick);
}
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
Q_UNUSED(center);
Q_UNUSED(notification);
return YES;
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
Q_UNUSED(center);
Q_UNUSED(notification);
emit systray->messageClicked();
}
#endif
@end
class QSystemTrayIconQMenu : public QPlatformMenu

View File

@ -81,7 +81,7 @@ QT_BEGIN_NAMESPACE
tray specification, including recent versions of KDE and GNOME.
\li All supported versions of Mac OS X. Note that the Growl
notification system must be installed for
QSystemTrayIcon::showMessage() to display messages.
QSystemTrayIcon::showMessage() to display messages on Mac OS X prior to 10.8 (Mountain Lion).
\endlist
To check whether a system tray is present on the user's desktop,