1package eu.siacs.conversations.parser;
2
3
4import java.text.ParseException;
5import java.text.SimpleDateFormat;
6import java.util.Date;
7import java.util.Locale;
8
9import eu.siacs.conversations.entities.Account;
10import eu.siacs.conversations.entities.Contact;
11import eu.siacs.conversations.services.XmppConnectionService;
12import eu.siacs.conversations.xml.Element;
13import eu.siacs.conversations.xmpp.jid.Jid;
14import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
15
16public abstract class AbstractParser {
17
18 protected XmppConnectionService mXmppConnectionService;
19
20 protected AbstractParser(XmppConnectionService service) {
21 this.mXmppConnectionService = service;
22 }
23
24 public static Long getTimestamp(Element element, Long defaultValue) {
25 Element delay = element.findChild("delay","urn:xmpp:delay");
26 if (delay != null) {
27 String stamp = delay.getAttribute("stamp");
28 if (stamp != null) {
29 try {
30 return AbstractParser.parseTimestamp(delay.getAttribute("stamp")).getTime();
31 } catch (ParseException e) {
32 return defaultValue;
33 }
34 }
35 }
36 return defaultValue;
37 }
38
39 protected long getTimestamp(Element packet) {
40 return getTimestamp(packet,System.currentTimeMillis());
41 }
42
43 public static Date parseTimestamp(String timestamp) throws ParseException {
44 timestamp = timestamp.replace("Z", "+0000");
45 SimpleDateFormat dateFormat;
46 timestamp = timestamp.substring(0,19)+timestamp.substring(timestamp.length() -5,timestamp.length());
47 dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US);
48 return dateFormat.parse(timestamp);
49 }
50
51 protected void updateLastseen(long timestamp, final Account account, final Jid from) {
52 final String presence = from == null || from.isBareJid() ? "" : from.getResourcepart();
53 final Contact contact = account.getRoster().getContact(from);
54 if (timestamp >= contact.lastseen.time) {
55 contact.lastseen.time = timestamp;
56 if (!presence.isEmpty()) {
57 contact.lastseen.presence = presence;
58 }
59 }
60 }
61
62 protected String avatarData(Element items) {
63 Element item = items.findChild("item");
64 if (item == null) {
65 return null;
66 }
67 return item.findChildContent("data", "urn:xmpp:avatar:data");
68 }
69}