package nu.fw.jeti.applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import nu.fw.jeti.backend.LoginInfo;
import nu.fw.jeti.backend.Start;
import nu.fw.jeti.events.StatusChangeListener;
import nu.fw.jeti.jabber.Backend;
import nu.fw.jeti.jabber.JID;
import nu.fw.jeti.jabber.UnknownJIDStatus;
import nu.fw.jeti.jabber.elements.IQPrivate;
import nu.fw.jeti.jabber.elements.InfoQuery;
import nu.fw.jeti.jabber.elements.InfoQueryBuilder;
import nu.fw.jeti.jabber.elements.Presence;
import nu.fw.jeti.jabber.elements.RosterBuilder;
import nu.fw.jeti.jabber.elements.RosterItemBuilder;
import nu.fw.jeti.ui.AddContact;
import nu.fw.jeti.ui.JIDInput;
import nu.fw.jeti.ui.StatusButton;
import nu.fw.jeti.util.I18N;
import nu.fw.jeti.util.Popups;
import nu.fw.jeti.util.Preferences;
import netscape.javascript.JSObject;
/*
* @author E.S. de Boer
* Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For questions, comments etc,
* use the website at http://jeti.jabberstudio.org
* or jabber/mail me at jeti@jabber.org
*
* Created on 14-nov-2003
*/
public class Jeti extends JApplet
{
private Start start;
private static Jeti applet;
private URL exitURL;
private static String groupchatRoom;
private static String groupchatServer;
private Backend backend;
private boolean started = false;
private SecureMethodRunner secureMethodInvoker;
public static JLabel from;
public static LoginInfo loginInfo;
public static String USERID;
public static String SERVEREXPRESSO;
public static String NAMECOMPANY;
public static String USEHTTPS;
public static String CNNAME;
public static JSObject WINDOWAPPLET;
public static List GROUPSUSER;
public static String SERVERJABBER;
public static String COUNTRY;
public static String LANGUAGE;
public static String JAVAPLUGINS;
/**
* @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
* @date : 10/06/2009
* @Brief : A variavel DEBUG_APPLET_VIEWER = true, para usar o appletviewer
*/
public static boolean DEBUG_APPLET_VIEWER = false;
public void init()
{
applet = this;
Thread t = new Thread()
{
public void run()
{
String server = new String(applet.getParameter("SERVER"));
String portText = new String(applet.getParameter("PORT"));
boolean ssl = Boolean.valueOf(applet.getParameter("SSL")).booleanValue();
String user = new String(applet.getParameter("USER"));
String password = new String(applet.getParameter("PASSWORD"));
String resource = new String(applet.getParameter("RESOURCE"));
String host = new String(applet.getParameter("HOST"));
String useJavaProxy = new String(applet.getParameter("USEPROXY").toUpperCase());
String serverExpresso = new String(applet.getParameter("EXPRESSO"));
String useHttps = new String(applet.getParameter("USEHTTPS"));
String nameCompany = new String(applet.getParameter("NAMECOMPANY"));
String cnName = new String(applet.getParameter("CNNAME"));
String country = new String(applet.getParameter("COUNTRY"));
String language = new String(applet.getParameter("LANGUAGE"));
String javaPlugins = new String(applet.getParameter("JAVAPLUGINS"));
/**
* @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
* @Date : 20/10/2008
* @Description : - Nome Servidor IM-Jabber para Constant SERVERJABBER
* - Nome da Maquina Expresso para Constant SERVEREXPRESSO
* - Nome da Empresa para Constant NAMECOMPANY
* - Informa qual o tipo de protocolo a ser usado http ou https.
* - Passa ao java o cn do ldap do servidor expresso.
* - Carrega plugins para o Java.
* - uid expresso;
*/
SERVERJABBER = server;
SERVEREXPRESSO = serverExpresso;
NAMECOMPANY = nameCompany;
USEHTTPS = useHttps;
CNNAME = cnName;
LANGUAGE = language;
COUNTRY = country;
JAVAPLUGINS = javaPlugins;
USERID = user;
// Reinit languages incase they changed
I18N.init(language, country);
groupchatRoom = getParameter("GROUPCHATROOM");
groupchatServer = getParameter("GROUPCHATSERVER");
try
{
exitURL = new URL(getDocumentBase(),getParameter("EXITPAGE"));
}
catch (MalformedURLException e){}
if( server !=null || resource != null || portText !=null )
{
int port = Integer.parseInt(portText);
if( useJavaProxy.equals("TRUE"))
loginInfo = new LoginInfo(server,host,user,password,resource,port,ssl,0);
else
loginInfo = new LoginInfo(server,host,user,password,resource,port,ssl,0,false);
}
Start.programURL = getCodeBase();
Start.applet = true;
final JPanel panel = new JPanel(new BorderLayout());
start = new Start( getCodeBase().toString(), panel );
backend = start.getBackend();
backend.getMain().translate();
if( loginInfo != null && loginInfo.getPassword() != null)
{
backend.autoLogin(loginInfo,10);
secureMethodInvoker = new SecureMethodRunner(backend);
secureMethodInvoker.start();
backend.addExtensionHandler("jeti:prefs",new PreferencesHandler());
try
{
backend.addListener(StatusChangeListener.class,new StatusChangeListener()
{
public void connectionChanged(boolean online)
{
if( online)
{
backend.send(new InfoQuery("get",new IQPrivate(new JetiPrivatePreferencesExtension())));
String chatTO = getParameter("CHATTO");
if(chatTO!=null && chatTO.length()>0)
{
if(Boolean.valueOf(getParameter("SHOWCHATTOASMAIN")).booleanValue())
{
setContentPane(backend.getMain().createChatPanel(new UnknownJIDStatus(JID.jidFromString(chatTO))));
validate();
}
else backend.getMain().startChat(JID.jidFromString(chatTO));
}
}
}
public void ownPresenceChanged(int show, String status) {}
public void exit(){}
});
}
catch(NoClassDefFoundError e)
{
e.printStackTrace();
}
new Popups(Jeti.this);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
setContentPane(panel);
validate();
}
});
started = true;
}else
started = false;
}
};
t.start();
splash();
if ( !DEBUG_APPLET_VIEWER )
{
JSObject winApplet = JSObject.getWindow(this);
WINDOWAPPLET = winApplet;
}
}
private void splash()
{
/**
* Splash - Inicializador -
* Modificado by Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
*/
JLabel label = new JLabel( "" +
"Expresso IM " +
"
" +
" Jeti - http://www.jeti-im.org ",
JLabel.CENTER );
label.setBackground(new Color(255,255,255));
getContentPane().setBackground(new Color(255,255,255));
getContentPane().add(label,BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setBackground(new Color(255,255,255));
from = new JLabel(NAMECOMPANY, JLabel.CENTER);
from.setBackground(new Color(255,255,255));
from.setFont(new java.awt.Font("Serif,Verdana,Arial",1,10));
panel.add(from);
getContentPane().add(panel,BorderLayout.SOUTH);
}
public String getGroupsToExpresso()
{
String[] Groups = applet.backend.getAllGroups();
String groups = "";
for(int i = 0 ; i < Groups.length; i++ )
groups += Groups[i] + ";";
return groups.toString();
}
public void changeStatusfromExpresso(int status, String msgStatus)
{
switch (status)
{
case Presence.AVAILABLE:
StatusButton.changeStatusExpresso(Presence.AVAILABLE, msgStatus);
break;
case Presence.FREE_FOR_CHAT:
StatusButton.changeStatusExpresso(Presence.FREE_FOR_CHAT, msgStatus);
break;
case Presence.AWAY:
StatusButton.changeStatusExpresso(Presence.AWAY, msgStatus);
break;
case Presence.XA:
StatusButton.changeStatusExpresso(Presence.XA, msgStatus);
break;
case Presence.DND:
StatusButton.changeStatusExpresso(Presence.DND, msgStatus);
break;
case Presence.UNAVAILABLE:
StatusButton.changeStatusExpresso(Presence.UNAVAILABLE, msgStatus);
break;
case Presence.INVISIBLE:
StatusButton.changeStatusExpresso(Presence.INVISIBLE, msgStatus);
break;
}
}
public static void showURL(String url)
{
try
{
applet.getAppletContext().showDocument(new URL(url),"_blank");
}
catch (MalformedURLException e){}
}
public void destroy()
{
try
{
if( secureMethodInvoker != null )
secureMethodInvoker.stopRunning();
start.close();
}
catch (Exception e)
{
//catch all exceptions else jvm hangs
e.printStackTrace();
}
}
public static void exit()
{
applet.getAppletContext().showDocument(applet.exitURL);
}
public boolean isReady()
{
System.out.println("ready " + started);
return started;
}
public boolean isLoggedIn()
{
if( !started )
return false;
return backend.isLoggedIn();
}
public void openChat(String jid)
{
try
{
JID j = JID.checkedJIDFromString(jid);
secureMethodInvoker.addData(new Object[]{"openChat",j});
}
catch (InstantiationException e)
{
e.printStackTrace();
}
}
public void addContact(String jid, String name, String group)
{
/**
* @Author(s) : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
* @Date : 24/04/2009
* @Brief : Adiciona novos contatos e ja pede autorizacao.
*/
try
{
JID Jid = JID.checkedJIDFromString(jid);
backend.send(new Presence(Jid, "subscribe"));
RosterBuilder rb = new RosterBuilder();
RosterItemBuilder rib = new RosterItemBuilder();
rib.addGroup(group);
rib.jid = Jid;
rib.name = name;
rib.ask = "subscribe";
rib.subscription = "none";
rb.addItem(rib.build());
InfoQueryBuilder iqb = new InfoQueryBuilder();
iqb.addExtension(rb.build());
iqb.setType("set");
backend.send(iqb.build());
}
catch(Exception error)
{
error.printStackTrace();
}
}
public void login(String username,String server,String password)
{
if( backend.isLoggedIn() )
{
JID j = backend.getMyJID();
if( j.getDomain().equals(server) && j.getUser().equals(username) )
return;
}
secureMethodInvoker.addData(new Object[]{"login",username,server,password});
}
public static Class getPlugin(String name)
{
try
{
return Class.forName("nu.fw.jeti.plugins."+name+".Plugin");
}
catch (ClassNotFoundException e)
{
System.err.println(MessageFormat.format(I18N.gettext("main.error.{0}_plugin_not_found"), new Object[]{name}));
return null;
}
}
public static void getPrefPanel(String name,Map loadedPreferencePanels)
{
Class prefPanel = null;
try
{
prefPanel = Class.forName("nu.fw.jeti.plugins."+name+".PrefPanel");
}
catch (ClassNotFoundException e)
{
return;
}
loadedPreferencePanels.put(name,prefPanel);
}
static class SecureMethodRunner extends Thread
{
private Backend backend;
private LinkedList queue = new LinkedList();
private volatile boolean isRunning=true;
SecureMethodRunner(Backend backend)
{
this.backend = backend;
}
public void addData(Object method)
{
synchronized(queue)
{
queue.addLast(method);
queue.notifyAll();
}
}
public void stopRunning()
{
isRunning = false;
synchronized(queue){queue.notifyAll();}
}
public void run()
{
Object[] method;
while (isRunning)
{
synchronized (queue)
{
if (queue.isEmpty())
{
try
{
queue.wait();
}
catch (InterruptedException e)
{
//bug when thrown? called when interrupted
e.printStackTrace();
return;
}
continue;
}
method = (Object[]) queue.removeFirst();
}
String m = (String)method[0];
if( m.equals("openChat") )
backend.getMain().startChat((JID)method[1]);
}
}
}
}
/*
* Overrides for emacs
* Local variables:
* tab-width: 4
* End:
*/