summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'SwifTools')
-rw-r--r--SwifTools/Notifier/NotificationCenterNotifier.h30
-rw-r--r--SwifTools/Notifier/NotificationCenterNotifier.mm89
-rw-r--r--SwifTools/Notifier/NotificationCenterNotifierDelegate.h21
-rw-r--r--SwifTools/Notifier/NotificationCenterNotifierDelegate.mm23
-rw-r--r--SwifTools/Notifier/SConscript5
5 files changed, 168 insertions, 0 deletions
diff --git a/SwifTools/Notifier/NotificationCenterNotifier.h b/SwifTools/Notifier/NotificationCenterNotifier.h
new file mode 100644
index 0000000..337f224
--- /dev/null
+++ b/SwifTools/Notifier/NotificationCenterNotifier.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2012 Tobias Markmann
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#pragma once
+
+#include <boost/filesystem/fstream.hpp>
+
+#include <SwifTools/Notifier/Notifier.h>
+
+namespace Swift {
+ class NotificationCenterNotifier : public Notifier {
+ public:
+ NotificationCenterNotifier(const std::string& name);
+ virtual ~NotificationCenterNotifier();
+
+ virtual void showMessage(Type type, const std::string& subject, const std::string& description, const boost::filesystem::path& picture, boost::function<void()> callback);
+
+ virtual void purgeCallbacks();
+
+ static bool isSupported();
+ void handleNotificationClicked(void* notification);
+
+ public:
+ class Private;
+ boost::shared_ptr<Private> p;
+ };
+}
diff --git a/SwifTools/Notifier/NotificationCenterNotifier.mm b/SwifTools/Notifier/NotificationCenterNotifier.mm
new file mode 100644
index 0000000..46bc7b1
--- /dev/null
+++ b/SwifTools/Notifier/NotificationCenterNotifier.mm
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2012 Tobias Markmann
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#include <SwifTools/Notifier/NotificationCenterNotifier.h>
+
+#include <SwifTools/Notifier/NotificationCenterNotifierDelegate.h>
+
+#import <Foundation/NSObjCRuntime.h>
+#import <Foundation/NSUserNotification.h>
+#import <Foundation/NSString.h>
+#import <objc/runtime.h>
+
+#include <boost/smart_ptr/make_shared.hpp>
+#include <SwifTools/Cocoa/CocoaUtil.h>
+
+#include <map>
+
+#include <Swiften/Base/Log.h>
+
+namespace Swift {
+
+class NotificationCenterNotifier::Private {
+ public:
+ void handleNotificationClicked(NSUserNotification* notification) {
+ unsigned long int id = [[[notification userInfo] objectForKey:@"id"] unsignedLongValue];
+ if (callbacks.find(id) != callbacks.end()) {
+ callbacks[id]();
+ callbacks.erase(id);
+ } else {
+ SWIFT_LOG(debug) << "Callback missing! id:"<< id << std::endl;
+ }
+ }
+
+ public:
+ unsigned long int lastID;
+ NSUserNotificationCenter* notificationCenter;
+ boost::intrusive_ptr<NotificationCenterNotifierDelegate> delegate;
+ std::map<unsigned long int, boost::function<void()> > callbacks;
+};
+
+NotificationCenterNotifier::NotificationCenterNotifier(const std::string& /*name*/) {
+ assert(isSupported());
+ p = boost::make_shared<Private>();
+ p->lastID = 0;
+ p->delegate = boost::intrusive_ptr<NotificationCenterNotifierDelegate>([[NotificationCenterNotifierDelegate alloc] init], false);
+ p->delegate.get().notifier = this;
+ p->notificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
+ [p->notificationCenter setDelegate:p->delegate.get()];
+}
+
+NotificationCenterNotifier::~NotificationCenterNotifier() {
+
+}
+
+void NotificationCenterNotifier::showMessage(Notifier::Type type, const std::string& subject, const std::string& description, const boost::filesystem::path& /* picture */, boost::function<void()> callback) {
+ std::vector<Notifier::Type> defaultTypes = getDefaultTypes();
+ if (std::find(defaultTypes.begin(), defaultTypes.end(), type) == defaultTypes.end()) {
+ return;
+ }
+
+ unsigned long int currentID = ++(p->lastID);
+
+ NSUserNotification *notification = [[NSUserNotification alloc] init];
+ [notification setTitle: [NSString stringWithUTF8String:subject.c_str()]];
+ [notification setInformativeText: [NSString stringWithUTF8String:description.c_str()]];
+ //[notification setActionButtonTitle:@"Accept Invitation"];
+ //[notification setHasActionButton:TRUE];
+ //[notification setSoundName:NSUserNotificationDefaultSoundName];
+ [notification setUserInfo: [NSDictionary dictionaryWithObject: [NSNumber numberWithUnsignedLong:currentID] forKey:@"id"]];
+ p->callbacks[currentID] = callback;
+ [p->notificationCenter scheduleNotification:notification];
+}
+
+void NotificationCenterNotifier::purgeCallbacks() {
+ p->callbacks.clear();
+}
+
+bool NotificationCenterNotifier::isSupported() {
+ return NSClassFromString(@"NSUserNotificationCenter") != nil;
+}
+
+void NotificationCenterNotifier::handleNotificationClicked(void *notification) {
+ p->handleNotificationClicked(static_cast<NSUserNotification*>(notification));
+}
+
+}
diff --git a/SwifTools/Notifier/NotificationCenterNotifierDelegate.h b/SwifTools/Notifier/NotificationCenterNotifierDelegate.h
new file mode 100644
index 0000000..0db4370
--- /dev/null
+++ b/SwifTools/Notifier/NotificationCenterNotifierDelegate.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2012 Tobias Markmann
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#import <Foundation/NSUserNotification.h>
+
+namespace Swift {
+ class NotificationCenterNotifier;
+}
+
+@interface NotificationCenterNotifierDelegate : NSObject<NSUserNotificationCenterDelegate> {
+ Swift::NotificationCenterNotifier* notifier;
+}
+
+@property (nonatomic) Swift::NotificationCenterNotifier* notifier;
+
+- (void) userNotificationCenter:(NSUserNotificationCenter *) center didActivateNotification:(NSUserNotification *) notification;
+
+@end
diff --git a/SwifTools/Notifier/NotificationCenterNotifierDelegate.mm b/SwifTools/Notifier/NotificationCenterNotifierDelegate.mm
new file mode 100644
index 0000000..271d953
--- /dev/null
+++ b/SwifTools/Notifier/NotificationCenterNotifierDelegate.mm
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2012 Tobias Markmann
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#import "NotificationCenterNotifierDelegate.h"
+
+#include <SwifTools/Notifier/NotificationCenterNotifier.h>
+
+@implementation NotificationCenterNotifierDelegate;
+
+@synthesize notifier;
+
+using namespace Swift;
+
+- (void) userNotificationCenter:(NSUserNotificationCenter *) center didActivateNotification:(NSUserNotification *) notification {
+ notifier->handleNotificationClicked(notification);
+ //Remove the notification
+ [center removeDeliveredNotification:notification];
+}
+
+@end
diff --git a/SwifTools/Notifier/SConscript b/SwifTools/Notifier/SConscript
index 98b5400..53bb38e 100644
--- a/SwifTools/Notifier/SConscript
+++ b/SwifTools/Notifier/SConscript
@@ -16,6 +16,11 @@ if swiftools_env.get("HAVE_SNARL", False) :
sources += [
"SnarlNotifier.cpp",
]
+if swiftools_env.get("HAVE_NOTIFICATION_CENTER", False) :
+ sources += [
+ "NotificationCenterNotifier.mm",
+ "NotificationCenterNotifierDelegate.mm",
+ ]
objects = myenv.StaticObject(sources)
swiftools_env.Append(SWIFTOOLS_OBJECTS = objects)