Avoid multiple string conversions to post Growl notifications (std::string -> C string -> NSString *)

This commit is contained in:
Alarantalara 2014-12-13 09:58:20 -05:00
parent 2a23585dd1
commit 758fce774f
3 changed files with 25 additions and 24 deletions

View File

@ -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 <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>
#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

View File

@ -17,8 +17,10 @@
#include <string>
#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

View File

@ -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