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