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 java.util.*;
022    import org.jivesoftware.smack.*;
023    import org.jivesoftware.smack.packet.*;
024    import org.jivesoftware.smackx.packet.*;
025    import java.text.*;
026    
027    import com.valhalla.jbother.*;
028    
029    /**
030     * Listens for and responds to jabber:iq:time requests
031     *
032     * @author Adam Olsen
033     * @version 1.0
034    */
035    public class TimeListener implements PacketListener
036    {
037            /**
038             * Event listener called when a Time packet is recieved
039             * @param message the version packet recieved
040            */
041            public void processPacket( Packet message )
042            {
043                    if( !( message instanceof Time ) || ((IQ)message).getType() != IQ.Type.GET ) return;
044    
045                    Time time = (Time)message;
046    
047                    String from = message.getFrom();
048                    String to = message.getTo();
049    
050                    Calendar cal = Calendar.getInstance();
051    
052                    SimpleDateFormat utcFormat = new SimpleDateFormat("yyyyMMdd'T'hh:mm:ss");
053                    DateFormat displayFormat = DateFormat.getDateTimeInstance();
054    
055            TimeZone timeZone = cal.getTimeZone();
056            time.setTz( cal.getTimeZone().getID() );
057            time.setDisplay( displayFormat.format(cal.getTime()) );
058            // Convert local time to the UTC time.
059            time.setUtc( utcFormat.format(new Date(
060                    cal.getTimeInMillis() - timeZone.getOffset(cal.getTimeInMillis()))) );
061                    time.setTo( from );
062                    time.setFrom( to );
063                    time.setType( IQ.Type.RESULT );
064    
065                    // send the response
066                    if( BuddyList.getInstance().checkConnection() )
067                            BuddyList.getInstance().getConnection().sendPacket( time );
068            }
069    }