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 node;
48 private final String message;
49
50 private Presence(Status status, String ver, String hash, String node, String message) {
51 this.status = status;
52 this.ver = ver;
53 this.hash = hash;
54 this.node = node;
55 this.message = message;
56 }
57
58 public static Presence parse(String show, Element caps, String message) {
59 final String hash = caps == null ? null : caps.getAttribute("hash");
60 final String ver = caps == null ? null : caps.getAttribute("ver");
61 final String node = caps == null ? null : caps.getAttribute("node");
62 return new Presence(Status.fromShowString(show), ver, hash, node, message);
63 }
64
65 public int compareTo(Object other) {
66 return this.status.compareTo(((Presence)other).status);
67 }
68
69 public Status getStatus() {
70 return this.status;
71 }
72
73 public boolean hasCaps() {
74 return ver != null && hash != null;
75 }
76
77 public String getVer() {
78 return this.ver;
79 }
80
81 public String getNode() {
82 return this.node;
83 }
84
85 public String getHash() {
86 return this.hash;
87 }
88
89 public String getMessage() {
90 return this.message;
91 }
92
93 public void setServiceDiscoveryResult(ServiceDiscoveryResult disco) {
94 this.disco = disco;
95 }
96
97 public ServiceDiscoveryResult getServiceDiscoveryResult() {
98 return disco;
99 }
100}