KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > protocols > comvc > JComVcPlugin


1 /*
2     =========================================================================
3     Package comvc - Implements the ComVc Protocol.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JComVcPlugin.java,v 1.27 2007/01/28 17:39:21 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.27 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.protocols.comvc;
40
41 import org.planetamessenger.plugin.*;
42
43
44 public class JComVcPlugin extends JPlugin implements JMessengerEventListener {
45   
46   JComVcSession session = null;
47   JUserDetails userDetails = null;
48   
49   /**
50    * Creates and initializes all JComVcPlugin
51    * class.
52    */

53   public JComVcPlugin() {
54     
55   }
56   
57   /**
58    * Implement the onCreate event listener.
59    * Performs the plugin creation.
60    */

61   public void onCreate() {
62   
63     super.onCreate();
64     
65     userDetails = new JUserDetails();
66     session = new JComVcSession();
67     session.addComVcEventListener( new JComVcEventImpl( this ) );
68     addMessengerEventListener( this );
69         
70     // Setup the status
71
addStatus( JPlugin.STATUS_ONLINE, new Integer JavaDoc( JComVcConstants.STATUS_ONLINE ) );
72     addStatus( JPlugin.STATUS_OFFLINE, new Integer JavaDoc( JComVcConstants.STATUS_OFFLINE ) );
73     addStatus( JPlugin.STATUS_INVISIBLE, new Integer JavaDoc( JComVcConstants.STATUS_INVISIBLE ) );
74     addStatus( JPlugin.STATUS_AWAY, new Integer JavaDoc( JComVcConstants.STATUS_AWAY ) );
75   }
76
77   /**
78    * Implement the onLogin event listener.
79    * @param nPluginStatus The initial plugin status;
80    */

81   public void onLogin( int nPluginStatus ) {
82
83     userDetails.setUserId( Long.parseLong( getUserId() ) );
84     userDetails.setPassword( getPassword() );
85     session.setUser( userDetails );
86     addToLibContactList();
87     
88     session.login( ( ( Integer JavaDoc ) translateStatusToPlugin( nPluginStatus ) ).intValue() );
89   }
90   
91   /**
92    * Implement the onLogout event listener.
93    */

94   public void onLogout() {
95
96     session.logout();
97     session.clearContactList();
98   }
99
100   /**
101     * Implement the onAddUserToContactList event listener
102     * @param strNewUserId The User's id that will be added;
103     */

104   public void onAddUserToContactList( java.lang.String JavaDoc strUserId ) {
105
106     try {
107       session.addToContactList( Long.parseLong( strUserId ), true );
108     } catch( java.lang.NumberFormatException JavaDoc e ) {
109       System.err.println( "JComVcPlugin.onAddUserToContactList(" + strUserId + ") - Error to add user" );
110     }
111   }
112   
113   /**
114    * Implement the onRemoveUserFromContactList event
115    * listener;
116    * @param strUserId The User's id that will be removed;
117    */

118   public void onRemoveUserFromContactList( java.lang.String JavaDoc strUserId ) {
119
120     session.removeFromContactList( Long.parseLong( strUserId ) );
121   }
122  
123   /**
124    * Implement the onSendMessage event listener
125    * @param strToUser The user that we'll send
126    * the message;
127    * @param msg The Message that will be sent;
128    * @param nMsgType The Message type:
129    * MESSAGE_TYPE_ONLINE - Online messages;
130    * MESSAGE_TYPE_OFFLINE - Offline messages;
131    */

132   public void onSendMessage( java.lang.String JavaDoc strToUser, java.lang.String JavaDoc msg, int nMsgType ) {
133     
134     session.sendMessage( Long.parseLong( strToUser ), msg );
135   }
136   
137   /**
138    * Implement the onRequestAuthorization event
139    * listener.
140    * @param strUserId The user id requesting
141    * the authorization to add him/her to
142    * contact list;
143    * @param strNickName The user nick name;
144    */

145   public void onRequestAuthorization( java.lang.String JavaDoc strUserId, java.lang.String JavaDoc strNickName ) {
146     
147   }
148
149   /**
150    * Implement the onUserDetails event listener;
151    * @param strUserId The User id owner of
152    * this detail;
153    */

154   public void onBuddyDetails( java.lang.String JavaDoc strUserId ) {
155   
156     try {
157       JBuddyDetails userDetails = session.getBuddyDetails( Long.parseLong( strUserId ) );
158       
159       getKernelManager().getContactListManager().updateScreenName( this, strUserId, userDetails.getNickName() );
160       
161     } catch( java.lang.NumberFormatException JavaDoc e ) {
162       System.err.println( "JComVcPlugin.onBuddyDetails(" + strUserId + ") - Error to get buddy details" );
163     }
164   }
165   
166   /**
167    * Implement the onAuthorizeBuddy event listener
168    * @param strUserId The user id authorized;
169    */

