conferenceutils.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 650 行 · 第 1/2 页

SVN-BASE
650
字号
     */
    public static final boolean isPasswordRequired(String roomJID) {
        // Check to see if the room is password protected
        ServiceDiscoveryManager discover = new ServiceDiscoveryManager(SparkManager.getConnection());


        try {
            DiscoverInfo info = discover.discoverInfo(roomJID);
            return info.containsFeature("muc_passwordprotected");
        }
        catch (XMPPException e) {
            Log.error(e);
        }
        return false;
    }

    /**
     * Creates a private conference.
     *
     * @param serviceName the service name to use for the private conference.
     * @param message     the message sent to each individual invitee.
     * @param roomName    the name of the room to create.
     * @param jids        a collection of the user JIDs to invite.
     * @throws XMPPException thrown if an error occurs during room creation.
     */
    public static void createPrivateConference(String serviceName, String message, String roomName, Collection<String> jids) throws XMPPException {
        final String roomJID = StringUtils.escapeNode(roomName) + "@" + serviceName;
        final MultiUserChat multiUserChat = new MultiUserChat(SparkManager.getConnection(), roomJID);
        final LocalPreferences pref = SettingsManager.getLocalPreferences();


        final GroupChatRoom room = new GroupChatRoom(multiUserChat);
        try {
            // Attempt to create room.
            multiUserChat.create(pref.getNickname());
        }
        catch (XMPPException e) {
            throw e;
        }

        try {
            // Since this is a private room, make the room not public and set user as owner of the room.
            Form submitForm = multiUserChat.getConfigurationForm().createAnswerForm();
            submitForm.setAnswer("muc#roomconfig_publicroom", false);
            submitForm.setAnswer("muc#roomconfig_roomname", roomName);

            final List<String> owners = new ArrayList<String>();
            owners.add(SparkManager.getSessionManager().getBareAddress());
            submitForm.setAnswer("muc#roomconfig_roomowners", owners);

            multiUserChat.sendConfigurationForm(submitForm);
        }
        catch (XMPPException e1) {
            Log.error("Unable to send conference room chat configuration form.", e1);
        }


        ChatManager chatManager = SparkManager.getChatManager();

        // Check if room already is open
        try {
            chatManager.getChatContainer().getChatRoom(room.getRoomname());
        }
        catch (ChatRoomNotFoundException e) {
            chatManager.getChatContainer().addChatRoom(room);
            chatManager.getChatContainer().activateChatRoom(room);
        }

        for (String jid : jids) {
            multiUserChat.invite(jid, message);
            room.getTranscriptWindow().insertNotificationMessage("Waiting for " + jid + " to join.", ChatManager.NOTIFICATION_COLOR);
        }
    }

    /**
     * Returns an explanation for the exception.
     *
     * @param ex the <code>XMPPException</code>
     * @return the reason for the exception.
     */
    public static String getReason(XMPPException ex) {
        String reason = "";
        int code = 0;
        if (ex.getXMPPError() != null) {
            code = ex.getXMPPError().getCode();
        }

        if (code == 0) {
            reason = "No response from server.";
        }
        else if (code == 401) {
            reason = "The password did not match the room's password.";
        }
        else if (code == 403) {
            reason = "You have been banned from this room.";
        }
        else if (code == 404) {
            reason = "The room you are trying to enter does not exist.";
        }
        else if (code == 405) {
            reason = "You do not have permission to create a room.";
        }
        else if (code == 407) {
            reason = "You are not a member of this room.\nThis room requires you to be a member to join.";
        }

        return reason;
    }

    /**
     * Enters a GroupChatRoom on the event thread.
     *
     * @param roomName the name of the room.
     * @param roomJID  the rooms jid.
     * @param password the rooms password (if any).
     * @return the GroupChatRoom created.
     */
    public static GroupChatRoom enterRoomOnSameThread(final String roomName, String roomJID, String password) {
        ChatManager chatManager = SparkManager.getChatManager();


        final LocalPreferences pref = SettingsManager.getLocalPreferences();

        final String nickname = pref.getNickname().trim();


        try {
            GroupChatRoom chatRoom = (GroupChatRoom)chatManager.getChatContainer().getChatRoom(roomJID);
            MultiUserChat muc = chatRoom.getMultiUserChat();
            if (!muc.isJoined()) {
                joinRoom(muc, nickname, password);
            }
            chatManager.getChatContainer().activateChatRoom(chatRoom);
            return chatRoom;
        }
        catch (ChatRoomNotFoundException e) {
        }

        final MultiUserChat groupChat = new MultiUserChat(SparkManager.getConnection(), roomJID);


        final GroupChatRoom room = new GroupChatRoom(groupChat);
        room.setTabTitle(roomName);


        if (isPasswordRequired(roomJID) && password == null) {
            password = JOptionPane.showInputDialog(null, "Enter Room Password", "Need Password", JOptionPane.QUESTION_MESSAGE);
            if (!ModelUtil.hasLength(password)) {
                return null;
            }
        }


        final List errors = new ArrayList();
        final String userPassword = password;


        if (!groupChat.isJoined()) {
            int groupChatCounter = 0;
            while (true) {
                groupChatCounter++;
                String joinName = nickname;
                if (groupChatCounter > 1) {
                    joinName = joinName + groupChatCounter;
                }
                if (groupChatCounter < 10) {
                    try {
                        if (ModelUtil.hasLength(userPassword)) {
                            groupChat.join(joinName, userPassword);
                        }
                        else {
                            groupChat.join(joinName);
                        }
                        break;
                    }
                    catch (XMPPException ex) {
                        int code = 0;
                        if (ex.getXMPPError() != null) {
                            code = ex.getXMPPError().getCode();
                        }

                        if (code == 0) {
                            errors.add("No response from server.");
                        }
                        else if (code == 401) {
                            errors.add("The password did not match the room's password.");
                        }
                        else if (code == 403) {
                            errors.add("You have been banned from this room.");
                        }
                        else if (code == 404) {
                            errors.add("The room you are trying to enter does not exist.");
                        }
                        else if (code == 407) {
                            errors.add("You are not a member of this room.\nThis room requires you to be a member to join.");
                        }
                        else if (code != 409) {
                            break;
                        }
                    }
                }
                else {
                    break;
                }
            }

        }


        if (errors.size() > 0) {
            String error = (String)errors.get(0);
            JOptionPane.showMessageDialog(SparkManager.getMainWindow(), error, "Unable to join the room at this time.", JOptionPane.ERROR_MESSAGE);
            return null;
        }
        else if (groupChat.isJoined()) {
            chatManager.getChatContainer().addChatRoom(room);
            chatManager.getChatContainer().activateChatRoom(room);
        }
        else {
            JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "Unable to join the room.", "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
        return room;
    }

    public static void enterRoom(final MultiUserChat groupChat, String tabTitle, final String nickname, final String password) {
        final GroupChatRoom room = new GroupChatRoom(groupChat);
        room.setTabTitle(tabTitle);
        if (room == null) {
            return;
        }


        final List errors = new ArrayList();


        if (!groupChat.isJoined()) {
            int groupChatCounter = 0;
            while (true) {
                groupChatCounter++;
                String joinName = nickname;
                if (groupChatCounter > 1) {
                    joinName = joinName + groupChatCounter;
                }
                if (groupChatCounter < 10) {
                    try {
                        if (ModelUtil.hasLength(password)) {
                            groupChat.join(joinName, password);
                        }
                        else {
                            groupChat.join(joinName);
                        }
                        break;
                    }
                    catch (XMPPException ex) {
                        int code = 0;
                        if (ex.getXMPPError() != null) {
                            code = ex.getXMPPError().getCode();
                        }

                        if (code == 0) {
                            errors.add("No response from server.");
                        }
                        else if (code == 401) {
                            errors.add("A Password is required to enter this room.");
                        }
                        else if (code == 403) {
                            errors.add("You have been banned from this room.");
                        }
                        else if (code == 404) {
                            errors.add("The room you are trying to enter does not exist.");
                        }
                        else if (code == 407) {
                            errors.add("You are not a member of this room.\nThis room requires you to be a member to join.");
                        }
                        else if (code != 409) {
                            break;
                        }
                    }
                }
                else {
                    break;
                }
            }
        }

        if (errors.size() > 0) {
            String error = (String)errors.get(0);
            JOptionPane.showMessageDialog(SparkManager.getMainWindow(), error, "Could Not Join Room", JOptionPane.ERROR_MESSAGE);
            return;
        }
        else if (groupChat.isJoined()) {
            ChatManager chatManager = SparkManager.getChatManager();
            chatManager.getChatContainer().addChatRoom(room);
            chatManager.getChatContainer().activateChatRoom(room);
        }
        else {
            JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "Unable to join room.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }

    }
    
    final static List<String> unclosableChatRooms = new ArrayList<String>();
	public synchronized static void addUnclosableChatRoom(String jid) {
		unclosableChatRooms.add(jid);
	}
	
	public static boolean isChatRoomClosable(Component c) {
		if(c instanceof GroupChatRoom ) {
			GroupChatRoom groupChatRoom = (GroupChatRoom) c;
    		String roomName = groupChatRoom.getChatRoom().getRoomname();
    		                        		
    		if(unclosableChatRooms.contains(roomName)){
    			return false;
    		}			
		}
		return true;
		
	}


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?