001/**
002 *
003 * Copyright © 2016 Florian Schmaus and Fernando Ramirez
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 */
017package org.jivesoftware.smackx.mam.element;
018
019import java.util.List;
020
021import org.jivesoftware.smack.packet.IQ;
022
023import org.jivesoftware.smackx.mam.element.MamElements.AlwaysJidListElement;
024import org.jivesoftware.smackx.mam.element.MamElements.NeverJidListElement;
025
026import org.jxmpp.jid.Jid;
027
028/**
029 * MAM Preferences IQ class.
030 *
031 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
032 *      Archive Management</a>
033 * @author Fernando Ramirez and Florian Schmaus
034 *
035 */
036public class MamPrefsIQ extends IQ {
037
038    public enum DefaultBehavior {
039        always,
040        never,
041        roster,
042    }
043
044    /**
045     * the preferences element.
046     */
047    public static final String ELEMENT = "prefs";
048
049    /**
050     * the IQ NAMESPACE.
051     */
052    public static final String NAMESPACE = MamElements.NAMESPACE;
053
054    /**
055     * list of always.
056     */
057    private final List<Jid> alwaysJids;
058
059    /**
060     * list of never.
061     */
062    private final List<Jid> neverJids;
063
064    /**
065     * default field.
066     */
067    private final DefaultBehavior defaultBehavior;
068
069    /**
070     * Construct a new MAM {@code <prefs/>} IQ retrieval request (IQ type 'get').
071     */
072    public MamPrefsIQ() {
073        super(ELEMENT, NAMESPACE);
074        alwaysJids = null;
075        neverJids = null;
076        defaultBehavior = null;
077    }
078
079    /**
080     * MAM preferences IQ constructor.
081     *
082     * @param alwaysJids TODO javadoc me please
083     * @param neverJids TODO javadoc me please
084     * @param defaultBehavior TODO javadoc me please
085     */
086    public MamPrefsIQ(List<Jid> alwaysJids, List<Jid> neverJids, DefaultBehavior defaultBehavior) {
087        super(ELEMENT, NAMESPACE);
088        setType(Type.set);
089        this.alwaysJids = alwaysJids;
090        this.neverJids = neverJids;
091        this.defaultBehavior = defaultBehavior;
092    }
093
094    /**
095     * Get the list of always store info JIDs.
096     *
097     * @return the always list
098     */
099    public List<Jid> getAlwaysJids() {
100        return alwaysJids;
101    }
102
103    /**
104     * Get the list of never store info JIDs.
105     *
106     * @return the never list
107     */
108    public List<Jid> getNeverJids() {
109        return neverJids;
110    }
111
112    /**
113     * Get the default behavior.
114     *
115     * @return the default behavior.
116     */
117    public DefaultBehavior getDefault() {
118        return defaultBehavior;
119    }
120
121    @Override
122    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
123
124        if (getType().equals(IQ.Type.set) || getType().equals(IQ.Type.result)) {
125            xml.attribute("default", defaultBehavior);
126        }
127
128        if (alwaysJids == null && neverJids == null) {
129            xml.setEmptyElement();
130            return xml;
131        }
132
133        xml.rightAngleBracket();
134
135        if (alwaysJids != null) {
136            MamElements.AlwaysJidListElement alwaysElement = new AlwaysJidListElement(alwaysJids);
137            xml.append(alwaysElement);
138        }
139
140        if (neverJids != null) {
141            MamElements.NeverJidListElement neverElement = new NeverJidListElement(neverJids);
142            xml.append(neverElement);
143        }
144
145        return xml;
146    }
147
148}