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     * list of always.
051     */
052    private final List<Jid> alwaysJids;
053
054    /**
055     * list of never.
056     */
057    private final List<Jid> neverJids;
058
059    /**
060     * default field.
061     */
062    private final DefaultBehavior defaultBehavior;
063
064    /**
065     * Construct a new MAM {@code <prefs/>} IQ retrieval request (IQ type 'get').
066     *
067     * @param version TODO javadoc me please     *
068     */
069    public MamPrefsIQ(MamVersion version) {
070        super(ELEMENT, version.getNamespace());
071        alwaysJids = null;
072        neverJids = null;
073        defaultBehavior = null;
074    }
075
076    /**
077     * MAM preferences IQ constructor.
078     *
079     * @param version TODO javadoc me please
080     * @param alwaysJids TODO javadoc me please
081     * @param neverJids TODO javadoc me please
082     * @param defaultBehavior TODO javadoc me please
083     */
084    @SuppressWarnings("this-escape")
085    public MamPrefsIQ(MamVersion version, List<Jid> alwaysJids, List<Jid> neverJids, DefaultBehavior defaultBehavior) {
086        super(ELEMENT, version.getNamespace());
087        setType(Type.set);
088        this.alwaysJids = alwaysJids;
089        this.neverJids = neverJids;
090        this.defaultBehavior = defaultBehavior;
091    }
092
093    /**
094     * Get the list of always store info JIDs.
095     *
096     * @return the always list
097     */
098    public List<Jid> getAlwaysJids() {
099        return alwaysJids;
100    }
101
102    /**
103     * Get the list of never store info JIDs.
104     *
105     * @return the never list
106     */
107    public List<Jid> getNeverJids() {
108        return neverJids;
109    }
110
111    /**
112     * Get the default behavior.
113     *
114     * @return the default behavior.
115     */
116    public DefaultBehavior getDefault() {
117        return defaultBehavior;
118    }
119
120    @Override
121    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
122
123        if (getType().equals(IQ.Type.set) || getType().equals(IQ.Type.result)) {
124            xml.attribute("default", defaultBehavior);
125        }
126
127        if (alwaysJids == null && neverJids == null) {
128            xml.setEmptyElement();
129            return xml;
130        }
131
132        xml.rightAngleBracket();
133
134        if (alwaysJids != null) {
135            MamElements.AlwaysJidListElement alwaysElement = new AlwaysJidListElement(alwaysJids);
136            xml.append(alwaysElement);
137        }
138
139        if (neverJids != null) {
140            MamElements.NeverJidListElement neverElement = new NeverJidListElement(neverJids);
141            xml.append(neverElement);
142        }
143
144        return xml;
145    }
146
147}