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