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