Home | History | Annotate | Download | only in packet
      1 /**
      2  * $Revision$
      3  * $Date$
      4  *
      5  * Copyright 2003-2007 Jive Software.
      6  *
      7  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *     http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  */
     19 
     20 package org.jivesoftware.smackx.workgroup.packet;
     21 
     22 import org.jivesoftware.smack.packet.PacketExtension;
     23 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     24 import org.xmlpull.v1.XmlPullParser;
     25 
     26 /**
     27  * An IQ packet that encapsulates both types of workgroup queue
     28  * status notifications -- position updates, and estimated time
     29  * left in the queue updates.
     30  */
     31 public class QueueUpdate implements PacketExtension {
     32 
     33     /**
     34      * Element name of the packet extension.
     35      */
     36     public static final String ELEMENT_NAME = "queue-status";
     37 
     38     /**
     39      * Namespace of the packet extension.
     40      */
     41     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
     42 
     43     private int position;
     44     private int remainingTime;
     45 
     46     public QueueUpdate(int position, int remainingTime) {
     47         this.position = position;
     48         this.remainingTime = remainingTime;
     49     }
     50 
     51     /**
     52      * Returns the user's position in the workgroup queue, or -1 if the
     53      * value isn't set on this packet.
     54      *
     55      * @return the position in the workgroup queue.
     56      */
     57     public int getPosition() {
     58         return this.position;
     59     }
     60 
     61     /**
     62      * Returns the user's estimated time left in the workgroup queue, or
     63      * -1 if the value isn't set on this packet.
     64      *
     65      * @return the estimated time left in the workgroup queue.
     66      */
     67     public int getRemaingTime() {
     68         return remainingTime;
     69     }
     70 
     71     public String toXML() {
     72         StringBuilder buf = new StringBuilder();
     73         buf.append("<queue-status xmlns=\"http://jabber.org/protocol/workgroup\">");
     74         if (position != -1) {
     75             buf.append("<position>").append(position).append("</position>");
     76         }
     77         if (remainingTime != -1) {
     78             buf.append("<time>").append(remainingTime).append("</time>");
     79         }
     80         buf.append("</queue-status>");
     81         return buf.toString();
     82     }
     83 
     84     public String getElementName() {
     85         return ELEMENT_NAME;
     86     }
     87 
     88     public String getNamespace() {
     89         return NAMESPACE;
     90     }
     91 
     92     public static class Provider implements PacketExtensionProvider {
     93 
     94         public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
     95             boolean done = false;
     96             int position = -1;
     97             int timeRemaining = -1;
     98             while (!done) {
     99                 parser.next();
    100                 String elementName = parser.getName();
    101                 if (parser.getEventType() == XmlPullParser.START_TAG && "position".equals(elementName)) {
    102                     try {
    103                         position = Integer.parseInt(parser.nextText());
    104                     }
    105                     catch (NumberFormatException nfe) {
    106                     }
    107                 }
    108                 else if (parser.getEventType() == XmlPullParser.START_TAG && "time".equals(elementName)) {
    109                     try {
    110                         timeRemaining = Integer.parseInt(parser.nextText());
    111                     }
    112                     catch (NumberFormatException nfe) {
    113                     }
    114                 }
    115                 else if (parser.getEventType() == XmlPullParser.END_TAG && "queue-status".equals(elementName)) {
    116                     done = true;
    117                 }
    118             }
    119             return new QueueUpdate(position, timeRemaining);
    120         }
    121     }
    122 }