diff options
Diffstat (limited to 'src/com/isode/stroke/vcards')
-rw-r--r-- | src/com/isode/stroke/vcards/GetVCardRequest.java | 22 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/SetVCardRequest.java | 23 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/VCardManager.java | 115 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/VCardMemoryStorage.java | 31 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/VCardStorage.java | 33 |
5 files changed, 224 insertions, 0 deletions
diff --git a/src/com/isode/stroke/vcards/GetVCardRequest.java b/src/com/isode/stroke/vcards/GetVCardRequest.java new file mode 100644 index 0000000..e563106 --- /dev/null +++ b/src/com/isode/stroke/vcards/GetVCardRequest.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2010-2015, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.vcards; + +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.VCard; +import com.isode.stroke.jid.JID; +import com.isode.stroke.queries.GenericRequest; +import com.isode.stroke.queries.IQRouter; + +public class GetVCardRequest extends GenericRequest<VCard> { + public static GetVCardRequest create(final JID jid, IQRouter router) { + return new GetVCardRequest(jid, router); + } + + private GetVCardRequest(final JID jid, IQRouter router) { + super(IQ.Type.Get, jid, new VCard(), router); + } + +} diff --git a/src/com/isode/stroke/vcards/SetVCardRequest.java b/src/com/isode/stroke/vcards/SetVCardRequest.java new file mode 100644 index 0000000..85b908f --- /dev/null +++ b/src/com/isode/stroke/vcards/SetVCardRequest.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2010-2015, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.vcards; + +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.VCard; +import com.isode.stroke.jid.JID; +import com.isode.stroke.queries.GenericRequest; +import com.isode.stroke.queries.IQRouter; + +public class SetVCardRequest extends GenericRequest<VCard> { + + public static SetVCardRequest create(VCard vcard, IQRouter router) { + return new SetVCardRequest(vcard, router); + } + + private SetVCardRequest(VCard vcard, IQRouter router) { + super(IQ.Type.Set, new JID(), vcard, router); + } + +} diff --git a/src/com/isode/stroke/vcards/VCardManager.java b/src/com/isode/stroke/vcards/VCardManager.java new file mode 100644 index 0000000..593d244 --- /dev/null +++ b/src/com/isode/stroke/vcards/VCardManager.java @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2010-2015, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.vcards; + +import java.util.HashSet; +import java.util.Set; + +import com.isode.stroke.elements.ErrorPayload; +import com.isode.stroke.elements.VCard; +import com.isode.stroke.jid.JID; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.signals.Signal1; +import com.isode.stroke.signals.Signal2; +import com.isode.stroke.signals.Slot2; + +public class VCardManager { + private final JID ownJID; + private final IQRouter iqRouter; + private final VCardStorage storage; + private final Set<JID> requestedVCards = new HashSet<JID>(); + + /** + * The JID will always be bare. + */ + public final Signal2<JID, VCard> onVCardChanged = new Signal2<JID, VCard>(); + + /** + * Emitted when our own vcard changes. + * + * onVCardChanged will also be emitted. + */ + public final Signal1<VCard> onOwnVCardChanged = new Signal1<VCard>(); + + public VCardManager(final JID ownJID, IQRouter iqRouter, VCardStorage vcardStorage) { + this.ownJID = ownJID; + this.iqRouter = iqRouter; + this.storage = vcardStorage; + } + + void delete() { + } + + public VCard getVCard(final JID jid) { + return storage.getVCard(jid); + } + + public VCard getVCardAndRequestWhenNeeded(final JID jid) { + VCard vcard = storage.getVCard(jid); + if (vcard == null) { + requestVCard(jid); + } + return vcard; + } + + void requestVCard(final JID requestedJID) { + final JID jid = requestedJID.compare(ownJID, JID.CompareType.WithoutResource) == 0 ? new JID() : requestedJID; + if (requestedVCards.contains(jid)) { + return; + } + GetVCardRequest request = GetVCardRequest.create(jid, iqRouter); + request.onResponse.connect(new Slot2<VCard, ErrorPayload>() { + @Override + public void call(VCard p1, ErrorPayload p2) { + handleVCardReceived(jid, p1, p2); + } + }); + request.send(); + requestedVCards.add(jid); + } + + public void requestOwnVCard() { + requestVCard(new JID()); + } + + + void handleVCardReceived(final JID actualJID, VCard vcard, ErrorPayload error) { + if (error != null || vcard == null) { + vcard = new VCard(); + } + requestedVCards.remove(actualJID); + JID jid = actualJID.isValid() ? actualJID : ownJID.toBare(); + setVCard(jid, vcard); + } + + SetVCardRequest createSetVCardRequest(final VCard vcard) { + SetVCardRequest request = SetVCardRequest.create(vcard, iqRouter); + request.onResponse.connect(new Slot2<VCard, ErrorPayload>() { + @Override + public void call(VCard p1, ErrorPayload p2) { + handleSetVCardResponse(vcard, p2); + } + }); + return request; + } + + void handleSetVCardResponse(VCard vcard, ErrorPayload error) { + if (error == null) { + setVCard(ownJID.toBare(), vcard); + } + } + + void setVCard(final JID jid, VCard vcard) { + storage.setVCard(jid, vcard); + onVCardChanged.emit(jid, vcard); + if (jid.compare(ownJID, JID.CompareType.WithoutResource) == 0) { + onOwnVCardChanged.emit(vcard); + } + } + +// String getPhotoHash(final JID jid) { +// return storage.getPhotoHash(jid); +// } +} diff --git a/src/com/isode/stroke/vcards/VCardMemoryStorage.java b/src/com/isode/stroke/vcards/VCardMemoryStorage.java new file mode 100644 index 0000000..d666d98 --- /dev/null +++ b/src/com/isode/stroke/vcards/VCardMemoryStorage.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2010-2015, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.vcards; + +import java.util.HashMap; +import java.util.Map; + +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.elements.VCard; +import com.isode.stroke.jid.JID; + +public class VCardMemoryStorage extends VCardStorage { + public VCardMemoryStorage(CryptoProvider crypto) { + super(crypto); + } + + private Map<JID, VCard> vcards = new HashMap<JID, VCard>(); + + @Override + public VCard getVCard(JID jid) { + return vcards.get(jid); + } + + @Override + public void setVCard(JID jid, VCard vcard) { + vcards.put(jid, vcard); + } + +} diff --git a/src/com/isode/stroke/vcards/VCardStorage.java b/src/com/isode/stroke/vcards/VCardStorage.java new file mode 100644 index 0000000..9021599 --- /dev/null +++ b/src/com/isode/stroke/vcards/VCardStorage.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2010-2015, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.vcards; + +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.elements.VCard; +import com.isode.stroke.jid.JID; +import com.isode.stroke.stringcodecs.Hexify; + +public abstract class VCardStorage { + private CryptoProvider crypto; + + public abstract VCard getVCard(JID jid); + public abstract void setVCard(JID jid, VCard vcard); + + public VCardStorage(CryptoProvider crypto) { + this.crypto = crypto; + } + + public void delete() {}; + + public String getPhotoHash(final JID jid) { + VCard vCard = getVCard(jid); + if (vCard != null && vCard.getPhoto().getSize() != 0) { + return Hexify.hexify(crypto.getSHA1Hash(vCard.getPhoto())); + } + else { + return ""; + } + } +} |