2016-01-17 21:28:38 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
|
|
|
import java.lang.Comparable;
|
2016-02-19 10:09:28 +00:00
|
|
|
import java.util.Locale;
|
2016-01-17 21:28:38 +00:00
|
|
|
|
|
|
|
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;
|
2016-02-03 09:40:02 +00:00
|
|
|
protected ServiceDiscoveryResult disco;
|
|
|
|
protected final String ver;
|
|
|
|
protected final String hash;
|
2016-01-17 21:46:32 +00:00
|
|
|
|
2016-02-03 09:40:02 +00:00
|
|
|
private Presence(Status status, String ver, String hash) {
|
|
|
|
this.status = status;
|
|
|
|
this.ver = ver;
|
|
|
|
this.hash = hash;
|
|
|
|
}
|
2016-01-17 21:28:38 +00:00
|
|
|
|
2016-02-19 10:09:28 +00:00
|
|
|
public static Presence parse(String show, Element caps) {
|
2016-02-03 09:40:02 +00:00
|
|
|
final String hash = caps == null ? null : caps.getAttribute("hash");
|
|
|
|
final String ver = caps == null ? null : caps.getAttribute("ver");
|
2016-02-19 10:09:28 +00:00
|
|
|
if (show == null) {
|
2016-02-03 09:40:02 +00:00
|
|
|
return new Presence(Status.ONLINE, ver, hash);
|
2016-01-17 21:28:38 +00:00
|
|
|
} else {
|
2016-02-19 10:09:28 +00:00
|
|
|
switch (show.toLowerCase(Locale.US)) {
|
|
|
|
case "away":
|
|
|
|
return new Presence(Status.AWAY, ver, hash);
|
|
|
|
case "xa":
|
|
|
|
return new Presence(Status.XA, ver, hash);
|
|
|
|
case "dnd":
|
|
|
|
return new Presence(Status.DND, ver, hash);
|
|
|
|
case "chat":
|
|
|
|
return new Presence(Status.CHAT, ver, hash);
|
|
|
|
default:
|
|
|
|
return new Presence(Status.ONLINE, ver, hash);
|
|
|
|
}
|
2016-01-17 21:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int compareTo(Object other) {
|
|
|
|
return this.status.compareTo(((Presence)other).status);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Status getStatus() {
|
|
|
|
return this.status;
|
|
|
|
}
|
2016-02-03 09:40:02 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-01-17 21:28:38 +00:00
|
|
|
}
|