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