1 20 21 package org.jivesoftware.smack.packet; 22 23 import org.jivesoftware.smack.util.StringUtils; 24 25 58 public class Presence extends Packet { 59 60 private Type type = Type.AVAILABLE; 61 private String status = null; 62 private int priority = -1; 63 private Mode mode = Mode.AVAILABLE; 64 65 70 public Presence(Type type) { 71 this.type = type; 72 } 73 74 82 public Presence(Type type, String status, int priority, Mode mode) { 83 this.type = type; 84 this.status = status; 85 this.priority = priority; 86 this.mode = mode; 87 } 88 89 94 public Type getType() { 95 return type; 96 } 97 98 103 public void setType(Type type) { 104 this.type = type; 105 } 106 107 114 public String getStatus() { 115 return status; 116 } 117 118 124 public void setStatus(String status) { 125 this.status = status; 126 } 127 128 133 public int getPriority() { 134 return priority; 135 } 136 137 143 public void setPriority(int priority) { 144 if (priority < -128 || priority > 128) { 145 throw new IllegalArgumentException ("Priority value " + priority + 146 " is not valid. Valid range is -128 through 128."); 147 } 148 this.priority = priority; 149 } 150 151 156 public Mode getMode() { 157 return mode; 158 } 159 160 166 public void setMode(Mode mode) { 167 this.mode = mode; 168 } 169 170 public String toXML() { 171 StringBuffer buf = new StringBuffer (); 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 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 toString() { 210 StringBuffer buf = new StringBuffer (); 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 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 value; 235 236 private Type(String value) { 237 this.value = value; 238 } 239 240 public String toString() { 241 return value; 242 } 243 244 247 public static Type fromString(String 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 else { 272 return AVAILABLE; 273 } 274 } 275 } 276 277 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 value; 290 291 private Mode(String value) { 292 this.value = value; 293 } 294 295 public String toString() { 296 return value; 297 } 298 299 302 public static Mode fromString(String 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 |