Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: edu/ou/kmi/buddyspace/core/BSPresenceInfo.java


1   package edu.ou.kmi.buddyspace.core;
2   
3   /*
4    * BSPresenceBean.java
5    *
6    * Project: BuddySpace
7    * (C) Copyright Knowledge Media Institute 2002
8    *
9    *
10   * Created on 18 July 2002, 15:32
11   */
12  
13  import java.util.*;
14  import org.jabber.jabberbeans.*;
15  import org.jabber.jabberbeans.util.*;
16  //import org.jabber.jabberbeans.Extension.*;
17  
18  /**
19   * <code>BSPresenceInfo</code> contains presence information.
20   * It includes <code>JID</code>, availability, show and status information.
21   *
22   * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
23   */
24  public class BSPresenceInfo {
25      protected String show = null;
26      protected String status = null;
27      protected int priority = 0;
28      protected JID jid = null;
29      protected boolean available = false;
30      protected boolean myself = false;
31      protected boolean lurker = false;
32      
33      /** types */
34      public final static String TYPE_AVAILABLE   = "available";
35      public final static String TYPE_UNAVAILABLE = "unavailable";
36  
37      /** presence show constants */
38      public final static String SHOW_ONLINE = "";
39      public final static String SHOW_AWAY   = "away";
40      public final static String SHOW_CHAT   = "chat";
41      public final static String SHOW_DND    = "dnd";
42      public final static String SHOW_XA     = "xa";
43      
44      /** constants for friendly show text */
45      public static String FRIENDLY_SHOW_ONLINE  = "Online";
46      public static String FRIENDLY_SHOW_CHAT    = "Free for chat";
47      public static String FRIENDLY_SHOW_AWAY    = "Away";
48      public static String FRIENDLY_SHOW_XA      = "Extended away";
49      public static String FRIENDLY_SHOW_DND     = "Do not disturb";
50      public static String FRIENDLY_SHOW_OFFLINE = "Offline";
51      public static String FRIENDLY_SHOW_LURKER  = "Not in roster";
52      //public static String FRIENDLY_SHOW_MYSELF  = "Myself";
53      
54      /**
55       * Constructs <code>BSPresenceInfo</code> from <code>Presence</code>
56       * packet.
57       */
58      public BSPresenceInfo(Presence p) {
59          if (p == null) return;
60          
61          jid = p.getFromAddress();
62          if (jid == null) return;
63          String type = p.getType();
64          
65          available = (type == null || type.equals("") || type.equals(TYPE_AVAILABLE));
66          if (available)
67              show = p.getStateShow();
68          status = p.getStatus();
69      }
70  
71      /**
72       * Constructor
73       */
74      public BSPresenceInfo(JID jid, boolean available, String show, String status) {
75          this.jid = jid;
76          this.available = available;
77          if (SHOW_ONLINE.equals(show) || SHOW_CHAT.equals(show) ||
78              SHOW_AWAY.equals(show)   || SHOW_XA.equals(show) ||
79              SHOW_DND.equals(show))
80              this.show = show;
81          else
82              this.show = null;
83          this.status = status;
84      }
85      
86      /** Returns new presence packet representing this. */
87      public Presence getPresencePacket(JID toAddress) 
88                      throws java.lang.InstantiationException {
89                          
90          PresenceBuilder pb = new PresenceBuilder();
91          if (toAddress != null)
92              pb.setToAddress(toAddress);
93          if (available) {
94              pb.setPriority(priority);
95              if (show != null)
96                  pb.setStateShow(show);
97              if (status != null)
98                  pb.setStatus(status);
99          }
100         else {
101             pb.setType(TYPE_UNAVAILABLE);
102             if (status != null)
103                 pb.setStatus(status);
104         }
105         return (Presence) pb.build();
106     }
107     
108     /** Returns if buddy is available/on-line */
109     public boolean isOnline() {
110         return available;
111     }
112     
113     /** Returns show */
114     public String getShow() {
115         return show;
116     }
117     
118     /** Returns status */
119     public String getStatus() {
120         return status;
121     }
122     
123     /** Returns friendly form of show for displaying */
124     public String getFriendlyShow() {
125         if (lurker && !available)
126             return FRIENDLY_SHOW_LURKER;
127         else if (!available)
128             return FRIENDLY_SHOW_OFFLINE;
129         else if (show == null || SHOW_ONLINE.equals(show))
130             return FRIENDLY_SHOW_ONLINE;
131         else if (SHOW_CHAT.equals(show))
132             return FRIENDLY_SHOW_CHAT;
133         else if (SHOW_AWAY.equals(show))
134             return FRIENDLY_SHOW_AWAY;
135         else if (SHOW_XA.equals(show))
136             return FRIENDLY_SHOW_XA;
137         else if (SHOW_DND.equals(show))
138             return FRIENDLY_SHOW_DND;
139         else
140             return FRIENDLY_SHOW_LURKER;
141     }
142     
143     /**
144      * Sets if the buddy is in roster.
145      * Typically used to set that the buddy is NOT in roster.
146      */
147     public void setIsInRoster(boolean inRoster) {
148         lurker = !inRoster;
149     }
150     
151     /**
152      * Returns if the buddy is in roster.
153      */
154     public boolean isInRoster() {
155         return !lurker;
156     }
157     
158     /**
159      * Sets if the buddy is myself it means the currently logged one.
160      */
161     public void setIsMyself(boolean isMyself) {
162         myself = isMyself;
163     }
164     
165     /**
166      * Returns if the buddy is myself it means the currently logged one.
167      */
168     public boolean isMyself() {
169         return myself;
170     }
171     
172     /**
173      * Sets priority level being set by this presence.
174      */
175     public void setPriority(int priority) {
176         this.priority = priority;
177     }
178     
179     /**
180      * Returns priority level being set by this presence.
181      */
182     public int getPriority() {
183         return priority;
184     }
185     
186     /** Returns JID of the buddy */
187     public JID getJID() {
188         return jid;
189     }
190     
191     /** Sets JID of the buddy */
192     public void setJID(JID jid) {
193         this.jid = jid;
194     }
195     
196 }