source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/applet/Jeti.java @ 3102

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