[sip-comm-cvs] CVS update: /sip-communicator/src/net/java/sip/communicator/impl/protocol/jabber/ChatRoomJabberImpl.java

emcho at dev.java.net emcho at dev.java.net
Mon Apr 23 20:12:16 CEST 2007


User: emcho   
Date: 2007-04-23 18:12:16+0000
Log:
 working on multi chat and a jabber implementation
 
 implemented most of the methods in here. still need to fix the presence though

File Changes:

Directory: /sip-communicator/src/net/java/sip/communicator/impl/protocol/jabber/
================================================================================

File [changed]: ChatRoomJabberImpl.java
Url: https://sip-communicator.dev.java.net/source/browse/sip-communicator/src/net/java/sip/communicator/impl/protocol/jabber/ChatRoomJabberImpl.java?r1=1.1&r2=1.2
Delta lines:  +393 -19
----------------------
--- ChatRoomJabberImpl.java	2007-03-10 14:32:59+0000	1.1
+++ ChatRoomJabberImpl.java	2007-04-23 18:12:14+0000	1.2
@@ -8,11 +8,11 @@
 
 import java.util.*;
 
+import org.jivesoftware.smack.*;
+import org.jivesoftware.smackx.muc.*;
 import net.java.sip.communicator.service.protocol.*;
 import net.java.sip.communicator.service.protocol.event.*;
 import net.java.sip.communicator.util.*;
-import org.jivesoftware.smackx.muc.*;
-import org.jivesoftware.smack.*;
 
 /**
  * Implements chat rooms for jabber. The class encapsulates instances of the
@@ -49,14 +49,49 @@
     private Vector participantStatusListeners = new Vector();
 
     /**
+     * The protocol provider that created us
+     */
+    private ProtocolProviderServiceJabberImpl provider = null;
+
+    /**
+     * The operation set that crated us.
+     */
+    OperationSetMultiUserChatJabberImpl opSetMuc = null;
+
+    /**
      * Listeners that will be notified every time
      * a new message is received on this chat room.
      */
     private Vector messageListeners = new Vector();
 
