Support new versions of Growl and Apple notification center.

This commit is contained in:
Alarantalara 2014-12-11 23:27:07 -05:00
parent ae86186db7
commit 7cb0f5c86a
5 changed files with 94 additions and 63 deletions

View file

@ -0,0 +1,80 @@
/*
Copyright (C) 2014 by Google Inc.
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "apple_notification.hpp"
#import <Foundation/Foundation.h>
#if HAVE_GROWL
#import "Growl/Growl.h"
@interface WesnothGrowlDelegate : NSObject <GrowlApplicationBridgeDelegate>
@end
@implementation WesnothGrowlDelegate
// Empty delegate to interact with Growl. Implement protocol if we want to handle messages from Growl.
@end
#endif
namespace apple_notifications {
void send_cocoa_notification(const std::string& owner, const std::string& message);
void send_growl_notification(const std::string& owner, const std::string& message, const std::string& note_name);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
void send_notification(const std::string& owner, const std::string& message, const std::string& note_name) {
Class appleNotificationClass = NSClassFromString(@"NSUserNotificationCenter");
if (appleNotificationClass) {
send_cocoa_notification(owner, message);
} else {
#if HAVE_GROWL
send_growl_notification(owner, message, note_name);
#endif
}
}
#pragma clang diagnostic pop
void send_cocoa_notification(const std::string& owner, const std::string& message) {
NSString *title = [NSString stringWithCString:owner.c_str() encoding:NSUTF8StringEncoding];
NSString *description = [NSString stringWithCString:message.c_str() encoding:NSUTF8StringEncoding];
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = title;
notification.informativeText = description;
notification.deliveryDate = [NSDate date];
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];
}
#if HAVE_GROWL
void send_growl_notification(const std::string& owner, const std::string& message, const std::string& note_name) {
static WesnothGrowlDelegate *delegate = nil;
if (!delegate) {
delegate = [[WesnothGrowlDelegate alloc] init];
[GrowlApplicationBridge setGrowlDelegate:delegate];
}
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
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
}
#endif
}

View file

@ -980,6 +980,7 @@
ECFB9FA8193BFAD900146ED0 /* carryover.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECFB9FA7193BFAD900146ED0 /* carryover.cpp */; };
ECFB9FAA193BFB4B00146ED0 /* game_board.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECFB9FA9193BFB4B00146ED0 /* game_board.cpp */; };
ECFB9FAC193BFB6E00146ED0 /* rect.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECFB9FAB193BFB6E00146ED0 /* rect.cpp */; };
F40A13BC1A3A88BA00C4D071 /* apple_notification.mm in Sources */ = {isa = PBXBuildFile; fileRef = F40A13BB1A3A88BA00C4D071 /* apple_notification.mm */; };
F419A1F414E21246002F9ADC /* game_end_exceptions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F419A1F314E21246002F9ADC /* game_end_exceptions.cpp */; };
F419A2C314F5BCFE002F9ADC /* info.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F419A2C114F5BCFE002F9ADC /* info.cpp */; };
F419A31214F8868A002F9ADC /* manager_ui.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F419A31014F8868A002F9ADC /* manager_ui.cpp */; };
@ -2257,6 +2258,8 @@
ECFB9FA7193BFAD900146ED0 /* carryover.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = carryover.cpp; sourceTree = "<group>"; };
ECFB9FA9193BFB4B00146ED0 /* game_board.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = game_board.cpp; sourceTree = "<group>"; };
ECFB9FAB193BFB6E00146ED0 /* rect.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = rect.cpp; path = sdl/rect.cpp; sourceTree = "<group>"; };
F40A13BB1A3A88BA00C4D071 /* apple_notification.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = apple_notification.mm; path = "Mac Sources/apple_notification.mm"; sourceTree = "<group>"; };
F40A13BD1A3AA56800C4D071 /* apple_notification.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = apple_notification.hpp; path = ../../src/desktop/apple_notification.hpp; sourceTree = "<group>"; };
F40C04821706613100B4DA68 /* tip.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = tip.hpp; sourceTree = "<group>"; };
F40C04831706613100B4DA68 /* popup.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = popup.hpp; sourceTree = "<group>"; };
F40C04841706613100B4DA68 /* mp_login.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = mp_login.hpp; sourceTree = "<group>"; };
@ -2433,6 +2436,8 @@
B559986A0EC616B3008DD061 /* SDLMain.h */,
B559986B0EC616B3008DD061 /* SDLMain.m */,
B5BB6CD80F89470B00444FBF /* server_main.m */,
F40A13BB1A3A88BA00C4D071 /* apple_notification.mm */,
F40A13BD1A3AA56800C4D071 /* apple_notification.hpp */,
);
name = "Mac-specific";
sourceTree = "<group>";
@ -4475,6 +4480,7 @@
B597EC140FC0835900CE81F5 /* controller.cpp in Sources */,
B597EC150FC0835900CE81F5 /* interface.cpp in Sources */,
B597EC160FC0835900CE81F5 /* part.cpp in Sources */,
F40A13BC1A3A88BA00C4D071 /* apple_notification.mm in Sources */,
EC89A12E1879D17D00A3B0B1 /* lcorolib.cpp in Sources */,
B597EC170FC0835900CE81F5 /* render.cpp in Sources */,
B597EC5A0FC08E0100CE81F5 /* canvas.cpp in Sources */,

