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 recieved 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.packet.IQ;
019    import org.jivesoftware.smack.packet.XMPPError;
020    import org.jivesoftware.smackx.filetransfer.*;
021    import org.jivesoftware.smack.*;
022    
023    import org.jivesoftware.smackx.packet.*;
024    import org.jivesoftware.smack.packet.Packet;
025    import org.jivesoftware.smack.PacketListener;
026    import java.io.*;
027    import java.net.*;
028    
029    import javax.swing.*;
030    import java.awt.event.KeyEvent;
031    import com.valhalla.settings.*;
032    import java.awt.event.ActionListener;
033    import java.awt.event.ActionEvent;
034    import java.awt.*;
035    import java.util.ResourceBundle;
036    import java.util.Locale;
037    
038    /**
039     * Dialog for receiving files. It's called each time <code><streamhost></code> packet
040     * arrives. User can accept or reject incoming file by pressing "Accept" and "Reject" buttons.
041     *
042     *@author     Lukasz Wiechec
043     *@created    Jan 19, 2005 4:25:58 PM
044     */
045    
046    public class FileReceiveDialog extends FileDialog {
047        private FileTransferManager ft;
048        private static JFileChooser fileChooser = new JFileChooser();
049        private FileTransferRequest recieve = null;
050        private FileTransfer rcv = null;
051        private JLabel sizeLabel = null;
052        private JTextField sizeField = new JTextField();
053    
054        java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
055    
056        private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
057    
058        public FileReceiveDialog(FileTransferRequest recieve) {
059            this.recieve = recieve;
060            this.ft = ft;
061            this.recieve = recieve;
062            setTitle( "Receive file" );
063            fromToLabel.setText( "From:" );
064            fromToTF.setEditable(false);
065            fileTF.setEditable(false);
066    
067            ayeButton.setText( "Accept" );
068            ayeButton.setEnabled(true);
069            ayeButton.setMnemonic( KeyEvent.VK_A );
070            nayButton.setText( "Reject" );
071            nayButton.setMnemonic( KeyEvent.VK_R );
072            descriptionArea.setEditable( false );
073    
074            sizeLabel = new JLabel(resources.getString("fileSize"));
075            sizeField.setEditable(false);
076    
077            addComponent(sizeLabel, sizeField);
078    
079            sizeField.setText(nf.format((recieve.getFileSize()) / 1024) + "k");
080            descriptionArea.setText(""); //recieve.getDescription());
081            fromToTF.setText(recieve.getRequestor());
082            fileTF.setText(recieve.getFileName());
083            descriptionArea.setText(recieve.getDescription());
084        }
085    
086    
087        /** "Accept" button pressed */
088        protected void doAye() {
089            // try really hard to make sure that file will not be overwritten
090            // and can be written to
091    
092            boolean done = false;
093            while( !done ) {
094    
095                fileChooser.setSelectedFile(new File(recieve.getFileName()));
096                int retval = fileChooser.showSaveDialog(this);
097                if(retval == JFileChooser.APPROVE_OPTION) {
098                    if(fileChooser.getSelectedFile().exists()) {
099                        int choice = JOptionPane.showConfirmDialog(this,
100                        "File exists - overwrite?","Receive File",JOptionPane.YES_NO_CANCEL_OPTION);
101                        if(choice == JOptionPane.CANCEL_OPTION) {
102                            doNay();
103                            return;
104                        } else if(choice == JOptionPane.NO_OPTION) {
105                            done = false;
106                        } else if(choice == JOptionPane.YES_OPTION) {
107                            done = true;
108                            if( ! fileChooser.getSelectedFile().canWrite() ) {
109                                JOptionPane.showMessageDialog(this,
110                                resources.getString( "writingToFileNotPermitted" ),
111                                resources.getString( "recieveFileError" ),
112                                JOptionPane.ERROR_MESSAGE
113                                );
114                                done = false;
115                            }
116                        }
117                    } else {
118    
119                        done = true;
120                    }
121                } else if(retval == JFileChooser.CANCEL_OPTION) {
122                    doNay();
123                    return;
124                }
125            }
126            // send confirmation message indicating that we want to recieve the file
127    
128            statusLabel.setText("Waiting for sender...");
129            disableAll();
130    
131            IncomingFileTransfer rcv = recieve.accept();
132    
133            FileProgressDialog.getInstance().addFile(rcv, rcv.getPeer(), fileChooser.getSelectedFile().getName(),
134                0, rcv.getFileSize());
135            try {
136                rcv.recieveFile(fileChooser.getSelectedFile());
137            }
138            catch(XMPPException ex ) { }
139            dispose();
140        }
141    
142        /** "Reject" button pressed */
143        protected void doNay() {
144            // we don't want to accept the file so we need to send the IQ error message
145            // to the Initiator
146            if( rcv != null ) rcv.cancel();
147            dispose();
148        }
149    
150        /**
151         * helper method for making sure that packet will be sent
152         * @param packet packet to send
153         */
154        private void sendPacket(Packet packet) {
155            if(BuddyList.getInstance().checkConnection()) {
156                BuddyList.getInstance().getConnection().sendPacket( packet );
157            }
158        }
159    }