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