KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > plugin > JPlugin


1 /*
2     =========================================================================
3     Package plugin - Plugin management.
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: JPlugin.java,v 1.62 2007/01/30 20:53:36 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.62 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.plugin;
40
41 import java.util.*;
42
43
44
45 public class JPlugin implements JPluginBase {
46
47   // User status constants
48
static public final int STATUS_OFFLINE = 0;
49   static public final int STATUS_ONLINE = 1;
50   static public final int STATUS_FREE_CHAT = 2;
51   static public final int STATUS_INVISIBLE = 3;
52   static public final int STATUS_AWAY = 4;
53   static public final int STATUS_BUSY = 5;
54   static public final int STATUS_IDLE = 6;
55   static public final int STATUS_DND = 7;
56   static public final int STATUS_ON_PHONE = 8;
57   static public final int STATUS_ON_LUNCH = 9;
58   static public final int STATUS_ON_VACATION = 10;
59   static public final int STATUS_NOT_AT_HOME = 11;
60   static public final int STATUS_NOT_AT_DESK = 12;
61   static public final int STATUS_NOT_IN_OFFICE = 13;
62   static public final int STATUS_NOT_IN_LIST = 14;
63   static public final int STATUS_CUSTOM = 15;
64   static public final int STATUS_URL = 98;
65   static public final int STATUS_MESSAGE = 99;
66   static public final int STATUS_LAST = 99;
67   
68   static public final int PRIVACY_UNBLOCK_USER = 0;
69   static public final int PRIVACY_BLOCK_USER = 1;
70   static public final int PRIVACY_PERMIT_STATUS = 2;
71   static public final int PRIVACY_DENY_STATUS = 3;
72
73   private JUserInfo userInfo = null;
74   private JPlugin selfObject = null;
75   private ArrayList<JPluginEngineListener> pluginEngineListener = null;
76   private ArrayList<JUserRegistrationListener> registrationListener = null;
77   private ArrayList<JMessengerEventListener> messengerEventListener = null;
78   private JKernelAPIListener kernelManager = null;
79   private HashMap<Object JavaDoc, JPlanetaMessengerStatus> hPluginStatus = null;
80   private JPluginProperties pluginProperties = null;
81   private JPluginCapabilities pluginCapabilities = JPluginCapabilities.getDefault();
82   private boolean bDestroying = false;
83
84
85   /**
86    * Constructor. Initializes all class data
87    */

88   public JPlugin() {
89     
90   }
91   
92   // Event management methods
93
/**
94    * Add a PluginEngineListener to this
95    * plugin class.
96    * @param listener The new listener to add;
97    */

98   public synchronized final void addPluginEngineListener( JPluginEngineListener listener ) {
99
100     pluginEngineListener.add( listener );
101   }
102
103   /**
104    * Remove a PluginEngineListener of this
105    * plugin class.
106    * @param listener The listener to remove;
107    */

108   public synchronized final void removePluginEngineListener( JPluginEngineListener listener ) {
109     
110     pluginEngineListener.remove( listener );
111   }
112   
113    /**
114    * Add a KernelAPIListener to this plugin class.
115    * @param listener The new listener to add;
116    */

117   public synchronized final void addKernelAPIListener( JKernelAPIListener listener ) {
118
119     kernelManager = listener;
120   }
121
122   /**
123    * Remove a PluginEngineListener of this
124    * plugin class.
125    * @param listener The listener to remove;
126    */

127   public synchronized final void removeKernelAPIListener( JKernelAPIListener listener ) {
128
129     kernelManager = null;
130   }
131   
132   /**
133    * Return the kernel API manager object to plugin use.
134    */

135   public synchronized final JKernelAPIListener getKernelManager() {
136     
137     return kernelManager;
138   }
139
140   /**
141    * Add a plugin event listener object.
142    * @param listener The listener object to add;
143    */

144   public synchronized void addMessengerEventListener( JMessengerEventListener listener ) {
145
146     messengerEventListener.add( listener );
147   }
148   
149   /**
150    * Remove a plugin event listener of object.
151    * @param listener The listener to remove.
152    */

153   public synchronized void removeMessengerEventListener( JMessengerEventListener listener ) {
154     
155     messengerEventListener.remove( listener );
156   }
157   
158  /**
159    * Add a RegistrationEventListener to this
160    * plugin class.
161    * @param listener The new listener to add;
162    */

163   public synchronized final void addRegistrationEngineListener( JUserRegistrationListener listener ) {
164     
165     registrationListener.add( listener );
166   }
167
168   /**
169    * Remove a RegistrationEventListener of this
170    * plugin class.
171    * @param listener The listener to remove;
172    */

173   public synchronized final void removeRegistrationEngineListener( JUserRegistrationListener listener ) {
174     
175     registrationListener.remove( listener );
176   }
177   
178   /**
179    * Remove a RegistrationEventListener of this
180    * plugin class.
181    * @param listener The listener to remove;
182    */

183   public synchronized final void removeAllRegistrationEngineListener() {
184
185     registrationListener.clear();
186   }
187
188   /**
189    * Assign the UserInfo to plugin.
190    * This object contains all information about registered
191    * plugin user.
192    * @param userInfo The new JUserInfo object to use on plugin;
193    */

