Anoncoin  0.9.4
P2P Digital Currency
macnotificationhandler.mm
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin Core developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
7 
8 #undef slots
9 #include <Cocoa/Cocoa.h>
10 
11 void MacNotificationHandler::showNotification(const QString &title, const QString &text)
12 {
13  // check if users OS has support for NSUserNotification
15  // okay, seems like 10.8+
16  QByteArray utf8 = title.toUtf8();
17  char* cString = (char *)utf8.constData();
18  NSString *titleMac = [[NSString alloc] initWithUTF8String:cString];
19 
20  utf8 = text.toUtf8();
21  cString = (char *)utf8.constData();
22  NSString *textMac = [[NSString alloc] initWithUTF8String:cString];
23 
24  // do everything weak linked (because we will keep <10.8 compatibility)
25  id userNotification = [[NSClassFromString(@"NSUserNotification") alloc] init];
26  [userNotification performSelector:@selector(setTitle:) withObject:titleMac];
27  [userNotification performSelector:@selector(setInformativeText:) withObject:textMac];
28 
29  id notificationCenterInstance = [NSClassFromString(@"NSUserNotificationCenter") performSelector:@selector(defaultUserNotificationCenter)];
30  [notificationCenterInstance performSelector:@selector(deliverNotification:) withObject:userNotification];
31 
32  [titleMac release];
33  [textMac release];
34  [userNotification release];
35  }
36 }
37 
38 // sendAppleScript just take a QString and executes it as apple script
39 void MacNotificationHandler::sendAppleScript(const QString &script)
40 {
41  QByteArray utf8 = script.toUtf8();
42  char* cString = (char *)utf8.constData();
43  NSString *scriptApple = [[NSString alloc] initWithUTF8String:cString];
44 
45  NSAppleScript *as = [[NSAppleScript alloc] initWithSource:scriptApple];
46  NSDictionary *err = nil;
47  [as executeAndReturnError:&err];
48  [as release];
49  [scriptApple release];
50 }
51 
53 {
54  Class possibleClass = NSClassFromString(@"NSUserNotificationCenter");
55 
56  // check if users OS has support for NSUserNotification
57  if(possibleClass!=nil) {
58  return true;
59  }
60  return false;
61 }
62 
63 
65 {
66  static MacNotificationHandler *s_instance = NULL;
67  if (!s_instance)
68  s_instance = new MacNotificationHandler();
69  return s_instance;
70 }
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).