summaryrefslogtreecommitdiffstats
blob: effdc4b26a22765f8d247b433228b44c3841f27a (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (c) 2012 Mateusz Piękos
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */


#include <Swiften/Whiteboard/WhiteboardSessionManager.h>

#include <Swiften/Base/foreach.h>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/bind.hpp>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/Whiteboard/WhiteboardResponder.h>
#include <Swiften/Presence/PresenceOracle.h>
#include "Swiften/Disco/EntityCapsProvider.h"

namespace Swift {
	WhiteboardSessionManager::WhiteboardSessionManager(IQRouter* router, PresenceOracle* presenceOracle, EntityCapsProvider* capsProvider) : router_(router), presenceOracle_(presenceOracle), capsProvider_(capsProvider) {
		responder = new WhiteboardResponder(this, router);
		responder->start();
	}

	WhiteboardSessionManager::~WhiteboardSessionManager() {
		responder->stop();
		delete responder;
	}

	WhiteboardSession::ref WhiteboardSessionManager::getSession(const JID& to) {
		if (sessions_.find(to.toBare()) == sessions_.end()) {
			return boost::shared_ptr<WhiteboardSession>();
		}
		return sessions_[to.toBare()];
	}

	OutgoingWhiteboardSession::ref WhiteboardSessionManager::createOutgoingSession(const JID& to) {
		JID fullJID = to;
		if (fullJID.isBare()) {
			fullJID = getFullJID(fullJID);
		}
		OutgoingWhiteboardSession::ref session = boost::make_shared<OutgoingWhiteboardSession>(fullJID, router_);
		sessions_[to.toBare()] = session;
		session->onSessionTerminateReceived.connect(boost::bind(&WhiteboardSessionManager::handleSessionTerminate, this, _1));
		session->onSessionCancelled.connect(boost::bind(&WhiteboardSessionManager::handleSessionCancel, this, _1));
		session->onRequestAccepted.connect(boost::bind(&WhiteboardSessionManager::handleSessionAccept, this, _1));
		session->onRequestRejected.connect(boost::bind(&WhiteboardSessionManager::handleRequestReject, this, _1));
		return session;
	}

	WhiteboardSession::ref WhiteboardSessionManager::requestSession(const JID& to) {
		WhiteboardSession::ref session = getSession(to);
		if (!session) {
			OutgoingWhiteboardSession::ref outgoingSession = createOutgoingSession(to);
			onSessionRequest(to, true);
			outgoingSession->startSession();
			return outgoingSession;
		} else {
			return session;
		}
	}

	void WhiteboardSessionManager::handleIncomingSession(IncomingWhiteboardSession::ref session) {
		sessions_[session->getTo().toBare()] = session;
		session->onSessionTerminateReceived.connect(boost::bind(&WhiteboardSessionManager::handleSessionTerminate, this, _1));
		session->onSessionCancelled.connect(boost::bind(&WhiteboardSessionManager::handleSessionCancel, this, _1));
		session->onRequestAccepted.connect(boost::bind(&WhiteboardSessionManager::handleSessionAccept, this, _1));
		onSessionRequest(session->getTo(), false);
	}

	JID WhiteboardSessionManager::getFullJID(const JID& bareJID) {
		JID fullReceipientJID;
		int priority = INT_MIN;
	
		//getAllPresence(bareJID) gives you all presences for the bare JID (i.e. all resources) Remko Tronçon @ 11:11
		std::vector<Presence::ref> presences = presenceOracle_->getAllPresence(bareJID);

		//iterate over them
		foreach(Presence::ref pres, presences) {
			if (pres->getPriority() > priority) {
				// look up caps from the jid
				DiscoInfo::ref info = capsProvider_->getCaps(pres->getFrom());
				if (info && info->hasFeature(DiscoInfo::WhiteboardFeature)) {
					priority = pres->getPriority();
					fullReceipientJID = pres->getFrom();
				}
			}
		}
	
		return fullReceipientJID;
	}

	void WhiteboardSessionManager::handleSessionTerminate(const JID& contact) {
		sessions_.erase(contact.toBare());
		onSessionTerminate(contact);
	}

	void WhiteboardSessionManager::handleSessionCancel(const JID& contact) {
		sessions_.erase(contact.toBare());
		onSessionTerminate(contact);
	}

	void WhiteboardSessionManager::handleSessionAccept(const JID& contact) {
		onRequestAccepted(contact);
	}

	void WhiteboardSessionManager::handleRequestReject(const JID& contact) {
		sessions_.erase(contact.toBare());
		onRequestRejected(contact);
	}
}