summaryrefslogtreecommitdiffstats
blob: 909ef8ea4043dd70a7125cae25dc498d304bf82b (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
/*
 * Copyright (c) 2010 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "Swiften/Roster/XMPPRosterController.h"

#include <boost/bind.hpp>

#include "Swiften/Base/foreach.h"
#include "Swiften/Elements/RosterItemPayload.h"
#include "Swiften/Queries/IQRouter.h"
#include "Swiften/Roster/GetRosterRequest.h"
#include "Swiften/Roster/Roster.h"
#include "Swiften/Roster/SetPresence.h"
#include "Swiften/Roster/OfflineRosterFilter.h"
#include "Swiften/Roster/XMPPRosterImpl.h"

namespace Swift {
	
/**
 * The controller does not gain ownership of these parameters.
 */
XMPPRosterController::XMPPRosterController(IQRouter* iqRouter, XMPPRosterImpl* xmppRoster) : iqRouter_(iqRouter), rosterPushResponder_(iqRouter), xmppRoster_(xmppRoster) {
	rosterPushResponder_.onRosterReceived.connect(boost::bind(&XMPPRosterController::handleRosterReceived, this, _1));
	rosterPushResponder_.start();
}

XMPPRosterController::~XMPPRosterController() {
	rosterPushResponder_.stop();
}

void XMPPRosterController::requestRoster() {
	xmppRoster_->clear();
	GetRosterRequest::ref rosterRequest = GetRosterRequest::create(iqRouter_);
	rosterRequest->onResponse.connect(boost::bind(&XMPPRosterController::handleRosterReceived, this, _1));
	rosterRequest->send();
}

void XMPPRosterController::handleRosterReceived(boost::shared_ptr<RosterPayload> rosterPayload) {
	if (rosterPayload) {
		foreach(const RosterItemPayload& item, rosterPayload->getItems()) {
			//Don't worry about the updated case, the XMPPRoster sorts that out.
			if (item.getSubscription() == RosterItemPayload::Remove) {
				xmppRoster_->removeContact(item.getJID());
			} else {
				xmppRoster_->addContact(item.getJID(), item.getName(), item.getGroups(), item.getSubscription());
			}
		}
	}
}

}