2013-12-16 22:54:02 +01:00
|
|
|
// Copyright (c) 2011-2013 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-12-16 22:54:02 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2013-05-10 22:20:51 +02:00
|
|
|
#include "macnotificationhandler.h"
|
|
|
|
|
|
|
|
#undef slots
|
2014-11-18 10:55:55 +01:00
|
|
|
#import <objc/runtime.h>
|
2013-05-10 22:20:51 +02:00
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
|
2014-11-18 10:55:55 +01:00
|
|
|
// Add an obj-c category (extension) to return the expected bundle identifier
|
|
|
|
@implementation NSBundle(returnCorrectIdentifier)
|
|
|
|
- (NSString *)__bundleIdentifier
|
|
|
|
{
|
|
|
|
if (self == [NSBundle mainBundle]) {
|
|
|
|
return @"org.bitcoinfoundation.Bitcoin-Qt";
|
|
|
|
} else {
|
|
|
|
return [self __bundleIdentifier];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2013-05-10 22:20:51 +02:00
|
|
|
void MacNotificationHandler::showNotification(const QString &title, const QString &text)
|
|
|
|
{
|
|
|
|
// check if users OS has support for NSUserNotification
|
|
|
|
if(this->hasUserNotificationCenterSupport()) {
|
|
|
|
// okay, seems like 10.8+
|
|
|
|
QByteArray utf8 = title.toUtf8();
|
|
|
|
char* cString = (char *)utf8.constData();
|
|
|
|
NSString *titleMac = [[NSString alloc] initWithUTF8String:cString];
|
|
|
|
|
|
|
|
utf8 = text.toUtf8();
|
|
|
|
cString = (char *)utf8.constData();
|
|
|
|
NSString *textMac = [[NSString alloc] initWithUTF8String:cString];
|
|
|
|
|
|
|
|
// do everything weak linked (because we will keep <10.8 compatibility)
|
|
|
|
id userNotification = [[NSClassFromString(@"NSUserNotification") alloc] init];
|
|
|
|
[userNotification performSelector:@selector(setTitle:) withObject:titleMac];
|
|
|
|
[userNotification performSelector:@selector(setInformativeText:) withObject:textMac];
|
|
|
|
|
|
|
|
id notificationCenterInstance = [NSClassFromString(@"NSUserNotificationCenter") performSelector:@selector(defaultUserNotificationCenter)];
|
|
|
|
[notificationCenterInstance performSelector:@selector(deliverNotification:) withObject:userNotification];
|
|
|
|
|
|
|
|
[titleMac release];
|
|
|
|
[textMac release];
|
|
|
|
[userNotification release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendAppleScript just take a QString and executes it as apple script
|
|
|
|
void MacNotificationHandler::sendAppleScript(const QString &script)
|
|
|
|
{
|
|
|
|
QByteArray utf8 = script.toUtf8();
|
|
|
|
char* cString = (char *)utf8.constData();
|
|
|
|
NSString *scriptApple = [[NSString alloc] initWithUTF8String:cString];
|
|
|
|
|
|
|
|
NSAppleScript *as = [[NSAppleScript alloc] initWithSource:scriptApple];
|
|
|
|
NSDictionary *err = nil;
|
|
|
|
[as executeAndReturnError:&err];
|
|
|
|
[as release];
|
|
|
|
[scriptApple release];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MacNotificationHandler::hasUserNotificationCenterSupport(void)
|
|
|
|
{
|
|
|
|
Class possibleClass = NSClassFromString(@"NSUserNotificationCenter");
|
|
|
|
|
|
|
|
// check if users OS has support for NSUserNotification
|
|
|
|
if(possibleClass!=nil) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MacNotificationHandler *MacNotificationHandler::instance()
|
|
|
|
{
|
|
|
|
static MacNotificationHandler *s_instance = NULL;
|
2014-11-18 10:55:55 +01:00
|
|
|
if (!s_instance) {
|
2013-05-10 22:20:51 +02:00
|
|
|
s_instance = new MacNotificationHandler();
|
2014-11-18 10:55:55 +01:00
|
|
|
|
|
|
|
Class aPossibleClass = objc_getClass("NSBundle");
|
|
|
|
if (aPossibleClass) {
|
|
|
|
// change NSBundle -bundleIdentifier method to return a correct bundle identifier
|
|
|
|
// a bundle identifier is required to use OSXs User Notification Center
|
|
|
|
method_exchangeImplementations(class_getInstanceMethod(aPossibleClass, @selector(bundleIdentifier)),
|
|
|
|
class_getInstanceMethod(aPossibleClass, @selector(__bundleIdentifier)));
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 22:20:51 +02:00
|
|
|
return s_instance;
|
|
|
|
}
|