1 20 21 package org.jivesoftware.smack; 22 23 import org.jivesoftware.smack.packet.RosterPacket; 24 import org.jivesoftware.smack.packet.IQ; 25 26 import java.util.*; 27 28 34 public class RosterEntry { 35 36 private String user; 37 private String name; 38 private RosterPacket.ItemType type; 39 private XMPPConnection connection; 40 41 49 RosterEntry(String user, String name, RosterPacket.ItemType type, XMPPConnection connection) { 50 this.user = user; 51 this.name = name; 52 this.type = type; 53 this.connection = connection; 54 } 55 56 61 public String getUser() { 62 return user; 63 } 64 65 70 public String getName() { 71 return name; 72 } 73 74 79 public void setName(String name) { 80 if (name != null && name.equals(this.name)) { 82 return; 83 } 84 this.name = name; 85 RosterPacket packet = new RosterPacket(); 86 packet.setType(IQ.Type.SET); 87 packet.addRosterItem(toRosterItem(this)); 88 connection.sendPacket(packet); 89 } 90 91 97 void updateState(String name, RosterPacket.ItemType type) { 98 this.name = name; 99 this.type = type; 100 } 101 102 107 public Iterator getGroups() { 108 List results = new ArrayList(); 109 for (Iterator i=connection.roster.getGroups(); i.hasNext(); ) { 112 RosterGroup group = (RosterGroup)i.next(); 113 if (group.contains(this)) { 114 results.add(group); 115 } 116 } 117 return results.iterator(); 118 } 119 120 126 public RosterPacket.ItemType getType() { 127 return type; 128 } 129 130 public String toString() { 131 StringBuffer buf = new StringBuffer (); 132 if (name != null) { 133 buf.append(name).append(": "); 134 } 135 buf.append(user); 136 Iterator groups = getGroups(); 137 if (groups.hasNext()) { 138 buf.append(" ["); 139 RosterGroup group = (RosterGroup)groups.next(); 140 buf.append(group.getName()); 141 while (groups.hasNext()) { 142 buf.append(", "); 143 group = (RosterGroup)groups.next(); 144 buf.append(group.getName()); 145 } 146 buf.append("]"); 147 } 148 return buf.toString(); 149 } 150 151 public boolean equals(Object object) { 152 if (this == object) { 153 return true; 154 } 155 if (object != null && object instanceof RosterEntry) { 156 return user.toLowerCase().equals(((RosterEntry)object).getUser().toLowerCase()); 157 } 158 else { 159 return false; 160 } 161 } 162 163 static RosterPacket.Item toRosterItem(RosterEntry entry) { 164 RosterPacket.Item item = new RosterPacket.Item(entry.getUser(), entry.getName()); 165 item.setItemType(entry.getType()); 166 for (Iterator j=entry.getGroups(); j.hasNext(); ) { 168 RosterGroup group = (RosterGroup)j.next(); 169 item.addGroupName(group.getName()); 170 } 171 return item; 172 } 173 } | Popular Tags |