001    /*
002     Copyright (C) 2003 Adam Olsen
003     This program is free software; you can redistribute it and/or modify
004     it under the terms of the GNU General Public License as published by
005     the Free Software Foundation; either version 1, or (at your option)
006     any later version.
007     This program is distributed in the hope that it will be useful,
008     but WITHOUT ANY WARRANTY; without even the implied warranty of
009     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010     GNU General Public License for more details.
011     You should have received a copy of the GNU General Public License
012     along with this program; if not, write to the Free Software
013     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014     */
015    package com.valhalla.jbother;
016    
017    import com.valhalla.jbother.jabber.smack.*;
018    import org.jivesoftware.smack.PacketListener;
019    import org.jivesoftware.smack.PacketCollector;
020    import org.jivesoftware.smack.SmackConfiguration;
021    import org.jivesoftware.smack.*;
022    import org.jivesoftware.smack.filter.PacketIDFilter;
023    import org.jivesoftware.smack.packet.Packet;
024    import org.jivesoftware.smack.packet.IQ;
025    import com.valhalla.jbother.preferences.*;
026    import org.jivesoftware.smackx.filetransfer.*;
027    
028    import java.awt.*;
029    import java.awt.event.KeyEvent;
030    import java.awt.event.ActionListener;
031    import java.awt.event.ActionEvent;
032    import java.util.*;
033    import java.net.*;
034    import java.io.File;
035    
036    import com.valhalla.settings.Settings;
037    
038    import javax.swing.*;
039    
040    /**
041     * dialog for sending files. It is also a packet listener
042     *
043     *@author     Lukasz Wiechec
044     *@created    Feb 2, 2005 4:52:15 PM
045     */
046    
047    public class FileSendDialog extends FileDialog {
048        private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
049        private static JFileChooser fileChooser = new JFileChooser();
050        private String jid;
051    
052        public FileSendDialog(String jid) throws HeadlessException {
053            setTitle( "Send file" );
054            fromToLabel.setText( "To:" );
055            this.jid = jid;
056            fromToTF.setText(jid);
057            fromToTF.setEditable(false);
058            ayeButton.setText( "Send" );
059            ayeButton.setMnemonic( KeyEvent.VK_S );
060            nayButton.setText( "Cancel" );
061            nayButton.setMnemonic( KeyEvent.VK_C );
062            descriptionArea.setEditable( true );
063    
064            selectFile();
065    
066        }
067    
068        class SendFileThread extends Thread
069        {
070            OutgoingFileTransfer send = null;
071            public SendFileThread(OutgoingFileTransfer send)
072            {
073                this.send = send;
074            }
075    
076            public void run()
077            {
078                try {
079                    send.sendFile(fileChooser.getSelectedFile().getPath(),
080                        fileChooser.getSelectedFile().length(), descriptionArea.getText());
081                }
082                catch(Exception ex) { }
083            }
084        }
085                
086    
087        /**
088         * "Send" button pressed
089         * note: sending initial StreamInitiation message is done in separate thread, in order to prevent
090         * locking up JBother while waiting for the reply
091         */
092        protected void doAye() {
093            FileTransferManager ft = ConnectorThread.getInstance().getFileTransferManager();
094            OutgoingFileTransfer send = ft.createOutgoingFileTransfer(jid);
095            FileProgressDialog.getInstance().addFile(send, jid, 
096                fileChooser.getSelectedFile().getName(), 0, fileChooser.getSelectedFile().length());
097            dispose();
098            new SendFileThread(send).start();
099        }
100    
101        /**
102         * "Cancel" button pressed
103         */
104        protected void doNay() {
105            setVisible(false);
106            dispose();
107        }
108    
109        /**
110         * displays file chooser window and lets user select file to send
111         */
112        private void selectFile() {
113            int retval = fileChooser.showOpenDialog(BuddyList.getInstance().getContainerFrame());
114    
115            if( retval == JFileChooser.APPROVE_OPTION ) {
116                File selectedFile = fileChooser.getSelectedFile();
117                if( ! selectedFile.exists() ) {
118                    JOptionPane.showMessageDialog(this,
119                    resources.getString("fileNotFound"),
120                    resources.getString("sendFileError"),
121                    JOptionPane.ERROR_MESSAGE);
122                    return;
123                }
124    
125                setVisible(true);
126    
127                fileTF.setText(selectedFile.getPath());
128                fileTF.setEditable(false);
129    
130            } else if( retval == JFileChooser.CANCEL_OPTION ) {
131                doNay();
132                return;
133            }
134        }
135    }