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.packet;
019
020import org.jivesoftware.smack.packet.PacketExtension;
021import org.jivesoftware.smack.provider.PacketExtensionProvider;
022import org.xmlpull.v1.XmlPullParser;
023
024/**
025 * An IQ packet that encapsulates both types of workgroup queue
026 * status notifications -- position updates, and estimated time
027 * left in the queue updates.
028 */
029public class QueueUpdate implements PacketExtension {
030
031    /**
032     * Element name of the packet extension.
033     */
034    public static final String ELEMENT_NAME = "queue-status";
035
036    /**
037     * Namespace of the packet extension.
038     */
039    public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
040
041    private int position;
042    private int remainingTime;
043
044    public QueueUpdate(int position, int remainingTime) {
045        this.position = position;
046        this.remainingTime = remainingTime;
047    }
048
049    /**
050     * Returns the user's position in the workgroup queue, or -1 if the
051     * value isn't set on this packet.
052     *
053     * @return the position in the workgroup queue.
054     */
055    public int getPosition() {
056        return this.position;
057    }
058
059    /**
060     * Returns the user's estimated time left in the workgroup queue, or
061     * -1 if the value isn't set on this packet.
062     *
063     * @return the estimated time left in the workgroup queue.
064     */
065    public int getRemaingTime() {
066        return remainingTime;
067    }
068
069    public String toXML() {
070        StringBuilder buf = new StringBuilder();
071        buf.append("<queue-status xmlns=\"http://jabber.org/protocol/workgroup\">");
072        if (position != -1) {
073            buf.append("<position>").append(position).append("</position>");
074        }
075        if (remainingTime != -1) {
076            buf.append("<time>").append(remainingTime).append("</time>");
077        }
078        buf.append("</queue-status>");
079        return buf.toString();
080    }
081
082    public String getElementName() {
083        return ELEMENT_NAME;
084    }
085
086    public String getNamespace() {
087        return NAMESPACE;
088    }
089
090    public static class Provider implements PacketExtensionProvider {
091
092        public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
093            boolean done = false;
094            int position = -1;
095            int timeRemaining = -1;
096            while (!done) {
097                parser.next();
098                String elementName = parser.getName();
099                if (parser.getEventType() == XmlPullParser.START_TAG && "position".equals(elementName)) {
100                    try {
101                        position = Integer.parseInt(parser.nextText());
102                    }
103                    catch (NumberFormatException nfe) {
104                    }
105                }
106                else if (parser.getEventType() == XmlPullParser.START_TAG && "time".equals(elementName)) {
107                    try {
108                        timeRemaining = Integer.parseInt(parser.nextText());
109                    }
110                    catch (NumberFormatException nfe) {
111                    }
112                }
113                else if (parser.getEventType() == XmlPullParser.END_TAG && "queue-status".equals(elementName)) {
114                    done = true;
115                }
116            }
117            return new QueueUpdate(position, timeRemaining);
118        }
119    }
120}