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