summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'SwifTools/Notifier/NotificationCenterNotifier.mm')
-rw-r--r--SwifTools/Notifier/NotificationCenterNotifier.mm89
1 files changed, 89 insertions, 0 deletions
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));
+}
+
+}