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;
022import org.jivesoftware.smackx.mam.element.MamElements.AlwaysJidListElement;
023import org.jivesoftware.smackx.mam.element.MamElements.NeverJidListElement;
024import org.jxmpp.jid.Jid;
025
026/**
027 * MAM Preferences IQ class.
028 * 
029 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
030 *      Archive Management</a>
031 * @author Fernando Ramirez and Florian Schmaus
032 * 
033 */
034public class MamPrefsIQ extends IQ {
035
036    public enum DefaultBehavior {
037        always,
038        never,
039        roster,
040        ;
041    }
042
043    /**
044     * the preferences element.
045     */
046    public static final String ELEMENT = "prefs";
047
048    /**
049     * the IQ NAMESPACE.
050     */
051    public static final String NAMESPACE = MamElements.NAMESPACE;
052
053    /**
054     * list of always.
055     */
056    private final List<Jid> alwaysJids;
057
058    /**
059     * list of never.
060     */
061    private final List<Jid> neverJids;
062
063    /**
064     * default field.
065     */
066    private final DefaultBehavior defaultBehavior;
067
068    /**
069     * Construct a new MAM {@code <prefs/>} IQ retrieval request (IQ type 'get').
070     */
071    public MamPrefsIQ() {
072        super(ELEMENT, NAMESPACE);
073        alwaysJids = null;
074        neverJids = null;
075        defaultBehavior = null;
076    }
077
078    /**
079     * MAM preferences IQ constructor.
080     * 
081     * @param alwaysJids
082     * @param neverJids
083     * @param defaultBehavior
084     */
085    public MamPrefsIQ(List<Jid> alwaysJids, List<Jid> neverJids, DefaultBehavior defaultBehavior) {
086        super(ELEMENT, NAMESPACE);
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.element(alwaysElement);
137        }
138
139        if (neverJids != null) {
140            MamElements.NeverJidListElement neverElement = new NeverJidListElement(neverJids);
141            xml.element(neverElement);
142        }
143
144        return xml;
145    }
146
147}