source: trunk/jabberit_messenger/java_source/src/nu/fw/jeti/jabber/Backend.java @ 1014

Revision 1014, 11.4 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #552 - Inclusão do projeto Java referente ao applet do módulo.

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(){return mainFrame;}
94       
95        public Jeti getMain(){return main;}
96/*
97        public void setBackend(JabberBackend backend)
98        {
99            this.backend = backend;
100        }
101*/
102        public void login(LoginInfo loginInfo)
103        {
104                connect.login(loginInfo);
105        }
106       
107        public void autoLogin(LoginInfo loginInfo,int tries)
108        {
109                connect.autoLogin(loginInfo,tries);
110        }
111       
112        public void abortLogin()
113        {
114                connect.abort();
115        }
116
117        public JID getMyJID()
118        {
119                return Connect.getMyJID();
120        }
121       
122       
123        /**
124         * Checks if the password matches the password used to login on this account
125         * @param password the password to check
126         * @return true if the password matches the password of this account
127         */
128        public boolean isPasswordValid(String password)
129        {
130                return connect.isPasswordValid(password);
131        }
132
133        public synchronized String getIdentifier()
134        {
135                return "JETI_" + identifier++;
136        }
137
138        public String createThread()
139        {
140                return  Integer.toHexString("JETI".hashCode()) + Long.toHexString(System.currentTimeMillis());
141        }
142
143        public String getAccountInfo()
144        {
145                return connect.getAccountInfo();
146        }
147       
148        public Map getAvailableTransports()
149        {
150                return server.getAvailableTransports();
151        }
152
153        public void changeStatus(int show,String status)
154        {
155                connect.changeStatus(show, status);
156        }
157
158        public void rosterLoaded()
159        {
160                connect.connected();
161        }
162
163        public void disconnect()
164        {
165                connect.disconnect();
166        }
167       
168        public boolean isLoggedIn()
169        {
170                return connect.isLoggedIn();
171        }
172
173        public void streamError(StreamError error)
174        {
175                connect.streamError(error);
176        }
177
178        public void exit()
179        {
180                if(!Start.applet)
181                {
182                        System.out.println("This window will close in one minute");
183                        Timer t = new Timer(true);
184                        t.schedule(new TimerTask()
185                        {
186                                public void run()
187                                {
188                                        System.exit(0);
189                                }
190                        },10000);
191                }
192                               
193                //end connection
194                connect.exit();
195               
196                Frame[] frames = Frame.getFrames();
197                for(int i =0;i<frames.length;i++)
198                {//remove all opened jeti frames
199                        String name = frames[i].getClass().getName();
200                        System.out.println(name);
201                        if(name.startsWith("nu.fw.jeti"))frames[i].dispose();
202                        //frames[i].dispose();
203                        if(frames[i].isDisplayable())System.out.println(name);
204                }
205               
206                capabilitiesCache.save();
207                //unload plugins
208                start.exit();
209                //remove references'
210//              start=null;
211//              connect=null;
212//              roster=null;
213//              server=null;
214//              main=null;
215//              mainFrame=null;
216//              mainWindow =null;
217                synchronized (this)
218                {
219                        eventListeners.clear();
220                        if(presenceListeners!=null)presenceListeners.clear();
221                        if(messageListeners!=null)messageListeners.clear();
222                }
223        }
224
225        /**
226         *      send packets
227         *      please send messages that need to be logged with sendMessage
228         *  @param packet
229         */
230        public void send(Packet packet)
231        {
232                connect.send(packet);
233        }
234       
235        /**
236         * Sends a infoquery and gives the result to listener
237         * @param query the infoquery to send
238         * @param listener the IQResultListener wich listens for the result
239         * @param timeout not yet implemented
240         */
241        public void send(InfoQuery query, IQResultListener listener,int timeout)
242        {
243                iqTimerQueue.add(query,listener,timeout);
244                send(query);
245        }
246
247        /**
248         * needed to log own send messages
249         * @param message
250         */
251        public void sendMessage(Message message)
252        {
253                //if(isr
254                for(Iterator j = getListeners(OwnMessageListener.class);j.hasNext();)
255                {
256                        ((OwnMessageListener)j.next()).sendMessage(message);
257                }
258                connect.send(message);
259        }
260
261//      public void browse(JID jid, BrowseListener listener)
262//      {
263//              connect.browse(jid,listener);
264//      }
265       
266        public void getItems(JID jid, DiscoveryListener listener, boolean useCache)
267        {
268                connect.getItems(jid,listener,useCache);
269        }
270       
271        public void getItems(JID jid, DiscoveryListener listener)
272        {
273                connect.getItems(jid,listener);
274        }
275       
276        public void getInfo(JID jid, DiscoveryListener listener)
277        {
278                connect.getInfo(jid,listener);
279        }
280       
281        public void getItems(JID jid,String node, DiscoveryListener listener)
282        {
283                connect.getItems(jid,node,listener);
284        }
285       
286        public void getInfo(JID jid,String node, DiscoveryListener listener)
287        {
288                connect.getInfo(jid,node,listener);
289        }
290       
291        /**
292         * gets the current Jabber status of the user
293         * @return the current status
294         */
295        public int getStatus()
296        {
297                return connect.getStatus();
298        }
299       
300       
301//      //muc hack
302//      public void browseNotCached(JID jid, BrowseListener listener)
303//      {
304//              connect.browseNotCached(jid,listener);
305//      }
306
307        public static JIDStatus getJIDStatus(JID jid)
308        {
309                return Roster.getJIDStatus(jid);
310        }
311
312        public String[] getAllGroups()
313        {
314                return roster.getAllGroups();
315        }
316
317        /**
318         * makes a new account
319         * (the current connection will be logged off)
320         * @param name Username (may be null)
321         * @param password User password (may be null)
322         */
323        public void newAccount(String server,int port,String name,String password)
324        {
325                disconnect();
326                new NewAccount(server,port,handlers,name,password);
327        }
328       
329        public void setCapability(JID jid, IQXCaps caps){
330                capabilitiesCache.setCapability(jid, caps);
331        }
332       
333        /**
334         * Has JID jid capability capability
335         * @param jid
336         * @param capability
337         * @return true if the jid has the capability
338         */
339        public boolean hasCapability(JID jid, String capability){
340                return capabilitiesCache.hasCapability(jid, capability);
341        }
342       
343        public void addCapability(String capability,String feature){
344                connect.addCapability(capability, feature);
345        }
346       
347        public void removeCapability(String capability,String feature){
348                connect.removeCapability(capability, feature);
349        }
350       
351       
352       
353        public void addExtensionHandler(String namespace,ExtensionHandler handler)
354        {
355                handlers.addExtensionHandler(namespace,handler);
356        }
357       
358        public void removeExtensionHandler(String namespace)
359        {
360                handlers.removeExtensionHandler(namespace);
361        }
362       
363        /**
364         * Add a listener for a specific presence
365         * @param jid The JID of which the presence is to be monitored (resource is not considered)
366         * @param listener The class that is interested in presence events
367         */
368        public synchronized void addPresenceListener(JID jid, PresenceListener listener)
369        {
370                 if(presenceListeners == null)presenceListeners = new HashMap();
371                 presenceListeners.put(jid,listener);
372        }
373       
374        /**
375         * removes a presencelistener
376         * @param jid The JID of which presence is monitored
377         */
378        public synchronized void removePresenceListener(JID jid)
379        {
380                if(presenceListeners==null)return;
381                presenceListeners.remove(jid);
382                if (presenceListeners.isEmpty()) presenceListeners = null;
383        }
384       
385        /**
386         * Gets the presenceListener which is monitoring this JID
387         * @param jid The JID of which the presenceListener is requested
388         * @return PresenceListener
389         */
390        public synchronized PresenceListener getPresenceListener(JID jid)
391        {
392                if(presenceListeners == null) return null;
393                return (PresenceListener)presenceListeners.get(jid);
394        }
395       
396        /**
397         * Add a listener for messages to this JID
398         * @param jid The JID of which the message is to be monitored (resource is not considered)
399         * @param listener The class that is interested in message events
400         */
401        public synchronized void addMessageListener(JID jid, MessageListener listener)
402        {
403                 if(messageListeners == null)messageListeners = new HashMap();
404                 messageListeners.put(jid,listener);
405        }
406       
407        /**
408         * removes a messagelistener
409         * @param jid The JID of which messages are monitored
410         */
411        public synchronized void removeMessageListener(JID jid)
412        {
413                if(messageListeners==null)return;
414                messageListeners.remove(jid);
415                if (messageListeners.isEmpty()) messageListeners = null;
416        }
417       
418        /**
419         * Gets the MessageListener which is monitoring this JID
420         * @param jid The JID of which the messageListener is requested
421         * @return MessageListener
422         */
423        public synchronized MessageListener getMessageListener(JID jid)
424        {
425                if(messageListeners == null) return null;
426                return (MessageListener)messageListeners.get(jid);
427        }
428       
429        public synchronized void addListener(Class type, JETIListener listener)
430        {//class meegeven?
431                LinkedList list = (LinkedList) eventListeners.get(type.getName());
432                if(list == null)
433                {
434                        list = new LinkedList();
435                    eventListeners.put(type.getName(),list);
436                }
437                list.add(listener);
438        }
439
440        public synchronized void removeListener(Class type, JETIListener listener)
441        {
442                LinkedList list = (LinkedList) eventListeners.get(type.getName());
443                if(list == null) return;
444                list.remove(listener);
445                if(list.isEmpty()) eventListeners.remove(type.getName());//good idea?
446        }
447
448        public synchronized Iterator getListeners(final Class type)
449        {
450                LinkedList listeners = (LinkedList)eventListeners.get(type.getName());
451                if(listeners == null)
452                {//no listeners so return a empty iterator, if happens use isRegisteredListener(class type)
453                        return new Iterator()
454                        {//empty iterator
455                                public Object next()
456                                {
457                                        throw new NoSuchElementException();
458                                }
459                                public boolean hasNext()
460                                {
461                                        //System.out.println(type + " listener not registerd");
462                                        //throw new RuntimeException();
463                                        return false;
464                                }
465                                public void remove()
466                                {
467                                        throw new UnsupportedOperationException();
468                                }
469                        };
470                }
471                return new LinkedList(listeners).iterator();//no concurrentmodexcep
472        }
473}
474
475/*
476 * Overrides for emacs
477 * Local variables:
478 * tab-width: 4
479 * End:
480 */
Note: See TracBrowser for help on using the repository browser.