170   public void onAuthorizeBuddy( java.lang.String JavaDoc strUserId ) {
171   
172     session.authorizeUser( Long.parseLong( strUserId ) );
173   }
174   
175   /**
176    * Implement the onUnauthorizeBuddy event listener
177    * @param strUserId The user id unauthorized;
178    */

179   public void onUnauthorizeBuddy( java.lang.String JavaDoc strUserId ) {
180
181   }
182   
183   /**
184    * Implements the onPluginStatusChaged event.
185    * o nStatus - The new status for this plugin;
186    */

187   public void onPluginStatusChanged( int nStatus ) {
188
189     session.setStatus( ( ( Integer JavaDoc ) translateStatusToPlugin( nStatus ) ).intValue() );
190   }
191   
192   /**
193     * implement the onRegisterExistingUser
194     * event listener;
195     * @param strNickName The NickName of
196     * the user;
197     * @param strNewUserId The User's id
198     * that will be registered;
199     * @param strPasswd The User password;
200     */

201   public void onRegisterExistingUser( java.lang.String JavaDoc strNickName, java.lang.String JavaDoc strNewUserId, java.lang.String JavaDoc strPasswd ) {
202     
203     JUserInfo userInfo = new JUserInfo( strNewUserId, strNickName, strPasswd, JPlugin.STATUS_ONLINE );
204     
205     setUserInfo( userInfo );
206     fireOnLogin();
207   }
208   
209   /**
210    * Implement the onRegisterNewUser event
211    * listener.
212    * @param strNickName The NickName of
213    * the user;
214    * @param strNewUserId The User's id
215    * that will be registered;
216    * @param strPasswd The User password;
217    */

218   public void onRegisterNewUser( java.lang.String JavaDoc strNickName, java.lang.StringBuffer JavaDoc strNewUserId, java.lang.String JavaDoc strPasswd ) {
219     
220     userDetails.setNick( strNickName );
221     userDetails.setPassword( strPasswd );
222     session.setUser( userDetails );
223       
224     if( !session.createNewUser() )
225       fireOnRegisterUserFailed();
226     else
227       try {
228         JUserInfo userInfo;
229
230         fireOnRegisterUserSucessfull();
231         strNewUserId.append( Long.toString( userDetails.getUserId() ) );
232         userInfo = new JUserInfo( Long.toString( userDetails.getUserId() ), userDetails.getNick(), strPasswd, JPlugin.STATUS_ONLINE );
233         setUserInfo( userInfo );
234         fireOnLogin();
235       } catch( java.lang.NumberFormatException JavaDoc e ) {
236         System.err.println( "JComVcPlugin.onRegisterNewUser(" + strNewUserId + ") - Error to register a new user" );
237       }
238   }
239   
240   /**
241    * Implement the onUnregisterUser event
242    * listener.
243    */

244   public void onUnregisterUser() {
245    
246     if( isConnected() ) {
247       fireOnLogout();
248       fireOnUnregisterUserSuccessfull();
249     }
250   }
251   
252   /**
253    * Implement the onChangePassword event
254    * listener
255    * param @strNewPasswd The new user's password;
256    */

257   public void onChangePassword( java.lang.String JavaDoc strNewPasswd ) {
258
259     if( session.changePassword( session.getUser().getPassword(), strNewPasswd ) )
260       fireOnPasswordChangedSuccessfull( strNewPasswd );
261     else
262       fireOnPasswordChangedFailed();
263   }
264
265   /**
266    * Implement the onChange privacy event
267    * handler.
268    * Performs a privacy control to a user
269    * for this plugin.
270    * @param strUserId The user that new privacy
271    * will be applied;
272    * @param nPrivacyType The privacy type (@see JPlugin
273    * for available types);
274    */

275   public void onChangePrivacy( java.lang.String JavaDoc strUserId, int nPrivacyType ) {
276     
277   }
278   
279   /**
280    * Implement the onPrivacyChanged event handler.
281    * This event is generated after all processing
282    * onChangePrivacy events.
283    */

284   public void onPrivacyChanged() {
285     
286   }
287   
288   /**
289    * Implement the onPluginPropertiesUpdated event handler.<br>
290    */

291   public void onPluginPropertiesUpdated() {
292     
293   }
294
295   /**
296    * Add the MOS contact list to
297    * jcq2k ContactList.
298    */

299   void addToLibContactList() {
300     
301     if( getKernelManager().getContactListManager() != null ) {
302       java.util.Enumeration JavaDoc eContactList = getKernelManager().getContactListManager().getContactList( this ).keys();
303
304       while( eContactList.hasMoreElements() )
305         session.addToContactList( Long.parseLong( eContactList.nextElement().toString() ), false );
306     }
307   }
308
309   /**
310    * Returns the plugin connection
311    * status .
312    */

313   public boolean isConnected() {
314     
315     if( session.getStatus() != JComVcConstants.STATUS_OFFLINE )
316       return true;
317     else
318       return false;
319   }
320   
321   
322   /**
323     *=========================================================================
324     * JComVcEventImpl class implementation
325     * This is a event manager for JComVcPlugin Class
326     *=========================================================================
327     */

