source: trunk/jabberit_messenger/java_source/src/nu/fw/jeti/applet/Jeti.java @ 1097

Revision 1097, 13.1 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #571 - Removido o arquivo plugins.xml, usado pelo applet ( Java ) para carregar os plugins.

Line 
1package nu.fw.jeti.applet;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.net.MalformedURLException;
6import java.net.URL;
7import java.text.MessageFormat;
8import java.util.LinkedList;
9import java.util.List;
10import java.util.Map;
11
12import javax.swing.JApplet;
13import javax.swing.JLabel;
14import javax.swing.JPanel;
15import javax.swing.SwingUtilities;
16
17import nu.fw.jeti.backend.LoginInfo;
18import nu.fw.jeti.backend.Start;
19import nu.fw.jeti.events.StatusChangeListener;
20import nu.fw.jeti.jabber.Backend;
21import nu.fw.jeti.jabber.JID;
22import nu.fw.jeti.jabber.UnknownJIDStatus;
23import nu.fw.jeti.jabber.elements.IQPrivate;
24import nu.fw.jeti.jabber.elements.InfoQuery;
25import nu.fw.jeti.jabber.elements.InfoQueryBuilder;
26import nu.fw.jeti.jabber.elements.Presence;
27import nu.fw.jeti.jabber.elements.RosterBuilder;
28import nu.fw.jeti.jabber.elements.RosterItemBuilder;
29import nu.fw.jeti.ui.AddContact;
30import nu.fw.jeti.ui.JIDInput;
31
32import nu.fw.jeti.ui.StatusButton;
33
34import nu.fw.jeti.util.I18N;
35import nu.fw.jeti.util.Popups;
36import nu.fw.jeti.util.Preferences;
37
38import netscape.javascript.JSObject;
39
40/*
41 *      @author E.S. de Boer
42 *      Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer 
43 *
44 *  This program is free software; you can redistribute it and/or modify
45 *  it under the terms of the GNU General Public License as published by
46 *  the Free Software Foundation; either version 2 of the License, or
47 *  (at your option) any later version.
48 *
49 *  This program is distributed in the hope that it will be useful,
50 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
51 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52 *      GNU General Public License for more details.
53 *
54 *  You should have received a copy of the GNU General Public License
55 *  along with this program; if not, write to the Free Software
56 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
57 *
58 *      For questions, comments etc,
59 *      use the website at http://jeti.jabberstudio.org
60 *  or jabber/mail me at jeti@jabber.org
61 *
62 *      Created on 14-nov-2003
63 */
64
65public class Jeti extends JApplet
66{
67        private Start start;
68        private static Jeti applet;
69        private URL exitURL;
70        private static String groupchatRoom;
71        private static String groupchatServer;
72        private Backend backend;
73        private boolean started = false;
74        private SecureMethodRunner secureMethodInvoker;
75        public static JLabel from;
76        public static LoginInfo loginInfo;
77        public static String SERVEREXPRESSO;
78        public static String NAMECOMPANY;
79        public static String USEHTTPS;
80        public static String CNNAME;
81        public static JSObject WINDOWAPPLET;
82        public static List GROUPSUSER;
83        public static String SERVERJABBER;
84        public static String COUNTRY;
85        public static String LANGUAGE;
86        public static String JAVAPLUGINS;
87       
88        /**
89         * @Author      : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
90         * @date        : 10/06/2009
91         * @Brief       : A variavel DEBUG_APPLET_VIEWER = true, para usar o appletviewer
92         */
93        public static boolean DEBUG_APPLET_VIEWER = false;
94       
95        public void init()
96        {
97                applet = this;
98
99                Thread t = new Thread()
100                {
101                        public void run()
102                        {
103                                String server = new String(applet.getParameter("SERVER"));
104                                String portText = new String(applet.getParameter("PORT"));
105                                boolean ssl = Boolean.valueOf(applet.getParameter("SSL")).booleanValue();
106                                String user = new String(applet.getParameter("USER"));
107                                String password = new String(applet.getParameter("PASSWORD"));
108                                String resource = new String(applet.getParameter("RESOURCE"));
109                                String host = new String(applet.getParameter("HOST"));
110                                String useJavaProxy = new String(applet.getParameter("USEPROXY").toUpperCase());
111                                String serverExpresso = new String(applet.getParameter("EXPRESSO"));
112                                String useHttps = new String(applet.getParameter("USEHTTPS"));
113                                String nameCompany = new String(applet.getParameter("NAMECOMPANY"));
114                                String cnName = new String(applet.getParameter("CNNAME"));
115                                String country = new String(applet.getParameter("COUNTRY"));
116                                String language = new String(applet.getParameter("LANGUAGE"));
117                                String javaPlugins = new String(applet.getParameter("JAVAPLUGINS"));
118
119                                /**
120                                 * @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
121                                 * @Date : 20/10/2008
122                                 * @Description : - Nome Servidor IM-Jabber para Constant SERVERJABBER
123                                 *                                - Nome da Maquina Expresso para Constant SERVEREXPRESSO
124                                 *                                - Nome da Empresa para Constant NAMECOMPANY
125                                 *                                - Informa qual o tipo de protocolo a ser usado http ou https.
126                                 *                                - Passa ao java o cn do ldap do servidor expresso.
127                                 *                                - Carrega plugins para o Java.                                                 
128                                 */
129                               
130                                SERVERJABBER = server; 
131                                SERVEREXPRESSO = serverExpresso;
132                                NAMECOMPANY = nameCompany;
133                                USEHTTPS = useHttps;
134                                CNNAME = cnName;
135                                LANGUAGE = language;
136                                COUNTRY = country;
137                                JAVAPLUGINS = javaPlugins;
138
139                                // Reinit languages incase they changed
140                                I18N.init(language, country);
141                               
142                                groupchatRoom = getParameter("GROUPCHATROOM");
143                                groupchatServer = getParameter("GROUPCHATSERVER");
144                               
145                                try
146                                {
147                     exitURL = new URL(getDocumentBase(),getParameter("EXITPAGE"));
148                                }
149                                catch (MalformedURLException e){}
150                               
151                                if( server !=null || resource != null || portText !=null )
152                                {
153                                        int port = Integer.parseInt(portText);
154                                       
155                                        if( useJavaProxy.equals("TRUE"))
156                                                loginInfo = new LoginInfo(server,host,user,password,resource,port,ssl,0);
157                                        else
158                                                loginInfo = new LoginInfo(server,host,user,password,resource,port,ssl,0,false);
159                                }
160
161                                Start.programURL = getCodeBase();
162                                Start.applet = true;
163                                final JPanel panel = new JPanel(new BorderLayout());
164                                start = new Start( getCodeBase().toString(), panel );
165                               
166                                backend = start.getBackend();
167                                backend.getMain().translate();
168                               
169                                if( loginInfo != null && loginInfo.getPassword() != null)
170                                {
171                                        backend.autoLogin(loginInfo,10);
172
173                                        secureMethodInvoker = new SecureMethodRunner(backend);
174                                        secureMethodInvoker.start();
175                                       
176                                        backend.addExtensionHandler("jeti:prefs",new PreferencesHandler());
177                                        try
178                                        {
179                                                backend.addListener(StatusChangeListener.class,new StatusChangeListener()
180                                                {
181                                                        public void connectionChanged(boolean online)
182                                                        {
183                                                                if( online)
184                                                                {
185                                                                        backend.send(new InfoQuery("get",new IQPrivate(new JetiPrivatePreferencesExtension())));
186                                                                        String chatTO = getParameter("CHATTO");
187                                                                        if(chatTO!=null && chatTO.length()>0)
188                                                                {
189                                                                        if(Boolean.valueOf(getParameter("SHOWCHATTOASMAIN")).booleanValue())
190                                                                        {
191                                                                                setContentPane(backend.getMain().createChatPanel(new UnknownJIDStatus(JID.jidFromString(chatTO))));
192                                                                                validate();
193                                                                        }
194                                                                        else backend.getMain().startChat(JID.jidFromString(chatTO));
195                                                                        }
196                                                                }
197                                                        }
198                                                        public void ownPresenceChanged(int show, String status) {}
199                                                        public void exit(){}
200                                                });
201                                        }
202                                        catch(NoClassDefFoundError e)
203                                        {       
204                                                e.printStackTrace();
205                                        }
206                                       
207                                        new Popups(Jeti.this);
208                                        SwingUtilities.invokeLater(new Runnable()
209                                        {
210                                                public void run()
211                                                {
212                                                        setContentPane(panel);
213                                                        validate();
214                                                }
215                                        });
216                                       
217                                        started = true;
218                                }else
219                                        started = false;
220                        }
221                };
222               
223                t.start();
224                splash();
225               
226                if ( !DEBUG_APPLET_VIEWER )
227                {       
228                        JSObject winApplet = JSObject.getWindow(this); 
229                        WINDOWAPPLET = winApplet;
230                }
231        }
232                       
233        private void splash()
234        {
235                /**
236                 * Splash - Inicializador -
237                 * Modificado by Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
238                 */
239               
240       JLabel label = new JLabel(  "<html>" +
241                                                                   "<span style='margin-left:10px;font:20pt Verdana,Arial,serif'>Expresso IM </span>" +
242                                                                   "<br/>" +
243                                                                   "<span style='font:9pt Verdana,Arial,serif'> Jeti - http://www.jeti-im.org </span></html>",
244                                                                   JLabel.CENTER );
245       
246       label.setBackground(new Color(255,255,255));
247       getContentPane().setBackground(new Color(255,255,255));
248       getContentPane().add(label,BorderLayout.CENTER);
249       
250       JPanel panel = new JPanel();
251       panel.setBackground(new Color(255,255,255));
252       from = new JLabel(NAMECOMPANY, JLabel.CENTER);
253       from.setBackground(new Color(255,255,255));
254       from.setFont(new java.awt.Font("Serif,Verdana,Arial",1,10));
255       panel.add(from);
256       getContentPane().add(panel,BorderLayout.SOUTH);
257        }
258       
259        public String getGroupsToExpresso()
260        {
261                String[] Groups = applet.backend.getAllGroups();
262                String groups = "";
263               
264                for(int i = 0 ; i < Groups.length; i++ )
265                        groups += Groups[i] + ";";
266               
267                return groups.toString();
268        }
269       
270        public void changeStatusfromExpresso(int status, String msgStatus)
271        {
272                switch (status)
273        {
274                    case Presence.AVAILABLE:     
275                        StatusButton.changeStatusExpresso(Presence.AVAILABLE, msgStatus);
276                        break;
277                    case Presence.FREE_FOR_CHAT:
278                        StatusButton.changeStatusExpresso(Presence.FREE_FOR_CHAT, msgStatus);
279                        break;
280                    case Presence.AWAY:
281                        StatusButton.changeStatusExpresso(Presence.AWAY, msgStatus);
282                        break;
283                    case Presence.XA:           
284                        StatusButton.changeStatusExpresso(Presence.XA, msgStatus);
285                        break;
286                    case Presence.DND:           
287                        StatusButton.changeStatusExpresso(Presence.DND, msgStatus);
288                        break;
289                    case Presence.UNAVAILABLE:   
290                        StatusButton.changeStatusExpresso(Presence.UNAVAILABLE, msgStatus);
291                        break;
292                    case Presence.INVISIBLE:     
293                        StatusButton.changeStatusExpresso(Presence.INVISIBLE, msgStatus);
294                        break;
295        }
296        }
297       
298        public static void showURL(String url)
299        {
300                try
301                {
302                        applet.getAppletContext().showDocument(new URL(url),"_blank");
303                }
304                catch (MalformedURLException e){}
305        }
306               
307        public void destroy()
308        {
309                try
310                {
311                        if( secureMethodInvoker != null )
312                                secureMethodInvoker.stopRunning();
313                        start.close();
314                }
315                catch (Exception e)
316                {
317                        //catch all exceptions else jvm hangs
318                        e.printStackTrace();
319                }
320        }
321               
322        public static void exit()
323        {
324                applet.getAppletContext().showDocument(applet.exitURL);
325        }
326       
327        public boolean isReady()
328        {
329                System.out.println("ready " + started);
330                return started;
331        }
332       
333        public boolean isLoggedIn()
334        {
335                if( !started )
336                        return false;
337               
338                return backend.isLoggedIn();
339        }
340       
341        public void openChat(String jid)
342        {
343                try
344                {
345                        JID j = JID.checkedJIDFromString(jid);
346                        secureMethodInvoker.addData(new Object[]{"openChat",j});
347                }
348                catch (InstantiationException e)
349                {
350                        e.printStackTrace();
351                }
352
353        }
354       
355        public void addContact(String jid, String name, String group)
356        {
357                /**
358                 * @Author(s) : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
359                 * @Date : 24/04/2009
360                 * @Brief : Adiciona novos contatos e ja pede autorizacao.
361                 */
362                try
363                {
364                        JID Jid = JID.checkedJIDFromString(jid);
365                        backend.send(new Presence(Jid, "subscribe"));
366                        RosterBuilder rb = new RosterBuilder();
367                        RosterItemBuilder rib = new RosterItemBuilder();
368                        rib.addGroup(group);
369                        rib.jid = Jid;
370                        rib.name = name;
371                        rib.ask = "subscribe";
372                        rib.subscription = "none";
373                        rb.addItem(rib.build());
374                        InfoQueryBuilder iqb = new InfoQueryBuilder();
375                        iqb.addExtension(rb.build());
376                        iqb.setType("set");
377                        backend.send(iqb.build());
378                }
379                catch(Exception error)
380                {
381                        error.printStackTrace();
382                }
383        }
384       
385        public void login(String username,String server,String password)
386        {
387                if( backend.isLoggedIn() )
388                {
389                        JID j = backend.getMyJID();
390                       
391                        if( j.getDomain().equals(server) && j.getUser().equals(username) )
392                                return;
393                }
394                secureMethodInvoker.addData(new Object[]{"login",username,server,password});
395        }
396       
397        public static Class getPlugin(String name)
398        {
399                try
400        {
401            return Class.forName("nu.fw.jeti.plugins."+name+".Plugin");
402                }
403        catch (ClassNotFoundException e)
404                {
405                        System.err.println(MessageFormat.format(I18N.gettext("main.error.{0}_plugin_not_found"), new Object[]{name}));
406                        return null;
407                }
408        }
409       
410        public static void getPrefPanel(String name,Map loadedPreferencePanels)
411        {
412                Class prefPanel = null;
413                try
414                {
415                        prefPanel = Class.forName("nu.fw.jeti.plugins."+name+".PrefPanel");
416                }
417                catch (ClassNotFoundException e)
418                {
419                        return;
420                }
421                loadedPreferencePanels.put(name,prefPanel);
422        }
423       
424        static class SecureMethodRunner extends Thread
425        {
426                private Backend backend;
427                private LinkedList queue = new LinkedList();
428                private volatile boolean isRunning=true;
429
430            SecureMethodRunner(Backend backend)
431                {
432                this.backend = backend;
433                }
434               
435            public void addData(Object method)
436            {
437                synchronized(queue)
438                        {
439                                queue.addLast(method);
440                                queue.notifyAll();
441                        }
442            }
443           
444            public void stopRunning()
445            {
446                isRunning = false;
447                        synchronized(queue){queue.notifyAll();}
448            }
449
450            public void run()
451                {
452                        Object[] method;
453                        while (isRunning)
454                        {
455                                synchronized (queue)
456                                {
457                                        if (queue.isEmpty())
458                                        {
459                                                try
460                                                {
461                                                        queue.wait();
462                                                }
463                                                catch (InterruptedException e)
464                                                {
465                                                        //bug when thrown? called when interrupted
466                                                        e.printStackTrace();
467                                                        return;
468                                                }
469                                                continue;
470                                        }
471                                        method = (Object[]) queue.removeFirst();
472                                }
473
474                                String m = (String)method[0];
475                               
476                                if( m.equals("openChat") )
477                                        backend.getMain().startChat((JID)method[1]);
478                        }
479                }
480        }
481}
482
483/*
484 * Overrides for emacs
485 * Local variables:
486 * tab-width: 4
487 * End:
488 */
Note: See TracBrowser for help on using the repository browser.