From 758fce774f0e050d45a3e8b14dc77042c912ac63 Mon Sep 17 00:00:00 2001 From: Alarantalara Date: Sat, 13 Dec 2014 09:58:20 -0500 Subject: [PATCH] Avoid multiple string conversions to post Growl notifications (std::string -> C string -> NSString *) --- .../Xcode/Mac Sources/apple_notification.mm | 30 +++++++++++++------ src/desktop/apple_notification.hpp | 4 ++- src/desktop/notifications.cpp | 15 +--------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/projectfiles/Xcode/Mac Sources/apple_notification.mm b/projectfiles/Xcode/Mac Sources/apple_notification.mm index 93cf142a90c..6e25ab88be2 100644 --- a/projectfiles/Xcode/Mac Sources/apple_notification.mm +++ b/projectfiles/Xcode/Mac Sources/apple_notification.mm @@ -14,11 +14,12 @@ #include "apple_notification.hpp" -#if defined MAC_OS_X_VERSION_10_8 && (MAC_OS_X_VERSION_10_8 <= MAC_OS_X_VERSION_MAX_ALLOWED) +#import + +#if (defined MAC_OS_X_VERSION_10_8) && (MAC_OS_X_VERSION_10_8 <= MAC_OS_X_VERSION_MAX_ALLOWED) #define HAVE_NS_USER_NOTIFICATION #endif -#import #ifdef HAVE_GROWL #import "Growl/Growl.h" @@ -36,29 +37,29 @@ namespace apple_notifications { void send_cocoa_notification(const std::string& owner, const std::string& message); #endif #ifdef HAVE_GROWL -void send_growl_notification(const std::string& owner, const std::string& message, const std::string& note_name); +void send_growl_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type); #endif #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-parameter" #ifdef HAVE_NS_USER_NOTIFICATION -void send_notification(const std::string& owner, const std::string& message, const std::string& note_name) { +void send_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type) { @autoreleasepool { Class appleNotificationClass = NSClassFromString(@"NSUserNotificationCenter"); if (appleNotificationClass) { send_cocoa_notification(owner, message); } else { #ifdef HAVE_GROWL - send_growl_notification(owner, message, note_name); + send_growl_notification(owner, message, note_type); #endif } } } #else -void send_notification(const std::string& owner, const std::string& message, const std::string& note_name) { +void send_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type) { @autoreleasepool { #ifdef HAVE_GROWL - send_growl_notification(owner, message, note_name); + send_growl_notification(owner, message, note_type); #endif } } @@ -79,16 +80,27 @@ void send_cocoa_notification(const std::string& owner, const std::string& messag #endif #ifdef HAVE_GROWL -void send_growl_notification(const std::string& owner, const std::string& message, const std::string& note_name) { +void send_growl_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type) { static WesnothGrowlDelegate *delegate = nil; if (!delegate) { delegate = [[WesnothGrowlDelegate alloc] init]; [GrowlApplicationBridge setGrowlDelegate:delegate]; } + NSString *notificationName = @""; + switch (note_type) { + case desktop::notifications::CHAT: + notificationName = @"Chat Message"; + break; + case desktop::notifications::TURN_CHANGED: + notificationName = @"Turn Changed"; + break; + case desktop::notifications::OTHER: + notificationName = @"Wesnoth"; + break; + } NSString *title = [NSString stringWithCString:owner.c_str() encoding:NSUTF8StringEncoding]; NSString *description = [NSString stringWithCString:message.c_str() encoding:NSUTF8StringEncoding]; - NSString *notificationName = [NSString stringWithCString:note_name.c_str() encoding:NSUTF8StringEncoding]; [GrowlApplicationBridge notifyWithTitle:title description:description notificationName:notificationName diff --git a/src/desktop/apple_notification.hpp b/src/desktop/apple_notification.hpp index 911eb91a0bc..1290b6fe0bd 100644 --- a/src/desktop/apple_notification.hpp +++ b/src/desktop/apple_notification.hpp @@ -17,8 +17,10 @@ #include +#include "notifications.hpp" + namespace apple_notifications { - void send_notification(const std::string& owner, const std::string& message, const std::string & note_name); + void send_notification(const std::string& owner, const std::string& message, const desktop::notifications::type note_type); } #endif diff --git a/src/desktop/notifications.cpp b/src/desktop/notifications.cpp index cdec780ff8c..a3344ecbce5 100644 --- a/src/desktop/notifications.cpp +++ b/src/desktop/notifications.cpp @@ -71,20 +71,7 @@ void send(const std::string& owner, const std::string& message, type t) #endif #ifdef __APPLE__ - std::string note_name = ""; - switch (t) { - case CHAT: - note_name = _("Chat Message"); - break; - case TURN_CHANGED: - note_name = _("Turn Changed"); - break; - case OTHER: - note_name = _("Wesnoth"); - break; - } - - apple_notifications::send_notification(owner, message, note_name); + apple_notifications::send_notification(owner, message, t); #endif #ifdef _WIN32