Fabcoin Core  0.16.2
P2P Digital Currency
macnotificationhandler.mm
Go to the documentation of this file.
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.
4 
6 
7 #undef slots
8 #import <objc/runtime.h>
9 #include <Cocoa/Cocoa.h>
10 
11 // Add an obj-c category (extension) to return the expected bundle identifier
13 - (NSString *)__bundleIdentifier
14 {
15  if (self == [NSBundle mainBundle]) {
16  return @"org.fabcoinfoundation.Fabcoin-Qt";
17  } else {
18  return [self __bundleIdentifier];
19  }
20 }
21 @end
22 
23 void MacNotificationHandler::showNotification(const QString &title, const QString &text)
24 {
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];
31 
32  utf8 = text.toUtf8();
33  cString = (char *)utf8.constData();
34  NSString *textMac = [[NSString alloc] initWithUTF8String:cString];
35 
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];
40 
41  id notificationCenterInstance = [NSClassFromString(@"NSUserNotificationCenter") performSelector:@selector(defaultUserNotificationCenter)];
42  [notificationCenterInstance performSelector:@selector(deliverNotification:) withObject:userNotification];
43 
44  [titleMac release];
45  [textMac release];
46  [userNotification release];
47  }
48 }
49 
50 // sendAppleScript just take a QString and executes it as apple script
51 void MacNotificationHandler::sendAppleScript(const QString &script)
52 {
53  QByteArray utf8 = script.toUtf8();
54  char* cString = (char *)utf8.constData();
55  NSString *scriptApple = [[NSString alloc] initWithUTF8String:cString];
56 
57  NSAppleScript *as = [[NSAppleScript alloc] initWithSource:scriptApple];
58  NSDictionary *err = nil;
59  [as executeAndReturnError:&err];
60  [as release];
61  [scriptApple release];
62 }
63 
65 {
66  Class possibleClass = NSClassFromString(@"NSUserNotificationCenter");
67 
68  // check if users OS has support for NSUserNotification
69  if(possibleClass!=nil) {
70  return true;
71  }
72  return false;
73 }
74 
75 
77 {
78  static MacNotificationHandler *s_instance = NULL;
79  if (!s_instance) {
80  s_instance = new MacNotificationHandler();
81 
82  Class aPossibleClass = objc_getClass("NSBundle");
83  if (aPossibleClass) {
84  // change NSBundle -bundleIdentifier method to return a correct bundle identifier
85  // a bundle identifier is required to use OSXs User Notification Center
86  method_exchangeImplementations(class_getInstanceMethod(aPossibleClass, @selector(bundleIdentifier)),
87  class_getInstanceMethod(aPossibleClass, @selector(__bundleIdentifier)));
88  }
89  }
90  return s_instance;
91 }
bool hasUserNotificationCenterSupport(void)
check if OS can handle UserNotifications
static MacNotificationHandler * instance()
void sendAppleScript(const QString &script)
executes AppleScript
void showNotification(const QString &title, const QString &text)
shows a 10.8+ UserNotification in the UserNotificationCenter
Macintosh-specific notification handler (supports UserNotificationCenter and Growl).