194   public final void setUserInfo( JUserInfo userInfo ) {
195     
196     this.userInfo = userInfo;
197   }
198   
199   /**
200    * Add a to this plugin object.
201    * Is used to convert the Plugin library
202    * specific status, to a PlanetaMessenger
203    * status.
204    * @param nPlanetaMessengerStatus The PlanetaMessenger
205    * status;
206    * @param objPluginStatus The Plugin Status that will
207    * be translated to corresponding PM status;
208    */

209   protected final void addStatus( int nPlanetaMessengerStatus, Object JavaDoc objPluginStatus ) {
210     
211     synchronized( hPluginStatus ) {
212       hPluginStatus.put( objPluginStatus, new JPlanetaMessengerStatus( nPlanetaMessengerStatus, objPluginStatus ) );
213     }
214   }
215
216   /**
217    * Translate the plugin specific status
218    * to a PM status;
219    * @param objPluginStatus The plugin status;
220    */

221   protected final int translateStatusFromPlugin( Object JavaDoc objPluginStatus ) {
222     
223     synchronized( hPluginStatus ) {
224       JPlanetaMessengerStatus status = ( hPluginStatus != null ? ( ( JPlanetaMessengerStatus ) hPluginStatus.get( objPluginStatus ) ) : null );
225     
226       return ( ( status != null ) ? status.getPlanetaMessengerStatus() : STATUS_OFFLINE );
227     }
228   }
229
230   /**
231    * Translate the plugin specific status
232    * to a PM status;
233    * @param nPMStatus The PlanetaMessenger status;
234    */

235   protected final Object JavaDoc translateStatusToPlugin( int nPlanetaMessengertStatus ) {
236
237     JPlanetaMessengerStatus status = null;
238
239
240     synchronized( hPluginStatus ) {
241
242       Iterator<JPlanetaMessengerStatus> itStatus = hPluginStatus.values().iterator();
243
244
245       while( itStatus.hasNext() ) {
246         status = itStatus.next();
247         if( status.getPlanetaMessengerStatus() == nPlanetaMessengertStatus )
248           break;
249       }
250     }
251
252     return ( status != null ? status.getPluginStatus() : null );
253   }
254
255   /**
256    * Return all plugin status (copy of internal
257    * hash object);
258    */

259   public final HashMap getAllPluginStatus() {
260
261     synchronized( hPluginStatus ) {
262       return ( HashMap ) hPluginStatus.clone();
263     }
264   }
265   
266   // Initialization management methods
267
/**
268    * Performs the plugin creation.
269    * Parameters:
270    * @param pluginProperties The plugin properties object with
271    * plugin information;
272    */

273   public final void fireOnCreate( JPluginProperties pluginProperties ) {
274
275     this.pluginProperties = pluginProperties;
276     hPluginStatus = new HashMap<Object JavaDoc, JPlanetaMessengerStatus>();
277     messengerEventListener = new ArrayList<JMessengerEventListener>();
278     pluginEngineListener = new ArrayList<JPluginEngineListener>();
279     registrationListener = new ArrayList<JUserRegistrationListener>();
280     selfObject = this;
281     bDestroying = false;
282     
283     addStatus( STATUS_MESSAGE, new Integer JavaDoc( STATUS_MESSAGE ) );
284     addStatus( STATUS_URL, new Integer JavaDoc( STATUS_URL ) );
285     
286     onCreate();
287   }
288   
289   /**
290    * The plugin must overload this
291    * method to perform specific
292    * initializations.
293    */

294   public void onCreate() {
295     
296   }
297
298   /**
299    * Perfomrs all clean in Plugin
300    * object.
301    */

302   public final void fireOnDestroy() {
303     
304     // Mark plugin to be destroyed on logout process
305
bDestroying = true;
306
307     // If connected will fire the Logout event.
308
fireOnLogout();
309   }
310   
311   /**
312    * The plugin must overload this
313    * method to perform specific
314    * finalizations.
315    */

316   public void onDestroy() {
317     
318   }
319
320   /**
321    * Performs the Login event management.
322    * This is a asynchronous event.
323    */

324   public final void fireOnLogin() {
325
326     final ArrayList<JPluginEngineListener> clonePluginEngineListener;
327     
328
329     synchronized( this ) {
330       clonePluginEngineListener = ( ArrayList<JPluginEngineListener> ) pluginEngineListener.clone();
331     }
332
333     if( userInfo.getLoginStatus() != STATUS_OFFLINE ) {
334       
335       // Dispatch the event through the listener
336
if( clonePluginEngineListener.size() > 0 ) {
337         for( int nCount = 0; nCount < clonePluginEngineListener.size(); nCount++ )
338           ( ( JPluginEngineListener ) clonePluginEngineListener.get( nCount ) ).onPluginConnectionStatusChanged( this, JPluginEngineListener.CONNECTING );
339       }
340
341       Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
342          
343         public void run() {
344            
345           ArrayList<JMessengerEventListener> clone;
346       
347           synchronized( selfObject ) {
348             clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
349           }
350
351           // Dispatch the event through the listener
352
if( clone.size() > 0 ) {
353             for( int nCount = 0; nCount < clone.size(); nCount++ )
354                ( ( JMessengerEventListener ) clone.get( nCount ) ).onLogin( selfObject.userInfo.getLoginStatus() );
355           }
356        
357           // Dispatch the event through the listener
358
if( clonePluginEngineListener.size() > 0 ) {
359             for( int nCount = 0; nCount < clonePluginEngineListener.size(); nCount++ )
360               ( ( JPluginEngineListener ) clonePluginEngineListener.get( nCount ) ).onPluginStatusChanged( selfObject, selfObject.userInfo.getLoginStatus() );
361           }
362          }
363       } );
364        
365       runner.start();
366     }
367     else {
368       // Dispatch the event through the listener
369
if( clonePluginEngineListener.size() > 0 ) {
370         for( int nCount = 0; nCount < clonePluginEngineListener.size(); nCount++ )
371           ( ( JPluginEngineListener ) clonePluginEngineListener.get( nCount ) ).onPluginConnectionStatusChanged( this, JPluginEngineListener.DISCONNECTED );
372       }
373     }
374   }
375   
376   /**
377    * Fire the onLoginAccept plugin event;
378    * Always call this method when login is
379    * sucessfull, to update some MOS UI controls.
380    */

