001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.muc.packet;
019
020import org.jivesoftware.smack.packet.NamedElement;
021import org.jivesoftware.smack.packet.Stanza;
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024import org.jxmpp.util.XmppDateTime;
025
026import java.util.Date;
027
028/**
029 * Represents extended presence information whose sole purpose is to signal the ability of 
030 * the occupant to speak the MUC protocol when joining a room. If the room requires a password 
031 * then the MUCInitialPresence should include one.
032 * <p>
033 * The amount of discussion history provided on entering a room (perhaps because the 
034 * user is on a low-bandwidth connection or is using a small-footprint client) could be managed by
035 * setting a configured History instance to the MUCInitialPresence instance. 
036 *
037 * @author Gaston Dombiak
038 * @see MUCInitialPresence#setHistory(MUCInitialPresence.History)
039 */
040public class MUCInitialPresence implements ExtensionElement {
041
042    public static final String ELEMENT = "x";
043    public static final String NAMESPACE = "http://jabber.org/protocol/muc";
044
045    private String password;
046    private History history; 
047
048    public String getElementName() {
049        return ELEMENT;
050    }
051
052    public String getNamespace() {
053        return NAMESPACE;
054    }
055
056    @Override
057    public XmlStringBuilder toXML() {
058        XmlStringBuilder xml = new XmlStringBuilder(this);
059        xml.rightAngleBracket();
060        xml.optElement("password", getPassword());
061        xml.optElement(getHistory());
062        xml.closeElement(this);
063        return xml;
064    }
065
066    /**
067     * Returns the history that manages the amount of discussion history provided on 
068     * entering a room.
069     * 
070     * @return the history that manages the amount of discussion history provided on 
071     * entering a room.
072     */
073    public History getHistory() {
074        return history;
075    }
076
077    /**
078     * Returns the password to use when the room requires a password.
079     * 
080     * @return the password to use when the room requires a password.
081     */
082    public String getPassword() {
083        return password;
084    }
085
086    /**
087     * Sets the History that manages the amount of discussion history provided on 
088     * entering a room.
089     * 
090     * @param history that manages the amount of discussion history provided on 
091     * entering a room.
092     */
093    public void setHistory(History history) {
094        this.history = history;
095    }
096
097    /**
098     * Sets the password to use when the room requires a password.
099     * 
100     * @param password the password to use when the room requires a password.
101     */
102    public void setPassword(String password) {
103        this.password = password;
104    }
105
106    /**
107     * Retrieve the MUCInitialPresence PacketExtension from packet, if any.
108     *
109     * @param packet
110     * @return the MUCInitialPresence PacketExtension or {@code null}
111     * @deprecated use {@link #from(Stanza)} instead
112     */
113    @Deprecated
114    public static MUCInitialPresence getFrom(Stanza packet) {
115        return from(packet);
116    }
117
118    /**
119     * Retrieve the MUCInitialPresence PacketExtension from packet, if any.
120     *
121     * @param packet
122     * @return the MUCInitialPresence PacketExtension or {@code null}
123     */
124    public static MUCInitialPresence from(Stanza packet) {
125        return packet.getExtension(ELEMENT, NAMESPACE);
126    }
127
128    /**
129     * The History class controls the number of characters or messages to receive
130     * when entering a room.
131     * 
132     * @author Gaston Dombiak
133     */
134    public static class History implements NamedElement {
135
136        public static final String ELEMENT = "history";
137
138        private int maxChars = -1;
139        private int maxStanzas = -1; 
140        private int seconds = -1; 
141        private Date since; 
142
143        /**
144         * Returns the total number of characters to receive in the history.
145         * 
146         * @return total number of characters to receive in the history.
147         */
148        public int getMaxChars() {
149            return maxChars;
150        }
151
152        /**
153         * Returns the total number of messages to receive in the history.
154         * 
155         * @return the total number of messages to receive in the history.
156         */
157        public int getMaxStanzas() {
158            return maxStanzas;
159        }
160
161        /**
162         * Returns the number of seconds to use to filter the messages received during that time. 
163         * In other words, only the messages received in the last "X" seconds will be included in 
164         * the history.
165         * 
166         * @return the number of seconds to use to filter the messages received during that time.
167         */
168        public int getSeconds() {
169            return seconds;
170        }
171
172        /**
173         * Returns the since date to use to filter the messages received during that time. 
174         * In other words, only the messages received since the datetime specified will be 
175         * included in the history.
176         * 
177         * @return the since date to use to filter the messages received during that time.
178         */
179        public Date getSince() {
180            return since;
181        }
182
183        /**
184         * Sets the total number of characters to receive in the history.
185         * 
186         * @param maxChars the total number of characters to receive in the history.
187         */
188        public void setMaxChars(int maxChars) {
189            this.maxChars = maxChars;
190        }
191
192        /**
193         * Sets the total number of messages to receive in the history.
194         * 
195         * @param maxStanzas the total number of messages to receive in the history.
196         */
197        public void setMaxStanzas(int maxStanzas) {
198            this.maxStanzas = maxStanzas;
199        }
200
201        /**
202         * Sets the number of seconds to use to filter the messages received during that time. 
203         * In other words, only the messages received in the last "X" seconds will be included in 
204         * the history.
205         * 
206         * @param seconds the number of seconds to use to filter the messages received during 
207         * that time.
208         */
209        public void setSeconds(int seconds) {
210            this.seconds = seconds;
211        }
212
213        /**
214         * Sets the since date to use to filter the messages received during that time. 
215         * In other words, only the messages received since the datetime specified will be 
216         * included in the history.
217         * 
218         * @param since the since date to use to filter the messages received during that time.
219         */
220        public void setSince(Date since) {
221            this.since = since;
222        }
223
224        public XmlStringBuilder toXML() {
225            XmlStringBuilder xml = new XmlStringBuilder(this);
226            xml.optIntAttribute("maxchars", getMaxChars());
227            xml.optIntAttribute("maxstanzas", getMaxStanzas());
228            xml.optIntAttribute("seconds", getSeconds());
229            if (getSince() != null) {
230                xml.attribute("since", XmppDateTime.formatXEP0082Date(getSince()));
231            }
232            xml.closeEmptyElement();
233            return xml;
234        }
235
236        @Override
237        public String getElementName() {
238            return ELEMENT;
239        }
240    }
241}