The Battle for Wesnoth  1.13.10+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 
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 bool available() {
39  Class notificationClass = NSClassFromString(@"NSUserNotificationCenter");
40  if(notificationClass) {
41  return true;
42  }
43  notificationClass = NSClassFromString(@"GrowlApplicationBridge");
44  if(notificationClass) {
45  return true;
46  }
47  return false;
48 }
49 
50 #ifdef HAVE_NS_USER_NOTIFICATION
51 void send_cocoa_notification(const std::string& owner, const std::string& message);
52 #endif
53 #ifdef HAVE_GROWL
54 void send_growl_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type);
55 #endif
56 
57 #pragma clang diagnostic push
58 #pragma clang diagnostic ignored "-Wunused-parameter"
59 #ifdef HAVE_NS_USER_NOTIFICATION
60 void send_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type) {
61  @autoreleasepool {
62  Class appleNotificationClass = NSClassFromString(@"NSUserNotificationCenter");
63  if (appleNotificationClass) {
64  send_cocoa_notification(owner, message);
65 #ifdef HAVE_GROWL
66  } else {
67  send_growl_notification(owner, message, note_type);
68 #endif
69  }
70  }
71 }
72 #else // HAVE_NS_USER_NOTIFICATION
73 void send_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type) {
74 #ifdef HAVE_GROWL
75  @autoreleasepool {
76  send_growl_notification(owner, message, note_type);
77  }
78 #endif
79 }
80 #endif //end else HAVE_NS_USER_NOTIFICATION
81 #pragma clang diagnostic pop
82 
83 #ifdef HAVE_NS_USER_NOTIFICATION
84 void send_cocoa_notification(const std::string& owner, const std::string& message) {
85  NSString *title = [NSString stringWithCString:owner.c_str() encoding:NSUTF8StringEncoding];
86  NSString *description = [NSString stringWithCString:message.c_str() encoding:NSUTF8StringEncoding];
87  NSUserNotification *notification = [[NSUserNotification alloc] init];
88  notification.title = title;
89  notification.informativeText = description;
90  notification.deliveryDate = [NSDate date];
91 
92  [[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];
93 }
94 #endif //end HAVE_NS_USER_NOTIFICATION
95 
96 #ifdef HAVE_GROWL
97 void send_growl_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type) {
98  static WesnothGrowlDelegate *delegate = nil;
99  if (!delegate) {
100  delegate = [[WesnothGrowlDelegate alloc] init];
101  [GrowlApplicationBridge setGrowlDelegate:delegate];
102  }
103 
104  NSString *notificationName = @"";
105  switch (note_type) {
107  notificationName = @"Chat Message";
108  break;
110  notificationName = @"Turn Changed";
111  break;
113  notificationName = @"Wesnoth";
114  break;
115  }
116  NSString *title = [NSString stringWithCString:owner.c_str() encoding:NSUTF8StringEncoding];
117  NSString *description = [NSString stringWithCString:message.c_str() encoding:NSUTF8StringEncoding];
118  [GrowlApplicationBridge notifyWithTitle:title
119  description:description
120  notificationName:notificationName
121  iconData:nil
122  priority:0
123  isSticky:NO
124  clickContext:nil];
125 }
126 #endif //end HAVE_GROWL
127 
128 }
129 #endif //end __APPLE__
std::vector< char_t > string
void send_notification(const std::string &owner, const std::string &message, const desktop::notifications::type note_type)
bool available()
Whether wesnoth was compiled with support for a clipboard.
Definition: clipboard.cpp:56