381   public final void fireOnLoginAccepted() {
382
383     ArrayList<JPluginEngineListener> clonePluginEngineListener;
384     
385
386     synchronized( this ) {
387       clonePluginEngineListener = ( ArrayList<JPluginEngineListener> ) pluginEngineListener.clone();
388     }
389     
390     // Dispatch the event through the listener
391
if( clonePluginEngineListener.size() > 0 ) {
392       for( int nCount = 0; nCount < clonePluginEngineListener.size(); nCount++ )
393         ( ( JPluginEngineListener ) clonePluginEngineListener.get( nCount ) ).onPluginConnectionStatusChanged( this, JPluginEngineListener.CONNECTED );
394     }
395   }
396   
397   /**
398    * Fire the onLoginFailed plugin event;
399    * Always call this method when login is
400    * sucessfull, to update some MOS UI controls.
401    */

402   public final void fireOnLoginFailed() {
403
404     ArrayList<JPluginEngineListener> clonePluginEngineListener;
405     
406
407     synchronized( this ) {
408       clonePluginEngineListener = ( ArrayList<JPluginEngineListener> ) pluginEngineListener.clone();
409     }
410     
411     // Dispatch the event through the listener
412
if( clonePluginEngineListener.size() > 0 ) {
413       for( int nCount = 0; nCount < clonePluginEngineListener.size(); nCount++ )
414         ( ( JPluginEngineListener ) clonePluginEngineListener.get( nCount ) ).onPluginConnectionStatusChanged( this, JPluginEngineListener.DISCONNECTED );
415     }
416   }
417   
418   /**
419    * Fire the onLogout plugin event;
420    * This is a asynchronous event.
421    */

422   public final void fireOnLogout() {
423   
424     Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
425          
426       public void run() {
427            
428         ArrayList<JMessengerEventListener> clone;
429         ArrayList<JPluginEngineListener> clonePluginEngineListener;
430         JPlugin selfObjectCopy = selfObject;
431
432
433         /*
434          * Event generated after previous destruction.
435          * Generally called from onLogout() plugin implementation.
436          */

437         if( selfObjectCopy == null )
438           return;
439         
440         synchronized( selfObjectCopy ) {
441           clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
442           clonePluginEngineListener = ( ArrayList<JPluginEngineListener> ) pluginEngineListener.clone();
443           getKernelManager().getContactListManager().setAllItemsStatus( selfObjectCopy, STATUS_OFFLINE );
444
445           // Mark plugin as destroyed
446
if( selfObjectCopy.bDestroying )
447             selfObject = null;
448         }
449         
450         if( selfObjectCopy.isConnected() ) {
451           // Dispatch the event through the listener
452
if( clone.size() > 0 ) {
453             for( int nCount = 0; nCount < clone.size(); nCount++ )
454               ( ( JMessengerEventListener ) clone.get( nCount ) ).onLogout();
455           }
456         }
457        
458         // Dispatch the event through the listener
459
if( clonePluginEngineListener.size() > 0 ) {
460           for( int nCount = 0; nCount < clonePluginEngineListener.size(); nCount++ )
461             ( ( JPluginEngineListener ) clonePluginEngineListener.get( nCount ) ).onPluginConnectionStatusChanged( selfObjectCopy, JPluginEngineListener.DISCONNECTED );
462         }
463
464         // Plugin must be destroyed ???
465
if( selfObjectCopy.bDestroying ) {
466           synchronized( selfObjectCopy ) {
467             synchronized( hPluginStatus ) {
468               hPluginStatus.clear();
469             }
470             messengerEventListener.clear();
471             pluginEngineListener.clear();
472             registrationListener.clear();
473             userInfo.destroy();
474             messengerEventListener = null;
475             pluginEngineListener = null;
476             registrationListener = null;
477             hPluginStatus = null;
478             pluginProperties = null;
479             userInfo = null;
480             bDestroying = false;
481             onDestroy();
482           }
483         }
484       }
485     } );
486
487     runner.start();
488   }
489   
490   // Message management methods
491
/**
492    * Fire the onSendMessage event.
493    * @param strToUser The user to send the
494    * message;
495    * @param msg The message to send;
496    * This is a asynchronous event.
497    */

