summaryrefslogtreecommitdiffstats
blob: c62ce3dd7a4cd9777c9744bb9c37a2df9e5ade0f (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
93
94
/*
 * Copyright (c) 2012 Mateusz Piękos
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <Swift/Controllers/WhiteboardManager.h>

#include <boost/bind.hpp>

#include <Swiften/Base/foreach.h>
#include <Swift/Controllers/UIEvents/RequestWhiteboardUIEvent.h>
#include <Swift/Controllers/UIEvents/AcceptWhiteboardSessionUIEvent.h>
#include <Swift/Controllers/UIEvents/CancelWhiteboardSessionUIEvent.h>
#include <Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h>
#include <Swiften/Client/StanzaChannel.h>
#include <Swiften/Whiteboard/WhiteboardSessionManager.h>

namespace Swift {
	typedef std::pair<JID, WhiteboardWindow*> JIDWhiteboardWindowPair;

	WhiteboardManager::WhiteboardManager(WhiteboardWindowFactory* whiteboardWindowFactory, UIEventStream* uiEventStream, WhiteboardSessionManager* whiteboardSessionManager) : whiteboardWindowFactory_(whiteboardWindowFactory), uiEventStream_(uiEventStream), whiteboardSessionManager_(whiteboardSessionManager) {
		uiEventConnection_ = uiEventStream_->onUIEvent.connect(boost::bind(&WhiteboardManager::handleUIEvent, this, _1));
	}

	WhiteboardManager::~WhiteboardManager() {
		foreach (JIDWhiteboardWindowPair whiteboardWindowPair, whiteboardWindows_) {
			delete whiteboardWindowPair.second;
		}
	}

	WhiteboardWindow* WhiteboardManager::createNewWhiteboardWindow(const JID& contact, WhiteboardSession::ref session) {
		WhiteboardWindow *window = whiteboardWindowFactory_->createWhiteboardWindow(session);
		whiteboardWindows_[contact.toBare()] = window;
		return window;
	}

	WhiteboardWindow* WhiteboardManager::findWhiteboardWindow(const JID& contact) {
		if (whiteboardWindows_.find(contact.toBare()) == whiteboardWindows_.end()) {
			return NULL;
		}
		return whiteboardWindows_[contact.toBare()];
	}

	void WhiteboardManager::handleUIEvent(boost::shared_ptr<UIEvent> event) {
		boost::shared_ptr<RequestWhiteboardUIEvent> requestWhiteboardEvent = boost::dynamic_pointer_cast<RequestWhiteboardUIEvent>(event);
		if (requestWhiteboardEvent) {
			JID contact = requestWhiteboardEvent->getContact();
			WhiteboardSession::ref session = whiteboardSessionManager_->requestSession(contact);
			WhiteboardWindow* window = findWhiteboardWindow(contact);
			if (window == NULL) {
				createNewWhiteboardWindow(contact, session);
			} else {
				window->setSession(session);
			}
		}
		boost::shared_ptr<AcceptWhiteboardSessionUIEvent> sessionAcceptEvent = boost::dynamic_pointer_cast<AcceptWhiteboardSessionUIEvent>(event);
		if (sessionAcceptEvent) {
			acceptSession(sessionAcceptEvent->getContact());
		}
		boost::shared_ptr<CancelWhiteboardSessionUIEvent> sessionCancelEvent = boost::dynamic_pointer_cast<CancelWhiteboardSessionUIEvent>(event);
		if (sessionCancelEvent) {
			cancelSession(sessionCancelEvent->getContact());
		}
		boost::shared_ptr<ShowWhiteboardUIEvent> showWindowEvent = boost::dynamic_pointer_cast<ShowWhiteboardUIEvent>(event);
		if (showWindowEvent) {
			WhiteboardWindow* window = findWhiteboardWindow(showWindowEvent->getContact());
			if (window != NULL) {
				window->activateWindow();
			}
		}
	}

	void WhiteboardManager::acceptSession(const JID& from) {
		IncomingWhiteboardSession::ref session = boost::dynamic_pointer_cast<IncomingWhiteboardSession>(whiteboardSessionManager_->getSession(from));
		if (session) {
			session->accept();
			WhiteboardWindow* window = findWhiteboardWindow(from);
			if (window == NULL) {
				window = createNewWhiteboardWindow(from, session);
			} else {
				window->setSession(session);
			}
			window->show();
		}
	}

	void WhiteboardManager::cancelSession(const JID& from) {
		IncomingWhiteboardSession::ref session = boost::dynamic_pointer_cast<IncomingWhiteboardSession>(whiteboardSessionManager_->getSession(from));
		if (session) {
			session->cancel();
		}
	}
}