MUCInitialPresence.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.muc.packet;

  18. import java.util.Date;

  19. import javax.xml.namespace.QName;

  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.packet.NamedElement;
  22. import org.jivesoftware.smack.packet.Stanza;
  23. import org.jivesoftware.smack.util.XmlStringBuilder;

  24. import org.jxmpp.util.XmppDateTime;

  25. /**
  26.  * Represents extended presence information whose sole purpose is to signal the ability of
  27.  * the occupant to speak the MUC protocol when joining a room. If the room requires a password
  28.  * then the MUCInitialPresence should include one.
  29.  * <p>
  30.  * The amount of discussion history provided on entering a room (perhaps because the
  31.  * user is on a low-bandwidth connection or is using a small-footprint client) could be managed by
  32.  * setting a configured History instance to the MUCInitialPresence instance.
  33.  *
  34.  * @author Gaston Dombiak
  35.  * @see MUCInitialPresence#setHistory(MUCInitialPresence.History)
  36.  */
  37. public class MUCInitialPresence implements ExtensionElement {

  38.     public static final String ELEMENT = "x";
  39.     public static final String NAMESPACE = "http://jabber.org/protocol/muc";
  40.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  41.     // TODO make those fields final once deprecated setter methods have been removed.
  42.     private String password;
  43.     private History history;

  44.     /**
  45.      * Deprecated constructor.
  46.      * @deprecated use {@link #MUCInitialPresence(String, int, int, int, Date)} instead.
  47.      */
  48.     @Deprecated
  49.     public MUCInitialPresence() {
  50.     }

  51.     /**
  52.      * Construct a new MUC initial presence extension.
  53.      *
  54.      * @param password the optional password used to enter the room.
  55.      * @param maxChars the maximal count of characters of history to request.
  56.      * @param maxStanzas the maximal count of stanzas of history to request.
  57.      * @param seconds the last seconds since when to request history.
  58.      * @param since the date since when to request history.
  59.      */
  60.     public MUCInitialPresence(String password, int maxChars, int maxStanzas, int seconds, Date since) {
  61.         this.password = password;
  62.         if (maxChars > -1 || maxStanzas > -1 || seconds > -1 || since != null) {
  63.             this.history = new History(maxChars, maxStanzas, seconds, since);
  64.         } else {
  65.             this.history = null;
  66.         }
  67.     }

  68.     @Override
  69.     public String getElementName() {
  70.         return ELEMENT;
  71.     }

  72.     @Override
  73.     public String getNamespace() {
  74.         return NAMESPACE;
  75.     }

  76.     @Override
  77.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  78.         XmlStringBuilder xml = new XmlStringBuilder(this);
  79.         xml.rightAngleBracket();
  80.         xml.optElement("password", getPassword());
  81.         xml.optElement(getHistory());
  82.         xml.closeElement(this);
  83.         return xml;
  84.     }

  85.     /**
  86.      * Returns the history that manages the amount of discussion history provided on
  87.      * entering a room.
  88.      *
  89.      * @return the history that manages the amount of discussion history provided on
  90.      * entering a room.
  91.      */
  92.     public History getHistory() {
  93.         return history;
  94.     }

  95.     /**
  96.      * Returns the password to use when the room requires a password.
  97.      *
  98.      * @return the password to use when the room requires a password.
  99.      */
  100.     public String getPassword() {
  101.         return password;
  102.     }

  103.     /**
  104.      * Sets the History that manages the amount of discussion history provided on
  105.      * entering a room.
  106.      *
  107.      * @param history that manages the amount of discussion history provided on
  108.      * entering a room.
  109.      * @deprecated use {@link #MUCInitialPresence(String, int, int, int, Date)} instead.
  110.      */
  111.     @Deprecated
  112.     public void setHistory(History history) {
  113.         this.history = history;
  114.     }

  115.     /**
  116.      * Sets the password to use when the room requires a password.
  117.      *
  118.      * @param password the password to use when the room requires a password.
  119.      * @deprecated use {@link #MUCInitialPresence(String, int, int, int, Date)} instead.
  120.      */
  121.     @Deprecated
  122.     public void setPassword(String password) {
  123.         this.password = password;
  124.     }

  125.     /**
  126.      * Retrieve the MUCInitialPresence PacketExtension from packet, if any.
  127.      *
  128.      * @param packet TODO javadoc me please
  129.      * @return the MUCInitialPresence PacketExtension or {@code null}
  130.      * @deprecated use {@link #from(Stanza)} instead
  131.      */
  132.     @Deprecated
  133.     public static MUCInitialPresence getFrom(Stanza packet) {
  134.         return from(packet);
  135.     }

  136.     /**
  137.      * Retrieve the MUCInitialPresence PacketExtension from packet, if any.
  138.      *
  139.      * @param packet TODO javadoc me please
  140.      * @return the MUCInitialPresence PacketExtension or {@code null}
  141.      */
  142.     public static MUCInitialPresence from(Stanza packet) {
  143.         return packet.getExtension(MUCInitialPresence.class);
  144.     }

  145.     /**
  146.      * The History class controls the number of characters or messages to receive
  147.      * when entering a room.
  148.      *
  149.      * @author Gaston Dombiak
  150.      */
  151.     public static class History implements NamedElement {

  152.         public static final String ELEMENT = "history";

  153.         private final int maxChars;
  154.         private final int maxStanzas;
  155.         private final int seconds;
  156.         private final Date since;

  157.         public History(int maxChars, int maxStanzas, int seconds, Date since) {
  158.             if (maxChars < 0 && maxStanzas < 0 && seconds < 0 && since == null) {
  159.                 throw new IllegalArgumentException();
  160.             }
  161.             this.maxChars = maxChars;
  162.             this.maxStanzas = maxStanzas;
  163.             this.seconds = seconds;
  164.             this.since = since;
  165.         }

  166.         /**
  167.          * Returns the total number of characters to receive in the history.
  168.          *
  169.          * @return total number of characters to receive in the history.
  170.          */
  171.         public int getMaxChars() {
  172.             return maxChars;
  173.         }

  174.         /**
  175.          * Returns the total number of messages to receive in the history.
  176.          *
  177.          * @return the total number of messages to receive in the history.
  178.          */
  179.         public int getMaxStanzas() {
  180.             return maxStanzas;
  181.         }

  182.         /**
  183.          * Returns the number of seconds to use to filter the messages received during that time.
  184.          * In other words, only the messages received in the last "X" seconds will be included in
  185.          * the history.
  186.          *
  187.          * @return the number of seconds to use to filter the messages received during that time.
  188.          */
  189.         public int getSeconds() {
  190.             return seconds;
  191.         }

  192.         /**
  193.          * Returns the since date to use to filter the messages received during that time.
  194.          * In other words, only the messages received since the datetime specified will be
  195.          * included in the history.
  196.          *
  197.          * @return the since date to use to filter the messages received during that time.
  198.          */
  199.         public Date getSince() {
  200.             return since;
  201.         }

  202.         @Override
  203.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  204.             XmlStringBuilder xml = new XmlStringBuilder(this);
  205.             xml.optIntAttribute("maxchars", getMaxChars());
  206.             xml.optIntAttribute("maxstanzas", getMaxStanzas());
  207.             xml.optIntAttribute("seconds", getSeconds());
  208.             if (getSince() != null) {
  209.                 xml.attribute("since", XmppDateTime.formatXEP0082Date(getSince()));
  210.             }
  211.             xml.closeEmptyElement();
  212.             return xml;
  213.         }

  214.         @Override
  215.         public String getElementName() {
  216.             return ELEMENT;
  217.         }
  218.     }
  219. }