summaryrefslogtreecommitdiffstats
blob: 7743d6f605f0c197c87b8bb80af356f787d403b8 (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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "Swift/Controllers/EventNotifier.h"

#include <boost/bind.hpp>

#include "Swift/Controllers/XMPPEvents/EventController.h"
#include "SwifTools/Notifier/Notifier.h"
#include "Swiften/Avatars/AvatarManager.h"
#include "Swift/Controllers/NickResolver.h"
#include "Swiften/JID/JID.h"
#include "Swift/Controllers/XMPPEvents/MessageEvent.h"
#include "Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h"
#include "Swift/Controllers/XMPPEvents/ErrorEvent.h"
#include "Swift/Controllers/UIEvents/UIEventStream.h"
#include "Swift/Controllers/UIEvents/ToggleNotificationsUIEvent.h"
#include "Swiften/Settings/SettingsProvider.h"

namespace Swift {

EventNotifier::EventNotifier(EventController* eventController, Notifier* notifier, AvatarManager* avatarManager, NickResolver* nickResolver, UIEventStream* uiEvents, SettingsProvider* settings) : eventController(eventController), notifier(notifier), avatarManager(avatarManager), nickResolver(nickResolver), uiEvents(uiEvents), settings(settings) {
	eventController->onEventQueueEventAdded.connect(boost::bind(&EventNotifier::handleEventAdded, this, _1));
	bool enabled = settings->getBoolSetting("showNotifications", true);
	notifier->setEnabled(enabled);
	uiEvents->send(boost::shared_ptr<ToggleNotificationsUIEvent>(new ToggleNotificationsUIEvent(enabled)));
}

EventNotifier::~EventNotifier() {
	eventController->onEventQueueEventAdded.disconnect(boost::bind(&EventNotifier::handleEventAdded, this, _1));
}

void EventNotifier::handleEventAdded(boost::shared_ptr<StanzaEvent> event) {
	if (boost::shared_ptr<MessageEvent> messageEvent = boost::dynamic_pointer_cast<MessageEvent>(event)) {
		JID jid = messageEvent->getStanza()->getFrom();
		String title = nickResolver->jidToNick(jid);
		notifier->showMessage(Notifier::IncomingMessage, title, messageEvent->getStanza()->getBody(), avatarManager->getAvatarPath(jid), boost::bind(&EventNotifier::handleNotificationActivated, this, jid));
	}
	else if(boost::shared_ptr<SubscriptionRequestEvent> subscriptionEvent = boost::dynamic_pointer_cast<SubscriptionRequestEvent>(event)) {
		JID jid = subscriptionEvent->getJID();
		String title = "";
		String message = nickResolver->jidToNick(jid) + " wants to add you to his/her roster";
		notifier->showMessage(Notifier::SystemMessage, title, message, boost::filesystem::path(), boost::function<void()>());
	}
	else if(boost::shared_ptr<ErrorEvent> errorEvent = boost::dynamic_pointer_cast<ErrorEvent>(event)) {
		notifier->showMessage(Notifier::SystemMessage, "Error", errorEvent->getText(), boost::filesystem::path(), boost::function<void()>());
	}
}


void EventNotifier::handleNotificationActivated(JID jid) {
	onNotificationActivated(jid);
}

void EventNotifier::handleUIEvent(boost::shared_ptr<UIEvent> event) {
	boost::shared_ptr<ToggleNotificationsUIEvent> notificationsEvent = boost::dynamic_pointer_cast<ToggleNotificationsUIEvent>(event);
	if (notificationsEvent) {
		bool enabled = notificationsEvent->getEnabled();
		if (enabled != notifier->getEnabled()) {
			notifier->setEnabled(enabled);
			settings->storeBool("showNotifications", enabled);
		}
	}
}
}