1 20 21 package org.jivesoftware.smack; 22 23 import org.jivesoftware.smack.packet.RosterPacket; 24 import org.jivesoftware.smack.packet.IQ; 25 import org.jivesoftware.smack.util.StringUtils; 26 import org.jivesoftware.smack.filter.PacketIDFilter; 27 28 import java.util.*; 29 30 36 public class RosterGroup { 37 38 private String name; 39 private XMPPConnection connection; 40 private List entries; 41 42 48 RosterGroup(String name, XMPPConnection connection) { 49 this.name = name; 50 this.connection = connection; 51 entries = new ArrayList(); 52 } 53 54 59 public String getName() { 60 return name; 61 } 62 63 71 public void setName(String name) { 72 synchronized (entries) { 73 for (int i=0; i<entries.size(); i++) { 74 RosterPacket packet = new RosterPacket(); 75 packet.setType(IQ.Type.SET); 76 RosterEntry entry = (RosterEntry)entries.get(i); 77 RosterPacket.Item item = RosterEntry.toRosterItem(entry); 78 item.removeGroupName(this.name); 79 item.addGroupName(name); 80 packet.addRosterItem(item); 81 connection.sendPacket(packet); 82 } 83 } 84 } 85 86 91 public int getEntryCount() { 92 synchronized (entries) { 93 return entries.size(); 94 } 95 } 96 97 102 public Iterator getEntries() { 103 synchronized (entries) { 104 return Collections.unmodifiableList(new ArrayList(entries)).iterator(); 105 } 106 } 107 108 115 public RosterEntry getEntry(String user) { 116 if (user == null) { 117 return null; 118 } 119 user = StringUtils.parseBareAddress(user); 122 synchronized (entries) { 123 for (Iterator i=entries.iterator(); i.hasNext(); ) { 124 RosterEntry entry = (RosterEntry)i.next(); 125 if (entry.getUser().toLowerCase().equals(user.toLowerCase())) { 126 return entry; 127 } 128 } 129 } 130 return null; 131 } 132 133 139 public boolean contains(RosterEntry entry) { 140 synchronized (entries) { 141 return entries.contains(entry); 142 } 143 } 144 145 151 public boolean contains(String user) { 152 if (user == null) { 153 return false; 154 } 155 user = StringUtils.parseBareAddress(user); 158 synchronized (entries) { 159 for (Iterator i=entries.iterator(); i.hasNext(); ) { 160 RosterEntry entry = (RosterEntry)i.next(); 161 if (entry.getUser().toLowerCase().equals(user.toLowerCase())) { 162 return true; 163 } 164 } 165 } 166 return false; 167 } 168 169 176 public void addEntry(RosterEntry entry) throws XMPPException { 177 PacketCollector collector = null; 178 synchronized (entries) { 180 if (!entries.contains(entry)) { 181 RosterPacket packet = new RosterPacket(); 182 packet.setType(IQ.Type.SET); 183 packet.addRosterItem(RosterEntry.toRosterItem(entry)); 184 collector = connection 186 .createPacketCollector(new PacketIDFilter(packet.getPacketID())); 187 connection.sendPacket(packet); 188 } 189 } 190 if (collector != null) { 191 IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 192 collector.cancel(); 193 if (response == null) { 194 throw new XMPPException("No response from the server."); 195 } 196 else if (response.getType() == IQ.Type.ERROR) { 198 throw new XMPPException(response.getError()); 199 } 200 entries.add(entry); 202 } 203 } 204 205 213 public void removeEntry(RosterEntry entry) throws XMPPException { 214 PacketCollector collector = null; 215 synchronized (entries) { 220 if (entries.contains(entry)) { 221 RosterPacket packet = new RosterPacket(); 222 packet.setType(IQ.Type.SET); 223 RosterPacket.Item item = RosterEntry.toRosterItem(entry); 224 item.removeGroupName(this.getName()); 225 packet.addRosterItem(item); 226 collector = connection 228 .createPacketCollector(new PacketIDFilter(packet.getPacketID())); 229 connection.sendPacket(packet); 230 } 231 } 232 if (collector != null) { 233 IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 234 collector.cancel(); 235 if (response == null) { 236 throw new XMPPException("No response from the server."); 237 } 238 else if (response.getType() == IQ.Type.ERROR) { 240 throw new XMPPException(response.getError()); 241 } 242 entries.remove(entry); 244 } 245 } 246 247 void addEntryLocal(RosterEntry entry) { 248 synchronized (entries) { 250 entries.remove(entry); 251 entries.add(entry); 252 } 253 } 254 255 void removeEntryLocal(RosterEntry entry) { 256 synchronized (entries) { 258 if (entries.contains(entry)) { 259 entries.remove(entry); 260 } 261 } 262 } 263 } | Popular Tags |