View file

@ -12,12 +12,12 @@
See the COPYING file for more details.
*/
#ifndef GROWL_NOTIFICATION_HPP_
#define GROWL_NOTIFICATION_HPP_
#ifndef APPLE_NOTIFICATION_HPP_
#define APPLE_NOTIFICATION_HPP_
#include <string>
namespace growl {
namespace apple_notifications {
void send_notification(const std::string& owner, const std::string& message, const std::string & note_name);
}

View file

@ -1,55 +0,0 @@
/*
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "growl_notification.hpp"
#include "global.hpp"
#include <string>
#ifndef HAVE_GROWL
#error "The HAVE_GROWL symbol is not defined, you do not have growl available, you should not be trying to compile growl_notification.cpp"
#endif
#include <Growl/GrowlApplicationBridge-Carbon.h>
#include <Carbon/Carbon.h>
namespace growl {
void send_notification(const std::string& owner, const std::string& message, const std::string & note_name)
{
static Growl_Delegate growl_obj;
CFStringRef app_name = CFStringCreateWithCString(NULL, "Wesnoth", kCFStringEncodingUTF8);
CFStringRef cf_owner = CFStringCreateWithCString(NULL, owner.c_str(), kCFStringEncodingUTF8);
CFStringRef cf_message = CFStringCreateWithCString(NULL, message.c_str(), kCFStringEncodingUTF8);
CFStringRef cf_note_name = CFStringCreateWithCString(NULL, note_name.c_str(), kCFStringEncodingUTF8);
growl_obj.applicationName = app_name;
growl_obj.registrationDictionary = NULL;
growl_obj.applicationIconData = NULL;
growl_obj.growlIsReady = NULL;
growl_obj.growlNotificationWasClicked = NULL;
growl_obj.growlNotificationTimedOut = NULL;
Growl_SetDelegate(&growl_obj);
Growl_NotifyWithTitleDescriptionNameIconPriorityStickyClickContext(cf_owner, cf_message, cf_note_name, NULL, NULL, NULL, NULL);
CFRelease(app_name);
CFRelease(cf_owner);
CFRelease(cf_message);
CFRelease(cf_note_name);
}
} //end namespace growl

View file

@ -30,8 +30,8 @@
#include "dbus_notification.hpp"
#endif
#ifdef HAVE_GROWL
#include "growl_notification.hpp"
#ifdef __APPLE__
#include "apple_notification.hpp"
#endif
#ifdef _WIN32
@ -42,7 +42,7 @@ namespace desktop {
namespace notifications {
#if !(defined(HAVE_LIBDBUS) || defined(HAVE_GROWL) || defined(_WIN32))
#if !(defined(HAVE_LIBDBUS) || defined(__APPLE__) || defined(_WIN32))
bool available() { return false; }
@ -70,7 +70,7 @@ void send(const std::string& owner, const std::string& message, type t)
dbus::send_notification(owner, message, t == CHAT);
#endif
#ifdef HAVE_GROWL
#ifdef __APPLE__
std::string note_name = "";
switch (t) {
case CHAT:
@ -84,7 +84,7 @@ void send(const std::string& owner, const std::string& message, type t)
break;
}
growl::send_notification(owner, message, note_name);
apple_notifications::send_notification(owner, message, note_name);
#endif
#ifdef _WIN32