Search in sources :

Example 41 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class JoinRoomSwingWorker method construct.

@Override
public Object construct() {
    try {
        groupChat = MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).getMultiUserChat(roomJID);
        // Create a UI component, if one was not yet created. It is important that this happens before the MUC is
        // joined server-side, as the UI component needs to be able to display data that is sent by the server upon
        // joining the room.
        ChatRoom room;
        try {
            final String roomName = XmppStringUtils.parseBareJid(groupChat.getRoom());
            room = SparkManager.getChatManager().getChatContainer().getChatRoom(roomName);
        } catch (ChatRoomNotFoundException e) {
            room = UIComponentRegistry.createGroupChatRoom(groupChat);
            ((GroupChatRoom) room).setPassword(password);
            ((GroupChatRoom) room).setTabTitle(tabTitle);
        }
        // Use the default nickname, if none has been provided.
        if (!ModelUtil.hasLength(nickname)) {
            nickname = SettingsManager.getRelodLocalPreferences().getNickname().trim();
        }
        // Join the MUC server-sided, if we're not already in.
        if (!groupChat.isJoined()) {
            if (password == null && ConferenceUtils.isPasswordRequired(roomJID)) {
                JLabel label = new JLabel(Res.getString("message.enter.room.password"));
                JPasswordField passwordField = new JPasswordField();
                passwordField.addAncestorListener(new RequestFocusListener());
                JOptionPane.showConfirmDialog(null, new Object[] { label, passwordField }, Res.getString("title.password.required"), JOptionPane.OK_CANCEL_OPTION);
                password = new String(passwordField.getPassword());
                if (!ModelUtil.hasLength(password)) {
                    return null;
                }
            }
            if (!ConferenceUtils.confirmToRevealVisibility()) {
                return null;
            }
            if (ModelUtil.hasLength(password)) {
                groupChat.join(nickname, password);
            } else {
                groupChat.join(nickname);
            }
        }
        return room;
    } catch (XMPPException | SmackException ex) {
        Log.error("An exception occurred while trying to join room '" + roomJID + "'.", ex);
        XMPPError error = null;
        if (ex instanceof XMPPException.XMPPErrorException) {
            error = ((XMPPException.XMPPErrorException) ex).getXMPPError();
            if (XMPPError.Condition.conflict.equals(error.getCondition())) {
                final Object userInput = JOptionPane.showInputDialog(SparkManager.getMainWindow(), Res.getString("message.nickname.in.use"), Res.getString("title.change.nickname"), JOptionPane.WARNING_MESSAGE, null, // null selection values implies text field.
                null, nickname);
                if (userInput != null) {
                    Log.debug("Retry joining room '" + roomJID + "', using nickname: " + userInput);
                    this.nickname = (String) userInput;
                    return construct();
                }
            }
        }
        final String errorText = ConferenceUtils.getReason(error);
        errors.add(errorText);
        return null;
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) XMPPError(org.jivesoftware.smack.packet.XMPPError) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) ChatRoomNotFoundException(org.jivesoftware.spark.ui.ChatRoomNotFoundException) RequestFocusListener(org.jivesoftware.spark.ui.RequestFocusListener) XMPPException(org.jivesoftware.smack.XMPPException)

Example 42 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class DefaultTabHandler method isTabHandled.

public boolean isTabHandled(SparkTab tab, Component component, boolean isSelectedTab, boolean chatFrameFocused) {
    if (component instanceof ChatRoom) {
        ChatRoom room = (ChatRoom) component;
        boolean isStaleRoom = SparkManager.getChatManager().isStaleRoom(room);
        boolean isTyping = SparkManager.getChatManager().containsTypingNotification((ChatRoom) component);
        // Check if is typing.
        if (isTyping) {
            tab.setIcon(SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_EDIT_IMAGE));
        } else if (room instanceof ChatRoomImpl && !isStaleRoom) {
            // User is not typing, therefore show default presence icon.
            String participantJID = ((ChatRoomImpl) room).getParticipantJID();
            Presence presence = PresenceManager.getPresence(participantJID);
            Icon icon = PresenceManager.getIconFromPresence(presence);
            tab.setIcon(icon);
        }
        if (!chatFrameFocused || !isSelectedTab) {
            if (room.getUnreadMessageCount() > 0) {
                // Make tab red.
                tab.setTitleColor((Color) UIManager.get("Chat.unreadMessageColor"));
                tab.setTabBold(true);
            }
            // Handle unread message count.
            int unreadMessageCount = room.getUnreadMessageCount();
            String appendedMessage = "";
            if (unreadMessageCount > 1) {
                appendedMessage = " (" + unreadMessageCount + ")";
            }
            tab.setTabTitle(room.getTabTitle() + appendedMessage);
        }
        // Check if the room is stale.
        if (isStaleRoom && component instanceof ChatRoomImpl) {
            decorateStaleTab(tab, (ChatRoom) component);
        } else // and the tab is the selected component.
        if (isSelectedTab && chatFrameFocused) {
            tab.setTitleColor(Color.black);
            // tab.setTabFont(tab.getDefaultFont());
            tab.setTabTitle(room.getTabTitle());
            // Clear unread message count.
            room.clearUnreadMessageCount();
        }
    } else {
        if (!chatFrameFocused || !isSelectedTab) {
            // Make tab red.
            tab.setTitleColor((Color) UIManager.get("Chat.unreadMessageColor"));
            tab.setTabBold(true);
        }
        if (isSelectedTab && chatFrameFocused) {
            tab.setTitleColor(Color.black);
            tab.setTabFont(tab.getDefaultFont());
        }
    }
    return true;
}
Also used : ChatRoom(org.jivesoftware.spark.ui.ChatRoom) Presence(org.jivesoftware.smack.packet.Presence) Icon(javax.swing.Icon) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl)

