mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02:00
add the ability to register for roster events before logging in (SMACK-156)
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@11826 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
7a3818783b
commit
a5693609b2
9 changed files with 469 additions and 88 deletions
|
@ -62,7 +62,7 @@ public class Roster {
|
|||
private final List<RosterListener> rosterListeners;
|
||||
private Map<String, Map<String, Presence>> presenceMap;
|
||||
// The roster is marked as initialized when at least a single roster packet
|
||||
// has been recieved and processed.
|
||||
// has been received and processed.
|
||||
boolean rosterInitialized = false;
|
||||
private PresencePacketListener presencePacketListener;
|
||||
|
||||
|
@ -111,8 +111,10 @@ public class Roster {
|
|||
PacketFilter presenceFilter = new PacketTypeFilter(Presence.class);
|
||||
presencePacketListener = new PresencePacketListener();
|
||||
connection.addPacketListener(presencePacketListener, presenceFilter);
|
||||
|
||||
// Listen for connection events
|
||||
connection.addConnectionListener(new ConnectionListener() {
|
||||
final ConnectionListener connectionListener = new AbstractConnectionListener() {
|
||||
|
||||
public void connectionClosed() {
|
||||
// Changes the presence available contacts to unavailable
|
||||
setOfflinePresences();
|
||||
|
@ -123,18 +125,22 @@ public class Roster {
|
|||
setOfflinePresences();
|
||||
}
|
||||
|
||||
public void reconnectingIn(int seconds) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
public void reconnectionFailed(Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
public void reconnectionSuccessful() {
|
||||
// Ignore
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// if not connected add listener after successful login
|
||||
if(!this.connection.isConnected()) {
|
||||
Connection.addConnectionCreationListener(new ConnectionCreationListener() {
|
||||
|
||||
public void connectionCreated(Connection connection) {
|
||||
if(connection.equals(Roster.this.connection)) {
|
||||
Roster.this.connection.addConnectionListener(connectionListener);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
} else {
|
||||
connection.addConnectionListener(connectionListener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -171,8 +177,17 @@ public class Roster {
|
|||
* Reloads the entire roster from the server. This is an asynchronous operation,
|
||||
* which means the method will return immediately, and the roster will be
|
||||
* reloaded at a later point when the server responds to the reload request.
|
||||
*
|
||||
* @throws IllegalStateException if connection is not logged in or logged in anonymously
|
||||
*/
|
||||
public void reload() {
|
||||
if (!connection.isAuthenticated()) {
|
||||
throw new IllegalStateException("Not logged in to server.");
|
||||
}
|
||||
if (connection.isAnonymous()) {
|
||||
throw new IllegalStateException("Anonymous users can't have a roster.");
|
||||
}
|
||||
|
||||
connection.sendPacket(new RosterPacket());
|
||||
}
|
||||
|
||||
|
@ -206,11 +221,19 @@ public class Roster {
|
|||
*
|
||||
* @param name the name of the group.
|
||||
* @return a new group.
|
||||
* @throws IllegalStateException if connection is not logged in or logged in anonymously
|
||||
*/
|
||||
public RosterGroup createGroup(String name) {
|
||||
if (!connection.isAuthenticated()) {
|
||||
throw new IllegalStateException("Not logged in to server.");
|
||||
}
|
||||
if (connection.isAnonymous()) {
|
||||
throw new IllegalStateException("Anonymous users can't have a roster.");
|
||||
}
|
||||
if (groups.containsKey(name)) {
|
||||
throw new IllegalArgumentException("Group with name " + name + " alread exists.");
|
||||
}
|
||||
|
||||
RosterGroup group = new RosterGroup(name, connection);
|
||||
groups.put(name, group);
|
||||
return group;
|
||||
|
@ -225,8 +248,16 @@ public class Roster {
|
|||
* @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
|
||||
* the roster entry won't belong to a group.
|
||||
* @throws XMPPException if an XMPP exception occurs.
|
||||
* @throws IllegalStateException if connection is not logged in or logged in anonymously
|
||||
*/
|
||||
public void createEntry(String user, String name, String[] groups) throws XMPPException {
|
||||
if (!connection.isAuthenticated()) {
|
||||
throw new IllegalStateException("Not logged in to server.");
|
||||
}
|
||||
if (connection.isAnonymous()) {
|
||||
throw new IllegalStateException("Anonymous users can't have a roster.");
|
||||
}
|
||||
|
||||
// Create and send roster entry creation packet.
|
||||
RosterPacket rosterPacket = new RosterPacket();
|
||||
rosterPacket.setType(IQ.Type.SET);
|
||||
|
@ -267,8 +298,16 @@ public class Roster {
|
|||
*
|
||||
* @param entry a roster entry.
|
||||
* @throws XMPPException if an XMPP error occurs.
|
||||
* @throws IllegalStateException if connection is not logged in or logged in anonymously
|
||||
*/
|
||||
public void removeEntry(RosterEntry entry) throws XMPPException {
|
||||
if (!connection.isAuthenticated()) {
|
||||
throw new IllegalStateException("Not logged in to server.");
|
||||
}
|
||||
if (connection.isAnonymous()) {
|
||||
throw new IllegalStateException("Anonymous users can't have a roster.");
|
||||
}
|
||||
|
||||
// Only remove the entry if it's in the entry list.
|
||||
// The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)
|
||||
if (!entries.containsKey(entry.getUser())) {
|
||||
|
@ -389,7 +428,7 @@ public class Roster {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns an unmodiable collections of all the roster groups.
|
||||
* Returns an unmodifiable collections of all the roster groups.
|
||||
*
|
||||
* @return an iterator for all roster groups.
|
||||
*/
|
||||
|
@ -859,14 +898,14 @@ public class Roster {
|
|||
|
||||
// We have the list of old and new group names. We now need to
|
||||
// remove the entry from the all the groups it may no longer belong
|
||||
// to. We do this by subracting the new group set from the old.
|
||||
// to. We do this by subtracting the new group set from the old.
|
||||
for (String newGroupName : newGroupNames) {
|
||||
currentGroupNames.remove(newGroupName);
|
||||
}
|
||||
}
|
||||
|
||||
// Loop through any groups that remain and remove the entries.
|
||||
// This is neccessary for the case of remote entry removals.
|
||||
// This is necessary for the case of remote entry removals.
|
||||
for (String groupName : currentGroupNames) {
|
||||
RosterGroup group = getGroup(groupName);
|
||||
group.removeEntryLocal(entry);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue