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 org.jivesoftware.smack.packet.*;
022    
023    import java.util.*;
024    
025    /**
026     * IQ packet for representing jabber:iq:last
027     *
028     * @author Andrey Zakirov
029     * @version 1.0
030    */
031    public class LastActivity extends IQ
032    {
033            private String seconds = null;
034            private String reason = null;
035    
036            /**
037             * Default constructor - sets up the packet with no
038             * information
039            */
040        public LastActivity()
041            {
042            this( "", "" );
043        }
044    
045            /**
046             * Sets up the packet with information
047            */
048        public LastActivity( String seconds, String reason)
049            {
050               this.seconds = seconds;
051          this.reason = reason;
052        }
053    
054        public String showTime() {
055            String show = "";
056            if( seconds == null ) return "N/A";
057            if( seconds.equals( "0s" ) ) return "0s";
058    
059            long l = 0;
060            try {
061                l = Long.parseLong (seconds);
062            }
063            catch(NumberFormatException e)
064            {
065            }
066    
067            if( l == 0 ) return "0s";
068    
069            long seconds = (long)(l % 60);
070            long minutes = (long)(l / 60 % 60);
071            long hours = (long)(l / 3600 % 60);
072            long days = (long)(l / 3600 / 24);
073    
074            if( days > 0 )
075            {
076                show += days + "d ";
077            }
078            if( hours > 0 )
079            {
080                show += hours + "h ";
081            }
082            if( minutes > 0 )
083            {
084                show += minutes + "m ";
085            }
086    
087    
088            show += seconds + "s";
089    
090            return show;
091    
092        }
093        public String getSeconds() { return seconds; }
094        public String getReason() { return reason; }
095        public void setSeconds( String seconds ) { this.seconds = seconds; }
096        public void setReason( String reason ) { this.reason = reason; }
097        public String getChildElementXML()
098            {
099            StringBuffer buf = new StringBuffer();
100            buf.append("<query xmlns=\'jabber:iq:last\'");
101            if( seconds!= null && getType() != IQ.Type.GET )
102            {
103                    buf.append( " seconds=\'" ).append(this.seconds).append("\'");
104                    if( reason != null )
105                    {
106                            buf.append(">");
107                            buf.append(reason);
108                            buf.append("</query>");
109                    }
110                    else
111                    {
112                            buf.append("/>");
113    
114                    }
115            }
116            else
117            {
118                    buf.append("/>");
119            }
120            return buf.toString();
121        }
122    }