001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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 */
017
018package org.jivesoftware.smackx.offline.packet;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.jivesoftware.smack.packet.IQ;
026import org.jivesoftware.smack.packet.XmlEnvironment;
027import org.jivesoftware.smack.provider.IQProvider;
028import org.jivesoftware.smack.xml.XmlPullParser;
029import org.jivesoftware.smack.xml.XmlPullParserException;
030import org.jivesoftware.smackx.offline.OfflineMessageManager;
031
032/**
033 * Represents a request to get some or all the offline messages of a user. This class can also
034 * be used for deleting some or all the offline messages of a user.
035 *
036 * @author Gaston Dombiak
037 */
038public class OfflineMessageRequest extends IQ {
039
040    public static final String ELEMENT = "offline";
041    public static final String NAMESPACE = OfflineMessageManager.NAMESPACE;
042
043    private final List<Item> items = new ArrayList<>();
044    private boolean purge = false;
045    private boolean fetch = false;
046
047    public OfflineMessageRequest() {
048        super(ELEMENT, NAMESPACE);
049    }
050
051    /**
052     * Returns a List of item children that holds information about offline messages to
053     * view or delete.
054     *
055     * @return a List of item children that holds information about offline messages to
056     *         view or delete.
057     */
058    public List<Item> getItems() {
059        synchronized (items) {
060            return Collections.unmodifiableList(new ArrayList<>(items));
061        }
062    }
063
064    /**
065     * Adds an item child that holds information about offline messages to view or delete.
066     *
067     * @param item the item child that holds information about offline messages to view or delete.
068     */
069    public void addItem(Item item) {
070        synchronized (items) {
071            items.add(item);
072        }
073    }
074
075    /**
076     * Returns true if all the offline messages of the user should be deleted.
077     *
078     * @return true if all the offline messages of the user should be deleted.
079     */
080    public boolean isPurge() {
081        return purge;
082    }
083
084    /**
085     * Sets if all the offline messages of the user should be deleted.
086     *
087     * @param purge true if all the offline messages of the user should be deleted.
088     */
089    public void setPurge(boolean purge) {
090        this.purge = purge;
091    }
092
093    /**
094     * Returns true if all the offline messages of the user should be retrieved.
095     *
096     * @return true if all the offline messages of the user should be retrieved.
097     */
098    public boolean isFetch() {
099        return fetch;
100    }
101
102    /**
103     * Sets if all the offline messages of the user should be retrieved.
104     *
105     * @param fetch true if all the offline messages of the user should be retrieved.
106     */
107    public void setFetch(boolean fetch) {
108        this.fetch = fetch;
109    }
110
111    @Override
112    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
113        buf.rightAngleBracket();
114
115        synchronized (items) {
116            for (Item item : items) {
117                buf.append(item.toXML());
118            }
119        }
120        if (purge) {
121            buf.append("<purge/>");
122        }
123        if (fetch) {
124            buf.append("<fetch/>");
125        }
126
127        return buf;
128    }
129
130    /**
131     * Item child that holds information about offline messages to view or delete.
132     *
133     * @author Gaston Dombiak
134     */
135    public static class Item {
136        private String action;
137        private String jid;
138        private String node;
139
140        /**
141         * Creates a new item child.
142         *
143         * @param node the actor's affiliation to the room
144         */
145        public Item(String node) {
146            this.node = node;
147        }
148
149        public String getNode() {
150            return node;
151        }
152
153        /**
154         * Returns "view" or "remove" that indicate if the server should return the specified
155         * offline message or delete it.
156         *
157         * @return "view" or "remove" that indicate if the server should return the specified
158         *         offline message or delete it.
159         */
160        public String getAction() {
161            return action;
162        }
163
164        /**
165         * Sets if the server should return the specified offline message or delete it. Possible
166         * values are "view" or "remove".
167         *
168         * @param action if the server should return the specified offline message or delete it.
169         */
170        public void setAction(String action) {
171            this.action = action;
172        }
173
174        public String getJid() {
175            return jid;
176        }
177
178        public void setJid(String jid) {
179            this.jid = jid;
180        }
181
182        public String toXML() {
183            StringBuilder buf = new StringBuilder();
184            buf.append("<item");
185            if (getAction() != null) {
186                buf.append(" action=\"").append(getAction()).append('"');
187            }
188            if (getJid() != null) {
189                buf.append(" jid=\"").append(getJid()).append('"');
190            }
191            if (getNode() != null) {
192                buf.append(" node=\"").append(getNode()).append('"');
193            }
194            buf.append("/>");
195            return buf.toString();
196        }
197    }
198
199    public static class Provider extends IQProvider<OfflineMessageRequest> {
200
201        @Override
202        public OfflineMessageRequest parse(XmlPullParser parser,
203                        int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
204                        IOException {
205            OfflineMessageRequest request = new OfflineMessageRequest();
206            boolean done = false;
207            while (!done) {
208                XmlPullParser.Event eventType = parser.next();
209                if (eventType == XmlPullParser.Event.START_ELEMENT) {
210                    if (parser.getName().equals("item")) {
211                        request.addItem(parseItem(parser));
212                    }
213                    else if (parser.getName().equals("purge")) {
214                        request.setPurge(true);
215                    }
216                    else if (parser.getName().equals("fetch")) {
217                        request.setFetch(true);
218                    }
219                } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
220                    if (parser.getName().equals("offline")) {
221                        done = true;
222                    }
223                }
224            }
225
226            return request;
227        }
228
229        private static Item parseItem(XmlPullParser parser)
230                        throws XmlPullParserException, IOException {
231            boolean done = false;
232            Item item = new Item(parser.getAttributeValue("", "node"));
233            item.setAction(parser.getAttributeValue("", "action"));
234            item.setJid(parser.getAttributeValue("", "jid"));
235            while (!done) {
236                XmlPullParser.Event eventType = parser.next();
237                if (eventType == XmlPullParser.Event.END_ELEMENT) {
238                    if (parser.getName().equals("item")) {
239                        done = true;
240                    }
241                }
242            }
243            return item;
244        }
245    }
246}