source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/jabber/Backend.java @ 3102

Revision 3102, 11.4 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1/*
2 *      Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer 
3 *
4 *  This program is free software; you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation; either version 2 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *      GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with this program; if not, write to the Free Software
16 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 *
18 *      For questions, comments etc,
19 *      use the website at http://jeti.jabberstudio.org
20 *  or mail/im me at jeti@jabber.org
21 */
22package nu.fw.jeti.jabber;
23
24import java.awt.Container;
25import java.awt.Frame;
26import java.awt.Window;
27import java.util.*;
28
29import javax.swing.JFrame;
30
31import nu.fw.jeti.backend.*;
32import nu.fw.jeti.backend.roster.Roster;
33import nu.fw.jeti.backend.roster.Server;
34import nu.fw.jeti.events.*;
35import nu.fw.jeti.jabber.elements.IQXCaps;
36import nu.fw.jeti.jabber.elements.InfoQuery;
37import nu.fw.jeti.jabber.elements.Message;
38import nu.fw.jeti.jabber.elements.Packet;
39import nu.fw.jeti.jabber.elements.StreamError;
40import nu.fw.jeti.jabber.handlers.ExtensionHandler;
41import nu.fw.jeti.ui.Jeti;
42
43/**
44 * @author E.S. de Boer
45  */
46
47public class Backend
48{
49    private Map eventListeners;
50        private Map presenceListeners;
51        private Map messageListeners;   
52        private int identifier;
53        private Connect connect;
54        private Roster roster;
55        private Server server;
56        private Jeti main;
57        private JFrame mainFrame;
58        private Container mainWindow;
59        private Start start;
60        private IQTimerQueue iqTimerQueue;
61        private Handlers handlers;
62        private CapabilitiesCache capabilitiesCache;
63       
64       
65    public Backend(Start start)
66    {
67                eventListeners = new HashMap(10);
68                iqTimerQueue = new IQTimerQueue();
69                handlers = new Handlers();
70                connect = new Connect(this,iqTimerQueue,handlers);
71                server =new Server(this);
72                roster = new Roster(this,server);
73                capabilitiesCache = new CapabilitiesCache(this);
74                this.start = start;
75    }
76   
77        public void setMain(Jeti main,Window window, JFrame frame)
78        {
79                this.main = main;
80                this.mainWindow = window;
81                this.mainFrame = frame;
82        }
83
84        public Container getMainWindow()
85        {//TODO bit of a hack, think of something nicer
86                if( mainWindow == null )
87                {
88                        mainWindow=main.getTopLevelAncestor();
89                }
90                return mainWindow;
91        }
92       
93        public JFrame getMainFrame()
94        {
95                return mainFrame;
96        }
97       
98        public Jeti getMain(){return main;}
99/*
100        public void setBackend(JabberBackend backend)
101        {
102            this.backend = backend;
103        }
104*/
105        public void login(LoginInfo loginInfo)
106        {
107                connect.login(loginInfo);
108        }
109       
110        public void autoLogin(LoginInfo loginInfo,int tries)
111        {
112                connect.autoLogin(loginInfo,tries);
113        }
114       
115        public void abortLogin()
116        {
117                connect.abort();
118        }
119
120        public JID getMyJID()
121        {
122                return Connect.getMyJID();
123        }
124       
125        /**
126         * Checks if the password matches the password used to login on this account
127         * @param password the password to check
128         * @return true if the password matches the password of this account
129         */
130        public boolean isPasswordValid(String password)
131        {
132                return connect.isPasswordValid(password);
133        }
134
135        public synchronized String getIdentifier()
136        {
137                return "JETI_" + identifier++;
138        }
139
140        public String createThread()
141        {
142                return  Integer.toHexString("JETI".hashCode()) + Long.toHexString(System.currentTimeMillis());
143        }
144
145        public String getAccountInfo()
146        {
147                return connect.getAccountInfo();
148        }
149       
150        public Map getAvailableTransports()
151        {
152                return server.getAvailableTransports();
153        }
154
155        public void changeStatus(int show,String status)
156        {
157                connect.changeStatus(show, status);
158        }
159
160        public void rosterLoaded()
161        {
162                connect.connected();
163        }
164
165        public void disconnect()
166        {
167                connect.disconnect();
168        }
169       
170        public boolean isLoggedIn()
171        {
172                return connect.isLoggedIn();
173        }
174
175        public void streamError(StreamError error)
176        {
177                connect.streamError(error);
178        }
179
180        public void exit()
181        {
182                if(!Start.applet)
183                {
184                        System.out.println("This window will close in one minute");
185                        Timer t = new Timer(true);
186                        t.schedule(new TimerTask()
187                        {
188                                public void run()
189                                {
190                                        System.exit(0);
191                                }
192                        },10000);
193                }
194                               
195                //end connection
196                connect.exit();
197               
198                Frame[] frames = Frame.getFrames();
199                for(int i =0;i<frames.length;i++)
200                {//remove all opened jeti frames
201                        String name = frames[i].getClass().getName();
202                        System.out.println(name);
203                        if(name.startsWith("nu.fw.jeti"))frames[i].dispose();
204                        //frames[i].dispose();
205                        if(frames[i].isDisplayable())System.out.println(name);
206                }
207               
208                capabilitiesCache.save();
209                //unload plugins
210                start.exit();
211                //remove references'
212//              start=null;
213//              connect=null;
214//              roster=null;
215//              server=null;
216//              main=null;
217//              mainFrame=null;
218//              mainWindow =null;
219                synchronized (this)
220                {
221                        eventListeners.clear();
222                        if(presenceListeners!=null)presenceListeners.clear();
223                        if(messageListeners!=null)messageListeners.clear();
224                }
225        }
226
227        /**
228         *      send packets
229         *      please send messages that need to be logged with sendMessage
230         *  @param packet
231         */
232        public void send(Packet packet)
233        {
234                connect.send(packet);
235        }
236       
237        /**
238         * Sends a infoquery and gives the result to listener
239         * @param query the infoquery to send
240         * @param listener the IQResultListener wich listens for the result
241         * @param timeout not yet implemented
242         */
243        public void send(InfoQuery query, IQResultListener listener,int timeout)
244        {
245                iqTimerQueue.add(query,listener,timeout);
246                send(query);
247        }
248
249        /**
250         * needed to log own send messages
251         * @param message
252         */
253        public void sendMessage(Message message)
254        {
255                //if(isr
256                for(Iterator j = getListeners(OwnMessageListener.class);j.hasNext();)
257                {
258                        ((OwnMessageListener)j.next()).sendMessage(message);
259                }
260                connect.send(message);
261        }
262
263//      public void browse(JID jid, BrowseListener listener)
264//      {
265//              connect.browse(jid,listener);
266//      }
267       
268        public void getItems(JID jid, DiscoveryListener listener, boolean useCache)
269        {
270                connect.getItems(jid,listener,useCache);
271        }
272       
273        public void getItems(JID jid, DiscoveryListener listener)
274        {
275                connect.getItems(jid,listener);
276        }
277       
278        public void getInfo(JID jid, DiscoveryListener listener)
279        {
280                connect.getInfo(jid,listener);
281        }
282       
283        public void getItems(JID jid,String node, DiscoveryListener listener)
284        {
285                connect.getItems(jid,node,listener);
286        }
287       
288        public void getInfo(JID jid,String node, DiscoveryListener listener)
289        {
290                connect.getInfo(jid,node,listener);
291        }
292       
293        /**
294         * gets the current Jabber status of the user
295         * @return the current status
296         */
297        public int getStatus()
298        {
299                return connect.getStatus();
300        }
301       
302       
303//      //muc hack
304//      public void browseNotCached(JID jid, BrowseListener listener)
305//      {
306//              connect.browseNotCached(jid,listener);
307//      }
308
309        public static JIDStatus getJIDStatus(JID jid)
310        {
311                return Roster.getJIDStatus(jid);
312        }
313
314        public String[] getAllGroups()
315        {
316                return roster.getAllGroups();
317        }
318
319        /**
320         * makes a new account
321         * (the current connection will be logged off)
322         * @param name Username (may be null)
323         * @param password User password (may be null)
324         */
325        public void newAccount(String server,int port,String name,String password)
326        {
327                disconnect();
328                new NewAccount(server,port,handlers,name,password);
329        }
330       
331        public void setCapability(JID jid, IQXCaps caps){
332                capabilitiesCache.setCapability(jid, caps);
333        }
334       
335        /**
336         * Has JID jid capability capability
337         * @param jid
338         * @param capability
339         * @return true if the jid has the capability
340         */
341        public boolean hasCapability(JID jid, String capability){
342                return capabilitiesCache.hasCapability(jid, capability);
343        }
344       
345        public void addCapability(String capability,String feature){
346                connect.addCapability(capability, feature);
347        }
348       
349        public void removeCapability(String capability,String feature){
350                connect.removeCapability(capability, feature);
351        }
352       
353       
354       
355        public void addExtensionHandler(String namespace,ExtensionHandler handler)
356        {
357                handlers.addExtensionHandler(namespace,handler);
358        }
359       
360        public void removeExtensionHandler(String namespace)
361        {
362                handlers.removeExtensionHandler(namespace);
363        }
364       
365        /**
366         * Add a listener for a specific presence
367         * @param jid The JID of which the presence is to be monitored (resource is not considered)
368         * @param listener The class that is interested in presence events
369         */
370        public synchronized void addPresenceListener(JID jid, PresenceListener listener)
371        {
372                 if(presenceListeners == null)presenceListeners = new HashMap();
373                 presenceListeners.put(jid,listener);
374        }
375       
376        /**
377         * removes a presencelistener
378         * @param jid The JID of which presence is monitored
379         */
380        public synchronized void removePresenceListener(JID jid)
381        {
382                if(presenceListeners==null)return;
383                presenceListeners.remove(jid);
384                if (presenceListeners.isEmpty()) presenceListeners = null;
385        }
386       
387        /**
388         * Gets the presenceListener which is monitoring this JID
389         * @param jid The JID of which the presenceListener is requested
390         * @return PresenceListener
391         */
392        public synchronized PresenceListener getPresenceListener(JID jid)
393        {
394                if(presenceListeners == null) return null;
395                return (PresenceListener)presenceListeners.get(jid);
396        }
397       
398        /**
399         * Add a listener for messages to this JID
400         * @param jid The JID of which the message is to be monitored (resource is not considered)
401         * @param listener The class that is interested in message events
402         */
403        public synchronized void addMessageListener(JID jid, MessageListener listener)
404        {
405                 if(messageListeners == null)messageListeners = new HashMap();
406                 messageListeners.put(jid,listener);
407        }
408       
409        /**
410         * removes a messagelistener
411         * @param jid The JID of which messages are monitored
412         */
413        public synchronized void removeMessageListener(JID jid)
414        {
415                if(messageListeners==null)return;
416                messageListeners.remove(jid);
417                if (messageListeners.isEmpty()) messageListeners = null;
418        }
419       
420        /**
421         * Gets the MessageListener which is monitoring this JID
422         * @param jid The JID of which the messageListener is requested
423         * @return MessageListener
424         */
425        public synchronized MessageListener getMessageListener(JID jid)
426        {
427                if(messageListeners == null) return null;
428                return (MessageListener)messageListeners.get(jid);
429        }
430       
431        public synchronized void addListener(Class type, JETIListener listener)
432        {//class meegeven?
433                LinkedList list = (LinkedList) eventListeners.get(type.getName());
434                if(list == null)
435                {
436                        list = new LinkedList();
437                    eventListeners.put(type.getName(),list);
438                }
439                list.add(listener);
440        }
441
442        public synchronized void removeListener(Class type, JETIListener listener)
443        {
444                LinkedList list = (LinkedList) eventListeners.get(type.getName());
445                if(list == null) return;
446                list.remove(listener);
447                if(list.isEmpty()) eventListeners.remove(type.getName());//good idea?
448        }
449
450        public synchronized Iterator getListeners(final Class type)
451        {
452                LinkedList listeners = (LinkedList)eventListeners.get(type.getName());
453                if(listeners == null)
454                {//no listeners so return a empty iterator, if happens use isRegisteredListener(class type)
455                        return new Iterator()
456                        {//empty iterator
457                                public Object next()
458                                {
459                                        throw new NoSuchElementException();
460                                }
461                                public boolean hasNext()
462                                {
463                                        //System.out.println(type + " listener not registerd");
464                                        //throw new RuntimeException();
465                                        return false;
466                                }
467                                public void remove()
468                                {
469                                        throw new UnsupportedOperationException();
470                                }
471                        };
472                }
473                return new LinkedList(listeners).iterator();//no concurrentmodexcep
474        }
475}
476
477/*
478 * Overrides for emacs
479 * Local variables:
480 * tab-width: 4
481 * End:
482 */
Note: See TracBrowser for help on using the repository browser.