498   public final boolean fireOnSendMessage( final java.lang.String JavaDoc strToUser, final java.lang.String JavaDoc msg ) {
499     
500     boolean bRet = false;
501
502     
503     if( isConnected() ) {
504
505       boolean bCanSendOfflineMsg = getPluginCapabilities().getBool( JPluginCapabilities.CAPABILITY_OFFLINE_MESSAGE );
506       JContactListItem contactItem = ( JContactListItem ) getKernelManager().getContactListManager().getContactList( selfObject ).get( strToUser );
507       int nMsgType = ( contactItem.getStatus() == STATUS_OFFLINE ? JMessengerEventListener.MESSAGE_TYPE_OFFLINE : JMessengerEventListener.MESSAGE_TYPE_ONLINE );
508
509       // Can send offline messages ????
510
if( !bCanSendOfflineMsg && ( nMsgType == JMessengerEventListener.MESSAGE_TYPE_OFFLINE ) )
511         bRet = false;
512       else
513         bRet = true;
514       
515       Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
516
517         public void run() {
518            
519           ArrayList<JMessengerEventListener> clone;
520           ArrayList<JPluginEngineListener> clonePluginEngineListener;
521       
522           synchronized( selfObject ) {
523             clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
524             clonePluginEngineListener = ( ArrayList<JPluginEngineListener> ) pluginEngineListener.clone();
525           }
526
527           // Dispatch the event through the listener
528
if( clone.size() > 0 ) {
529
530             boolean bCanSendOfflineMsg = selfObject.getPluginCapabilities().getBool( JPluginCapabilities.CAPABILITY_OFFLINE_MESSAGE );
531             JContactListItem contactItem = ( JContactListItem ) selfObject.getKernelManager().getContactListManager().getContactList( selfObject ).get( strToUser );
532             int nMsgType = ( contactItem.getStatus() == STATUS_OFFLINE ? JMessengerEventListener.MESSAGE_TYPE_OFFLINE : JMessengerEventListener.MESSAGE_TYPE_ONLINE );
533
534             // Check if plugin can send offline messages
535
if( ( bCanSendOfflineMsg && ( nMsgType == JMessengerEventListener.MESSAGE_TYPE_OFFLINE ) ) ||
536                 ( nMsgType == JMessengerEventListener.MESSAGE_TYPE_ONLINE ) ) {
537               for( int nCount = 0; nCount < clone.size(); nCount++ )
538                 ( ( JMessengerEventListener ) clone.get( nCount ) ).onSendMessage( strToUser, msg, nMsgType );
539             }
540             else {
541               for( int nCount = 0; nCount < clonePluginEngineListener.size(); nCount++ )
542                 ( ( JPluginEngineListener ) clonePluginEngineListener.get( nCount ) ).onCapabilityNotFound( selfObject, JPluginEngineListener.CAPABILITY_OFFLINE_MESSAGES );
543             }
544           }
545         }
546       } );
547
548       runner.start();
549     }
550     
551     return bRet;
552   }
553   
554   /**
555    * Fire the on receive message event.
556    * @param strFromUser The user that sent
557    * this message;
558    * @param date The date/time that this message
559    * was sent;
560    * @parm strMessage The message received;
561    */

562   public final void fireOnReceiveMessage( java.lang.String JavaDoc strFromUser, java.util.GregorianCalendar JavaDoc date, java.lang.String JavaDoc strMessage ) {
563   
564     synchronized( this ) {
565       getKernelManager().getContactListManager().saveMessage( this, strFromUser, STATUS_MESSAGE, strMessage );
566     }
567   }
568   
569   // Status management methods
570
/**
571    * Fire the onStatusChanged event.
572    * @param nStatus The new plugin status;
573    * This is a asynchronous event.
574    */

575   public final void fireOnPluginStatusChanged( final int nStatus ) {
576     
577     if( isConnected() ) {
578       Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
579          
580         public void run() {
581            
582           ArrayList<JMessengerEventListener> clone;
583       
584           synchronized( selfObject ) {
585             clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
586           }
587        
588           // Dispatch the event through the listener
589
if( clone.size() > 0 ) {
590             for( int nCount = 0; nCount < clone.size(); nCount++ )
591               ( ( JMessengerEventListener ) clone.get( nCount ) ).onPluginStatusChanged( nStatus );
592           }
593         }
594       } );
595        
596       runner.start();
597     }
598   }
599   
600   // Contact management methods
601
/**
602    * Fire the onBuddyDetails event listener;
603    * @param strUserId The user to get the details.
604    * This is a asynchronous event.
605    */

606   public final void fireOnBuddyDetails( final java.lang.String JavaDoc strUserId ) {
607
608     if( isConnected() ) {
609       Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
610          
611         public void run() {
612            
613           ArrayList<JMessengerEventListener> clone;
614       
615           synchronized( selfObject ) {
616             clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
617           }
618        
619           // Dispatch the event through the listener
620
if( clone.size() > 0 ) {
621             for( int nCount = 0; nCount < clone.size(); nCount++ )
622               ( ( JMessengerEventListener ) clone.get( nCount ) ).onBuddyDetails( strUserId );
623           }
624         }
625       } );
626        
627       runner.start();
628     }
629   }
630
631   /**
632    * Default implementation for the onBuddyStatusChanged
633    * event handler.
634    * This method update the ContactList status;
635    * @param strUserId The user id that will be updated;
636    * @param nNewStatus The new status for this item;
637    */

