001    /*
002     *  Copyright (C) 2003 Adam Olsen
003     *  This program is free software; you can redistribute it and/or modify
004     *  it under the terms of the GNU General Public License as published by
005     *  the Free Software Foundation; either version 1, or (at your option)
006     *  any later version.
007     *  This program is distributed in the hope that it will be useful,
008     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
009     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010     *  GNU General Public License for more details.
011     *  You should have received a copy of the GNU General Public License
012     *  along with this program; if not, write to the Free Software
013     *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014     */
015    package com.valhalla.jbother.jabber;
016    
017    import java.util.Iterator;
018    import java.util.Locale;
019    import java.util.ResourceBundle;
020    import java.util.Vector;
021    
022    import org.jivesoftware.smack.packet.Presence;
023    
024    /**
025     * Vector of user statuses (SelfStatuses)
026     * 
027     * @author Yury Soldak (tail)
028     * @created November 11, 2004
029     * @see com.valhalla.jbother.jabber.SelfStatus
030     */
031    public class SelfStatuses {
032        private ResourceBundle resources = ResourceBundle.getBundle(
033                "JBotherBundle", Locale.getDefault());
034    
035        private static SelfStatuses singleton = null;
036    
037        private Vector content = new Vector();
038    
039        /**
040         * Gets the SelfStatuses singleton
041         * 
042         * @return the SelfStatuses singleton
043         */
044        public static SelfStatuses getInstance() {
045            if (singleton == null) {
046                singleton = new SelfStatuses();
047            }
048            return singleton;
049        }
050    
051        /**
052         * Creates the SelfStatus Constructor is private, this is a singleton. See
053         * the <code>getSingleton</code> method
054         */
055        private SelfStatuses() {
056    
057            SelfStatus offline = new SelfStatus(resources.getString("offline"),
058                    "offline", null);
059            SelfStatus available = new SelfStatus(resources.getString("available"),
060                    "online", Presence.Mode.AVAILABLE);
061            SelfStatus away = new SelfStatus(resources.getString("away"), "away",
062                    Presence.Mode.AWAY);
063            SelfStatus chat = new SelfStatus(resources.getString("chat"), "ffc",
064                    Presence.Mode.CHAT);
065            SelfStatus dnd = new SelfStatus(resources.getString("dnd"), "dnd",
066                    Presence.Mode.DO_NOT_DISTURB);
067            SelfStatus xa = new SelfStatus(resources.getString("xa"), "xa",
068                    Presence.Mode.EXTENDED_AWAY);
069            SelfStatus invisible = new SelfStatus(resources.getString("invisible"),
070                    "invisible", Presence.Mode.INVISIBLE);
071    
072            content.add(available);
073            content.add(away);
074            content.add(chat);
075            content.add(dnd);
076            content.add(xa);
077            content.add(invisible);
078            content.add(offline);
079        }
080    
081        /**
082         * Gets a self status for the specified string
083         * 
084         * @param title
085         *            the title of the status to get
086         * @return the SelfStatus
087         */
088        public SelfStatus getStatus(String title) {
089            SelfStatus result = null;
090    
091            Iterator statusIterator = content.iterator();
092            while (statusIterator.hasNext()) {
093                result = (SelfStatus) statusIterator.next();
094                if (result.getTitle().equals(title)) {
095                    break;
096                }
097            }
098    
099            return result;
100        }
101    
102        /**
103         * Gets a status for a mode
104         * 
105         * @param mode
106         *            the mode to get the self status for
107         * @return the requested SelfStatus
108         */
109        public SelfStatus getStatus(Presence.Mode mode) {
110            SelfStatus result = null;
111    
112            Iterator statusIterator = content.iterator();
113            while (statusIterator.hasNext()) {
114                result = (SelfStatus) statusIterator.next();
115                if (result.getMode() != null && result.getMode().equals(mode)) {
116                    break;
117                }
118            }
119    
120            return result;
121        }
122    
123        /**
124         * Gets all the self statuses
125         * 
126         * @return The content value
127         */
128        public Vector getContent() {
129            return content;
130        }
131    
132    }
133