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