638   public final void fireOnBuddyStatusChanged( java.lang.String JavaDoc strUserId, int nNewStatus ) {
639
640     synchronized( this ) {
641       if( isConnected() )
642         getKernelManager().getContactListManager().setItemStatus( this, strUserId, nNewStatus );
643     }
644   }
645
646   /**
647    * Handles the onBuddyRequestAuthorization
648    * event. Since here this method cannot
649    * be overloaded more;
650    * @param strUserId The userId that's requesting
651    * contact list add authorization;
652    * @param strNickName User Alias (NickName);
653    */

654   public final void fireOnBuddyRequestAuthorization( java.lang.String JavaDoc strUserId, java.lang.String JavaDoc strNickName ) {
655
656     if( isConnected() ) {
657       
658       boolean bAuthorized = getKernelManager().getContactListManager().onBuddyRequestAuthorization( strUserId, strNickName );
659       ArrayList<JMessengerEventListener> clone;
660      
661        
662       synchronized( this ) {
663         clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
664       }
665        
666       // Dispatch the event through the listener
667
if( clone.size() > 0 ) {
668         for( int nCount = 0; nCount < clone.size(); nCount++ ) {
669           if( bAuthorized )
670             ( ( JMessengerEventListener ) clone.get( nCount ) ).onAuthorizeBuddy( strUserId );
671           else
672             ( ( JMessengerEventListener ) clone.get( nCount ) ).onUnauthorizeBuddy( strUserId );
673         }
674       }
675     }
676   }
677
678   /**
679    * Fire the OnaddUserToContactList event.
680    * @param strUserId The contact list user id added;
681    */

682   public final void fireOnAddUserToContactList( final java.lang.String JavaDoc strUserId ) {
683     
684     if( isConnected() ) {
685       Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
686          
687         public void run() {
688
689           ArrayList<JMessengerEventListener> clone;
690
691
692           synchronized( selfObject ) {
693             clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
694           }
695        
696           // Dispatch the event through the listener
697
selfObject.getKernelManager().getContactListManager().addToContactList( selfObject, strUserId, strUserId );
698           
699           if( clone.size() > 0 )
700             for( int nCount = 0; nCount < clone.size(); nCount++ )
701               ( ( JMessengerEventListener ) clone.get( nCount ) ).onAddUserToContactList( strUserId );
702
703           selfObject.fireOnBuddyDetails( strUserId );
704         }
705       } );
706     
707       runner.start();
708     }
709   }
710   
711   /**
712    * Remove a specified user from contact list and
713    * fire the event listener;
714    * @param strUserId The User's id that
715    * will be removed;
716    */

717   public final boolean fireOnRemoveUserFromContactList( final java.lang.String JavaDoc strUserId ) {
718
719     boolean bCanRemoveContactWhenDisconnected = getPluginCapabilities().getBool( JPluginCapabilities.CAPABILITY_REMOVE_CONTACT_WHEN_DISCONNECTED );
720     final ArrayList<JPluginEngineListener> clonePluginEngineListener;
721     
722
723     synchronized( this ) {
724       clonePluginEngineListener = ( ArrayList<JPluginEngineListener> ) pluginEngineListener.clone();
725     }
726     
727     if( isConnected() ) {
728       Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
729          
730         public void run() {
731
732           JContactListListener listener = selfObject.getKernelManager().getContactListManager();
733           ArrayList<JMessengerEventListener> clone;
734
735
736           if( listener != null )
737             listener.removeFromContactList( selfObject, strUserId );
738     
739           synchronized( selfObject ) {
740             clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
741           }
742        
743           // Dispatch the event through the listener
744
if( clone.size() > 0 ) {
745             for( int nCount = 0; nCount < clone.size(); nCount++ )
746               ( ( JMessengerEventListener ) clone.get( nCount ) ).onRemoveUserFromContactList( strUserId );
747           }
748         }
749       } );
750     
751       runner.start();
752     }
753     else {
754       for( int nCount = 0; nCount < clonePluginEngineListener.size(); nCount++ )
755         ( ( JPluginEngineListener ) clonePluginEngineListener.get( nCount ) ).onCapabilityNotFound( selfObject, JPluginEngineListener.CAPABILITY_REMOVE_CONTACTS_WHEN_OFFLINE );
756       
757       return false;
758     }
759     
760     return true;
761   }
762   
763   /**
764    * Fire the onRequestAuthorization
765    * event.
766    * @param strUserId The userid of user
767    * that requesting authorization.
768    * @param strNickName The user alias;
769    */

770   public final void fireOnRequestAuthorization( final java.lang.String JavaDoc strUserId, final java.lang.String JavaDoc strNickName ) {
771     
772     if( isConnected() ) {
773       Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
774          
775         public void run() {
776
777           ArrayList<JMessengerEventListener> clone;
778     
779     
780           synchronized( selfObject ) {
781             clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
782           }
783        
784           // Dispatch the event through the listener
785
if( clone.size() > 0 ) {
786             for( int nCount = 0; nCount < clone.size(); nCount++ )
787               ( ( JMessengerEventListener ) clone.get( nCount ) ).onRequestAuthorization( strUserId, strNickName );
788           }
789         }
790       } );
791     
792       runner.start();
793     }
794   }
795   
796   // User management methods
797
/**
798     * Fire the OnRegisterExistingUser event.
799     * @param strNickName The NickName of user;
800     * @param strUserId The User's id that will
801     * be registered;
802     * @param strPasswd The User password;
803     * This is a asynchronous event.
804     */

