Presence.java

 1package eu.siacs.conversations.entities;
 2
 3import java.lang.Comparable;
 4import java.util.Locale;
 5
 6import eu.siacs.conversations.xml.Element;
 7
 8public class Presence implements Comparable {
 9
10	public enum Status {
11		CHAT, ONLINE, AWAY, XA, DND, OFFLINE;
12
13		public String toShowString() {
14			switch(this) {
15				case CHAT: return "chat";
16				case AWAY: return "away";
17				case XA:   return "xa";
18				case DND:  return "dnd";
19			}
20
21			return null;
22		}
23	}
24
25	protected final Status status;
26	protected ServiceDiscoveryResult disco;
27	protected final String ver;
28	protected final String hash;
29
30	private Presence(Status status, String ver, String hash) {
31		this.status = status;
32		this.ver = ver;
33		this.hash = hash;
34	}
35
36	public static Presence parse(String show, Element caps) {
37		final String hash = caps == null ? null : caps.getAttribute("hash");
38		final String ver = caps == null ? null : caps.getAttribute("ver");
39		if (show == null) {
40			return new Presence(Status.ONLINE, ver, hash);
41		} else {
42			switch (show.toLowerCase(Locale.US)) {
43				case "away":
44					return new Presence(Status.AWAY, ver, hash);
45				case "xa":
46					return new Presence(Status.XA, ver, hash);
47				case "dnd":
48					return new Presence(Status.DND, ver, hash);
49				case "chat":
50					return new Presence(Status.CHAT, ver, hash);
51				default:
52					return new Presence(Status.ONLINE, ver, hash);
53			}
54		}
55	}
56
57	public int compareTo(Object other) {
58		return this.status.compareTo(((Presence)other).status);
59	}
60
61	public Status getStatus() {
62		return this.status;
63	}
64
65	public boolean hasCaps() {
66		return ver != null && hash != null;
67	}
68
69	public String getVer() {
70		return this.ver;
71	}
72
73	public String getHash() {
74		return this.hash;
75	}
76
77	public void setServiceDiscoveryResult(ServiceDiscoveryResult disco) {
78		this.disco = disco;
79	}
80}