summaryrefslogtreecommitdiffstats
blob: 251b787ac7921e45586a99c61933547557a559a0 (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
/*
 * Copyright (c) 2010-2016, 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;
import java.util.Date;
import java.util.TimeZone;

public class VCardManager {
    private JID ownJID = new JID();
    private IQRouter iqRouter;
    private VCardStorage storage;
    private Set<JID> requestedVCards = new HashSet<JID>();

    /**
     * The JID will always be bare.
     */
    public final Signal2<JID, VCard> onVCardChanged = new Signal2<JID, VCard>();

    /**
     * Emitted when we received an error on looking up a vCard.
     */
    public final Signal2<JID,ErrorPayload> onVCardRetrievalError = new Signal2<JID,ErrorPayload>();
    
    /**
     * 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;
    }

    public void delete() {
    }

    public VCard getVCard(final JID jid) {
        return storage.getVCard(jid);
    }

    public VCard getVCardAndRequestWhenNeeded(final JID jid) {
        return getVCardAndRequestWhenNeeded(jid, null);
    }

    public VCard getVCardAndRequestWhenNeeded(final JID jid, final Date allowedAge) {
        VCard vcard = storage.getVCard(jid);
        Date vcardFetchedTime = storage.getVCardWriteTime(jid);
        boolean vcardTooOld = (vcard != null) && (vcardFetchedTime == null || (allowedAge != null && ((new Date().getTime() - vcardFetchedTime.getTime()) > allowedAge.getTime())));
        if (vcard == null || vcardTooOld) {
            requestVCard(jid);
        }
        return vcard;
    }

    public 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());
    }

    private void handleVCardReceived(final JID actualJID, VCard vcard, ErrorPayload error) {
        if (error == null) {
            if (vcard == null) {
                vcard = new VCard();
            }
            requestedVCards.remove(actualJID);
            JID jid = actualJID.isValid() ? actualJID : ownJID.toBare();
            setVCard(jid, vcard);
        }
        else {
            onVCardRetrievalError.emit(actualJID, error);
        }
    }

    public 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;
    }

    private void handleSetVCardResponse(VCard vcard, ErrorPayload error) {
        if (error == null) {
            setVCard(ownJID.toBare(), vcard);
        }
    }

    private 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);
        }
    }

	public String getPhotoHash(final JID jid) {
		return storage.getPhotoHash(jid);
	}
}