1package eu.siacs.conversations.entities;
2
3import java.util.Hashtable;
4import java.util.Iterator;
5import java.util.Map.Entry;
6
7import eu.siacs.conversations.xml.Element;
8
9public class Presences {
10
11 public static final int CHAT = -1;
12 public static final int ONLINE = 0;
13 public static final int AWAY = 1;
14 public static final int XA = 2;
15 public static final int DND = 3;
16 public static final int OFFLINE = 4;
17
18 private Hashtable<String, Integer> presences = new Hashtable<String, Integer>();
19
20 public Hashtable<String, Integer> getPresences() {
21 return this.presences;
22 }
23
24 public void updatePresence(String resource, int status) {
25 this.presences.put(resource, status);
26 }
27
28 public void removePresence(String resource) {
29 this.presences.remove(resource);
30 }
31
32 public void clearPresences() {
33 this.presences.clear();
34 }
35
36 public int getMostAvailableStatus() {
37 int status = OFFLINE;
38 Iterator<Entry<String, Integer>> it = presences.entrySet().iterator();
39 while (it.hasNext()) {
40 Entry<String, Integer> entry = it.next();
41 if (entry.getValue()<status) status = entry.getValue();
42 }
43 return status;
44 }
45
46 public static int parseShow(Element show) {
47 if ((show == null)||(show.getContent() == null)) {
48 return Presences.ONLINE;
49 } else if (show.getContent().equals("away")) {
50 return Presences.AWAY;
51 } else if (show.getContent().equals("xa")) {
52 return Presences.XA;
53 } else if (show.getContent().equals("chat")) {
54 return Presences.CHAT;
55 } else if (show.getContent().equals("dnd")) {
56 return Presences.DND;
57 } else {
58 return Presences.OFFLINE;
59 }
60 }
61
62 public int size() {
63 return presences.size();
64 }
65
66 public String[] asStringArray() {
67 final String[] presencesArray = new String[presences.size()];
68 presences.keySet().toArray(presencesArray);
69 return presencesArray;
70 }
71
72 public boolean has(String presence) {
73 return presences.containsKey(presence);
74 }
75}