Added presence types to support all possibilities.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1793 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2003-01-16 15:41:42 +00:00 committed by mtucker
parent 15cb5768ae
commit e83cc69730
1 changed files with 76 additions and 42 deletions

View File

@ -55,8 +55,8 @@ package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
/** /**
* Represents XMPP presence packets. Each packet will either be an "available" or * Represents XMPP presence packets. Every presence packet has a type, while a
* "unavailable" message. A number of attributes are optional: * number of attributes are optional:
* <ul> * <ul>
* <li>Status -- free-form text describing a user's presence (i.e., gone to lunch). * <li>Status -- free-form text describing a user's presence (i.e., gone to lunch).
* <li>Priority -- non-negative numerical priority of a sender's resource. The * <li>Priority -- non-negative numerical priority of a sender's resource. The
@ -66,21 +66,16 @@ import org.jivesoftware.smack.*;
* dnd (do not disturb). * dnd (do not disturb).
* </ul> * </ul>
* *
* Note: XMPP presence packets are also used to subscribe and unsubscribe users from * Presence packets are used for two purposes. First, to notify the server of our
* rosters. However, that functionality is controlled by the Roster object rather * the clients current presence status. Second, they are used to subscribe and
* than Presence objects. * unsubscribe users from the roster.
* *
* @see Roster
* @author Matt Tucker * @author Matt Tucker
*/ */
public class Presence extends Packet { public class Presence extends Packet {
public static final Mode CHAT = new Mode("chat"); private Type type = Type.AVAILABLE;
public static final Mode AWAY = new Mode("away");
public static final Mode EXTENDED_AWAY = new Mode("xa");
public static final Mode DO_NOT_DISTURB = new Mode("dnd");
public static final Mode INVISIBLE = new Mode("invisible");
private boolean available = true;
private String status = null; private String status = null;
private int priority = -1; private int priority = -1;
private Mode mode = null; private Mode mode = null;
@ -88,46 +83,33 @@ public class Presence extends Packet {
/** /**
* Creates a new presence update. Status, priority, and mode are left un-set. * Creates a new presence update. Status, priority, and mode are left un-set.
* *
* @param available true if this is an "available" presence packet, or false for * @param type the type.
* "unavailable".
*/ */
public Presence(boolean available) { public Presence(Type type) {
this.available = available; this.type = type;
} }
/** /**
* Creates a new presence update with a specified status, priority, and mode. * Creates a new presence update with a specified status, priority, and mode.
* *
* @param available true if this is an "available" presence update, or false for * @param type the type.
* "unavailable".
* @param status a text message describing the presence update. * @param status a text message describing the presence update.
* @param priority the priority of this presence update. * @param priority the priority of this presence update.
* @param mode the mode type for this presence update. * @param mode the mode type for this presence update.
*/ */
public Presence(boolean available, String status, int priority, Mode mode) { public Presence(Type type, String status, int priority, Mode mode) {
this.available = available; this.type = type;
this.status = status; this.status = status;
this.priority = priority; this.priority = priority;
this.mode = mode; this.mode = mode;
} }
/** public Type getType() {
* Returns true if this is an "available" presence update and false if "unavailable". return type;
*
* @return true if an "available" presence update, false otherwise.
*/
public boolean isAvailable() {
return available;
} }
/** public void setType(Type type) {
* Toggles the presence update between "available" and "unavailable". this.type = type;
*
* @param available true to make this an "available" presence update, or false
* for "unavailable".
*/
public void setAvailable(boolean available) {
this.available = available;
} }
/** /**
@ -199,12 +181,7 @@ public class Presence extends Packet {
if (getFrom() != null) { if (getFrom() != null) {
buf.append("from=\"").append(getFrom()).append("\" "); buf.append("from=\"").append(getFrom()).append("\" ");
} }
if (available) { buf.append("type=\"").append(type).append("\">");
buf.append("type=\"available\">");
}
else {
buf.append("type=\"unavailable\">");
}
if (status != null) { if (status != null) {
buf.append("<status>").append(status).append("</status>"); buf.append("<status>").append(status).append("</status>");
} }
@ -218,11 +195,68 @@ public class Presence extends Packet {
return buf.toString(); return buf.toString();
} }
/**
* A typsafe enum class to represent the presecence type.
*/
public static class Type {
public static final Type AVAILABLE = new Type("available");
public static final Type UNAVAILABLE = new Type("unavailable");
public static final Type SUBSCRIBE = new Type("subscribe");
public static final Type SUBSCRIBED = new Type("subscribed");
public static final Type UNSUBSCRIBE = new Type("unsubscribe");
public static final Type UNSUBSCRIBED = new Type("unsubscribed");
private String value;
private Type(String value) {
this.value = value;
}
public String toString() {
return value;
}
/**
* Returns the type constant associated with the String value.
*/
public static Type fromString(String value) {
if ("available".equals(value)) {
return AVAILABLE;
}
else if ("unavailable".equals(value)) {
return UNAVAILABLE;
}
else if ("subscribe".equals(value)) {
return SUBSCRIBE;
}
else if ("subscribed".equals(value)) {
return SUBSCRIBED;
}
else if ("unsubscribe".equals(value)) {
return UNSUBSCRIBE;
}
else if ("unsubscribed".equals(value)) {
return UNSUBSCRIBED;
}
// Default to available.
else {
return AVAILABLE;
}
}
}
/** /**
* A typsafe enum class to represent the presecence mode. * A typsafe enum class to represent the presecence mode.
*/ */
public static class Mode { public static class Mode {
public static final Mode CHAT = new Mode("chat");
public static final Mode AWAY = new Mode("away");
public static final Mode EXTENDED_AWAY = new Mode("xa");
public static final Mode DO_NOT_DISTURB = new Mode("dnd");
public static final Mode INVISIBLE = new Mode("invisible");
private String value; private String value;
private Mode(String value) { private Mode(String value) {
@ -234,7 +268,7 @@ public class Presence extends Packet {
} }
/** /**
* Returns the Mode constant * Returns the mode constant associated with the String value.
*/ */
public static Mode fromString(String value) { public static Mode fromString(String value) {
if (value == null) { if (value == null) {