Example 43 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class SparkTransferManager method addPresenceListener.

private void addPresenceListener() {
    SparkManager.getConnection().addAsyncStanzaListener(stanza -> {
        Presence presence = (Presence) stanza;
        if (presence.isAvailable()) {
            String bareJID = XmppStringUtils.parseBareJid(presence.getFrom());
            // Iterate through map.
            ArrayList<File> list = waitMap.get(bareJID);
            if (list != null) {
                // Iterate through list and send.
                Iterator<File> iter = list.iterator();
                ChatRoom room = null;
                while (iter.hasNext()) {
                    File file = iter.next();
                    room = sendFile(file, bareJID);
                }
                if (room != null) {
                    Message message = new Message();
                    message.setBody(Res.getString("message.sent.offline.files"));
                    room.sendMessage(message);
                }
            }
            waitMap.remove(bareJID);
        }
    }, new StanzaTypeFilter(Presence.class));
}
Also used : StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) Message(org.jivesoftware.smack.packet.Message) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) Presence(org.jivesoftware.smack.packet.Presence) File(java.io.File)

Example 44 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class SparkTransferManager method sendFile.

/**
 * Send a file to a user.
 *
 * @param file the file to send.
 * @param jid  the jid of the user to send the file to.
 * @return the ChatRoom of the user.
 */
