KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > packet > Presence


1 /**
2  * $RCSfile$
3  * $Revision: 2468 $
4  * $Date: 2005-03-28 13:54:49 -0300 (Mon, 28 Mar 2005) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smack.packet;
22
23 import org.jivesoftware.smack.util.StringUtils;
24
25 /**
26  * Represents XMPP presence packets. Every presence packet has a type, which is one of
27  * the following values:
28  * <ul>
29  * <li><tt>Presence.Type.AVAILABLE</tt> -- (Default) indicates the user is available to
30  * receive messages.
31  * <li><tt>Presence.Type.UNAVAILABLE</tt> -- the user is unavailable to receive messages.
32  * <li><tt>Presence.Type.SUBSCRIBE</tt> -- request subscription to recipient's presence.
33  * <li><tt>Presence.Type.SUBSCRIBED</tt> -- grant subscription to sender's presence.
34  * <li><tt>Presence.Type.UNSUBSCRIBE</tt> -- request removal of subscription to sender's
35  * presence.
36  * <li><tt>Presence.Type.UNSUBSCRIBED</tt> -- grant removal of subscription to sender's
37  * presence.
38  * <li><tt>Presence.Type.ERROR</tt> -- the presence packet contains an error message.
39  * </ul><p>
40  *
41  * A number of attributes are optional:
42  * <ul>
43  * <li>Status -- free-form text describing a user's presence (i.e., gone to lunch).
44  * <li>Priority -- non-negative numerical priority of a sender's resource. The
45  * highest resource priority is the default recipient of packets not addressed
46  * to a particular resource.
47  * <li>Mode -- one of five presence modes: available (the default), chat, away,
48  * xa (extended away, and dnd (do not disturb).
49  * </ul><p>
50  *
51  * Presence packets are used for two purposes. First, to notify the server of our
52  * the clients current presence status. Second, they are used to subscribe and
53  * unsubscribe users from the roster.
54  *
55  * @see RosterPacket
56  * @author Matt Tucker
57  */

58 public class Presence extends Packet {
59
60     private Type type = Type.AVAILABLE;
61     private String JavaDoc status = null;
62     private int priority = -1;
63     private Mode mode = Mode.AVAILABLE;
64
65     /**
66      * Creates a new presence update. Status, priority, and mode are left un-set.
67      *
68      * @param type the type.
69      */

70     public Presence(Type type) {
71         this.type = type;
72     }
73
74     /**
75      * Creates a new presence update with a specified status, priority, and mode.
76      *
77      * @param type the type.
78      * @param status a text message describing the presence update.
79      * @param priority the priority of this presence update.
80      * @param mode the mode type for this presence update.
81      */

82     public Presence(Type type, String JavaDoc status, int priority, Mode mode) {
83         this.type = type;
84         this.status = status;
85         this.priority = priority;
86         this.mode = mode;
87     }
88
89     /**
90      * Returns the type of this presence packet.
91      *
92      * @return the type of the presence packet.
93      */

94     public Type getType() {
95         return type;
96     }
97
98     /**
99      * Sets the type of the presence packet.
100      *
101      * @param type the type of the presence packet.
102      */

103     public void setType(Type type) {
104         this.type = type;
105     }
106
107     /**
108      * Returns the status message of the presence update, or <tt>null</tt> if there
109      * is not a status. The status is free-form text describing a user's presence
110      * (i.e., "gone to lunch").
111      *
112      * @return the status message.
113      */

114     public String JavaDoc getStatus() {
115         return status;
116     }
117
118     /**
119      * Sets the status message of the presence update. The status is free-form text
120      * describing a user's presence (i.e., "gone to lunch").
121      *
122      * @param status the status message.
123      */

124     public void setStatus(String JavaDoc status) {
125         this.status = status;
126     }
127
128     /**
129      * Returns the priority of the presence, or -1 if no priority has been set.
130      *
131      * @return the priority.
132      */

133     public int getPriority() {
134         return priority;
135     }
136
137     /**
138      * Sets the priority of the presence. The valid range is -128 through 128.
139      *
140      * @param priority the priority of the presence.
141      * @throws IllegalArgumentException if the priority is outside the valid range.
142      */

143     public void setPriority(int priority) {
144         if (priority < -128 || priority > 128) {
145             throw new IllegalArgumentException JavaDoc("Priority value " + priority +
146                     " is not valid. Valid range is -128 through 128.");
147         }
148         this.priority = priority;
149     }
150
151     /**
152      * Returns the mode of the presence update.
153      *
154      * @return the mode.
155      */

156     public Mode getMode() {
157         return mode;
158     }
159
160     /**
161      * Sets the mode of the presence update. For the standard "available" state, set
162      * the mode to <tt>null</tt>.
163      *
164      * @param mode the mode.
165      */

166     public void setMode(Mode mode) {
167         this.mode = mode;
168     }
169
170     public String JavaDoc toXML() {
171         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
172         buf.append("<presence");
173         if (getPacketID() != null) {
174             buf.append(" id=\"").append(getPacketID()).append("\"");
175         }
176         if (getTo() != null) {
177             buf.append(" to=\"").append(StringUtils.escapeForXML(getTo())).append("\"");
178         }
179         if (getFrom() != null) {
180             buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\"");
181         }
182         if (type != Type.AVAILABLE) {
183             buf.append(" type=\"").append(type).append("\"");
184         }
185         buf.append(">");
186         if (status != null) {
187             buf.append("<status>").append(status).append("</status>");
188         }
189         if (priority != -1) {
190             buf.append("<priority>").append(priority).append("</priority>");
191         }
192         if (mode != null && mode != Mode.AVAILABLE) {
193             buf.append("<show>").append(mode).append("</show>");
194         }
195
196         buf.append(this.getExtensionsXML());
197
198         // Add the error sub-packet, if there is one.
199
XMPPError error = getError();
200         if (error != null) {
201             buf.append(error.toXML());
202         }
203
204         buf.append("</presence>");
205         
206         return buf.toString();
207     }
208
209     public String JavaDoc toString() {
210         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
211         buf.append(type);
212         if (mode != null) {
213             buf.append(": ").append(mode);
214         }
215         if (status != null) {
216             buf.append(" (").append(status).append(")");
217         }
218         return buf.toString();
219     }
220
221     /**
222      * A typsafe enum class to represent the presecence type.
223      */