328   
329   class JComVcEventImpl implements JComVcEventListener {
330     
331     
332     JPlugin plugin;
333     
334
335     /**
336      * Constructor. Initializes all class
337      * data.
338      * @param plugin The plugin to be attached
339      * to this class;
340      */

341     JComVcEventImpl( JPlugin plugin ) {
342       
343       this.plugin = plugin;
344     }
345
346     /**
347      * Handles the comvc onBuddyRequestAuthorization
348      * event.
349      * @param see JPlugin The parameters are the
350      * same (except by type long -> String);
351      */

352     public void onBuddyRequestAuthorization( long nUserId, java.lang.String JavaDoc strNickName ) {
353
354       plugin.fireOnBuddyRequestAuthorization( Long.toString( nUserId ), strNickName );
355     }
356
357     /**
358      * Handles the onBuddyStatusChanged event.
359      * @param see JPlugin The parameters are the
360      * same (except by type long -> String);
361      */

362     public void onBuddyStatusChanged( long nUserId, int nNewStatus ) {
363       
364       plugin.fireOnBuddyStatusChanged( Long.toString( nUserId ), translateStatusFromPlugin( new Integer JavaDoc( nNewStatus ) ) );
365     }
366
367     /**
368      * Handles the onChangePasswordError
369      * event
370      * @param see JPlugin The parameters are the
371      * same (except by type long -> String);
372      */

373     public void onChangePasswordError( long nUserId ) {
374       
375     }
376
377     /**
378      * Handles the onCreateUserError
379      * event.
380      * @param see JPlugin The parameters are the
381      * same (except by type long -> String);
382      */

383     public void onCreateUserError() {
384
385     }
386
387     /**
388      * Handles the onLoginAccepted
389      * event.
390      * Parameters:
391      * @param see JPlugin The parameters are the
392      * same (except by type long -> String);
393      */

394     public void onLoginAccepted( long nUserId ) {
395       
396       plugin.fireOnLoginAccepted();
397       plugin.fireOnRegisterUserSucessfull(); // Called here because this login could be a user registration (onRegisterexistingUser)
398
}
399
400     /**
401      * Handles the onLoginError
402      * event.
403      * @param see JPlugin The parameters are the
404      * same (except by type long -> String);
405      */

406     public void onLoginError( long nUserId ) {
407       
408       plugin.fireOnLoginFailed();
409       fireOnRegisterUserFailed(); // Called here because this login could be a user registration (onRegisterexistingUser)
410
}
411
412     /**
413      * Handles the onMessageError
414      * event.
415      * @param see JPlugin The parameters are the
416      * same (except by type long -> String);
417      */

418     public void onMessageError( long nToUser ) {
419
420     }
421
422     /**
423      * Handles the onMessageSent
424      * event.
425      * @param see JPlugin The parameters are the
426      * same (except by type long -> String);
427      */

428     public void onMessageSent( long nToUser ) {
429       
430     }
431
432     /**
433      * Handles the onPasswordChangd
434      * event.
435      * @param see JPlugin The parameters are the
436      * same (except by type long -> String);
437      */

438     public void onPasswordChanged(long nUserId) {
439       
440     }
441
442     /**
443      * Handles the onReceiveMessage
444      * event.
445      * @param see JPlugin The parameters are the
446      * same (except by type long -> String);
447      */

448     public void onReceiveMessage( long nFromUser, java.util.GregorianCalendar JavaDoc date, java.lang.String JavaDoc strMessage ) {
449       
450       plugin.fireOnReceiveMessage( Long.toString( nFromUser ), date, strMessage );
451     }
452
453     /**
454      * Handles throws onSendMessage
455      * event.
456      * @param see JPlugin The parameters are the
457      * same (except by type long -> String);
458      */

459     public void onSendMessage( long nToUser, java.lang.String JavaDoc msg ) {
460
461     }
462
463     /**
464      * Handles the onStatusChanged
465      * event.
466      * @param see JPlugin The parameters are the
467      * same (except by type long -> String);
468      */

469     public void onStatusChanged( int nNewStatus ) {
470       
471       if( nNewStatus == JComVcConstants.STATUS_OFFLINE )
472         fireOnLogout();
473       else
474         plugin.fireOnPluginStatusChanged( translateStatusFromPlugin( new Integer JavaDoc( nNewStatus ) ) );
475     }
476
477     /**
478      * Handles the onStatusError
479      * event.
480      * @param see JPlugin The parameters are the
481      * same (except by type long -> String);
482      */

483     public void onStatusError( int nStatus ) {
484       
485     }
486
487     /**
488      * Handles the onUserCreated
489      * event.
490      * @param see JPlugin The parameters are the
491      * same (except by type long -> String);
492      */

493     public void onUserCreated( long nNewUserId ) {
494       
495     }
496     
497   } // JComVcEventImpl
498

499 } // JComVcPlugin class
500
Popular Tags