summaryrefslogtreecommitdiffstats
blob: 681e94b8adbca660b7bfed108768042b149eebb7 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (c) 2010-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.avatars;

import java.util.Map;
import java.util.HashMap;

import com.isode.stroke.avatars.AvatarProvider;
import com.isode.stroke.jid.JID;
import com.isode.stroke.elements.VCard;
import com.isode.stroke.elements.Presence;
import com.isode.stroke.elements.ErrorPayload;
import com.isode.stroke.elements.VCardUpdate;
import com.isode.stroke.client.StanzaChannel;
import com.isode.stroke.crypto.CryptoProvider;
import com.isode.stroke.stringcodecs.Hexify;
import com.isode.stroke.avatars.AvatarStorage;
import com.isode.stroke.base.ByteArray;
import com.isode.stroke.muc.MUCRegistry;
import com.isode.stroke.vcards.VCardManager;
import com.isode.stroke.signals.Slot2;
import com.isode.stroke.signals.Slot1;

import java.util.logging.Logger;

import com.isode.stroke.signals.SignalConnection;

public class VCardUpdateAvatarManager extends AvatarProvider {

	private VCardManager vcardManager_;
	private AvatarStorage avatarStorage_;
	private CryptoProvider crypto_;
	private MUCRegistry mucRegistry_;
	private final Map<JID, String> avatarHashes_ = new HashMap<JID, String>();
	private final SignalConnection onPresenceReceivedConnection;
	private final SignalConnection onAvailableChangedConnection;
	private final SignalConnection onVCardChangedConnection;
	private final Logger logger_ = Logger.getLogger(this.getClass().getName());

	public VCardUpdateAvatarManager(VCardManager vcardManager, StanzaChannel stanzaChannel, AvatarStorage avatarStorage, CryptoProvider crypto) {
		this(vcardManager, stanzaChannel, avatarStorage, crypto, null);
	}

	public VCardUpdateAvatarManager(VCardManager vcardManager, StanzaChannel stanzaChannel, AvatarStorage avatarStorage, CryptoProvider crypto, MUCRegistry mucRegistry) {
		this.vcardManager_ = vcardManager;
		this.avatarStorage_ = avatarStorage;
		this.crypto_ = crypto;
		this.mucRegistry_ = mucRegistry;
		onPresenceReceivedConnection = stanzaChannel.onPresenceReceived.connect(new Slot1<Presence>() {
			@Override
			public void call(Presence p1) {
				handlePresenceReceived(p1);
			}
		});
		onAvailableChangedConnection = stanzaChannel.onAvailableChanged.connect(new Slot1<Boolean>() {
			@Override
			public void call(Boolean b) {
				handleStanzaChannelAvailableChanged(b);
			}
		});
		onVCardChangedConnection = vcardManager_.onVCardChanged.connect(new Slot2<JID, VCard>() {
			@Override
			public void call(JID p1, VCard vcard) {
				handleVCardChanged(p1, vcard);
			}
		});
	}
	
	@Override
	public void delete() {
		onPresenceReceivedConnection.disconnect();
		onAvailableChangedConnection.disconnect();
		onVCardChangedConnection.disconnect();
	}

	@Override
	public String getAvatarHash(JID jid) {
		if(avatarHashes_.containsKey(jid)) {
			return avatarHashes_.get(jid);
		} else {
			return null;
		}
	}

	private void handlePresenceReceived(Presence presence) {
		VCardUpdate update = presence.getPayload(new VCardUpdate());
		if (update == null || presence.getPayload(new ErrorPayload()) != null) {
			return;
		}
		JID from = getAvatarJID(presence.getFrom());
		if (update.getPhotoHash().equals(getAvatarHash(from))) {
			return;
		}
		logger_.fine("Updated hash: " + from + "-> " + update.getPhotoHash() + "\n");
		if (avatarStorage_.hasAvatar(update.getPhotoHash())) {
			setAvatarHash(from, update.getPhotoHash());
		}
		else {
			vcardManager_.requestVCard(from);
		}
	}

	private void handleStanzaChannelAvailableChanged(boolean available) {
		if (available) {
			Map<JID, String> oldAvatarHashes = new HashMap<JID, String>();
			oldAvatarHashes.putAll(avatarHashes_);
			avatarHashes_.clear();
			for (Map.Entry<JID, String> entry : oldAvatarHashes.entrySet()) {
				onAvatarChanged.emit(entry.getKey());
			}
		}
	}

	private void handleVCardChanged(JID from, VCard vCard) {
		if (vCard == null) {
			logger_.fine("Missing element: " + from + ": null vcard payload\n");
			return;
		}

		final ByteArray photo = vCard.getPhoto();
		if (photo == null) {
			setAvatarHash(from, "");
		}
		else {
			String hash = Hexify.hexify(crypto_.getSHA1Hash(photo));
			if (!avatarStorage_.hasAvatar(hash)) {
				avatarStorage_.addAvatar(hash, photo);
			}
			setAvatarHash(from, hash);
		}
	}

	private void setAvatarHash(JID from, String hash) {
		logger_.fine("Updating hash: " + from + " -> " + hash + "\n");
		avatarHashes_.put(from, hash);
		onAvatarChanged.emit(from);
	}

	private JID getAvatarJID(JID jid) {
		JID bareFrom = jid.toBare();
		return (mucRegistry_ != null && mucRegistry_.isMUC(bareFrom)) ? jid : bareFrom;
	}
}