001 /* 002 Copyright (C) 2003 Adam Olsen 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU General Public License as published by 006 the Free Software Foundation; either version 1, or (at your option) 007 any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU General Public License for more details. 013 014 You should have received a copy of the GNU General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 017 */ 018 019 package com.valhalla.jbother.jabber; 020 021 import com.valhalla.jbother.*; 022 import java.util.*; 023 import org.jivesoftware.smack.packet.*; 024 import org.jivesoftware.smack.*; 025 import com.valhalla.jbother.ConversationPanel; 026 027 /** 028 * Tracks a users different presences and resources 029 * 030 * @author Adam Olsen 031 * @version 1.0 032 */ 033 public class BuddyStatus extends Hashtable 034 { 035 private ResourceBundle sources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() ); 036 private String userId = ""; 037 private ConversationPanel conversation = null; 038 private String name = null; 039 private boolean removed = false; 040 private boolean hasSignedOn = false; 041 private String resources = ""; 042 private Hashtable presences = null; 043 private Properties statusMessages = null; 044 private String tempGroup = null; 045 private String versionInfo = null; 046 047 /** 048 * Sets up the buddy container 049 * @param buddyId the buddy/user to track 050 */ 051 public BuddyStatus( String buddyId ) 052 { 053 this.userId = buddyId; 054 } 055 056 /** 057 * Gets the resource with the higest priority 058 * @return the resource with the highest priority, or null if the user is offline 059 */ 060 public String getHighestResource() 061 { 062 String resource = null; 063 064 Set keys = keySet(); 065 Iterator iterator = keys.iterator(); 066 067 int highest = -2; 068 069 while( iterator.hasNext() ) 070 { 071 Object key = iterator.next(); 072 Integer value = (Integer)get( key ); 073 074 if( value.intValue() > highest ) 075 { 076 resource = (String)key; 077 highest = value.intValue(); 078 } 079 } 080 081 return resource; 082 } 083 084 /** 085 * Resets the buddy so that it appears as though they never signed on 086 */ 087 public void resetBuddy() 088 { 089 clear(); 090 presences = null; 091 statusMessages = null; 092 } 093 094 /** 095 * Adds a resource to the tracker 096 * @param resource the resource name 097 * @param priority the priority level of this resource 098 * @param mode the current presence mode 099 * @param statusMessage the status message if there is one 100 */ 101 public void addResource( String resource, int priority, Presence.Mode mode, String statusMessage ) 102 { 103 if( presences == null ) presences = new Hashtable(); 104 if( statusMessages == null ) statusMessages = new Properties(); 105 presences.put( resource, mode ); 106 107 if( statusMessage == null ) statusMessage = ""; 108 statusMessages.setProperty( resource, statusMessage ); 109 put( resource, new Integer( priority ) ); 110 } 111 112 /** 113 * Gets the status message of the highest resource 114 * @return the status message of the highest resource, or an empty string if there is no status message 115 */ 116 public String getStatusMessage( String resource ) 117 { 118 if( resource == null ) return ""; 119 if( statusMessages == null ) statusMessages = new Properties(); 120 String message = statusMessages.getProperty( resource ); 121 if( message == null ) message = ""; 122 return message; 123 } 124 125 /** 126 * Stops tracking a resource 127 * @param resource the resource to remove 128 */ 129 public void removeResource( String resource ) 130 { 131 if( presences != null ) presences.remove( resource ); 132 if( statusMessages != null ) statusMessages.remove( resource ); 133 remove( resource ); 134 } 135 136 /** 137 * Gets the presence mode of the highest priority 138 * @return the presence mode of the highest priority or null if the user is offline 139 */ 140 public Presence.Mode getPresence( String resource ) 141 { 142 if( presences == null ) presences = new Hashtable(); 143 return (Presence.Mode)presences.get( resource ); 144 } 145 146 /** 147 * Sets a temporary group name (if the user is displayed before the group actually changes on the server) 148 * @param group the temporary group to use 149 */ 150 public void setTempGroup( String group ) { this.tempGroup = group; } 151 152 /** 153 * Returns the temporary group 154 * @return the temp group 155 */ 156 public String getTempGroup() 157 { 158 String temp = tempGroup; 159 tempGroup = null; 160 161 return temp; 162 } 163 164 /** 165 * Gets the group the user is in in the Roster 166 * @return the group the user is in 167 */ 168 public String getGroup() 169 { 170 RosterEntry entry = getRosterEntry(); 171 172 if( entry == null ) return sources.getString( "contactsGroup" ); 173 Iterator groups = entry.getGroups(); 174 175 String group = sources.getString( "contactsGroup" ); 176 if( groups.hasNext() ) group = ((RosterGroup)groups.next()).getName(); 177 if( userId.indexOf( "@" ) < 0 ) group = sources.getString( "transportsGroup" ); 178 return group; 179 } 180 181 /** 182 * Gets the roster entry for this user 183 * @return the RosterEntry for this user 184 */ 185 public RosterEntry getRosterEntry() 186 { 187 if( !BuddyList.getInstance().checkConnection() ) return null; 188 189 Iterator i = BuddyList.getInstance().getConnection().getRoster().getEntries(); 190 while( i.hasNext() ) 191 { 192 RosterEntry entry = (RosterEntry)i.next(); 193 if( entry.getUser().toLowerCase().equals( userId.toLowerCase() ) ) 194 { 195 return entry; 196 } 197 } 198 199 return null; 200 } 201 202 /** 203 * Sets the users jabber:iq:version information 204 * @param info the users jabber:iq:version information 205 */ 206 public void setVersionInfo( String info ) { this.versionInfo = info; } 207 208 /** 209 * @return the users jabber:iq:version information 210 */ 211 public String getVersionInfo() { return versionInfo; } 212 213 /** 214 * Whether or not the user has signed on 215 * @param on set to true if the user has signed on 216 */ 217 public void setHasSignedOn( boolean on ) 218 { 219 this.hasSignedOn = on; 220 } 221 222 /** 223 * @return true if the user has signed on 224 */ 225 public boolean getHasSignedOn() { return this.hasSignedOn; } 226 227 /** 228 * @param removed set to true if this user has been removed from the roster 229 */ 230 public void setRemoved( boolean removed ) { this.removed = removed; } 231 232 /** 233 * @return true if the user has been removed from the roster 234 */ 235 public boolean getRemoved() { return this.removed; } 236 237 /** 238 * @return the users alias 239 */ 240 public String getName() 241 { 242 if( name != null ) return name; 243 RosterEntry entry = getRosterEntry(); 244 if( entry != null ) return entry.getName(); 245 else return userId; 246 } 247 248 /** 249 * @param name the buddy's alias 250 */ 251 public void setName( String name ) { this.name = name; } 252 253 /** 254 * @param buddyId the userId 255 */ 256 public void setUser( String buddyId ) { this.userId = buddyId.toLowerCase(); } 257 258 /** 259 * @return the JID for this Buddy 260 */ 261 public String getUser() { return this.userId; } 262 263 /** 264 * Sets the ConversationPanel for this buddy 265 * @param window the conversation for this buddy 266 */ 267 public void setConversation( ConversationPanel window ) { this.conversation = window; } 268 269 /** 270 * Gets the conversation window for this buddy 271 * @return the conversation for this buddy 272 */ 273 public ConversationPanel getConversation() { return this.conversation; } 274 }