/* * Copyright (c) 2012 Tobias Markmann * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include #include #import #import #import #import #include #include #include #include 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 delegate; std::map > callbacks; }; NotificationCenterNotifier::NotificationCenterNotifier(const std::string& /*name*/) { assert(isSupported()); p = boost::make_shared(); p->lastID = 0; p->delegate = boost::intrusive_ptr([[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 callback) { std::vector 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(notification)); } }