Fixed compilation on mac + added possibility to display os x notification without bundle identifier (terminal programs)

This commit is contained in:
jendas 2015-07-27 14:46:41 +02:00
parent 7e22bf6c4b
commit 8f3b159f96
1 changed files with 46 additions and 7 deletions

View File

@ -4,12 +4,12 @@
#include "libsnore/utils.h" #include "libsnore/utils.h"
#include "libsnore/snore.h" #include "libsnore/snore.h"
#include "libsnore/log.h" #include "libsnore/log.h"
#include <QDebug.h>
#import <QThread.h> #import <QThread.h>
#import <QApplication.h> #import <QApplication.h>
#import <QMap> #import <QMap>
#include <Foundation/Foundation.h> #include <Foundation/Foundation.h>
#import <objc/runtime.h>
using namespace Snore; using namespace Snore;
QMap<int, Notification> m_IdToNotification; QMap<int, Notification> m_IdToNotification;
@ -20,6 +20,36 @@ void emitNotificationClicked(Notification notification) {
emit SnoreCore::instance().actionInvoked(notification); emit SnoreCore::instance().actionInvoked(notification);
} }
//
// Overcome need for bundle identifier to display notification
//
#pragma mark - Swizzle NSBundle
@implementation NSBundle(swizle)
// Overriding bundleIdentifier works, but overriding NSUserNotificationAlertStyle does not work.
- (NSString *)__bundleIdentifier
{
if (self == [NSBundle mainBundle] && ![[self __bundleIdentifier] length]) {
return @"com.apple.terminal";
} else {
return [self __bundleIdentifier];
}
}
@end
BOOL installNSBundleHook()
{
Class cls = objc_getClass("NSBundle");
if (cls) {
method_exchangeImplementations(class_getInstanceMethod(cls, @selector(bundleIdentifier)),
class_getInstanceMethod(cls, @selector(__bundleIdentifier)));
return YES;
}
return NO;
}
// //
// Enable reaction when user clicks on NSUserNotification // Enable reaction when user clicks on NSUserNotification
// //
@ -70,22 +100,31 @@ public:
}; };
// store some variables that are needed (since obj-c++ does not allow having obj-c classes as c++ members) // store some variables that are needed (since obj-c++ does not allow having obj-c classes as c++ members)
namespace { namespace {
NSString * NSStringFromQString(QString qstr) { NSString * NSStringFromQString(QString qstr) {
return [NSString stringWithUTF8String: qstr.toUtf8().constData()]; return qstr.toNSString();
}
QString QStringFromNSString(NSString * str) {
return QString::fromNSString(str);
} }
} }
UserNotificationItemClass * delegate; static UserNotificationItemClass * delegate = 0;
OSXNotificationCenter::OSXNotificationCenter() OSXNotificationCenter::OSXNotificationCenter()
{ {
m_IdToNSNotification = [[[NSMutableDictionary alloc] init] autorelease]; installNSBundleHook();
m_IdToNSNotification = [[NSMutableDictionary alloc] init];
if (not delegate) {
delegate = new UserNotificationItemClass(); delegate = new UserNotificationItemClass();
} }
}
OSXNotificationCenter::~OSXNotificationCenter() OSXNotificationCenter::~OSXNotificationCenter()
{ {
delete delegate; delete delegate;
@ -95,7 +134,7 @@ void OSXNotificationCenter::slotNotify(Snore::Notification notification)
{ {
NSUserNotification * osxNotification = [[[NSUserNotification alloc] init] autorelease]; NSUserNotification * osxNotification = [[[NSUserNotification alloc] init] autorelease];
NSString * notificationId = [NSString stringWithFormat:@"%d",notification.id()]; NSString * notificationId = [NSString stringWithFormat:@"%d",notification.id()];
osxNotification.title = NSStringFromQString(notification.title()); osxNotification.title = notification.title().toNSString();
osxNotification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:notificationId, @"id", nil]; osxNotification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:notificationId, @"id", nil];
osxNotification.informativeText = NSStringFromQString(notification.text()); osxNotification.informativeText = NSStringFromQString(notification.text());