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