Changeset View
Changeset View
Standalone View
Standalone View
src/qt/macnotificationhandler.mm
// Copyright (c) 2011-2013 The Bitcoin Core developers | // Copyright (c) 2011-2013 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "macnotificationhandler.h" | #include "macnotificationhandler.h" | ||||
#undef slots | #undef slots | ||||
#import <objc/runtime.h> | |||||
#include <Cocoa/Cocoa.h> | #include <Cocoa/Cocoa.h> | ||||
#import <objc/runtime.h> | |||||
// Add an obj-c category (extension) to return the expected bundle identifier | // Add an obj-c category (extension) to return the expected bundle identifier | ||||
@implementation NSBundle(returnCorrectIdentifier) | @implementation NSBundle (returnCorrectIdentifier) | ||||
- (NSString *)__bundleIdentifier | - (NSString *)__bundleIdentifier { | ||||
{ | |||||
if (self == [NSBundle mainBundle]) { | if (self == [NSBundle mainBundle]) { | ||||
return @"org.bitcoinabc.BitcoinABC-Qt"; | return @"org.bitcoinabc.BitcoinABC-Qt"; | ||||
} else { | } else { | ||||
return [self __bundleIdentifier]; | return [self __bundleIdentifier]; | ||||
} | } | ||||
} | } | ||||
@end | @end | ||||
void MacNotificationHandler::showNotification(const QString &title, const QString &text) | void MacNotificationHandler::showNotification(const QString &title, | ||||
{ | const QString &text) { | ||||
// check if users OS has support for NSUserNotification | // check if users OS has support for NSUserNotification | ||||
if(this->hasUserNotificationCenterSupport()) { | if (this->hasUserNotificationCenterSupport()) { | ||||
// okay, seems like 10.8+ | // okay, seems like 10.8+ | ||||
QByteArray utf8 = title.toUtf8(); | QByteArray utf8 = title.toUtf8(); | ||||
char* cString = (char *)utf8.constData(); | char *cString = (char *)utf8.constData(); | ||||
NSString *titleMac = [[NSString alloc] initWithUTF8String:cString]; | NSString *titleMac = [[NSString alloc] initWithUTF8String:cString]; | ||||
utf8 = text.toUtf8(); | utf8 = text.toUtf8(); | ||||
cString = (char *)utf8.constData(); | cString = (char *)utf8.constData(); | ||||
NSString *textMac = [[NSString alloc] initWithUTF8String:cString]; | NSString *textMac = [[NSString alloc] initWithUTF8String:cString]; | ||||
// do everything weak linked (because we will keep <10.8 compatibility) | // do everything weak linked (because we will keep <10.8 compatibility) | ||||
id userNotification = [[NSClassFromString(@"NSUserNotification") alloc] init]; | id userNotification = | ||||
[userNotification performSelector:@selector(setTitle:) withObject:titleMac]; | [[NSClassFromString(@"NSUserNotification") alloc] init]; | ||||
[userNotification performSelector:@selector(setInformativeText:) withObject:textMac]; | [userNotification performSelector:@selector(setTitle:) | ||||
withObject:titleMac]; | |||||
id notificationCenterInstance = [NSClassFromString(@"NSUserNotificationCenter") performSelector:@selector(defaultUserNotificationCenter)]; | [userNotification performSelector:@selector(setInformativeText:) | ||||
[notificationCenterInstance performSelector:@selector(deliverNotification:) withObject:userNotification]; | withObject:textMac]; | ||||
id notificationCenterInstance = | |||||
[NSClassFromString(@"NSUserNotificationCenter") | |||||
performSelector:@selector(defaultUserNotificationCenter)]; | |||||
[notificationCenterInstance | |||||
performSelector:@selector(deliverNotification:) | |||||
withObject:userNotification]; | |||||
[titleMac release]; | [titleMac release]; | ||||
[textMac release]; | [textMac release]; | ||||
[userNotification release]; | [userNotification release]; | ||||
} | } | ||||
} | } | ||||
bool MacNotificationHandler::hasUserNotificationCenterSupport() | bool MacNotificationHandler::hasUserNotificationCenterSupport() { | ||||
{ | |||||
Class possibleClass = NSClassFromString(@"NSUserNotificationCenter"); | Class possibleClass = NSClassFromString(@"NSUserNotificationCenter"); | ||||
// check if users OS has support for NSUserNotification | // check if users OS has support for NSUserNotification | ||||
if(possibleClass!=nil) { | if (possibleClass != nil) { | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
MacNotificationHandler *MacNotificationHandler::instance() { | |||||
MacNotificationHandler *MacNotificationHandler::instance() | |||||
{ | |||||
static MacNotificationHandler *s_instance = nullptr; | static MacNotificationHandler *s_instance = nullptr; | ||||
if (!s_instance) { | if (!s_instance) { | ||||
s_instance = new MacNotificationHandler(); | s_instance = new MacNotificationHandler(); | ||||
Class aPossibleClass = objc_getClass("NSBundle"); | Class aPossibleClass = objc_getClass("NSBundle"); | ||||
if (aPossibleClass) { | if (aPossibleClass) { | ||||
// change NSBundle -bundleIdentifier method to return a correct bundle identifier | // change NSBundle -bundleIdentifier method to return a correct | ||||
// a bundle identifier is required to use OSXs User Notification Center | // bundle identifier a bundle identifier is required to use OSXs | ||||
method_exchangeImplementations(class_getInstanceMethod(aPossibleClass, @selector(bundleIdentifier)), | // User Notification Center | ||||
class_getInstanceMethod(aPossibleClass, @selector(__bundleIdentifier))); | method_exchangeImplementations( | ||||
class_getInstanceMethod(aPossibleClass, | |||||
@selector(bundleIdentifier)), | |||||
class_getInstanceMethod(aPossibleClass, | |||||
@selector(__bundleIdentifier))); | |||||
} | } | ||||
} | } | ||||
return s_instance; | return s_instance; | ||||
} | } |