summaryrefslogtreecommitdiffstats
blob: 28cacb655d50495096c45f9ccb18095dc0fa35f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright (c) 2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <SwifTools/Notifier/NotificationCenterNotifier.h>

#include <map>
#include <string>

#include <boost/smart_ptr/make_shared.hpp>

#include <Swiften/Base/Log.h>

#import <Cocoa/Cocoa.h>

#include <SwifTools/Notifier/NotificationCenterNotifierDelegate.h>
#include <SwifTools/Cocoa/CocoaUtil.h>

namespace {
	struct Context {
		Context(const boost::function<void()>& callback) : callback(new boost::function<void()>(callback)) {
		}

		~Context() {
			delete callback;
		}

		boost::function<void()>* callback;
	};
}

namespace Swift {

class NotificationCenterNotifier::Private {
	public:
		std::map<std::string, boost::shared_ptr<Context> > callbacksForNotifications;
		boost::intrusive_ptr<NotificationCenterNotifierDelegate> delegate;
};

NotificationCenterNotifier::NotificationCenterNotifier() {
	p = boost::make_shared<Private>();
	p->delegate = boost::intrusive_ptr<NotificationCenterNotifierDelegate>([[NotificationCenterNotifierDelegate alloc] init], false);
	[p->delegate.get() setNotifier: this];

	[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: p->delegate.get()];
}

NotificationCenterNotifier::~NotificationCenterNotifier() {
	[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: nil];
	p->callbacksForNotifications.clear();
}

void NotificationCenterNotifier::showMessage(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;
	}

	NSUserNotification* notification = [[NSUserNotification alloc] init];
	notification.title = STD2NSSTRING(typeToString(type));
	notification.subtitle = STD2NSSTRING(subject);
	notification.informativeText = STD2NSSTRING(description);
	notification.contentImage = [[NSImage alloc] initWithContentsOfFile: STD2NSSTRING(picture.string())];

	// The OS X Notification Center API does not allow to attach custom data, like a pointer to a callback function,
	// to the NSUserNotification object. Therefore we maintain a mapping from a NSUserNotification instance's identification
	// to their respective callbacks.
	notification.identifier = [[NSUUID UUID] UUIDString];

	/// \todo Currently the elements are only removed on application exit. Ideally the notifications not required anymore
	///       are removed from the map; e.g. when visiting a chat view, all notifications from that view can be removed from
	///       the map and the NSUserNotificationCenter.
	p->callbacksForNotifications[NS2STDSTRING(notification.identifier)] = boost::make_shared<Context>(callback);
	[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}

void NotificationCenterNotifier::purgeCallbacks() {
	p->callbacksForNotifications.clear();
}

void NotificationCenterNotifier::handleUserNotificationActivated(const std::string& identifier) {
	if (p->callbacksForNotifications.find(identifier) != p->callbacksForNotifications.end()) {
		(*p->callbacksForNotifications[identifier]->callback)();
	}
	else {
		SWIFT_LOG(warning) << "Missing callback entry for activated notification. The activate notification may come from another instance." << std::endl;
	}
}

}