PRCYCoin  2.0.0.7rc1
P2P Digital Currency
macnotificationhandler.mm
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin Core developers
2 // Copyright (c) 2015-2018 The PIVX developers
3 // Copyright (c) 2018-2020 The DAPS Project developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
8 
9 #undef slots
10 #import <objc/runtime.h>
11 #include <Cocoa/Cocoa.h>
12 
13 // Add an obj-c category (extension) to return the expected bundle identifier
15 - (NSString *)__bundleIdentifier
16 {
17  if (self == [NSBundle mainBundle]) {
18  return @"io.prcycoin.Prcycoin-Qt";
19  } else {
20  return [self __bundleIdentifier];
21  }
22 }
23 @end
24 
25 void MacNotificationHandler::showNotification(const QString &title, const QString &text)
26 {
27  // check if users OS has support for NSUserNotification
28  if(this->hasUserNotificationCenterSupport()) {
29  // okay, seems like 10.8+
30  QByteArray utf8 = title.toUtf8();
31  char* cString = (char *)utf8.constData();
32  NSString *titleMac = [[NSString alloc] initWithUTF8String:cString];
33 
34  utf8 = text.toUtf8();
35  cString = (char *)utf8.constData();
36  NSString *textMac = [[NSString alloc] initWithUTF8String:cString];
37 
38  // do everything weak linked (because we will keep <10.8 compatibility)
39  id userNotification = [[NSClassFromString(@"NSUserNotification") alloc] init];
40  [userNotification performSelector:@selector(setTitle:) withObject:titleMac];
41  [userNotification performSelector:@selector(setInformativeText:) withObject:textMac];
42 
43  id notificationCenterInstance = [NSClassFromString(@"NSUserNotificationCenter") performSelector:@selector(defaultUserNotificationCenter)];
44  [notificationCenterInstance performSelector:@selector(deliverNotification:) withObject:userNotification];
45 
46  [titleMac release];
47  [textMac release];
48  [userNotification release];
49  }
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 
70  Class aPossibleClass = objc_getClass("NSBundle");
71  if (aPossibleClass) {
72  // change NSBundle -bundleIdentifier method to return a correct bundle identifier
73  // a bundle identifier is required to use OSXs User Notification Center
74  method_exchangeImplementations(class_getInstanceMethod(aPossibleClass, @selector(bundleIdentifier)),
75  class_getInstanceMethod(aPossibleClass, @selector(__bundleIdentifier)));
76  }
77  }
78  return s_instance;
79 }
macnotificationhandler.h
NSBundle(returnCorrectIdentifier)
Definition: macnotificationhandler.mm:14
MacNotificationHandler::instance
static MacNotificationHandler * instance()
Definition: macnotificationhandler.mm:64
MacNotificationHandler::hasUserNotificationCenterSupport
bool hasUserNotificationCenterSupport(void)
check if OS can handle UserNotifications
Definition: macnotificationhandler.mm:52
MacNotificationHandler
Macintosh-specific notification handler (supports UserNotificationCenter).
Definition: macnotificationhandler.h:12
MacNotificationHandler::showNotification
void showNotification(const QString &title, const QString &text)
shows a 10.8+ UserNotification in the UserNotificationCenter
Definition: macnotificationhandler.mm:25