osxnotificationcenter seems to work now (displays notifications), but no icon set

This commit is contained in:
kevin.urban 2014-02-26 22:59:10 +01:00 committed by Patrick von Reth
parent 8ed4053554
commit b69a24c05e
2 changed files with 55 additions and 23 deletions

View File

@ -1,12 +1,12 @@
if(APPLE) if(APPLE)
message(STATUS "adding osx notification center backend") message(STATUS "adding osx notification center backend")
#find_library(FOUNDATION Foundation) #find_library(FOUNDATION Foundation)
set(OSXNOTIFICATIONCENTER_SRC set(OSXNOTIFICATIONCENTER_SRC
osxnotificationcenter.mm osxnotificationcenter.mm
#NotificationCenterDelegate.m #NotificationCenterDelegate.m
) )
add_library(libsnore_backend_osxnotificationcenter MODULE ${OSXNOTIFICATIONCENTER_SRC}) add_library(libsnore_backend_osxnotificationcenter MODULE ${OSXNOTIFICATIONCENTER_SRC})
#target_link_libraries(libsnore_backend_osxnotificationcenter snorecore ${FOUNDATION}) #target_link_libraries(libsnore_backend_osxnotificationcenter snorecore ${FOUNDATION})
target_link_libraries(libsnore_backend_osxnotificationcenter snorecore /System/Library/Frameworks/Foundation.framework) target_link_libraries(libsnore_backend_osxnotificationcenter snorecore /System/Library/Frameworks/Foundation.framework)
install(TARGETS libsnore_backend_osxnotificationcenter ${SNORE_PLUGIN_INSTALL_PATH}) install(TARGETS libsnore_backend_osxnotificationcenter ${SNORE_PLUGIN_INSTALL_PATH})
endif(APPLE) endif(APPLE)

View File

@ -1,18 +1,45 @@
#include "osxnotificationcenter.h" #include "osxnotificationcenter.h"
#include "core/plugins/snorebackend.h" #include "core/plugins/snorebackend.h"
#import <Foundation/NSUserNotification.h> #import <Foundation/Foundation.h>
//#import "NotificationCenterDelegate.h" #import <objc/runtime.h>
#import <QThread.h>
#import <QApplication.h>
Q_EXPORT_PLUGIN2(libsnore_backend_osxnotificationcenter, OSXNotificationCenter) Q_EXPORT_PLUGIN2(libsnore_backend_osxnotificationcenter, OSXNotificationCenter)
using namespace Snore; using namespace Snore;
// store some variables that are needed (since obj-c++ does not allow having obj-c classes as c++ members)
namespace { namespace {
NSUserNotificationCenter *notification_center; NSUserNotificationCenter *notification_center;
//NotificationCenterDelegate *notification_center_delegate; NSString *appID = @"dummyID.snorenotify";
// make sure __bundleIdentifier is called on every NSBundle object instead of bundleIdentifier
bool installNSBundleHook() {
Class cls = objc_getClass("NSBundle");
if (cls) {
method_exchangeImplementations(class_getInstanceMethod(cls, @selector(bundleIdentifier)),
class_getInstanceMethod(cls, @selector(__bundleIdentifier)));
return true;
}
return false;
}
NSString *NSStringFromQString(QString qstr) {
return [NSString stringWithUTF8String: qstr.toUtf8().data()];
}
} }
// this category adds the __bundleIdentifier method to every NSBundle object
@implementation NSBundle(snore)
- (NSString *)__bundleIdentifier {
return appID;
}
@end
OSXNotificationCenter::OSXNotificationCenter() : SnoreBackend("OSX Notification Center", false, false, false) OSXNotificationCenter::OSXNotificationCenter() : SnoreBackend("OSX Notification Center", false, false, false)
{ {
} }
@ -23,21 +50,26 @@ OSXNotificationCenter::~OSXNotificationCenter()
bool OSXNotificationCenter::initialize(SnoreCore *snore) bool OSXNotificationCenter::initialize(SnoreCore *snore)
{ {
notification_center = [NSUserNotificationCenter defaultUserNotificationCenter]; installNSBundleHook();
//notification_center_delegate = [[NotificationCenterDelegate alloc] init]; notification_center = [NSUserNotificationCenter defaultUserNotificationCenter];
//[notification_center setDelegate:notification_center_delegate];
return SnoreBackend::initialize(snore); return SnoreBackend::initialize(snore);
} }
void OSXNotificationCenter::slotNotify(Snore::Notification notification) void OSXNotificationCenter::slotNotify(Snore::Notification notification)
{ {
NSUserNotification *osx_notification = [[NSUserNotification alloc] init]; QString qAppID = QString("%1.%2").arg(qApp->organizationName(), qApp->applicationName()).remove(" ");
osx_notification.title = @"Test"; appID = NSStringFromQString(qAppID);
osx_notification.informativeText = @"Hello";
[notification_center deliverNotification: osx_notification]; snoreDebug(SNORE_DEBUG) << "bundle identifier for notification:" << [[[NSBundle mainBundle] bundleIdentifier] UTF8String];
[osx_notification release];
NSUserNotification *osx_notification = [[NSUserNotification alloc] init];
osx_notification.title = NSStringFromQString(notification.title());
osx_notification.informativeText = NSStringFromQString(notification.text());
[notification_center deliverNotification: osx_notification];
[osx_notification release];
} }