Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / src / qt / macnotificationhandler.mm
blob1b16c5f5240cdfbf4d5fe0de15778677733d7900
1 // Copyright (c) 2011-2013 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "macnotificationhandler.h"
7 #undef slots
8 #import <objc/runtime.h>
9 #include <Cocoa/Cocoa.h>
11 // Add an obj-c category (extension) to return the expected bundle identifier
12 @implementation NSBundle(returnCorrectIdentifier)
13 - (NSString *)__bundleIdentifier
15     if (self == [NSBundle mainBundle]) {
16         return @"org.bitcoinfoundation.Bitcoin-Qt";
17     } else {
18         return [self __bundleIdentifier];
19     }
21 @end
23 void MacNotificationHandler::showNotification(const QString &title, const QString &text)
25     // check if users OS has support for NSUserNotification
26     if(this->hasUserNotificationCenterSupport()) {
27         // okay, seems like 10.8+
28         QByteArray utf8 = title.toUtf8();
29         char* cString = (char *)utf8.constData();
30         NSString *titleMac = [[NSString alloc] initWithUTF8String:cString];
32         utf8 = text.toUtf8();
33         cString = (char *)utf8.constData();
34         NSString *textMac = [[NSString alloc] initWithUTF8String:cString];
36         // do everything weak linked (because we will keep <10.8 compatibility)
37         id userNotification = [[NSClassFromString(@"NSUserNotification") alloc] init];
38         [userNotification performSelector:@selector(setTitle:) withObject:titleMac];
39         [userNotification performSelector:@selector(setInformativeText:) withObject:textMac];
41         id notificationCenterInstance = [NSClassFromString(@"NSUserNotificationCenter") performSelector:@selector(defaultUserNotificationCenter)];
42         [notificationCenterInstance performSelector:@selector(deliverNotification:) withObject:userNotification];
44         [titleMac release];
45         [textMac release];
46         [userNotification release];
47     }
50 bool MacNotificationHandler::hasUserNotificationCenterSupport(void)
52     Class possibleClass = NSClassFromString(@"NSUserNotificationCenter");
54     // check if users OS has support for NSUserNotification
55     if(possibleClass!=nil) {
56         return true;
57     }
58     return false;
62 MacNotificationHandler *MacNotificationHandler::instance()
64     static MacNotificationHandler *s_instance = nullptr;
65     if (!s_instance) {
66         s_instance = new MacNotificationHandler();
67         
68         Class aPossibleClass = objc_getClass("NSBundle");
69         if (aPossibleClass) {
70             // change NSBundle -bundleIdentifier method to return a correct bundle identifier
71             // a bundle identifier is required to use OSXs User Notification Center
72             method_exchangeImplementations(class_getInstanceMethod(aPossibleClass, @selector(bundleIdentifier)),
73                                            class_getInstanceMethod(aPossibleClass, @selector(__bundleIdentifier)));
74         }
75     }
76     return s_instance;