aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/entities/Presence.java
blob: 69cde83278f1e655f688b922a52c09a875c97695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package eu.siacs.conversations.entities;

import java.lang.Comparable;

import eu.siacs.conversations.xml.Element;

public class Presence implements Comparable {

	public enum Status {
		CHAT, ONLINE, AWAY, XA, DND, OFFLINE;

		public String toShowString() {
			switch(this) {
				case CHAT: return "chat";
				case AWAY: return "away";
				case XA:   return "xa";
				case DND:  return "dnd";
			}

			return null;
		}
	}

	protected final Status status;
	protected ServiceDiscoveryResult disco;
	protected final String ver;
	protected final String hash;

	private Presence(Status status, String ver, String hash) {
		this.status = status;
		this.ver = ver;
		this.hash = hash;
	}

	public static Presence parse(Element show, Element caps) {
		final String hash = caps == null ? null : caps.getAttribute("hash");
		final String ver = caps == null ? null : caps.getAttribute("ver");
		if ((show == null) || (show.getContent() == null)) {
			return new Presence(Status.ONLINE, ver, hash);
		} else if (show.getContent().equals("away")) {
			return new Presence(Status.AWAY, ver, hash);
		} else if (show.getContent().equals("xa")) {
			return new Presence(Status.XA, ver, hash);
		} else if (show.getContent().equals("chat")) {
			return new Presence(Status.CHAT, ver, hash);
		} else if (show.getContent().equals("dnd")) {
			return new Presence(Status.DND, ver, hash);
		} else {
			return new Presence(Status.OFFLINE, ver, hash);
		}
	}

	public int compareTo(Object other) {
		return this.status.compareTo(((Presence)other).status);
	}

	public Status getStatus() {
		return this.status;
	}

	public boolean hasCaps() {
		return ver != null && hash != null;
	}

	public String getVer() {
		return this.ver;
	}

	public String getHash() {
		return this.hash;
	}

	public void setServiceDiscoveryResult(ServiceDiscoveryResult disco) {
		this.disco = disco;
	}
}