805   public final void fireOnRegisterExistingUser( final java.lang.String JavaDoc strNickName, final java.lang.String JavaDoc strUserId, final java.lang.String JavaDoc strPasswd ) {
806
807     Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
808          
809       public void run() {
810            
811         ArrayList<JMessengerEventListener> clone;
812       
813         synchronized( selfObject ) {
814           clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
815         }
816        
817         // Dispatch the event through the listener
818
if( clone.size() > 0 ) {
819           for( int nCount = 0; nCount < clone.size(); nCount++ )
820             ( ( JMessengerEventListener ) clone.get( nCount ) ).onRegisterExistingUser( strNickName, strUserId, strPasswd );
821         }
822       }
823     } );
824        
825     runner.start();
826   }
827   
828   /**
829     * Fire the onRegisterUserSucessfull event.
830     * This is a asynchronous event.
831     */

832   public final void fireOnRegisterUserSucessfull() {
833
834     Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
835          
836       public void run() {
837            
838         ArrayList<JUserRegistrationListener> clone;
839       
840         synchronized( selfObject ) {
841           clone = ( ArrayList<JUserRegistrationListener> ) registrationListener.clone();
842         }
843        
844         // Dispatch the event through the listener
845
if( clone.size() > 0 ) {
846           for( int nCount = 0; nCount < clone.size(); nCount++ )
847             ( ( JUserRegistrationListener ) clone.get( nCount ) ).onRegisterUserSucessfull();
848         }
849       }
850     } );
851        
852     runner.start();
853   }
854   
855   /**
856     * Fire the onRegisterUserFailed event.
857     * This is a asynchronous event.
858     */

859   public final void fireOnRegisterUserFailed() {
860
861     Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
862          
863       public void run() {
864            
865         ArrayList<JUserRegistrationListener> clone;
866       
867         synchronized( selfObject ) {
868           clone = ( ArrayList<JUserRegistrationListener> ) registrationListener.clone();
869         }
870        
871         // Dispatch the event through the listener
872
if( clone.size() > 0 ) {
873           for( int nCount = 0; nCount < clone.size(); nCount++ )
874             ( ( JUserRegistrationListener ) clone.get( nCount ) ).onRegisterUserFailed();
875         }
876       }
877     } );
878        
879     runner.start();
880   }
881   
882   /**
883     * Fire the OnRegisterNewUser event
884     * @param strNickName The NickName of user;
885     * @param strNewUserId The User's id that will
886     * be registered (The new user id will be returned
887     * here);
888     * @param strPasswd The User password;
889     * This is a asynchronous event.
890     */

891   public final void fireOnRegisterNewUser( final java.lang.String JavaDoc strNickName, final java.lang.StringBuffer JavaDoc strNewUserId, final java.lang.String JavaDoc strPasswd ) {
892
893     Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
894          
895       public void run() {
896            
897         ArrayList<JMessengerEventListener> clone;
898       
899         synchronized( selfObject ) {
900           clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
901         }
902        
903         // Dispatch the event through the listener
904
if( clone.size() > 0 ) {
905           for( int nCount = 0; nCount < clone.size(); nCount++ )
906             ( ( JMessengerEventListener ) clone.get( nCount ) ).onRegisterNewUser( strNickName, strNewUserId, strPasswd );
907         }
908       }
909     } );
910        
911     runner.start();
912   }
913
914   /**
915    * Fire the onUnregisterUser event
916    * This is a asynchronous event.
917    */

918   public final void fireOnUnregisterUser() {
919
920     Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
921          
922       public void run() {
923            
924         ArrayList<JMessengerEventListener> clone;
925       
926         synchronized( selfObject ) {
927           clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
928         }
929        
930         // Dispatch the event through the listener
931
if( clone.size() > 0 ) {
932           for( int nCount = 0; nCount < clone.size(); nCount++ )
933             ( ( JMessengerEventListener ) clone.get( nCount ) ).onUnregisterUser();
934         }
935       }
936     } );
937        
938     runner.start();
939   }
940   
941   /**
942     * Fire the onUnregisterUserSuccessfull event.
943     * This is a asynchronous event.
944     */

945   public final void fireOnUnregisterUserSuccessfull() {
946
947     Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
948          
949       public void run() {
950            
951         ArrayList<JUserRegistrationListener> clone;
952       
953         synchronized( selfObject ) {
954           clone = ( ArrayList<JUserRegistrationListener> ) registrationListener.clone();
955         }
956        
957         // Dispatch the event through the listener
958
if( clone.size() > 0 ) {
959           for( int nCount = 0; nCount < clone.size(); nCount++ )
960             ( ( JUserRegistrationListener ) clone.get( nCount ) ).onUnregisterUserSucessfull();
961         }
962       }
963     } );
964        
965     runner.start();
966   }
967
968   /**
969    * Fire the onChangePassword event
970    * @param strNewPasswd The new user password;
971    * This is a asynchronous event.
972    */

