2004-11-03 00:53:30 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
|
|
|
* Copyright 2003-2004 Jive Software.
|
|
|
|
*
|
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2006-07-18 07:14:33 +02:00
|
|
|
import org.jivesoftware.smack.packet.RosterPacket;
|
2003-02-10 06:01:01 +01:00
|
|
|
|
|
|
|
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;
|
2005-11-03 17:53:07 +01:00
|
|
|
private RosterPacket.ItemStatus status;
|
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.
|
2005-11-03 17:53:07 +01:00
|
|
|
* @param status the subscription status (related to subscriptions pending to be approbed).
|
2003-04-25 22:12:56 +02:00
|
|
|
* @param connection a connection to the XMPP server.
|
|
|
|
*/
|
2005-11-03 17:53:07 +01:00
|
|
|
RosterEntry(String user, String name, RosterPacket.ItemType type,
|
|
|
|
RosterPacket.ItemStatus status, 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;
|
2005-11-03 17:53:07 +01:00
|
|
|
this.status = status;
|
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-09-24 20:03:33 +02:00
|
|
|
if (name != null && name.equals(this.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);
|
|
|
|
}
|
|
|
|
|
2003-09-25 15:00:48 +02:00
|
|
|
/**
|
|
|
|
* Updates the state of the entry with the new values.
|
|
|
|
*
|
|
|
|
* @param name the nickname for the entry.
|
|
|
|
* @param type the subscription type.
|
2005-11-03 17:53:07 +01:00
|
|
|
* @param status the subscription status (related to subscriptions pending to be approbed).
|
2003-09-25 15:00:48 +02:00
|
|
|
*/
|
2005-11-03 17:53:07 +01:00
|
|
|
void updateState(String name, RosterPacket.ItemType type, RosterPacket.ItemStatus status) {
|
2003-09-25 15:00:48 +02:00
|
|
|
this.name = name;
|
|
|
|
this.type = type;
|
2005-11-03 17:53:07 +01:00
|
|
|
this.status = status;
|
2003-09-25 15:00:48 +02:00
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
/**
|
2006-07-17 10:39:08 +02:00
|
|
|
* Returns an unmodifiable collection of the roster groups that this entry belongs to.
|
2003-02-10 06:01:01 +01:00
|
|
|
*
|
|
|
|
* @return an iterator for the groups this entry belongs to.
|
|
|
|
*/
|
2006-07-17 10:39:08 +02:00
|
|
|
public Collection<RosterGroup> getGroups() {
|
|
|
|
List<RosterGroup> results = new ArrayList<RosterGroup>();
|
2003-02-10 06:01:01 +01:00
|
|
|
// Loop through all roster groups and find the ones that contain this
|
|
|
|
// entry. This algorithm should be fine
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterGroup group: connection.roster.getGroups()) {
|
2003-02-10 06:01:01 +01:00
|
|
|
if (group.contains(this)) {
|
|
|
|
results.add(group);
|
|
|
|
}
|
|
|
|
}
|
2006-07-17 10:39:08 +02:00
|
|
|
return Collections.unmodifiableCollection(results);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
2003-04-25 22:12:56 +02:00
|
|
|
/**
|
|
|
|
* Returns the roster subscription type of the entry. When the type is
|
2007-01-08 00:03:16 +01:00
|
|
|
* {@link RosterPacket.ItemType#none} or {@link RosterPacket.ItemType#from},
|
2005-11-19 19:13:50 +01:00
|
|
|
* refer to {@link RosterEntry getStatus()} to see if a subscription request
|
|
|
|
* is pending.
|
2003-04-25 22:12:56 +02:00
|
|
|
*
|
|
|
|
* @return the type.
|
|
|
|
*/
|
|
|
|
public RosterPacket.ItemType getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2005-11-03 17:53:07 +01:00
|
|
|
/**
|
|
|
|
* Returns the roster subscription status of the entry. When the status is
|
2005-11-19 19:13:50 +01:00
|
|
|
* RosterPacket.ItemStatus.SUBSCRIPTION_PENDING, the contact has to answer the
|
|
|
|
* subscription request.
|
2005-11-03 17:53:07 +01:00
|
|
|
*
|
|
|
|
* @return the status.
|
|
|
|
*/
|
|
|
|
public RosterPacket.ItemStatus getStatus() {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2003-08-05 04:33:11 +02:00
|
|
|
public String toString() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2003-09-24 21:06:27 +02:00
|
|
|
if (name != null) {
|
|
|
|
buf.append(name).append(": ");
|
|
|
|
}
|
2003-08-05 04:33:11 +02:00
|
|
|
buf.append(user);
|
2006-07-17 10:39:08 +02:00
|
|
|
Collection<RosterGroup> groups = getGroups();
|
|
|
|
if (!groups.isEmpty()) {
|
2003-08-05 04:33:11 +02:00
|
|
|
buf.append(" [");
|
2006-07-17 10:39:08 +02:00
|
|
|
Iterator<RosterGroup> iter = groups.iterator();
|
|
|
|
RosterGroup group = iter.next();
|
2003-08-05 04:33:11 +02:00
|
|
|
buf.append(group.getName());
|
2006-07-17 10:39:08 +02:00
|
|
|
while (iter.hasNext()) {
|
2003-08-05 04:33:11 +02:00
|
|
|
buf.append(", ");
|
2006-07-17 10:39:08 +02:00
|
|
|
group = iter.next();
|
2003-08-05 04:33:11 +02:00
|
|
|
buf.append(group.getName());
|
|
|
|
}
|
|
|
|
buf.append("]");
|
|
|
|
}
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
public boolean equals(Object object) {
|
|
|
|
if (this == object) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (object != null && object instanceof RosterEntry) {
|
2006-01-24 19:14:41 +01:00
|
|
|
return user.equals(((RosterEntry)object).getUser());
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
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());
|
2005-11-03 17:53:07 +01:00
|
|
|
item.setItemStatus(entry.getStatus());
|
2003-02-10 06:01:01 +01:00
|
|
|
// Set the correct group names for the item.
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterGroup group : entry.getGroups()) {
|
2003-02-10 06:01:01 +01:00
|
|
|
item.addGroupName(group.getName());
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|