-    public ChatRoomJabberImpl(MultiUserChat multiUserChat)
+    /**
+     * The members of this chat room.
+     */
+    private Hashtable members = new Hashtable();
+
+    /**
+     * Creates an instance of a chat room that has been.
+     *
+     * @param multiUserChat MultiUserChat
+     * @param provider a reference to the currently valid jabber protocol
+     * provider.
+     */
+    public ChatRoomJabberImpl(MultiUserChat multiUserChat,
+                              ProtocolProviderServiceJabberImpl provider)
     {
         this.multiUserChat = multiUserChat;
+        this.provider = provider;
+        this.opSetMuc = (OperationSetMultiUserChatJabberImpl)provider
+            .getOperationSet(OperationSetMultiUserChat.class);
+
+        /** @todo add listeners to the chat room */
+        /** @todo multiUserChat.addMessageListener(); */
+        /** @todo multiUserChat.addParticipantListener();*/
+        /** @todo multiUserChat.addParticipantStatusListener(); */
+        /** @todo multiUserChat.addPresenceInterceptor(); */
+        /** @todo multiUserChat.addSubjectUpdatedListener(); */
+        /** @todo multiUserChat.addUserStatusListener(); */
+        //multiUserChat.addParticipantStatusListener(this);
     }
 
     /**
@@ -162,7 +197,7 @@
      * @param listener a participant status listener.
      */
     public void addParticipantStatusListener(
-                    ChatRoomParticipantStatusListener listener)
+                    ChatRoomMemberListener listener)
     {
         synchronized(participantStatusListeners)
         {
@@ -179,7 +214,7 @@
      * @param listener a participant status listener.
      */
     public void removeParticipantStatusListener(
-        ChatRoomParticipantStatusListener listener)
+        ChatRoomMemberListener listener)
     {
         synchronized(participantStatusListeners)
         {
@@ -201,7 +236,11 @@
     public Message createMessage(byte[] content, String contentType,
                                  String contentEncoding, String subject)
     {
-        return null;
+        return new MessageJabberImpl(
+                new String(content)
+                , contentType
+                , contentEncoding
+                , subject);
     }
 
 
@@ -214,19 +253,25 @@
      */
     public Message createMessage(String messageText)
     {
-        return null;
+        Message msg
+            = new MessageJabberImpl(
+                messageText
+                , OperationSetBasicInstantMessaging.DEFAULT_MIME_TYPE
+                , OperationSetBasicInstantMessaging.DEFAULT_MIME_ENCODING
+                , null);
+        return msg;
     }
 
     /**
-     * Returns a <tt>List</tt> of <tt>Contact</tt>s corresponding to all
+     * Returns a <tt>List</tt> of <tt>Member</tt>s corresponding to all
      * members currently participating in this room.
      *
-     * @return a <tt>List</tt> of <tt>Contact</tt> corresponding to all room
+     * @return a <tt>List</tt> of <tt>Member</tt> corresponding to all room
      *   members.
      */
     public List getMembers()
     {
-        return null;
+        return new LinkedList(members.entrySet());
     }
 
     /**
@@ -238,7 +283,7 @@
      */
     public int getMembersCount()
     {
-        return 0;
+        return multiUserChat.getOccupantsCount();
     }
 
     /**
@@ -249,7 +294,7 @@
      */
     public String getName()
     {
-        return "";
+        return multiUserChat.getRoom();
     }
 
     /**
@@ -259,9 +304,9 @@
      * @return the nickname currently being used by the local user in the
      *   context of the local chat room.
      */
-    public String getNickname()
+    public String getUserNickname()
     {
-        return "";
+        return this.multiUserChat.getNickname();
     }
 
     /**
@@ -273,7 +318,7 @@
      */
     public String getSubject()
     {
-        return "";
+        return this.multiUserChat.getNickname();
     }
 
     /**
@@ -286,6 +331,7 @@
      */
     public void invite(String userAddress, String reason)
     {
+        multiUserChat.invite(userAddress, reason);
     }
 
     /**
@@ -297,7 +343,7 @@
      */
     public boolean isJoined()
     {
-        return false;
+        return multiUserChat.isJoined();
     }
 
     /**
@@ -312,6 +358,7 @@
     public void join(byte[] password)
         throws OperationFailedException
     {
+        joinAs(provider.getAccountID().getUserID(), password);
     }
 
     /**
@@ -324,6 +371,7 @@
     public void join()
         throws OperationFailedException
     {
+        joinAs(provider.getAccountID().getUserID());
     }
 
     /**
@@ -339,6 +387,20 @@
     public void joinAs(String nickname, byte[] password)
         throws OperationFailedException
     {
+        try
+        {
+            multiUserChat.join(nickname, new String(password));
+
+            initMembers();
+        }
+        catch (XMPPException ex)
+        {
+            logger.error("Failed to join chat room "
+                          + getName()
+                          + " with nickname "
+                          + nickname
+                          , ex );
+        }
     }
 
     /**
@@ -352,6 +414,79 @@
     public void joinAs(String nickname)
         throws OperationFailedException
     {
+        try
+        {
+            multiUserChat.join(nickname);
+
+            initMembers();
+        }
+        catch (XMPPException ex)
+        {
+            logger.error("Failed to join room "
+                         + getName()
+                         + " with nickname: "
+                         + nickname
+                         , ex);
+            throw new OperationFailedException(
+                "Failed to join room "
+                         + getName()
+                         + " with nickname: "
+                         + nickname
+                         , OperationFailedException.GENERAL_ERROR
+                         , ex);
+        }
+    }
+
+    /**
+     * Initialises the list of chat room members.
+     *
+     * @throws XMPPException if we fail retrieving the members.
+     */
+    private void initMembers()
+        throws XMPPException
+    {
+        Iterator occupantsIter = multiUserChat.getOccupants();
+
+        while (occupantsIter.hasNext())
+        {
+            String occupantStr = (String) occupantsIter.next();
+
+            Occupant occupant = multiUserChat.getOccupant(occupantStr);
+
+            ChatRoomMemberRole role = smackRoleToScRole(occupant.getRole());
+
+            //smack returns fully qualified occupant names.
+            ChatRoomMemberJabberImpl member = new ChatRoomMemberJabberImpl(
+                  this
+                , occupant.getNick()
+                , occupant.getJid()
+                , role);
+            this.members.put(member.getContactAddress(), member);
+        }
+    }
+
+    /**
+     * Returns that <tt>ChatRoomJabberRole</tt> instance corresponding to the
+     * <tt>smackRole</tt> string.
+     *
+     * @param smackRole the smack role as returned by
+     * <tt>Occupant.getRole()</tt>.
+     * @return ChatRoomMemberRole
+     */
+    private ChatRoomMemberRole smackRoleToScRole(String smackRole)
+    {
+        if (smackRole.equalsIgnoreCase("moderator"))
+        {
+            return ChatRoomMemberRole.MODERATOR;
+        }
+        else if (smackRole.equalsIgnoreCase("participant"))
+        {
+            return ChatRoomMemberRole.MEMBER;
+        }
+        else
+        {
+            return ChatRoomMemberRole.GUEST;
+        }
     }
 
     /**
@@ -360,6 +495,7 @@
      */
     public void leave()
     {
+        multiUserChat.leave();
     }
 
     /**
@@ -367,12 +503,24 @@
      * <tt>to</tt> contact.
      *
      * @param message the <tt>Message</tt> to send.
-     * @throws IllegalStateException if the underlying stack is not
-     *   registered or initialized or if the chat room is not joined.
+     * @throws OperationFailedException if sending the message fails for some
+     * reason.
      */
     public void sendMessage(Message message)
-        throws IllegalStateException
+        throws OperationFailedException
+    {
+        try
+        {
+            multiUserChat.sendMessage(message.getContent());
+        }
+        catch (XMPPException ex)
     {
+            logger.error("Failed to send message " + message, ex);
+            throw new OperationFailedException(
+                "Failed to send message " + message
+                , OperationFailedException.GENERAL_ERROR
+                , ex);
+        }
     }
 
     /**
@@ -424,4 +572,230 @@
         }
 
     }
+
+    /**
+     * Returns a reference to the provider that created this room.
+     *
+     * @return a reference to the <tt>ProtocolProviderService</tt> instance
+     * that created this room.
+     */
+    public ProtocolProviderService getParentProvider()
+    {
+        return provider;
+    }
+
+    /**
+     * Instances of this class should be registered as
+     * <tt>ParticipantStatusListener</tt> in smack and translates events .
+     */
+    private class MemberListener implements ParticipantStatusListener
+    {
+        /**
+         * Called when an administrator or owner banned a participant from the
+         * room. This means that banned participant will no longer be able to
+         * join the room unless the ban has been removed.
+         *
+         * @param participant the participant that was banned from the room
+         * (e.g. room at conference.jabber.org/nick).
+         * @param actor the administrator that banned the occupant (e.g.
+         * user at host.org).
+         * @param reason the reason provided by the administrator to ban the
+         * occupant.
+         */
+        public void banned(String participant, String actor, String reason)
+        {
+            /** @todo implement banned() */
+        }
+
+        /**
+         * Called when an owner grants administrator privileges to a user. This
+         * means that the user will be able to perform administrative functions
+         * such as banning users and edit moderator list.
+         *
+         * @param participant the participant that was granted administrator
+         * privileges (e.g. room at conference.jabber.org/nick).
+         */
+        public void adminGranted(String participant)
+        {
+            /** @todo implement banned() */
+        }
+
+        /**
+         * Called when an owner revokes administrator privileges from a user.
+         * This means that the user will no longer be able to perform
+         * administrative functions such as banning users and edit moderator
+         * list.
+         *
+         * @param participant the participant that was revoked administrator
+         * privileges (e.g. room at conference.jabber.org/nick).
+         */
+        public void adminRevoked(String participant)
+        {
+            /** @todo implement banned() */
+        }
+
+        /**
+         * Called when a new room occupant has joined the room. Note: Take in
+         * consideration that when you join a room you will receive the list of
+         * current occupants in the room. This message will be sent for each
+         * occupant.
+         *
+         * @param participant the participant that has just joined the room
+         * (e.g. room at conference.jabber.org/nick).
+         */
+        public void joined(String participant)
+        {
+            /** @todo implement joined() */
+        }
+
+        /**
+         * Called when a room occupant has left the room on its own. This means
+         * that the occupant was neither kicked nor banned from the room.
+         *
+         * @param participant the participant that has left the room on its own.
+         * (e.g. room at conference.jabber.org/nick).
+         */
+        public void left(String participant)
+        {
+            /** @todo implement left() */
+        }
+
+        /**
+         * Called when a participant changed his/her nickname in the room. The
+         * new participant's nickname will be informed with the next available
+         * presence.
+         *
+         * @param participant the participant that was revoked administrator
+         * privileges (e.g. room at conference.jabber.org/nick).
+         * @param newNickname the new nickname that the participant decided to
+         * use.
+         */
+        public void nicknameChanged(String participant, String newNickname)
+        {
+            /** @todo implement nicknameChanged() */
+        }
+
+        /**
+         * Called when an owner revokes a user ownership on the room. This
+         * means that the user will no longer be able to change defining room
+         * features as well as perform all administrative functions.
+         *
+         * @param participant the participant that was revoked ownership on the
+         * room (e.g. room at conference.jabber.org/nick).
+         */
+        public void ownershipRevoked(String participant)
+        {
+            /** @todo implement ownershipRevoked() */
+        }
+
+        /**
+         * Called when a room participant has been kicked from the room. This
+         * means that the kicked participant is no longer participating in the
+         * room.
+         *
+         * @param participant the participant that was kicked from the room
+         * (e.g. room at conference.jabber.org/nick).
+         * @param actor the moderator that kicked the occupant from the room
+         * (e.g. user at host.org).
+         * @param reason the reason provided by the actor to kick the occupant
+         * from the room.
+         */
+        public void kicked(String participant, String actor, String reason)
+        {
+            /** @todo implement kicked() */
+        }
+
+        /**
+         * Called when an administrator grants moderator privileges to a user.
+         * This means that the user will be able to kick users, grant and
+         * revoke voice, invite other users, modify room's subject plus all the
+         * partcipants privileges.
+         *
+         * @param participant the participant that was granted moderator
+         * privileges in the room (e.g. room at conference.jabber.org/nick).
+         */
+        public void moderatorGranted(String participant)
+        {
+            /** @todo implement moderatorGranted() */
+        }
+
+        /**
+         * Called when a moderator revokes voice from a participant. This means
+         * that the participant in the room was able to speak and now is a
+         * visitor that can't send messages to the room occupants.
+         *
+         * @param participant the participant that was revoked voice from the
+         * room (e.g. room at conference.jabber.org/nick).
+         */
+        public void voiceRevoked(String participant)
+        {
+            /** @todo implement voiceRevoked() */
+        }
+
+        /**
+         * Called when an administrator grants a user membership to the room.
+         * This means that the user will be able to join the members-only room.
+         *
+         * @param participant the participant that was granted membership in
+         * the room (e.g. room at conference.jabber.org/nick).
+         */
+        public void membershipGranted(String participant)
+        {
+            /** @todo implement membershipGranted() */
+        }
+
+        /**
+         * Called when an administrator revokes moderator privileges from a
+         * user. This means that the user will no longer be able to kick users,
+         * grant and revoke voice, invite other users, modify room's subject
+         * plus all the partcipants privileges.
+         *
+         * @param participant the participant that was revoked moderator
+         * privileges in the room (e.g. room at conference.jabber.org/nick).
+         */
+        public void moderatorRevoked(String participant)
+        {
+            /** @todo implement moderatorRevoked() */
+        }
+
+        /**
+         * Called when a moderator grants voice to a visitor. This means that
+         * the visitor can now participate in the moderated room sending
+         * messages to all occupants.
+         *
+         * @param participant the participant that was granted voice in the room
+         * (e.g. room at conference.jabber.org/nick).
+         */
+        public void voiceGranted(String participant)
+        {
+            /** @todo implement voiceGranted() */
+        }
+
+        /**
+         * Called when an administrator revokes a user membership to the room.
+         * This means that the user will not be able to join the members-only
+         * room.
+         *
+         * @param participant the participant that was revoked membership from
+         * the room
+         * (e.g. room at conference.jabber.org/nick).
+         */
+        public void membershipRevoked(String participant)
+        {
+            /** @todo implement membershipRevoked() */
+        }
+
+        /**
+         * Called when an owner grants a user ownership on the room. This means
+         * that the user will be able to change defining room features as well
+         * as perform all administrative functions.
+         *
+         * @param participant the participant that was granted ownership on the
+         * room (e.g. room at conference.jabber.org/nick).
+         */
+        public void ownershipGranted(String participant)
+        {
+            /** @todo implement ownershipGranted() */
+        }
+    }
 }




---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe at sip-communicator.dev.java.net
For additional commands, e-mail: cvs-help at sip-communicator.dev.java.net





More information about the commits mailing list