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 org.jivesoftware.smack.packet.NamedElement;
  19. import org.jivesoftware.smack.packet.Stanza;
  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;
  22. import org.jxmpp.util.XmppDateTime;

  23. import java.util.Date;

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

  37.     public static final String ELEMENT = "x";
  38.     public static final String NAMESPACE = "http://jabber.org/protocol/muc";

  39.     private String password;
  40.     private History history;

  41.     public String getElementName() {
  42.         return ELEMENT;
  43.     }

  44.     public String getNamespace() {
  45.         return NAMESPACE;
  46.     }

  47.     @Override
  48.     public XmlStringBuilder toXML() {
  49.         XmlStringBuilder xml = new XmlStringBuilder(this);
  50.         xml.rightAngleBracket();
  51.         xml.optElement("password", getPassword());
  52.         xml.optElement(getHistory());
  53.         xml.closeElement(this);
  54.         return xml;
  55.     }

  56.     /**
  57.      * Returns the history that manages the amount of discussion history provided on
  58.      * entering a room.
  59.      *
  60.      * @return the history that manages the amount of discussion history provided on
  61.      * entering a room.
  62.      */
  63.     public History getHistory() {
  64.         return history;
  65.     }

  66.     /**
  67.      * Returns the password to use when the room requires a password.
  68.      *
  69.      * @return the password to use when the room requires a password.
  70.      */
  71.     public String getPassword() {
  72.         return password;
  73.     }

  74.     /**
  75.      * Sets the History that manages the amount of discussion history provided on
  76.      * entering a room.
  77.      *
  78.      * @param history that manages the amount of discussion history provided on
  79.      * entering a room.
  80.      */
  81.     public void setHistory(History history) {
  82.         this.history = history;
  83.     }

  84.     /**
  85.      * Sets the password to use when the room requires a password.
  86.      *
  87.      * @param password the password to use when the room requires a password.
  88.      */
  89.     public void setPassword(String password) {
  90.         this.password = password;
  91.     }

  92.     /**
  93.      * Retrieve the MUCInitialPresence PacketExtension from packet, if any.
  94.      *
  95.      * @param packet
  96.      * @return the MUCInitialPresence PacketExtension or {@code null}
  97.      * @deprecated use {@link #from(Stanza)} instead
  98.      */
  99.     @Deprecated
  100.     public static MUCInitialPresence getFrom(Stanza packet) {
  101.         return from(packet);
  102.     }

  103.     /**
  104.      * Retrieve the MUCInitialPresence PacketExtension from packet, if any.
  105.      *
  106.      * @param packet
  107.      * @return the MUCInitialPresence PacketExtension or {@code null}
  108.      */
  109.     public static MUCInitialPresence from(Stanza packet) {
  110.         return packet.getExtension(ELEMENT, NAMESPACE);
  111.     }

  112.     /**
  113.      * The History class controls the number of characters or messages to receive
  114.      * when entering a room.
  115.      *
  116.      * @author Gaston Dombiak
  117.      */
  118.     public static class History implements NamedElement {

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

  120.         private int maxChars = -1;
  121.         private int maxStanzas = -1;
  122.         private int seconds = -1;
  123.         private Date since;

  124.         /**
  125.          * Returns the total number of characters to receive in the history.
  126.          *
  127.          * @return total number of characters to receive in the history.
  128.          */
  129.         public int getMaxChars() {
  130.             return maxChars;
  131.         }

  132.         /**
  133.          * Returns the total number of messages to receive in the history.
  134.          *
  135.          * @return the total number of messages to receive in the history.
  136.          */
  137.         public int getMaxStanzas() {
  138.             return maxStanzas;
  139.         }

  140.         /**
  141.          * Returns the number of seconds to use to filter the messages received during that time.
  142.          * In other words, only the messages received in the last "X" seconds will be included in
  143.          * the history.
  144.          *
  145.          * @return the number of seconds to use to filter the messages received during that time.
  146.          */
  147.         public int getSeconds() {
  148.             return seconds;
  149.         }

  150.         /**
  151.          * Returns the since date to use to filter the messages received during that time.
  152.          * In other words, only the messages received since the datetime specified will be
  153.          * included in the history.
  154.          *
  155.          * @return the since date to use to filter the messages received during that time.
  156.          */
  157.         public Date getSince() {
  158.             return since;
  159.         }

  160.         /**
  161.          * Sets the total number of characters to receive in the history.
  162.          *
  163.          * @param maxChars the total number of characters to receive in the history.
  164.          */
  165.         public void setMaxChars(int maxChars) {
  166.             this.maxChars = maxChars;
  167.         }

  168.         /**
  169.          * Sets the total number of messages to receive in the history.
  170.          *
  171.          * @param maxStanzas the total number of messages to receive in the history.
  172.          */
  173.         public void setMaxStanzas(int maxStanzas) {
  174.             this.maxStanzas = maxStanzas;
  175.         }

  176.         /**
  177.          * Sets the number of seconds to use to filter the messages received during that time.
  178.          * In other words, only the messages received in the last "X" seconds will be included in
  179.          * the history.
  180.          *
  181.          * @param seconds the number of seconds to use to filter the messages received during
  182.          * that time.
  183.          */
  184.         public void setSeconds(int seconds) {
  185.             this.seconds = seconds;
  186.         }

  187.         /**
  188.          * Sets the since date to use to filter the messages received during that time.
  189.          * In other words, only the messages received since the datetime specified will be
  190.          * included in the history.
  191.          *
  192.          * @param since the since date to use to filter the messages received during that time.
  193.          */
  194.         public void setSince(Date since) {
  195.             this.since = since;
  196.         }

  197.         public XmlStringBuilder toXML() {
  198.             XmlStringBuilder xml = new XmlStringBuilder(this);
  199.             xml.optIntAttribute("maxchars", getMaxChars());
  200.             xml.optIntAttribute("maxstanzas", getMaxStanzas());
  201.             xml.optIntAttribute("seconds", getSeconds());
  202.             if (getSince() != null) {
  203.                 xml.attribute("since", XmppDateTime.formatXEP0082Date(getSince()));
  204.             }
  205.             xml.closeEmptyElement();
  206.             return xml;
  207.         }

  208.         @Override
  209.         public String getElementName() {
  210.             return ELEMENT;
  211.         }
  212.     }
  213. }