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