mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 04:22:05 +01:00
Modified RosterListener to fire more specific events when entries are added, deleted or modified. SMACK-106
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@3032 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
1cdefcc796
commit
e5ea043798
2 changed files with 45 additions and 6 deletions
|
@ -549,16 +549,25 @@ public class Roster {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fires roster changed event to roster listeners.
|
* Fires roster changed event to roster listeners indicating that the
|
||||||
|
* specified collections of contacts have been added, updated or deleted
|
||||||
|
* from the roster.
|
||||||
|
*
|
||||||
|
* @param addedEntries the collection of address of the added contacts.
|
||||||
|
* @param updatedEntries the collection of address of the updated contacts.
|
||||||
|
* @param deletedEntries the collection of address of the deleted contacts.
|
||||||
*/
|
*/
|
||||||
private void fireRosterChangedEvent() {
|
private void fireRosterChangedEvent(Collection addedEntries, Collection updatedEntries,
|
||||||
|
Collection deletedEntries) {
|
||||||
RosterListener [] listeners = null;
|
RosterListener [] listeners = null;
|
||||||
synchronized (rosterListeners) {
|
synchronized (rosterListeners) {
|
||||||
listeners = new RosterListener[rosterListeners.size()];
|
listeners = new RosterListener[rosterListeners.size()];
|
||||||
rosterListeners.toArray(listeners);
|
rosterListeners.toArray(listeners);
|
||||||
}
|
}
|
||||||
for (int i=0; i<listeners.length; i++) {
|
for (int i=0; i<listeners.length; i++) {
|
||||||
listeners[i].rosterModified();
|
listeners[i].entriesAdded(addedEntries);
|
||||||
|
listeners[i].entriesUpdated(updatedEntries);
|
||||||
|
listeners[i].entriesDeleted(deletedEntries);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -667,6 +676,12 @@ public class Roster {
|
||||||
private class RosterPacketListener implements PacketListener {
|
private class RosterPacketListener implements PacketListener {
|
||||||
|
|
||||||
public void processPacket(Packet packet) {
|
public void processPacket(Packet packet) {
|
||||||
|
// Keep a registry of the entries that were added, deleted or updated. An event
|
||||||
|
// will be fired for each affected entry
|
||||||
|
Collection addedEntries = new ArrayList();
|
||||||
|
Collection updatedEntries = new ArrayList();
|
||||||
|
Collection deletedEntries = new ArrayList();
|
||||||
|
|
||||||
RosterPacket rosterPacket = (RosterPacket)packet;
|
RosterPacket rosterPacket = (RosterPacket)packet;
|
||||||
for (Iterator i=rosterPacket.getRosterItems(); i.hasNext(); ) {
|
for (Iterator i=rosterPacket.getRosterItems(); i.hasNext(); ) {
|
||||||
RosterPacket.Item item = (RosterPacket.Item)i.next();
|
RosterPacket.Item item = (RosterPacket.Item)i.next();
|
||||||
|
@ -690,11 +705,15 @@ public class Roster {
|
||||||
String key = StringUtils.parseName(item.getUser()) + "@" +
|
String key = StringUtils.parseName(item.getUser()) + "@" +
|
||||||
StringUtils.parseServer(item.getUser());
|
StringUtils.parseServer(item.getUser());
|
||||||
presenceMap.remove(key);
|
presenceMap.remove(key);
|
||||||
|
// Keep note that an entry has been removed
|
||||||
|
deletedEntries.add(item.getUser());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Make sure the entry is in the entry list.
|
// Make sure the entry is in the entry list.
|
||||||
if (!entries.contains(entry)) {
|
if (!entries.contains(entry)) {
|
||||||
entries.add(entry);
|
entries.add(entry);
|
||||||
|
// Keep note that an entry has been added
|
||||||
|
addedEntries.add(item.getUser());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// If the entry was in then list then update its state with the new values
|
// If the entry was in then list then update its state with the new values
|
||||||
|
@ -702,6 +721,8 @@ public class Roster {
|
||||||
(RosterEntry) entries.get(entries.indexOf(entry));
|
(RosterEntry) entries.get(entries.indexOf(entry));
|
||||||
existingEntry
|
existingEntry
|
||||||
.updateState(entry.getName(), entry.getType(), entry.getStatus());
|
.updateState(entry.getName(), entry.getType(), entry.getStatus());
|
||||||
|
// Keep note that an entry has been updated
|
||||||
|
updatedEntries.add(item.getUser());
|
||||||
}
|
}
|
||||||
// If the roster entry belongs to any groups, remove it from the
|
// If the roster entry belongs to any groups, remove it from the
|
||||||
// list of unfiled entries.
|
// list of unfiled entries.
|
||||||
|
@ -787,7 +808,7 @@ public class Roster {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fire event for roster listeners.
|
// Fire event for roster listeners.
|
||||||
fireRosterChangedEvent();
|
fireRosterChangedEvent(addedEntries, updatedEntries, deletedEntries);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -20,6 +20,8 @@
|
||||||
|
|
||||||
package org.jivesoftware.smack;
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A listener that is fired any time a roster is changed or the presence of
|
* A listener that is fired any time a roster is changed or the presence of
|
||||||
* a user in the roster is changed.
|
* a user in the roster is changed.
|
||||||
|
@ -29,9 +31,25 @@ package org.jivesoftware.smack;
|
||||||
public interface RosterListener {
|
public interface RosterListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a roster entry is added or removed.
|
* Called when roster entries are added.
|
||||||
|
*
|
||||||
|
* @param addresses the XMPP addresses of the contacts that have been added to the roster.
|
||||||
*/
|
*/
|
||||||
public void rosterModified();
|
public void entriesAdded(Collection addresses);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a roster entries are updated.
|
||||||
|
*
|
||||||
|
* @param addresses the XMPP addresses of the contacts whose entries have been updated.
|
||||||
|
*/
|
||||||
|
public void entriesUpdated(Collection addresses);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a roster entries are removed.
|
||||||
|
*
|
||||||
|
* @param addresses the XMPP addresses of the contacts that have been removed from the roster.
|
||||||
|
*/
|
||||||
|
public void entriesDeleted(Collection addresses);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the presence of a roster entry is changed.
|
* Called when the presence of a roster entry is changed.
|
||||||
|
|
Loading…
Reference in a new issue