workspace.java.svn-base

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

SVN-BASE
467
字号

        // Until we have better plugin management, will init after presence updates.
        gatewayPlugin = new GatewayPlugin();
        gatewayPlugin.initialize();

        // Load all non-presence related items.
        conferences.loadConferenceBookmarks();
        SearchManager.getInstance();
        transcriptPlugin = new ChatTranscriptPlugin();

        // Load Broadcast Plugin
        broadcastPlugin = new BroadcastPlugin();
        broadcastPlugin.initialize();

        // Load BookmarkPlugin
        bookmarkPlugin = new BookmarkPlugin();
        bookmarkPlugin.initialize();

        // Schedule loading of the plugins after two seconds.
        TaskEngine.getInstance().schedule(new TimerTask() {
            public void run() {
                final PluginManager pluginManager = PluginManager.getInstance();
                pluginManager.loadPlugins();
                pluginManager.initializePlugins();

                // Subscriptions are always manual
                Roster roster = SparkManager.getConnection().getRoster();
                roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
            }
        }, 2000);

        // Check URI Mappings
        SparkManager.getChatManager().handleURIMapping(Spark.ARGUMENTS);
    }


    /**
     * Returns the status box for the User.
     *
     * @return the status box for the user.
     */
    public StatusBar getStatusBar() {
        return statusBox;
    }

    /**
     * This is to handle agent to agent conversations.
     *
     * @param packet the smack packet to process.
     */
    public void processPacket(final Packet packet) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                handleIncomingPacket(packet);
            }
        });
    }


    private void handleIncomingPacket(Packet packet) {
        // We only handle message packets here.
        if (packet instanceof Message) {
            final Message message = (Message)packet;
            boolean isGroupChat = message.getType() == Message.Type.groupchat;

            // Check if Conference invite. If so, do not handle here.
            if (message.getExtension("x", "jabber:x:conference") != null) {
                return;
            }

            final String body = message.getBody();
            boolean broadcast = message.getProperty("broadcast") != null;

            // Handle offline message.
            DelayInformation offlineInformation = (DelayInformation)message.getExtension("x", "jabber:x:delay");
            if (offlineInformation != null && (Message.Type.chat == message.getType() ||
                Message.Type.normal == message.getType())) {
                handleOfflineMessage(message);
            }

            if (body == null ||
                isGroupChat ||
                broadcast ||
                message.getType() == Message.Type.normal ||
                message.getType() == Message.Type.headline ||
                message.getType() == Message.Type.error) {
                return;
            }

            // Create new chat room for Agent Invite.
            final String from = packet.getFrom();
            final String host = SparkManager.getSessionManager().getServerAddress();

            // Don't allow workgroup notifications to come through here.
            final String bareJID = StringUtils.parseBareAddress(from);
            if (host.equalsIgnoreCase(from) || from == null) {
                return;
            }


            ChatRoom room = null;
            try {
                room = SparkManager.getChatManager().getChatContainer().getChatRoom(bareJID);
            }
            catch (ChatRoomNotFoundException e) {
                // Ignore
            }

            // Check for non-existent rooms.
            if (room == null) {
                createOneToOneRoom(bareJID, message);
            }
        }
    }

    /**
     * Creates a new room if necessary and inserts an offline message.
     *
     * @param message The Offline message.
     */
    private void handleOfflineMessage(Message message) {
        if(!ModelUtil.hasLength(message.getBody())){
            return;
        }
        
        DelayInformation offlineInformation = (DelayInformation)message.getExtension("x", "jabber:x:delay");
        String bareJID = StringUtils.parseBareAddress(message.getFrom());
        ContactItem contact = contactList.getContactItemByJID(bareJID);
        String nickname = StringUtils.parseName(bareJID);
        if (contact != null) {
            nickname = contact.getNickname();
        }

        // Create the room if it does not exist.
        ChatRoom room = SparkManager.getChatManager().createChatRoom(bareJID, nickname, nickname);

        // Insert offline message
        room.getTranscriptWindow().insertMessage(nickname, message, ChatManager.FROM_COLOR);
        room.addToTranscript(message, true);

        // Send display and notified message back.
        SparkManager.getMessageEventManager().sendDeliveredNotification(message.getFrom(), message.getPacketID());
        SparkManager.getMessageEventManager().sendDisplayedNotification(message.getFrom(), message.getPacketID());
    }

    /**
     * Creates a new room based on an anonymous user.
     *
     * @param bareJID the bareJID of the anonymous user.
     * @param message the message from the anonymous user.
     */
    private void createOneToOneRoom(String bareJID, Message message) {
        ContactItem contact = contactList.getContactItemByJID(bareJID);
        String nickname = StringUtils.parseName(bareJID);
        if (contact != null) {
            nickname = contact.getNickname();
        }
        else {
            // Attempt to load VCard from users who we are not subscribed to.
            VCard vCard = SparkManager.getVCardManager().getVCard(bareJID);
            if (vCard != null && vCard.getError() == null) {
                String firstName = vCard.getFirstName();
                String lastName = vCard.getLastName();
                String userNickname = vCard.getNickName();
                if (ModelUtil.hasLength(userNickname)) {
                    nickname = userNickname;
                }
                else if (ModelUtil.hasLength(firstName) && ModelUtil.hasLength(lastName)) {
                    nickname = firstName + " " + lastName;
                }
                else if (ModelUtil.hasLength(firstName)) {
                    nickname = firstName;
                }
            }
        }

        SparkManager.getChatManager().createChatRoom(bareJID, nickname, nickname);
        try {
            insertMessage(bareJID, message);
        }
        catch (ChatRoomNotFoundException e) {
            Log.error("Could not find chat room.", e);
        }
    }


    private void insertMessage(final String bareJID, final Message message) throws ChatRoomNotFoundException {
        ChatRoom chatRoom = SparkManager.getChatManager().getChatContainer().getChatRoom(bareJID);
        chatRoom.insertMessage(message);
        int chatLength = chatRoom.getTranscriptWindow().getDocument().getLength();
        chatRoom.getTranscriptWindow().setCaretPosition(chatLength);
        chatRoom.getChatInputEditor().requestFocusInWindow();
    }


    /**
     * Returns the Workspace TabbedPane. If you wish to add your
     * component, simply use addTab( name, icon, component ) call.
     *
     * @return the workspace JideTabbedPane
     */
    public SparkTabbedPane getWorkspacePane() {
        return workspacePane;
    }


    /**
     * Returns the <code>ContactList</code> associated with this workspace.
     *
     * @return the ContactList associated with this workspace.
     */
    public ContactList getContactList() {
        return contactList;
    }

    public void changeCardLayout(String layout) {
        cardLayout.show(cardPanel, layout);
    }

    public JPanel getCardPanel() {
        return cardPanel;
    }

    /**
     * Returns the <code>CommandPanel</code> of this Workspace.
     *
     * @return the CommandPanel.
     */
    public CommandPanel getCommandPanel() {
        return commandPanel;
    }
}

⌨️ 快捷键说明

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