/* * Copyright (c) 2012 Mateusz Piękos * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include #include #include #include #include #include #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(); } 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(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)); 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 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); } }