/**
   * Searches for chat room wrapper in chat room list by chat room.
   *
   * @param chatRoom the chat room.
   * @param create if <tt>true</tt> and the chat room wrapper is not found new chatRoomWrapper is
   *     created.
   * @return found chat room wrapper or the created chat room wrapper.
   */
  @Override
  public ChatRoomWrapper getChatRoomWrapperByChatRoom(ChatRoom chatRoom, boolean create) {
    ChatRoomWrapper chatRoomWrapper = chatRoomList.findChatRoomWrapperFromChatRoom(chatRoom);

    if ((chatRoomWrapper == null) && create) {
      ChatRoomProviderWrapper parentProvider =
          chatRoomList.findServerWrapperFromProvider(chatRoom.getParentProvider());

      chatRoomWrapper = new ChatRoomWrapperImpl(parentProvider, chatRoom);

      chatRoomList.addChatRoom(chatRoomWrapper);
    }
    return chatRoomWrapper;
  }
 /**
  * Destroys the given <tt>ChatRoom</tt> from the list of all chat rooms.
  *
  * @param chatRoomWrapper the <tt>ChatRoomWrapper</tt> to be destroyed.
  * @param reason the reason for destroying.
  * @param alternateAddress the alternate address.
  */
 public void destroyChatRoom(
     ChatRoomWrapper chatRoomWrapper, String reason, String alternateAddress) {
   if (chatRoomWrapper.getChatRoom().destroy(reason, alternateAddress)) {
     MUCActivator.getUIService().closeChatRoomWindow(chatRoomWrapper);
     chatRoomList.removeChatRoom(chatRoomWrapper);
   } else {
     // if we leave a chat room which is not persistent
     // the room can be destroyed on the server, and error is returned
     // when we try to destroy it not-authorized(401)
     if (!chatRoomWrapper.getChatRoom().isPersistent()
         && !chatRoomWrapper.getChatRoom().isJoined()) {
       chatRoomList.removeChatRoom(chatRoomWrapper);
     }
   }
 }
  /**
   * Leaves the given chat room.
   *
   * @param chatRoomWrapper the chat room to leave.
   * @return <tt>ChatRoomWrapper</tt> instance associated with the chat room.
   */
  public ChatRoomWrapper leaveChatRoom(ChatRoomWrapper chatRoomWrapper) {
    ChatRoom chatRoom = chatRoomWrapper.getChatRoom();

    if (chatRoom == null) {
      ResourceManagementService resources = MUCActivator.getResources();

      MUCActivator.getAlertUIService()
          .showAlertDialog(
              resources.getI18NString("service.gui.WARNING"),
              resources.getI18NString("service.gui.CHAT_ROOM_LEAVE_NOT_CONNECTED"));

      return null;
    }

    if (chatRoom.isJoined()) chatRoom.leave();

    ChatRoomWrapper existChatRoomWrapper = chatRoomList.findChatRoomWrapperFromChatRoom(chatRoom);

    if (existChatRoomWrapper == null) return null;

    // We save the choice of the user, before the chat room is really
    // joined, because even the join fails we want the next time when
    // we login to join this chat room automatically.
    ConfigurationUtils.updateChatRoomStatus(
        chatRoomWrapper.getParentProvider().getProtocolProvider(),
        chatRoomWrapper.getChatRoomID(),
        GlobalStatusEnum.OFFLINE_STATUS);

    return existChatRoomWrapper;
  }
  /**
   * Joins the given chat room and manages all the exceptions that could occur during the join
   * process.
   *
   * @param chatRoom the chat room to join
   * @param nickname the nickname we're using to join
   * @param password the password we're using to join
   */
  public void joinChatRoom(ChatRoom chatRoom, String nickname, byte[] password) {
    ChatRoomWrapper chatRoomWrapper = chatRoomList.findChatRoomWrapperFromChatRoom(chatRoom);

    if (chatRoomWrapper == null) {
      ChatRoomProviderWrapper parentProvider =
          chatRoomList.findServerWrapperFromProvider(chatRoom.getParentProvider());

      chatRoomWrapper = new ChatRoomWrapperImpl(parentProvider, chatRoom);

      chatRoomList.addChatRoom(chatRoomWrapper);

      fireChatRoomListChangedEvent(chatRoomWrapper, ChatRoomListChangeEvent.CHAT_ROOM_ADDED);
    }

    this.joinChatRoom(chatRoomWrapper, nickname, password);
  }
  /**
   * Goes through the locally stored chat rooms list and for each {@link ChatRoomWrapper} tries to
   * find the corresponding server stored {@link ChatRoom} in the specified operation set. Joins
   * automatically all found chat rooms.
   *
   * @param protocolProvider the protocol provider for the account to synchronize
   * @param opSet the multi user chat operation set, which give us access to chat room server
   */
  public void synchronizeOpSetWithLocalContactList(
      ProtocolProviderService protocolProvider, final OperationSetMultiUserChat opSet) {
    ChatRoomProviderWrapper chatRoomProvider = findServerWrapperFromProvider(protocolProvider);

    if (chatRoomProvider == null) {
      chatRoomProvider = chatRoomList.addRegisteredChatProvider(protocolProvider);
    }

    if (chatRoomProvider != null) {
      chatRoomProvider.synchronizeProvider();
    }
  }
  /**
   * Joins the room with the given name though the given chat room provider.
   *
   * @param chatRoomName the name of the room to join.
   * @param chatRoomProvider the chat room provider to join through.
   */
  public void joinChatRoom(String chatRoomName, ChatRoomProviderWrapper chatRoomProvider) {
    OperationSetMultiUserChat groupChatOpSet =
        chatRoomProvider.getProtocolProvider().getOperationSet(OperationSetMultiUserChat.class);

    ChatRoom chatRoom = null;
    try {
      chatRoom = groupChatOpSet.findRoom(chatRoomName);
    } catch (Exception e) {
      if (logger.isTraceEnabled())
        logger.trace("Un exception occurred while searching for room:" + chatRoomName, e);
    }

    if (chatRoom != null) {
      ChatRoomWrapper chatRoomWrapper = chatRoomList.findChatRoomWrapperFromChatRoom(chatRoom);

      if (chatRoomWrapper == null) {
        ChatRoomProviderWrapper parentProvider =
            chatRoomList.findServerWrapperFromProvider(chatRoom.getParentProvider());

        chatRoomWrapper = new ChatRoomWrapperImpl(parentProvider, chatRoom);

        chatRoomList.addChatRoom(chatRoomWrapper);

        fireChatRoomListChangedEvent(chatRoomWrapper, ChatRoomListChangeEvent.CHAT_ROOM_ADDED);
      }
      joinChatRoom(chatRoomWrapper);
    } else
      MUCActivator.getAlertUIService()
          .showAlertDialog(
              MUCActivator.getResources().getI18NString("service.gui.ERROR"),
              MUCActivator.getResources()
                  .getI18NString(
                      "service.gui.CHAT_ROOM_NOT_EXIST",
                      new String[] {
                        chatRoomName,
                        chatRoomProvider.getProtocolProvider().getAccountID().getService()
                      }));
  }
 /**
  * Returns the <tt>ChatRoomWrapper</tt> that correspond to the given <tt>ChatRoom</tt>. If the
  * list of chat rooms doesn't contain a corresponding wrapper - returns null.
  *
  * @param chatRoom the <tt>ChatRoom</tt> that we're looking for
  * @return the <tt>ChatRoomWrapper</tt> object corresponding to the given <tt>ChatRoom</tt>
  */
 public ChatRoomWrapper findChatRoomWrapperFromChatRoom(ChatRoom chatRoom) {
   return chatRoomList.findChatRoomWrapperFromChatRoom(chatRoom);
 }
 /**
  * Returns the <tt>ChatRoomProviderWrapper</tt> that correspond to the given
  * <tt>ProtocolProviderService</tt>. If the list doesn't contain a corresponding wrapper - returns
  * null.
  *
  * @param protocolProvider the protocol provider that we're looking for
  * @return the <tt>ChatRoomProvider</tt> object corresponding to the given
  *     <tt>ProtocolProviderService</tt>
  */
 public ChatRoomProviderWrapper findServerWrapperFromProvider(
     ProtocolProviderService protocolProvider) {
   return chatRoomList.findServerWrapperFromProvider(protocolProvider);
 }
 /**
  * Removes the ChatRoomProviderWrapperListener to the listener list.
  *
  * @param listener the ChatRoomProviderWrapperListener to be removed
  */
 public void removeChatRoomProviderWrapperListener(ChatRoomProviderWrapperListener listener) {
   chatRoomList.removeChatRoomProviderWrapperListener(listener);
 }
 /**
  * Adds a ChatRoomProviderWrapperListener to the listener list.
  *
  * @param listener the ChatRoomProviderWrapperListener to be added
  */
 public void addChatRoomProviderWrapperListener(ChatRoomProviderWrapperListener listener) {
   chatRoomList.addChatRoomProviderWrapperListener(listener);
 }
  /**
   * Creates a chat room, by specifying the chat room name, the parent protocol provider and
   * eventually, the contacts invited to participate in this chat room.
   *
   * @param roomName the name of the room
   * @param protocolProvider the parent protocol provider.
   * @param contacts the contacts invited when creating the chat room.
   * @param reason
   * @param join whether we should join the room after creating it.
   * @param persistent whether the newly created room will be persistent.
   * @param isPrivate whether the room will be private or public.
   * @return the <tt>ChatRoomWrapper</tt> corresponding to the created room
   */
  public ChatRoomWrapper createChatRoom(
      String roomName,
      ProtocolProviderService protocolProvider,
      Collection<String> contacts,
      String reason,
      boolean join,
      boolean persistent,
      boolean isPrivate) {
    ChatRoomWrapper chatRoomWrapper = null;
    OperationSetMultiUserChat groupChatOpSet =
        protocolProvider.getOperationSet(OperationSetMultiUserChat.class);

    // If there's no group chat operation set we have nothing to do here.
    if (groupChatOpSet == null) return null;

    ChatRoom chatRoom = null;
    try {

      HashMap<String, Object> roomProperties = new HashMap<String, Object>();
      roomProperties.put("isPrivate", isPrivate);
      chatRoom = groupChatOpSet.createChatRoom(roomName, roomProperties);

      if (join) {
        chatRoom.join();

        for (String contact : contacts) chatRoom.invite(contact, reason);
      }
    } catch (OperationFailedException ex) {
      logger.error("Failed to create chat room.", ex);

      MUCActivator.getAlertUIService()
          .showAlertDialog(
              MUCActivator.getResources().getI18NString("service.gui.ERROR"),
              MUCActivator.getResources()
                  .getI18NString(
                      "service.gui.CREATE_CHAT_ROOM_ERROR",
                      new String[] {protocolProvider.getProtocolDisplayName()}),
              ex);
    } catch (OperationNotSupportedException ex) {
      logger.error("Failed to create chat room.", ex);

      MUCActivator.getAlertUIService()
          .showAlertDialog(
              MUCActivator.getResources().getI18NString("service.gui.ERROR"),
              MUCActivator.getResources()
                  .getI18NString(
                      "service.gui.CREATE_CHAT_ROOM_ERROR",
                      new String[] {protocolProvider.getProtocolDisplayName()}),
              ex);
    }

    if (chatRoom != null) {
      ChatRoomProviderWrapper parentProvider =
          chatRoomList.findServerWrapperFromProvider(protocolProvider);

      // if there is the same room ids don't add new wrapper as old one
      // maybe already created
      chatRoomWrapper = chatRoomList.findChatRoomWrapperFromChatRoom(chatRoom);

      if (chatRoomWrapper == null) {
        chatRoomWrapper = new ChatRoomWrapperImpl(parentProvider, chatRoom);
        chatRoomWrapper.setPersistent(persistent);
        chatRoomList.addChatRoom(chatRoomWrapper);
      }
    }

    return chatRoomWrapper;
  }
 /**
  * Fires a <tt>ChatRoomListChangedEvent</tt> event.
  *
  * @param chatRoomWrapper the chat room.
  * @param eventID the id of the event.
  */
 public void fireChatRoomListChangedEvent(ChatRoomWrapper chatRoomWrapper, int eventID) {
   chatRoomList.fireChatRoomListChangedEvent(chatRoomWrapper, eventID);
 }
 /**
  * Removes the given <tt>ChatRoom</tt> from the list of all chat rooms.
  *
  * @param chatRoomWrapper the <tt>ChatRoomWrapper</tt> to remove
  */
 public void removeChatRoom(ChatRoomWrapper chatRoomWrapper) {
   chatRoomList.removeChatRoom(chatRoomWrapper);
 }
 /**
  * Returns an iterator to the list of chat room providers.
  *
  * @return an iterator to the list of chat room providers.
  */
 public Iterator<ChatRoomProviderWrapper> getChatRoomProviders() {
   return chatRoomList.getChatRoomProviders();
 }
 /**
  * Adds a change listener to the <tt>ChatRoomList</tt>.
  *
  * @param l the listener.
  */
 public void addChatRoomListChangeListener(ChatRoomListChangeListener l) {
   chatRoomList.addChatRoomListChangeListener(l);
 }
 /**
  * Finds the <tt>ChatRoomWrapper</tt> instance associated with the chat room.
  *
  * @param chatRoomID the id of the chat room.
  * @param pps the provider of the chat room.
  * @return the <tt>ChatRoomWrapper</tt> instance.
  */
 public ChatRoomWrapper findChatRoomWrapperFromChatRoomID(
     String chatRoomID, ProtocolProviderService pps) {
   return chatRoomList.findChatRoomWrapperFromChatRoomID(chatRoomID, pps);
 }
 /**
  * Finds the <tt>ChatRoomWrapper</tt> instance associated with the source contact.
  *
  * @param contact the source contact.
  * @return the <tt>ChatRoomWrapper</tt> instance.
  */
 public ChatRoomWrapper findChatRoomWrapperFromSourceContact(SourceContact contact) {
   if (!(contact instanceof ChatRoomSourceContact)) return null;
   ChatRoomSourceContact chatRoomContact = (ChatRoomSourceContact) contact;
   return chatRoomList.findChatRoomWrapperFromChatRoomID(
       chatRoomContact.getChatRoomID(), chatRoomContact.getProvider());
 }
 /**
  * Removes a change listener to the <tt>ChatRoomList</tt>.
  *
  * @param l the listener.
  */
 public void removeChatRoomListChangeListener(ChatRoomListChangeListener l) {
   chatRoomList.removeChatRoomListChangeListener(l);
 }