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    public MamPrefsIQ(MamVersion version, List<Jid> alwaysJids, List<Jid> neverJids, DefaultBehavior defaultBehavior) {
085        super(ELEMENT, version.getNamespace());
086        setType(Type.set);
087        this.alwaysJids = alwaysJids;
088        this.neverJids = neverJids;
089        this.defaultBehavior = defaultBehavior;
090    }
091
092    /**
093     * Get the list of always store info JIDs.
094     *
095     * @return the always list
096     */
097    public List<Jid> getAlwaysJids() {
098        return alwaysJids;
099    }
100
101    /**
102     * Get the list of never store info JIDs.
103     *
104     * @return the never list
105     */
106    public List<Jid> getNeverJids() {
107        return neverJids;
108    }
109
110    /**
111     * Get the default behavior.
112     *
113     * @return the default behavior.
114     */
115    public DefaultBehavior getDefault() {
116        return defaultBehavior;
117    }
118
119    @Override
120    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
121
122        if (getType().equals(IQ.Type.set) || getType().equals(IQ.Type.result)) {
123            xml.attribute("default", defaultBehavior);
124        }
125
126        if (alwaysJids == null && neverJids == null) {
127            xml.setEmptyElement();
128            return xml;
129        }
130
131        xml.rightAngleBracket();
132
133        if (alwaysJids != null) {
134            MamElements.AlwaysJidListElement alwaysElement = new AlwaysJidListElement(alwaysJids);
135            xml.append(alwaysElement);
136        }
137
138        if (neverJids != null) {
139            MamElements.NeverJidListElement neverElement = new NeverJidListElement(neverJids);
140            xml.append(neverElement);
141        }
142
143        return xml;
144    }
145
146}