2003-02-10 06:01:01 +01:00
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.RosterPacket;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Each user in your roster is represented by a roster entry, which contains the user's
|
|
|
|
* JID and a name or nickname you assign.
|
|
|
|
*
|
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
|
|
|
public class RosterEntry {
|
|
|
|
|
|
|
|
private String user;
|
|
|
|
private String name;
|
2003-04-25 22:12:56 +02:00
|
|
|
private RosterPacket.ItemType type;
|
2003-02-10 06:01:01 +01:00
|
|
|
private XMPPConnection connection;
|
|
|
|
|
2003-04-25 22:12:56 +02:00
|
|
|
/**
|
|
|
|
* Creates a new roster entry.
|
|
|
|
*
|
|
|
|
* @param user the user.
|
|
|
|
* @param name the nickname for the entry.
|
|
|
|
* @param type the subscription type.
|
|
|
|
* @param connection a connection to the XMPP server.
|
|
|
|
*/
|
|
|
|
RosterEntry(String user, String name, RosterPacket.ItemType type, XMPPConnection connection) {
|
2003-02-10 06:01:01 +01:00
|
|
|
this.user = user;
|
|
|
|
this.name = name;
|
2003-04-25 22:12:56 +02:00
|
|
|
this.type = type;
|
2003-02-10 06:01:01 +01:00
|
|
|
this.connection = connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the JID of the user associated with this entry.
|
|
|
|
*
|
|
|
|
* @return the user associated with this entry.
|
|
|
|
*/
|
|
|
|
public String getUser() {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name associated with this entry.
|
|
|
|
*
|
|
|
|
* @return the name.
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2003-04-25 22:12:56 +02:00
|
|
|
/**
|
|
|
|
* Sets the name associated with this entry.
|
|
|
|
*
|
|
|
|
* @param name the name.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public void setName(String name) {
|
2003-04-25 22:12:56 +02:00
|
|
|
// Do nothing if the name hasn't changed.
|
2003-05-05 06:33:22 +02:00
|
|
|
if (name != null && this.name.equals(name)) {
|
2003-04-25 22:12:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
this.name = name;
|
|
|
|
RosterPacket packet = new RosterPacket();
|
|
|
|
packet.setType(IQ.Type.SET);
|
|
|
|
packet.addRosterItem(toRosterItem(this));
|
|
|
|
connection.sendPacket(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator for all the roster groups that this entry belongs to.
|
|
|
|
*
|
|
|
|
* @return an iterator for the groups this entry belongs to.
|
|
|
|
*/
|
|
|
|
public Iterator getGroups() {
|
|
|
|
List results = new ArrayList();
|
|
|
|
// Loop through all roster groups and find the ones that contain this
|
|
|
|
// entry. This algorithm should be fine
|
2003-04-07 07:40:28 +02:00
|
|
|
for (Iterator i=connection.roster.getGroups(); i.hasNext(); ) {
|
2003-02-10 06:01:01 +01:00
|
|
|
RosterGroup group = (RosterGroup)i.next();
|
|
|
|
if (group.contains(this)) {
|
|
|
|
results.add(group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results.iterator();
|
|
|
|
}
|
|
|
|
|
2003-04-25 22:12:56 +02:00
|
|
|
/**
|
|
|
|
* Returns the roster subscription type of the entry. When the type is
|
|
|
|
* {@link RosterPacket.ItemType#NONE}, the subscription request is pending.
|
|
|
|
*
|
|
|
|
* @return the type.
|
|
|
|
*/
|
|
|
|
public RosterPacket.ItemType getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
public boolean equals(Object object) {
|
|
|
|
if (this == object) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (object != null && object instanceof RosterEntry) {
|
|
|
|
return user.equals(((RosterEntry)object).getUser());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static RosterPacket.Item toRosterItem(RosterEntry entry) {
|
|
|
|
RosterPacket.Item item = new RosterPacket.Item(entry.getUser(), entry.getName());
|
2003-04-25 22:12:56 +02:00
|
|
|
item.setItemType(entry.getType());
|
2003-02-10 06:01:01 +01:00
|
|
|
// Set the correct group names for the item.
|
|
|
|
for (Iterator j=entry.getGroups(); j.hasNext(); ) {
|
|
|
|
RosterGroup group = (RosterGroup)j.next();
|
|
|
|
item.addGroupName(group.getName());
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|