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.workgroup.settings;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Iterator;
024import java.util.List;
025
026import org.jivesoftware.smack.packet.IQ;
027import org.jivesoftware.smack.packet.IqData;
028import org.jivesoftware.smack.packet.XmlEnvironment;
029import org.jivesoftware.smack.provider.IqProvider;
030import org.jivesoftware.smack.xml.XmlPullParser;
031import org.jivesoftware.smack.xml.XmlPullParserException;
032
033import org.jxmpp.JxmppContext;
034
035public class ChatSettings extends IQ {
036
037    /**
038     * Defined as image type.
039     */
040    public static final int IMAGE_SETTINGS = 0;
041
042    /**
043     * Defined as Text settings type.
044     */
045    public static final int TEXT_SETTINGS = 1;
046
047    /**
048     * Defined as Bot settings type.
049     */
050    public static final int BOT_SETTINGS = 2;
051
052    private final List<ChatSetting> settings;
053    private String key;
054    private int type = -1;
055
056    /**
057     * Element name of the stanza extension.
058     */
059    public static final String ELEMENT_NAME = "chat-settings";
060
061    /**
062     * Namespace of the stanza extension.
063     */
064    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
065
066    public ChatSettings() {
067        super(ELEMENT_NAME, NAMESPACE);
068        settings = new ArrayList<>();
069    }
070
071    @SuppressWarnings("this-escape")
072    public ChatSettings(String key) {
073        this();
074        setKey(key);
075    }
076
077    public void setKey(String key) {
078        this.key = key;
079    }
080
081    public void setType(int type) {
082        this.type = type;
083    }
084
085    public void addSetting(ChatSetting setting) {
086        settings.add(setting);
087    }
088
089    public Collection<ChatSetting> getSettings() {
090        return settings;
091    }
092
093    public ChatSetting getChatSetting(String key) {
094        Collection<ChatSetting> col = getSettings();
095        if (col != null) {
096            Iterator<ChatSetting> iter = col.iterator();
097            while (iter.hasNext()) {
098                ChatSetting chatSetting = iter.next();
099                if (chatSetting.getKey().equals(key)) {
100                    return chatSetting;
101                }
102            }
103        }
104        return null;
105    }
106
107    public ChatSetting getFirstEntry() {
108        if (settings.size() > 0) {
109            return settings.get(0);
110        }
111        return null;
112    }
113
114    @Override
115    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
116        if (key != null) {
117            buf.append(" key=\"" + key + "\"");
118        }
119
120        if (type != -1) {
121            buf.append(" type=\"" + type + "\"");
122        }
123        buf.setEmptyElement();
124        return buf;
125    }
126
127    /**
128     * Stanza extension provider for AgentStatusRequest packets.
129     */
130    public static class InternalProvider extends IqProvider<ChatSettings> {
131
132        @Override
133        public ChatSettings parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
134            if (parser.getEventType() != XmlPullParser.Event.START_ELEMENT) {
135                throw new IllegalStateException("Parser not in proper position, or bad XML.");
136            }
137
138            ChatSettings chatSettings = new ChatSettings();
139
140            boolean done = false;
141            while (!done) {
142                XmlPullParser.Event eventType = parser.next();
143                if (eventType == XmlPullParser.Event.START_ELEMENT && "chat-setting".equals(parser.getName())) {
144                    chatSettings.addSetting(parseChatSetting(parser));
145
146                }
147                else if (eventType == XmlPullParser.Event.END_ELEMENT && ELEMENT_NAME.equals(parser.getName())) {
148                    done = true;
149                }
150            }
151            return chatSettings;
152        }
153
154        private static ChatSetting parseChatSetting(XmlPullParser parser) throws XmlPullParserException, IOException {
155
156            boolean done = false;
157            String key = null;
158            String value = null;
159            int type = 0;
160
161            while (!done) {
162                XmlPullParser.Event eventType = parser.next();
163                if (eventType == XmlPullParser.Event.START_ELEMENT && "key".equals(parser.getName())) {
164                    key = parser.nextText();
165                }
166                else if (eventType == XmlPullParser.Event.START_ELEMENT && "value".equals(parser.getName())) {
167                    value = parser.nextText();
168                }
169                else if (eventType == XmlPullParser.Event.START_ELEMENT && "type".equals(parser.getName())) {
170                    type = Integer.parseInt(parser.nextText());
171                }
172                else if (eventType == XmlPullParser.Event.END_ELEMENT && "chat-setting".equals(parser.getName())) {
173                    done = true;
174                }
175            }
176            return new ChatSetting(key, value, type);
177        }
178    }
179}
180