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

#include "Swiften/VCards/VCardManager.h"

#include <boost/bind.hpp>

#include "Swiften/JID/JID.h"
#include "Swiften/VCards/VCardStorage.h"
#include "Swiften/VCards/GetVCardRequest.h"

namespace Swift {

VCardManager::VCardManager(const JID& ownJID, IQRouter* iqRouter, VCardStorage* vcardStorage) : ownJID(ownJID), iqRouter(iqRouter), storage(vcardStorage) {
}

VCard::ref VCardManager::getVCard(const JID& jid) const {
	return storage->getVCard(jid);
}

VCard::ref VCardManager::getVCardAndRequestWhenNeeded(const JID& jid) {
	VCard::ref vcard = storage->getVCard(jid);
	if (!vcard) {
		requestVCard(jid);
	}
	return vcard;
}

void VCardManager::requestVCard(const JID& requestedJID) {
	JID jid = requestedJID.equals(ownJID, JID::WithoutResource) ? JID() : requestedJID;
	if (requestedVCards.find(jid) != requestedVCards.end()) {
		return;
	}
	GetVCardRequest::ref request = GetVCardRequest::create(jid, iqRouter);
	request->onResponse.connect(boost::bind(&VCardManager::handleVCardReceived, this, jid, _1, _2));
	request->send();
	requestedVCards.insert(jid);
}

void VCardManager::requestOwnVCard() {
	requestVCard(JID());
}


void VCardManager::handleVCardReceived(const JID& actualJID, VCard::ref vcard, ErrorPayload::ref error) {
	if (error) {
		vcard = VCard::ref(new VCard());
	}
	requestedVCards.erase(actualJID);
	JID jid = actualJID.isValid() ? actualJID : ownJID.toBare();
	storage->setVCard(jid, vcard);
	onVCardChanged(jid, vcard);
}

}