224     public static class Type {
225
226         public static final Type AVAILABLE = new Type("available");
227         public static final Type UNAVAILABLE = new Type("unavailable");
228         public static final Type SUBSCRIBE = new Type("subscribe");
229         public static final Type SUBSCRIBED = new Type("subscribed");
230         public static final Type UNSUBSCRIBE = new Type("unsubscribe");
231         public static final Type UNSUBSCRIBED = new Type("unsubscribed");
232         public static final Type ERROR = new Type("error");
233
234         private String JavaDoc value;
235
236         private Type(String JavaDoc value) {
237             this.value = value;
238         }
239
240         public String JavaDoc toString() {
241             return value;
242         }
243
244         /**
245          * Returns the type constant associated with the String value.
246          */

247         public static Type fromString(String JavaDoc value) {
248             if (value == null) {
249                 return AVAILABLE;
250             }
251             value = value.toLowerCase();
252             if ("unavailable".equals(value)) {
253                 return UNAVAILABLE;
254             }
255             else if ("subscribe".equals(value)) {
256                 return SUBSCRIBE;
257             }
258             else if ("subscribed".equals(value)) {
259                 return SUBSCRIBED;
260             }
261             else if ("unsubscribe".equals(value)) {
262                 return UNSUBSCRIBE;
263             }
264             else if ("unsubscribed".equals(value)) {
265                 return UNSUBSCRIBED;
266             }
267             else if ("error".equals(value)) {
268                 return ERROR;
269             }
270             // Default to available.
271
else {
272                 return AVAILABLE;
273             }
274         }
275     }
276
277     /**
278      * A typsafe enum class to represent the presence mode.
279      */

280     public static class Mode {
281
282         public static final Mode AVAILABLE = new Mode("available");
283         public static final Mode CHAT = new Mode("chat");
284         public static final Mode AWAY = new Mode("away");
285         public static final Mode EXTENDED_AWAY = new Mode("xa");
286         public static final Mode DO_NOT_DISTURB = new Mode("dnd");
287         public static final Mode INVISIBLE = new Mode("invisible");
288
289         private String JavaDoc value;
290
291         private Mode(String JavaDoc value) {
292             this.value = value;
293         }
294
295         public String JavaDoc toString() {
296             return value;
297         }
298
299         /**
300          * Returns the mode constant associated with the String value.
301          */

302         public static Mode fromString(String JavaDoc value) {
303             if (value == null) {
304                 return AVAILABLE;
305             }
306             value = value.toLowerCase();
307             if (value.equals("chat")) {
308                 return CHAT;
309             }
310             else if (value.equals("away")) {
311                 return AWAY;
312             }
313             else if (value.equals("xa")) {
314                 return EXTENDED_AWAY;
315             }
316             else if (value.equals("dnd")) {
317                 return DO_NOT_DISTURB;
318             }
319             else if (value.equals("invisible")) {
320                 return INVISIBLE;
321             }
322             else {
323                 return AVAILABLE;
324             }
325         }
326     }
327 }
328
Popular Tags