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)
message(STATUS "adding osx notification center backend")
#find_library(FOUNDATION Foundation)
set(OSXNOTIFICATIONCENTER_SRC
osxnotificationcenter.mm
#NotificationCenterDelegate.m
)
add_library(libsnore_backend_osxnotificationcenter MODULE ${OSXNOTIFICATIONCENTER_SRC})
#target_link_libraries(libsnore_backend_osxnotificationcenter snorecore ${FOUNDATION})
target_link_libraries(libsnore_backend_osxnotificationcenter snorecore /System/Library/Frameworks/Foundation.framework)
install(TARGETS libsnore_backend_osxnotificationcenter ${SNORE_PLUGIN_INSTALL_PATH})
message(STATUS "adding osx notification center backend")
#find_library(FOUNDATION Foundation)
set(OSXNOTIFICATIONCENTER_SRC
osxnotificationcenter.mm
#NotificationCenterDelegate.m
)
add_library(libsnore_backend_osxnotificationcenter MODULE ${OSXNOTIFICATIONCENTER_SRC})
#target_link_libraries(libsnore_backend_osxnotificationcenter snorecore ${FOUNDATION})
target_link_libraries(libsnore_backend_osxnotificationcenter snorecore /System/Library/Frameworks/Foundation.framework)
install(TARGETS libsnore_backend_osxnotificationcenter ${SNORE_PLUGIN_INSTALL_PATH})
endif(APPLE)

View File

@ -1,18 +1,45 @@
#include "osxnotificationcenter.h"
#include "core/plugins/snorebackend.h"
#import <Foundation/NSUserNotification.h>
//#import "NotificationCenterDelegate.h"
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <QThread.h>
#import <QApplication.h>
Q_EXPORT_PLUGIN2(libsnore_backend_osxnotificationcenter, OSXNotificationCenter)
using namespace Snore;
// store some variables that are needed (since obj-c++ does not allow having obj-c classes as c++ members)
namespace {
NSUserNotificationCenter *notification_center;
//NotificationCenterDelegate *notification_center_delegate;
NSUserNotificationCenter *notification_center;
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)
{
}
@ -23,21 +50,26 @@ OSXNotificationCenter::~OSXNotificationCenter()
bool OSXNotificationCenter::initialize(SnoreCore *snore)
{
notification_center = [NSUserNotificationCenter defaultUserNotificationCenter];
//notification_center_delegate = [[NotificationCenterDelegate alloc] init];
//[notification_center setDelegate:notification_center_delegate];
installNSBundleHook();
notification_center = [NSUserNotificationCenter defaultUserNotificationCenter];
return SnoreBackend::initialize(snore);
return SnoreBackend::initialize(snore);
}
void OSXNotificationCenter::slotNotify(Snore::Notification notification)
{
NSUserNotification *osx_notification = [[NSUserNotification alloc] init];
osx_notification.title = @"Test";
osx_notification.informativeText = @"Hello";
QString qAppID = QString("%1.%2").arg(qApp->organizationName(), qApp->applicationName()).remove(" ");
appID = NSStringFromQString(qAppID);
[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];
}