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			return null;
21		}
22
23		public static Status fromShowString(String show) {
24			if (show == null) {
25				return ONLINE;
26			} else {
27				switch (show.toLowerCase(Locale.US)) {
28					case "away":
29						return AWAY;
30					case "xa":
31						return XA;
32					case "dnd":
33						return DND;
34					case "chat":
35						return CHAT;
36					default:
37						return ONLINE;
38				}
39			}
40		}
41	}
42
43	private final Status status;
44	private ServiceDiscoveryResult disco;
45	private final String ver;
46	private final String hash;
47	private final String message;
48
49	private Presence(Status status, String ver, String hash, String message) {
50		this.status = status;
51		this.ver = ver;
52		this.hash = hash;
53		this.message = message;
54	}
55
56	public static Presence parse(String show, Element caps, String message) {
57		final String hash = caps == null ? null : caps.getAttribute("hash");
58		final String ver = caps == null ? null : caps.getAttribute("ver");
59		return new Presence(Status.fromShowString(show), ver, hash, message);
60	}
61
62	public int compareTo(Object other) {
63		return this.status.compareTo(((Presence)other).status);
64	}
65
66	public Status getStatus() {
67		return this.status;
68	}
69
70	public boolean hasCaps() {
71		return ver != null && hash != null;
72	}
73
74	public String getVer() {
75		return this.ver;
76	}
77
78	public String getHash() {
79		return this.hash;
80	}
81
82	public String getMessage() {
83		return this.message;
84	}
85
86	public void setServiceDiscoveryResult(ServiceDiscoveryResult disco) {
87		this.disco = disco;
88	}
89
90	public ServiceDiscoveryResult getServiceDiscoveryResult() {
91		return disco;
92	}
93}