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