1
0
Fork 0
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:
Henning Staib 2010-08-15 16:32:09 +00:00 committed by henning
parent 7a3818783b
commit a5693609b2
9 changed files with 469 additions and 88 deletions

View file

@ -419,11 +419,15 @@ public abstract class Connection {
}
/**
* Returns the roster for the user logged into the server. If the user has not yet
* logged into the server (or if the user is logged in anonymously), this method will return
* <tt>null</tt>.
* Returns the roster for the user.
* <p>
* This method will never return <code>null</code>, instead if the user has not yet logged into
* the server or is logged in anonymously all modifying methods of the returned roster object
* like {@link Roster#createEntry(String, String, String[])},
* {@link Roster#removeEntry(RosterEntry)} , etc. except adding or removing
* {@link RosterListener}s will throw an IllegalStateException.
*
* @return the user's roster, or <tt>null</tt> if the user has not logged in yet.
* @return the user's roster.
*/
public abstract Roster getRoster();

View file

@ -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);

View file

@ -119,7 +119,7 @@ public class XMPPConnection extends Connection {
}
/**
* Creates a new XMPP conection in the same way {@link #XMPPConnection(String,CallbackHandler)} does, but
* Creates a new XMPP connection in the same way {@link #XMPPConnection(String,CallbackHandler)} does, but
* with no callback handler for password prompting of the keystore. This will work
* in most cases, provided the client is not required to provide a certificate to
* the server.
@ -135,7 +135,7 @@ public class XMPPConnection extends Connection {
}
/**
* Creates a new XMPP conection in the same way {@link #XMPPConnection(ConnectionConfiguration,CallbackHandler)} does, but
* Creates a new XMPP connection in the same way {@link #XMPPConnection(ConnectionConfiguration,CallbackHandler)} does, but
* with no callback handler for password prompting of the keystore. This will work
* in most cases, provided the client is not required to provide a certificate to
* the server.
@ -210,7 +210,7 @@ public class XMPPConnection extends Connection {
* @param resource the resource.
* @throws XMPPException if an error occurs.
* @throws IllegalStateException if not connected to the server, or already logged in
* to the serrver.
* to the server.
*/
public synchronized void login(String username, String password, String resource) throws XMPPException {
if (!isConnected()) {
@ -257,7 +257,11 @@ public class XMPPConnection extends Connection {
useCompression();
}
// Create the roster if it is not a reconnection.
// Indicate that we're now authenticated.
authenticated = true;
anonymous = false;
// Create the roster if it is not a reconnection or roster already created by getRoster()
if (this.roster == null) {
this.roster = new Roster(this);
}
@ -270,11 +274,7 @@ public class XMPPConnection extends Connection {
packetWriter.sendPacket(new Presence(Presence.Type.available));
}
// Indicate that we're now authenticated.
authenticated = true;
anonymous = false;
// Stores the autentication for future reconnection
// Stores the authentication for future reconnection
config.setLoginInfo(username, password, resource);
// If debugging is enabled, change the the debug window title to include the
@ -294,7 +294,7 @@ public class XMPPConnection extends Connection {
*
* @throws XMPPException if an error occurs or anonymous logins are not supported by the server.
* @throws IllegalStateException if not connected to the server, or already logged in
* to the serrver.
* to the server.
*/
public synchronized void loginAnonymously() throws XMPPException {
if (!isConnected()) {
@ -324,9 +324,6 @@ public class XMPPConnection extends Connection {
useCompression();
}
// Anonymous users can't have a roster.
roster = null;
// Set presence to online.
packetWriter.sendPacket(new Presence(Presence.Type.available));
@ -344,9 +341,18 @@ public class XMPPConnection extends Connection {
}
public Roster getRoster() {
if (roster == null) {
return null;
// synchronize against login()
synchronized(this) {
// if connection is authenticated the roster is already set by login()
// or a previous call to getRoster()
if (!isAuthenticated() || isAnonymous()) {
if (roster == null) {
roster = new Roster(this);
}
return roster;
}
}
if (!config.isRosterLoadedAtLogin()) {
roster.reload();
}