973   public final void fireOnChangePassword( final java.lang.String JavaDoc strNewPasswd ) {
974
975     Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
976          
977       public void run() {
978            
979         ArrayList<JMessengerEventListener> clone;
980       
981         synchronized( selfObject ) {
982           clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
983         }
984        
985         // Dispatch the event through the listener
986
if( clone.size() > 0 ) {
987           for( int nCount = 0; nCount < clone.size(); nCount++ )
988             ( ( JMessengerEventListener ) clone.get( nCount ) ).onChangePassword( strNewPasswd );
989         }
990       }
991     } );
992        
993     runner.start();
994   }
995   
996   /**
997     * Fire the onPasswordChangedSuccessfull event.
998     * This is a asynchronous event.
999     * @param nNewPasswd The new password changed;
1000    */

1001  public final void fireOnPasswordChangedSuccessfull( final String JavaDoc strNewPasswd ) {
1002
1003    Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
1004         
1005      public void run() {
1006           
1007        ArrayList<JUserRegistrationListener> clone;
1008      
1009        synchronized( selfObject ) {
1010          clone = ( ArrayList<JUserRegistrationListener> ) registrationListener.clone();
1011        }
1012       
1013        // Dispatch the event through the listener
1014
if( clone.size() > 0 ) {
1015          for( int nCount = 0; nCount < clone.size(); nCount++ )
1016            ( ( JUserRegistrationListener ) clone.get( nCount ) ).onPasswordChangedSuccessfull( strNewPasswd );
1017        }
1018      }
1019    } );
1020       
1021    runner.start();
1022  }
1023  
1024  /**
1025    * Fire the onPasswordChangedFailed event.
1026    * This is a asynchronous event.
1027    */

1028  public final void fireOnPasswordChangedFailed() {
1029
1030    Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
1031         
1032      public void run() {
1033           
1034        ArrayList<JUserRegistrationListener> clone;
1035      
1036        synchronized( selfObject ) {
1037          clone = ( ArrayList<JUserRegistrationListener> ) registrationListener.clone();
1038        }
1039       
1040        // Dispatch the event through the listener
1041
if( clone.size() > 0 ) {
1042          for( int nCount = 0; nCount < clone.size(); nCount++ )
1043            ( ( JUserRegistrationListener ) clone.get( nCount ) ).onPasswordChangedFailed();
1044        }
1045      }
1046    } );
1047       
1048    runner.start();
1049  }
1050  
1051  /**
1052   * Fire the onPrivacyChanged event.
1053   * Performs a privacy control to a user
1054   * for a plugin.
1055   * @param aContactList The array with all
1056   * contact list clone with changed and
1057   * unchanged contacts;
1058   */

1059  public final void fireOnChangePrivacy( final java.lang.Object JavaDoc[] aContactList ) {
1060    
1061    if( isConnected() ) {
1062      Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
1063         
1064        public void run() {
1065           
1066          ArrayList<JMessengerEventListener> clone;
1067          JContactListItem contactItem;
1068          JContactListItem modifiedItem;
1069          boolean bModified;
1070          boolean bNeedUpdateContactListUI = false;
1071        
1072      
1073          synchronized( selfObject ) {
1074            clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
1075          }
1076       
1077          // Dispatch the event through the listener
1078
if( clone.size() > 0 ) {
1079            for( int nCount = 0; nCount < clone.size(); nCount++ )
1080              for( int nCountItem = 0; nCountItem < aContactList.length; nCountItem++ ) {
1081                bModified = false;
1082                modifiedItem = ( JContactListItem ) aContactList[nCountItem];
1083                contactItem = ( JContactListItem ) getKernelManager().getContactListManager().getContactList( selfObject ).get( modifiedItem.getUserId() );
1084              
1085                /*
1086                // Changed in Visible list
1087                if( modifiedItem.getVisible() != contactItem.getVisible() ) {
1088                  int nPrivacyType = PRIVACY_DENY_STATUS;
1089                
1090                  if( modifiedItem.getVisible() )
1091                    nPrivacyType = PRIVACY_PERMIT_STATUS;
1092                
1093                  contactItem.setVisible( modifiedItem.getVisible() );
1094                  bModified = true;
1095                  bNeedUpdateContactListUI = true;
1096                  
1097                  ( ( JMessengerEventListener ) clone.get( nCount ) ).onChangePrivacy( modifiedItem.getUserId(), nPrivacyType );
1098                }
1099                */

1100              
1101                // Changed in blocked list
1102
if( modifiedItem.getBlocked() != contactItem.getBlocked() ) {
1103                  int nPrivacyType = PRIVACY_UNBLOCK_USER;
1104                
1105                  if( modifiedItem.getBlocked() )
1106                    nPrivacyType = PRIVACY_BLOCK_USER;
1107                
1108                  contactItem.setBlocked( modifiedItem.getBlocked() );
1109                  bModified = true;
1110                  bNeedUpdateContactListUI = true;
1111                  
1112                  ( ( JMessengerEventListener ) clone.get( nCount ) ).onChangePrivacy( modifiedItem.getUserId(), nPrivacyType );
1113                }
1114                
1115                // Persist changes in Database
1116
if( bModified )
1117                  getKernelManager().getContactListManager().updatePrivacy( contactItem );
1118              }
1119            
1120            // Dispatch the onPrivacyChanged event
1121
for( int nCount = 0; nCount < clone.size(); nCount++ )
1122              ( ( JMessengerEventListener ) clone.get( nCount ) ).onPrivacyChanged();
1123            
1124            // Need update the contact list UI ???
1125
if( bNeedUpdateContactListUI )
1126              getKernelManager().getContactListManager().updateUI();
1127          }
1128        }
1129      } );
1130
1131      runner.start();
1132    }
1133  }
1134
1135  /**
1136    * Fire the fireOnUserTyping event.
1137    */

