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