001    /*
002        Copyright (C) 2003 Adam Olsen
003    
004            This program is free software; you can redistribute it and/or modify
005        it under the terms of the GNU General Public License as published by
006        the Free Software Foundation; either version 1, or (at your option)
007        any later version.
008    
009        This program is distributed in the hope that it will be useful,
010        but WITHOUT ANY WARRANTY; without even the implied warranty of
011        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012        GNU General Public License for more details.
013    
014        You should have received a copy of the GNU General Public License
015        along with this program; if not, write to the Free Software
016        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017    */
018    
019    package com.valhalla.jbother.jabber.smack;
020    
021    import com.valhalla.gui.*;
022    import javax.swing.*;
023    import com.valhalla.jbother.*;
024    import com.valhalla.settings.*;
025    import org.jivesoftware.smack.packet.*;
026    import java.util.*;
027    
028    /**
029     * Listens to the connection, watches for drops, etc
030     * @author Adam Olsen
031     * @version 1.0
032    */
033    public class ConnectionListener implements org.jivesoftware.smack.ConnectionListener
034    {
035            private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
036            private Presence.Mode lastMode = null;
037            private String lastStatus = null;
038    
039            /**
040             * Called if the connection is lost
041             * @param e an Exception containing the reason for the connection loss
042            */
043            public void connectionClosedOnError( Exception e )
044            {
045                    lastMode = BuddyList.getInstance().getCurrentPresenceMode();
046                    lastStatus = BuddyList.getInstance().getCurrentStatusString();
047    
048                    com.valhalla.jbother.ConnectorThread.setHasHadError( true );
049                    String errorMessage = resources.getString( "connectionLost" );
050    
051                    if( e.getMessage() != null ) errorMessage = e.getMessage();
052                    com.valhalla.Logger.logException( e );
053    
054                    if( !Settings.getInstance().getBoolean( "reconnectOnDisconnect" ) )
055                            Standard.warningMessage( null, resources.getString( "connectionError" ), errorMessage );
056    
057                    BuddyList.getInstance().setSignoff( true );
058                    connectionClosed();
059            }
060    
061            /**
062             * Called if the connection is closed
063            */
064            public void connectionClosed()
065            {
066                    BuddyList.getInstance().saveSettings();
067                    if( BuddyList.getInstance().getSignoff() )
068                    {
069                            BuddyList.getInstance().signOff();
070    
071                            if( Settings.getInstance().getBoolean( "reconnectOnDisconnect" ) )
072                            {
073                                    boolean away = BuddyList.getInstance().getIdleAway();
074                                    ConnectorThread connect = new ConnectorThread( lastMode, lastStatus, away );
075                                    connect.setPersistent( true );
076                                    Thread thread = new Thread( connect );
077                                    thread.start();
078                            }
079                    }
080            }
081    }