diff options
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/isode/stroke/vcards/VCardManager.java | 19 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/VCardMemoryStorage.java | 14 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/VCardStorage.java | 2 |
3 files changed, 30 insertions, 5 deletions
diff --git a/src/com/isode/stroke/vcards/VCardManager.java b/src/com/isode/stroke/vcards/VCardManager.java index 2a7837c..a5b62e8 100644 --- a/src/com/isode/stroke/vcards/VCardManager.java +++ b/src/com/isode/stroke/vcards/VCardManager.java @@ -14,12 +14,14 @@ 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 final JID ownJID; - private final IQRouter iqRouter; - private final VCardStorage storage; - private final Set<JID> requestedVCards = new HashSet<JID>(); + private JID ownJID = new JID(); + private IQRouter iqRouter; + private VCardStorage storage; + private Set<JID> requestedVCards = new HashSet<JID>(); /** * The JID will always be bare. @@ -37,6 +39,7 @@ public class VCardManager { this.ownJID = ownJID; this.iqRouter = iqRouter; this.storage = vcardStorage; + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); } public void delete() { @@ -47,8 +50,14 @@ public class VCardManager { } public VCard getVCardAndRequestWhenNeeded(final JID jid) { + return getVCardAndRequestWhenNeeded(jid, null); + } + + public VCard getVCardAndRequestWhenNeeded(final JID jid, final Date allowedAge) { VCard vcard = storage.getVCard(jid); - if (vcard == null) { + 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; diff --git a/src/com/isode/stroke/vcards/VCardMemoryStorage.java b/src/com/isode/stroke/vcards/VCardMemoryStorage.java index d666d98..24ca6f4 100644 --- a/src/com/isode/stroke/vcards/VCardMemoryStorage.java +++ b/src/com/isode/stroke/vcards/VCardMemoryStorage.java @@ -10,22 +10,36 @@ import java.util.Map; import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.elements.VCard; import com.isode.stroke.jid.JID; +import java.util.Date; +import java.util.TimeZone; public class VCardMemoryStorage extends VCardStorage { public VCardMemoryStorage(CryptoProvider crypto) { super(crypto); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); } private Map<JID, VCard> vcards = new HashMap<JID, VCard>(); + private Map<JID, Date> vcardWriteTimes = new HashMap<JID, Date>(); @Override public VCard getVCard(JID jid) { return vcards.get(jid); } + /** + * @param JID jid. + * @return Date, May be Null. + */ + @Override + public Date getVCardWriteTime(final JID jid) { + return vcardWriteTimes.get(jid); + } + @Override public void setVCard(JID jid, VCard vcard) { vcards.put(jid, vcard); + vcardWriteTimes.put(jid, new Date()); } } diff --git a/src/com/isode/stroke/vcards/VCardStorage.java b/src/com/isode/stroke/vcards/VCardStorage.java index 9021599..cb732ea 100644 --- a/src/com/isode/stroke/vcards/VCardStorage.java +++ b/src/com/isode/stroke/vcards/VCardStorage.java @@ -8,11 +8,13 @@ import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.elements.VCard; import com.isode.stroke.jid.JID; import com.isode.stroke.stringcodecs.Hexify; +import java.util.Date; public abstract class VCardStorage { private CryptoProvider crypto; public abstract VCard getVCard(JID jid); + public abstract Date getVCardWriteTime(JID jid); public abstract void setVCard(JID jid, VCard vcard); public VCardStorage(CryptoProvider crypto) { |