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