1138  public final void fireOnUserTyping( String JavaDoc strUserId ) {
1139    
1140  }
1141
1142  // Plugin properties management
1143
/**
1144    * Fire the onPluginPropertiesUpdated event.<br>
1145    * This is a asynchronous event.<br>
1146    */

1147  public final void fireOnPluginPropertiesUpdated() {
1148
1149    if( isConnected() ) {
1150      Thread JavaDoc runner = new Thread JavaDoc( new Runnable JavaDoc() {
1151         
1152        public void run() {
1153           
1154          ArrayList<JMessengerEventListener> clone;
1155      
1156          synchronized( selfObject ) {
1157            clone = ( ArrayList<JMessengerEventListener> ) messengerEventListener.clone();
1158          }
1159       
1160          // Dispatch the event through the listener
1161
if( clone.size() > 0 ) {
1162            for( int nCount = 0; nCount < clone.size(); nCount++ )
1163              ( ( JMessengerEventListener ) clone.get( nCount ) ).onPluginPropertiesUpdated();
1164          }
1165        }
1166      } );
1167       
1168      runner.start();
1169    }
1170  }
1171  
1172  // Connection status
1173
/**
1174   * This method must be overloaded with the
1175   * network specific status retriever.
1176   */

1177  public boolean isConnected() {
1178   
1179    return false;
1180  }
1181  
1182  // Plugin properties and capabilities
1183
/**
1184   * Return the plugin properties.
1185   */

1186  public final JPluginProperties getPluginProperties() {
1187    
1188    return ( JPluginProperties ) pluginProperties.clone();
1189  }
1190  
1191  /**
1192   * Set the plugin capabilities object for the plugin based
1193   * object.
1194   * @param pluginCapabilities The new capabilities for plugin;
1195   */

1196  protected final void setPluginCapabilities( JPluginCapabilities pluginCapabilities ) {
1197    
1198    this.pluginCapabilities = pluginCapabilities;
1199  }
1200  
1201  /**
1202   * Return the plugin capabilities.
1203   */

1204  public final JPluginCapabilities getPluginCapabilities() {
1205    
1206    return ( JPluginCapabilities ) ( pluginCapabilities != null ? pluginCapabilities.clone() : JPluginCapabilities.getDefault() );
1207  }
1208
1209  // Identification methods
1210
/**
1211   * Returns the logged in user id;
1212   */

1213  public final String JavaDoc getUserId() {
1214    
1215    return ( userInfo != null ? userInfo.getUserId() : null );
1216  }
1217  
1218  /**
1219   * Return the password for logged in user.
1220   */

1221  public final String JavaDoc getPassword() {
1222    
1223    return ( userInfo != null ? userInfo.getPassword() : null );
1224  }
1225  
1226  /**
1227   * Set the alias name assigned to the
1228   * plugin current user.
1229   * @param strAliasName The new user alias;
1230   */

1231  public final void setAliasName( java.lang.String JavaDoc strAliasName ) {
1232  
1233    if( userInfo != null )
1234      userInfo.setAliasName( strAliasName );
1235  }
1236  
1237  /**
1238   * Returns the logged in user name.
1239   */

1240  public final String JavaDoc getAliasName() {
1241
1242    return ( userInfo != null ? userInfo.getAliasName() : null );
1243  }
1244  
1245  // Configuration methods
1246
/**
1247   * Return the preferences object to PlanetaMessenger.org's kernel;
1248   * Overload this method to provide the user interface for desired
1249   * preferences configuration.
1250   * The default implementation return null to kernel;
1251   */

1252  public JPluginObjectBase getPreferencesContainer() {
1253    
1254    return null;
1255  }
1256
1257  // Object overload
1258
/**
1259   * Implements the compare method
1260   * for this JPlugin class.
1261   * @param obj The Integer object that
1262   * represents the PluginId index;
1263   */

1264  public boolean equals( Object JavaDoc obj ) {
1265  
1266    Integer JavaDoc iPluginId;
1267    
1268    
1269    if( obj != null )
1270      iPluginId = ( ( obj instanceof Integer JavaDoc ) ? ( Integer JavaDoc ) obj : new Integer JavaDoc( ( ( JPlugin ) obj ).pluginProperties.getPluginId() ) );
1271    else
1272      iPluginId = new Integer JavaDoc( -1 );
1273    
1274    if( iPluginId.intValue() == pluginProperties.getPluginId() )
1275      return true;
1276    else
1277      return false;
1278  }
1279
1280  /**
1281   * Overrides the toString Method
1282   * to show the new class String
1283   * representation.
1284   */

1285  public java.lang.String JavaDoc toString() {
1286    
1287    return pluginProperties.getName() + " V " + pluginProperties.getVersion();
1288  }
1289}
1290
1291// JPlugin Class
1292
Popular Tags