Merge pull request #17 from jendas1/FixMac

Update osxnotificationbackend to handle apps without bundle id
This commit is contained in:
Patrick von Reth 2015-07-29 10:26:39 +02:00
commit d1aef594d8
2 changed files with 42 additions and 15 deletions

View File

@ -102,7 +102,7 @@ void SnoreCore::loadPlugins(SnorePlugin::PluginTypes types)
}
}
}
QMetaObject::invokeMethod(d, "slotInitPrimaryNotificationBackend", Qt::QueuedConnection);
QMetaObject::invokeMethod(d, "slotInitPrimaryNotificationBackend", Qt::DirectConnection); // Direct connection is used because the notification backend should be available after plugins are loaded
snoreDebug(SNORE_INFO) << "Loaded Plugins:" << d->m_pluginNames;
}

View File

@ -4,12 +4,12 @@
#include "libsnore/utils.h"
#include "libsnore/snore.h"
#include "libsnore/log.h"
#include <QDebug.h>
#import <QThread.h>
#import <QApplication.h>
#import <QMap>
#include <Foundation/Foundation.h>
#import <objc/runtime.h>
using namespace Snore;
QMap<int, Notification> m_IdToNotification;
@ -20,6 +20,36 @@ void emitNotificationClicked(Notification 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
//
@ -70,20 +100,17 @@ public:
};
// store some variables that are needed (since obj-c++ does not allow having obj-c classes as c++ members)
namespace {
NSString * NSStringFromQString(QString qstr) {
return [NSString stringWithUTF8String: qstr.toUtf8().constData()];
}
}
UserNotificationItemClass * delegate;
static UserNotificationItemClass * delegate = 0;
OSXNotificationCenter::OSXNotificationCenter()
{
m_IdToNSNotification = [[[NSMutableDictionary alloc] init] autorelease];
delegate = new UserNotificationItemClass();
installNSBundleHook();
m_IdToNSNotification = [[NSMutableDictionary alloc] init];
if (not delegate) {
delegate = new UserNotificationItemClass();
}
}
OSXNotificationCenter::~OSXNotificationCenter()
@ -95,9 +122,9 @@ void OSXNotificationCenter::slotNotify(Snore::Notification notification)
{
NSUserNotification * osxNotification = [[[NSUserNotification alloc] init] autorelease];
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.informativeText = NSStringFromQString(notification.text());
osxNotification.informativeText = notification.text().toNSString();
// Add notification to mapper from id to Nofification / NSUserNotification
m_IdToNotification.insert(notification.id(), notification);