Search in sources :

Example 1 with SendFileTransfer

use of org.jivesoftware.sparkimpl.plugin.filetransfer.transfer.ui.SendFileTransfer 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)

Aggregations

AWTException (java.awt.AWTException)1 Color (java.awt.Color)1 Cursor (java.awt.Cursor)1 Desktop (java.awt.Desktop)1 FileDialog (java.awt.FileDialog)1 Frame (java.awt.Frame)1 GraphicsDevice (java.awt.GraphicsDevice)1 GraphicsEnvironment (java.awt.GraphicsEnvironment)1 Rectangle (java.awt.Rectangle)1 Robot (java.awt.Robot)1 Toolkit (java.awt.Toolkit)1 DataFlavor (java.awt.datatransfer.DataFlavor)1 Transferable (java.awt.datatransfer.Transferable)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 KeyAdapter (java.awt.event.KeyAdapter)1 KeyEvent (java.awt.event.KeyEvent)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1