The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
apple_notification.mm
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Google Inc.
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifdef __APPLE__
16 
17 #include "apple_notification.hpp"
18 
19 #import <Foundation/Foundation.h>
20 
21 #if (defined MAC_OS_X_VERSION_10_8) && (MAC_OS_X_VERSION_10_8 <= MAC_OS_X_VERSION_MAX_ALLOWED)
22 #define HAVE_NS_USER_NOTIFICATION
23 #endif
24 
25 #ifdef HAVE_GROWL
26 #import "Growl/Growl.h"
27 
28 @interface WesnothGrowlDelegate : NSObject <GrowlApplicationBridgeDelegate>
29 @end
30 
31 @implementation WesnothGrowlDelegate
32 // Empty delegate to interact with Growl. Implement protocol if we want to handle messages from Growl.
33 @end
34 #endif
35 
36 namespace apple_notifications {
37 
38 #ifdef HAVE_NS_USER_NOTIFICATION
39 void send_cocoa_notification(const std::string& owner, const std::string& message);
40 #endif
41 #ifdef HAVE_GROWL
42 void send_growl_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type);
43 #endif
44 
45 #pragma clang diagnostic push
46 #pragma clang diagnostic ignored "-Wunused-parameter"
47 #ifdef HAVE_NS_USER_NOTIFICATION
48 void send_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type) {
49  @autoreleasepool {
50  Class appleNotificationClass = NSClassFromString(@"NSUserNotificationCenter");
51  if (appleNotificationClass) {
52  send_cocoa_notification(owner, message);
53  } else {
54 #ifdef HAVE_GROWL
55  send_growl_notification(owner, message, note_type);
56 #endif
57  }
58  }
59 }
60 #else // HAVE_NS_USER_NOTIFICATION
61 void send_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type) {
62  @autoreleasepool {
63 #ifdef HAVE_GROWL
64  send_growl_notification(owner, message, note_type);
65 #endif
66  }
67 }
68 #endif //end else HAVE_NS_USER_NOTIFICATION
69 #pragma clang diagnostic pop
70 
71 #ifdef HAVE_NS_USER_NOTIFICATION
72 void send_cocoa_notification(const std::string& owner, const std::string& message) {
73  NSString *title = [NSString stringWithCString:owner.c_str() encoding:NSUTF8StringEncoding];
74  NSString *description = [NSString stringWithCString:message.c_str() encoding:NSUTF8StringEncoding];
75  NSUserNotification *notification = [[NSUserNotification alloc] init];
76  notification.title = title;
77  notification.informativeText = description;
78  notification.deliveryDate = [NSDate date];
79 
80  [[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];
81 }
82 #endif //end HAVE_NS_USER_NOTIFICATION
83 
84 #ifdef HAVE_GROWL
85 void send_growl_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type) {
86  static WesnothGrowlDelegate *delegate = nil;
87  if (!delegate) {
88  delegate = [[WesnothGrowlDelegate alloc] init];
89  [GrowlApplicationBridge setGrowlDelegate:delegate];
90  }
91 
92  NSString *notificationName = @"";
93  switch (note_type) {
95  notificationName = @"Chat Message";
96  break;
98  notificationName = @"Turn Changed";
99  break;
101  notificationName = @"Wesnoth";
102  break;
103  }
104  NSString *title = [NSString stringWithCString:owner.c_str() encoding:NSUTF8StringEncoding];
105  NSString *description = [NSString stringWithCString:message.c_str() encoding:NSUTF8StringEncoding];
106  [GrowlApplicationBridge notifyWithTitle:title
107  description:description
108  notificationName:notificationName
109  iconData:nil
110  priority:0
111  isSticky:NO
112  clickContext:nil];
113 }
114 #endif //end HAVE_GROWL
115 
116 }
117 #endif //end __APPLE__
void send_notification(const std::string &owner, const std::string &message, const desktop::notifications::type note_type)
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
GLsizei const GLcharARB ** string
Definition: glew.h:4503