MamPrefsIQ.java

  1. /**
  2.  *
  3.  * Copyright © 2016 Florian Schmaus and Fernando Ramirez
  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.mam.element;

  18. import java.util.List;

  19. import org.jivesoftware.smack.packet.IQ;

  20. import org.jivesoftware.smackx.mam.element.MamElements.AlwaysJidListElement;
  21. import org.jivesoftware.smackx.mam.element.MamElements.NeverJidListElement;

  22. import org.jxmpp.jid.Jid;

  23. /**
  24.  * MAM Preferences IQ class.
  25.  *
  26.  * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
  27.  *      Archive Management</a>
  28.  * @author Fernando Ramirez and Florian Schmaus
  29.  *
  30.  */
  31. public class MamPrefsIQ extends IQ {

  32.     public enum DefaultBehavior {
  33.         always,
  34.         never,
  35.         roster,
  36.     }

  37.     /**
  38.      * the preferences element.
  39.      */
  40.     public static final String ELEMENT = "prefs";

  41.     /**
  42.      * list of always.
  43.      */
  44.     private final List<Jid> alwaysJids;

  45.     /**
  46.      * list of never.
  47.      */
  48.     private final List<Jid> neverJids;

  49.     /**
  50.      * default field.
  51.      */
  52.     private final DefaultBehavior defaultBehavior;

  53.     /**
  54.      * Construct a new MAM {@code <prefs/>} IQ retrieval request (IQ type 'get').
  55.      *
  56.      * @param version TODO javadoc me please     *
  57.      */
  58.     public MamPrefsIQ(MamVersion version) {
  59.         super(ELEMENT, version.getNamespace());
  60.         alwaysJids = null;
  61.         neverJids = null;
  62.         defaultBehavior = null;
  63.     }

  64.     /**
  65.      * MAM preferences IQ constructor.
  66.      *
  67.      * @param version TODO javadoc me please
  68.      * @param alwaysJids TODO javadoc me please
  69.      * @param neverJids TODO javadoc me please
  70.      * @param defaultBehavior TODO javadoc me please
  71.      */
  72.     public MamPrefsIQ(MamVersion version, List<Jid> alwaysJids, List<Jid> neverJids, DefaultBehavior defaultBehavior) {
  73.         super(ELEMENT, version.getNamespace());
  74.         setType(Type.set);
  75.         this.alwaysJids = alwaysJids;
  76.         this.neverJids = neverJids;
  77.         this.defaultBehavior = defaultBehavior;
  78.     }

  79.     /**
  80.      * Get the list of always store info JIDs.
  81.      *
  82.      * @return the always list
  83.      */
  84.     public List<Jid> getAlwaysJids() {
  85.         return alwaysJids;
  86.     }

  87.     /**
  88.      * Get the list of never store info JIDs.
  89.      *
  90.      * @return the never list
  91.      */
  92.     public List<Jid> getNeverJids() {
  93.         return neverJids;
  94.     }

  95.     /**
  96.      * Get the default behavior.
  97.      *
  98.      * @return the default behavior.
  99.      */
  100.     public DefaultBehavior getDefault() {
  101.         return defaultBehavior;
  102.     }

  103.     @Override
  104.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {

  105.         if (getType().equals(IQ.Type.set) || getType().equals(IQ.Type.result)) {
  106.             xml.attribute("default", defaultBehavior);
  107.         }

  108.         if (alwaysJids == null && neverJids == null) {
  109.             xml.setEmptyElement();
  110.             return xml;
  111.         }

  112.         xml.rightAngleBracket();

  113.         if (alwaysJids != null) {
  114.             MamElements.AlwaysJidListElement alwaysElement = new AlwaysJidListElement(alwaysJids);
  115.             xml.append(alwaysElement);
  116.         }

  117.         if (neverJids != null) {
  118.             MamElements.NeverJidListElement neverElement = new NeverJidListElement(neverJids);
  119.             xml.append(neverElement);
  120.         }

  121.         return xml;
  122.     }

  123. }