summaryrefslogtreecommitdiffstats
blob: f0b04b94a0b8f0c9c8a21a886756881a4709089d (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
#include "Swiften/Avatars/AvatarManager.h"

#include <boost/bind.hpp>

#include "Swiften/Client/StanzaChannel.h"
#include "Swiften/Elements/VCardUpdate.h"
#include "Swiften/Queries/Requests/GetVCardRequest.h"
#include "Swiften/StringCodecs/SHA1.h"
#include "Swiften/Avatars/AvatarStorage.h"

namespace Swift {

AvatarManager::AvatarManager(StanzaChannel* stanzaChannel, IQRouter* iqRouter, AvatarStorage* avatarStorage) : stanzaChannel_(stanzaChannel), iqRouter_(iqRouter), avatarStorage_(avatarStorage) {
	stanzaChannel->onPresenceReceived.connect(boost::bind(&AvatarManager::handlePresenceReceived, this, _1));
}

void AvatarManager::handlePresenceReceived(boost::shared_ptr<Presence> presence) {
	boost::shared_ptr<VCardUpdate> update = presence->getPayload<VCardUpdate>();
	if (!update) {
		return;
	}
	JID from = presence->getFrom().toBare();
	String& hash = avatarHashes_[from];
	if (hash != update->getPhotoHash()) {
		hash = update->getPhotoHash();
		if (!avatarStorage_->hasAvatar(hash)) {
			boost::shared_ptr<GetVCardRequest> request(new GetVCardRequest(from, iqRouter_));
			request->onResponse.connect(boost::bind(&AvatarManager::handleVCardReceived, this, from, _1, _2));
			request->send();
		}
		else {
			onAvatarChanged(from, hash);
		}
	}
}

void AvatarManager::handleVCardReceived(JID from, boost::shared_ptr<VCard> vCard, const boost::optional<Error>& error) {
	if (error) {
		// FIXME: What to do here?
		return;
	}
	String hash = SHA1::getHexHash(vCard->getPhoto());
	avatarStorage_->addAvatar(hash, vCard->getPhoto());
	onAvatarChanged(from, hash);
}

String AvatarManager::getAvatarHash(const JID& jid) const {
	std::map<JID, String>::const_iterator i = avatarHashes_.find(jid.toBare());
	if (i != avatarHashes_.end()) {
		return i->second;
	}
	else {
		return "";
	}
}

boost::filesystem::path AvatarManager::getAvatarPath(const JID& jid) const {
	String hash = getAvatarHash(jid);
	if (!hash.isEmpty()) {
		return avatarStorage_->getAvatarPath(hash);
	}
	return boost::filesystem::path();
}

}