public ChatRoom sendFile(File file, String jid) {
    long maxsize = Long.parseLong(Default.getString(Default.FILE_TRANSFER_MAXIMUM_SIZE));
    long warningsize = Long.parseLong(Default.getString(Default.FILE_TRANSFER_WARNING_SIZE));
    if (file.length() >= maxsize && maxsize != -1) {
        String maxsizeString = TransferUtils.getAppropriateByteWithSuffix(maxsize);
        String yoursizeString = TransferUtils.getAppropriateByteWithSuffix(file.length());
        String output = Res.getString("message.file.transfer.file.too.big.error", maxsizeString, yoursizeString);
        UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
        JOptionPane.showMessageDialog(null, output, Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
        return null;
    }
    if (file.length() >= warningsize && warningsize != -1) {
        int result = JOptionPane.showConfirmDialog(null, Res.getString("message.file.transfer.file.too.big.warning"), Res.getString("title.error"), JOptionPane.YES_NO_OPTION);
        if (result != 0) {
            return null;
        }
    }
    final ContactList contactList = SparkManager.getWorkspace().getContactList();
    String bareJID = XmppStringUtils.parseBareJid(jid);
    String fullJID = PresenceManager.getFullyQualifiedJID(jid);
    if (!PresenceManager.isOnline(jid)) {
        ArrayList<File> list = waitMap.get(jid);
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(file);
        waitMap.put(jid, list);
        ChatRoom chatRoom;
        ContactItem contactItem = contactList.getContactItemByJID(jid);
        if (contactItem != null) {
            chatRoom = SparkManager.getChatManager().createChatRoom(jid, contactItem.getDisplayName(), contactItem.getDisplayName());
        } else {
            chatRoom = SparkManager.getChatManager().createChatRoom(jid, jid, jid);
        }
        chatRoom.getTranscriptWindow().insertNotificationMessage("The user is offline. Will auto-send \"" + file.getName() + "\" when user comes back online.", ChatManager.ERROR_COLOR);
        return null;
    }
    // Create the outgoing file transfer
    final OutgoingFileTransfer transfer = transferManager.createOutgoingFileTransfer(fullJID);
    ContactItem contactItem = contactList.getContactItemByJID(bareJID);
    ChatRoom chatRoom;
    if (contactItem != null) {
        chatRoom = SparkManager.getChatManager().createChatRoom(bareJID, contactItem.getDisplayName(), contactItem.getDisplayName());
    } else {
        chatRoom = SparkManager.getChatManager().createChatRoom(bareJID, bareJID, bareJID);
    }
    TranscriptWindow transcriptWindow = chatRoom.getTranscriptWindow();
    SendFileTransfer sendingUI = new SendFileTransfer();
    try {
        transfer.sendFile(file, "Sending file");
    } catch (SmackException e) {
        Log.error(e);
    }
    // Add listener to cancel transfer is sending file to user who just went offline.
    AndFilter presenceFilter = new AndFilter(new StanzaTypeFilter(Presence.class), FromMatchesFilter.createBare(bareJID));
    final StanzaListener packetListener = stanza -> {
        Presence presence = (Presence) stanza;
        if (!presence.isAvailable()) {
            if (transfer != null) {
                transfer.cancel();
            }
        }
    };
    // Add presence listener to check if user is offline and cancel sending.
    SparkManager.getConnection().addAsyncStanzaListener(packetListener, presenceFilter);
    chatRoom.addClosingListener(() -> {
        SparkManager.getConnection().removeAsyncStanzaListener(packetListener);
        if (!transfer.isDone()) {
            transfer.cancel();
        }
    });
    try {
        sendingUI.sendFile(transfer, transferManager, fullJID, contactItem.getDisplayName());
    } catch (NullPointerException e) {
        Log.error(e);
    }
    transcriptWindow.addComponent(sendingUI);
    chatRoom.scrollToBottom();
    return chatRoom;
}
Also used : OutgoingFileTransfer(org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer) Color(java.awt.Color) UIManager(javax.swing.UIManager) URL(java.net.URL) Cursor(java.awt.Cursor) URISyntaxException(java.net.URISyntaxException) StringUtils(org.jivesoftware.smack.util.StringUtils) Res(org.jivesoftware.resource.Res) Log(org.jivesoftware.spark.util.log.Log) TransferUtils(org.jivesoftware.sparkimpl.plugin.filetransfer.transfer.ui.TransferUtils) ContactItem(org.jivesoftware.spark.ui.ContactItem) ResourceUtils(org.jivesoftware.spark.util.ResourceUtils) GraphicsEnvironment(java.awt.GraphicsEnvironment) FileDialog(java.awt.FileDialog) TranscriptWindow(org.jivesoftware.spark.ui.TranscriptWindow) Map(java.util.Map) MouseAdapter(java.awt.event.MouseAdapter) ImageIO(javax.imageio.ImageIO) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) MainWindow(org.jivesoftware.MainWindow) PreferenceManager(org.jivesoftware.spark.preference.PreferenceManager) URI(java.net.URI) FileTransferPreference(org.jivesoftware.spark.filetransfer.preferences.FileTransferPreference) Enterprise(org.jivesoftware.sparkimpl.plugin.manager.Enterprise) AndFilter(org.jivesoftware.smack.filter.AndFilter) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter) org.jivesoftware.smack(org.jivesoftware.smack) Frame(java.awt.Frame) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) ChatManager(org.jivesoftware.spark.ChatManager) FileTransferManager(org.jivesoftware.smackx.filetransfer.FileTransferManager) BufferedImage(java.awt.image.BufferedImage) SparkRes(org.jivesoftware.resource.SparkRes) SendFileTransfer(org.jivesoftware.sparkimpl.plugin.filetransfer.transfer.ui.SendFileTransfer) JMenu(javax.swing.JMenu) ChatRoomListenerAdapter(org.jivesoftware.spark.ui.ChatRoomListenerAdapter) KeyEvent(java.awt.event.KeyEvent) ImageSelectionPanel(org.jivesoftware.spark.ui.ImageSelectionPanel) Spark(org.jivesoftware.Spark) List(java.util.List) AWTException(java.awt.AWTException) Toolkit(java.awt.Toolkit) Downloads(org.jivesoftware.sparkimpl.plugin.filetransfer.transfer.Downloads) FileTransferRequest(org.jivesoftware.smackx.filetransfer.FileTransferRequest) Rectangle(java.awt.Rectangle) DataFlavor(java.awt.datatransfer.DataFlavor) ChatRoomButton(org.jivesoftware.spark.ui.ChatRoomButton) GraphicsDevice(java.awt.GraphicsDevice) Transferable(java.awt.datatransfer.Transferable) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) HashMap(java.util.HashMap) ReceiveFileTransfer(org.jivesoftware.sparkimpl.plugin.filetransfer.transfer.ui.ReceiveFileTransfer) KeyAdapter(java.awt.event.KeyAdapter) ArrayList(java.util.ArrayList) SwingUtilities(javax.swing.SwingUtilities) Robot(java.awt.Robot) JMenuItem(javax.swing.JMenuItem) Default(org.jivesoftware.resource.Default) PresenceManager(org.jivesoftware.spark.PresenceManager) OutgoingFileTransfer(org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer) Desktop(java.awt.Desktop) Presence(org.jivesoftware.smack.packet.Presence) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) JOptionPane(javax.swing.JOptionPane) MouseEvent(java.awt.event.MouseEvent) File(java.io.File) XmppStringUtils(org.jxmpp.util.XmppStringUtils) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) ContactList(org.jivesoftware.spark.ui.ContactList) SparkManager(org.jivesoftware.spark.SparkManager) InBandBytestreamManager(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager) Message(org.jivesoftware.smack.packet.Message) ChatFrame(org.jivesoftware.spark.ui.ChatFrame) ContactItem(org.jivesoftware.spark.ui.ContactItem) SendFileTransfer(org.jivesoftware.sparkimpl.plugin.filetransfer.transfer.ui.SendFileTransfer) ContactList(org.jivesoftware.spark.ui.ContactList) AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) TranscriptWindow(org.jivesoftware.spark.ui.TranscriptWindow) Presence(org.jivesoftware.smack.packet.Presence) File(java.io.File)

