You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

macnotificationhandler.mm 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "macnotificationhandler.h"
  5. #undef slots
  6. #import <objc/runtime.h>
  7. #include <Cocoa/Cocoa.h>
  8. // Add an obj-c category (extension) to return the expected bundle identifier
  9. @implementation NSBundle(returnCorrectIdentifier)
  10. - (NSString *)__bundleIdentifier
  11. {
  12. if (self == [NSBundle mainBundle]) {
  13. return @"org.bitcoinfoundation.Bitcoin-Qt";
  14. } else {
  15. return [self __bundleIdentifier];
  16. }
  17. }
  18. @end
  19. void MacNotificationHandler::showNotification(const QString &title, const QString &text)
  20. {
  21. // check if users OS has support for NSUserNotification
  22. if(this->hasUserNotificationCenterSupport()) {
  23. // okay, seems like 10.8+
  24. QByteArray utf8 = title.toUtf8();
  25. char* cString = (char *)utf8.constData();
  26. NSString *titleMac = [[NSString alloc] initWithUTF8String:cString];
  27. utf8 = text.toUtf8();
  28. cString = (char *)utf8.constData();
  29. NSString *textMac = [[NSString alloc] initWithUTF8String:cString];
  30. // do everything weak linked (because we will keep <10.8 compatibility)
  31. id userNotification = [[NSClassFromString(@"NSUserNotification") alloc] init];
  32. [userNotification performSelector:@selector(setTitle:) withObject:titleMac];
  33. [userNotification performSelector:@selector(setInformativeText:) withObject:textMac];
  34. id notificationCenterInstance = [NSClassFromString(@"NSUserNotificationCenter") performSelector:@selector(defaultUserNotificationCenter)];
  35. [notificationCenterInstance performSelector:@selector(deliverNotification:) withObject:userNotification];
  36. [titleMac release];
  37. [textMac release];
  38. [userNotification release];
  39. }
  40. }
  41. // sendAppleScript just take a QString and executes it as apple script
  42. void MacNotificationHandler::sendAppleScript(const QString &script)
  43. {
  44. QByteArray utf8 = script.toUtf8();
  45. char* cString = (char *)utf8.constData();
  46. NSString *scriptApple = [[NSString alloc] initWithUTF8String:cString];
  47. NSAppleScript *as = [[NSAppleScript alloc] initWithSource:scriptApple];
  48. NSDictionary *err = nil;
  49. [as executeAndReturnError:&err];
  50. [as release];
  51. [scriptApple release];
  52. }
  53. bool MacNotificationHandler::hasUserNotificationCenterSupport(void)
  54. {
  55. Class possibleClass = NSClassFromString(@"NSUserNotificationCenter");
  56. // check if users OS has support for NSUserNotification
  57. if(possibleClass!=nil) {
  58. return true;
  59. }
  60. return false;
  61. }
  62. MacNotificationHandler *MacNotificationHandler::instance()
  63. {
  64. static MacNotificationHandler *s_instance = NULL;
  65. if (!s_instance) {
  66. s_instance = new MacNotificationHandler();
  67. Class aPossibleClass = objc_getClass("NSBundle");
  68. if (aPossibleClass) {
  69. // change NSBundle -bundleIdentifier method to return a correct bundle identifier
  70. // a bundle identifier is required to use OSXs User Notification Center
  71. method_exchangeImplementations(class_getInstanceMethod(aPossibleClass, @selector(bundleIdentifier)),
  72. class_getInstanceMethod(aPossibleClass, @selector(__bundleIdentifier)));
  73. }
  74. }
  75. return s_instance;
  76. }