Example 45 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class BattleshipPlugin method showInvitationInChat.

private void showInvitationInChat(final GameOfferPacket invitation) {
    invitation.setType(IQ.Type.result);
    invitation.setTo(invitation.getFrom());
    final ChatRoom room = SparkManager.getChatManager().getChatRoom(XmppStringUtils.parseBareJid(invitation.getFrom()));
    String name = XmppStringUtils.parseLocalpart(invitation.getFrom());
    final JPanel panel = new JPanel();
    JLabel text = new JLabel("Game request from" + name);
    JLabel game = new JLabel("Battleships");
    game.setFont(new Font("Dialog", Font.BOLD, 24));
    game.setForeground(Color.RED);
    JButton accept = new JButton(Res.getString("button.accept").replace("&", ""));
    JButton decline = new JButton(Res.getString("button.decline").replace("&", ""));
    panel.add(text);
    panel.add(game);
    panel.add(accept);
    panel.add(decline);
    room.getTranscriptWindow().addComponent(panel);
    accept.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                SparkManager.getConnection().sendStanza(invitation);
            } catch (SmackException.NotConnectedException e1) {
                Log.warning("Unable to send invitation accept to " + invitation.getTo(), e1);
            }
            invitation.setStartingPlayer(!invitation.isStartingPlayer());
            ChatRoomOpeningListener.createWindow(invitation, invitation.getFrom());
            panel.remove(3);
            panel.remove(2);
            panel.repaint();
            panel.revalidate();
        }
    });
    decline.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            invitation.setType(IQ.Type.error);
            try {
                SparkManager.getConnection().sendStanza(invitation);
            } catch (SmackException.NotConnectedException e1) {
                Log.warning("Unable to send invitation decline to " + invitation.getTo(), e1);
            }
            panel.remove(3);
            panel.remove(2);
            panel.repaint();
            panel.revalidate();
        }
    });
}
Also used : JPanel(javax.swing.JPanel) ActionListener(java.awt.event.ActionListener) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) Font(java.awt.Font)

Aggregations

ChatRoom (org.jivesoftware.spark.ui.ChatRoom)45 ChatManager (org.jivesoftware.spark.ChatManager)10 ChatRoomImpl (org.jivesoftware.spark.ui.rooms.ChatRoomImpl)10 ActionEvent (java.awt.event.ActionEvent)8 Message (org.jivesoftware.smack.packet.Message)8 ChatRoomNotFoundException (org.jivesoftware.spark.ui.ChatRoomNotFoundException)8 ContactItem (org.jivesoftware.spark.ui.ContactItem)7 Presence (org.jivesoftware.smack.packet.Presence)6 ChatRoomListenerAdapter (org.jivesoftware.spark.ui.ChatRoomListenerAdapter)6 ActionListener (java.awt.event.ActionListener)5 MouseEvent (java.awt.event.MouseEvent)5 Date (java.util.Date)5 JButton (javax.swing.JButton)5 JLabel (javax.swing.JLabel)5 JPanel (javax.swing.JPanel)5 SmackException (org.jivesoftware.smack.SmackException)5 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 SimpleDateFormat (java.text.SimpleDateFormat)4 JPopupMenu (javax.swing.JPopupMenu)4 SparkManager (org.jivesoftware.spark.SparkManager)4