Package com.valhalla.jbother

Source Code of com.valhalla.jbother.FileReceiveDialog$FileReceiveThread

/*
Copyright (C) 2003 Adam Olsen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.valhalla.jbother;

import com.valhalla.jbother.jabber.smack.*;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.filetransfer.*;
import org.jivesoftware.smackx.packet.*;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.PacketListener;
import java.io.*;
import java.net.*;

import javax.swing.*;
import java.awt.event.KeyEvent;
import com.valhalla.settings.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.ResourceBundle;
import java.util.Locale;

/**
* Dialog for receiving files. It's called each time <code><streamhost></code> packet
* arrives. User can accept or reject incoming file by pressing "Accept" and "Reject" buttons.
*
*@author     Lukasz Wiechec
*@created    Jan 19, 2005 4:25:58 PM
*/

public class FileReceiveDialog extends FileDialog {
    private FileReceive receive;
    private FileTransferManager ft;
    private static JFileChooser fileChooser = new JFileChooser();
    private JLabel sizeLabel = null;
    private JTextField sizeField = new JTextField();
    java.text.NumberFormat nf = java.text.NumberFormat.getInstance();

    private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );

    public FileReceiveDialog(FileReceive receive) {
        this.receive = receive;
        this.ft = ft;
        setTitle( "Receive file" );
        fromToLabel.setText( "From:" );
        fromToTF.setEditable(false);
        fileTF.setEditable(false);

        ayeButton.setText( "Accept" );
        ayeButton.setEnabled(true);
        ayeButton.setMnemonic( KeyEvent.VK_A );
        nayButton.setText( "Reject" );
        nayButton.setMnemonic( KeyEvent.VK_R );
        descriptionArea.setEditable( false );

        sizeLabel = new JLabel(resources.getString("fileSize"));
        sizeField.setEditable(false);

        addComponent(sizeLabel, sizeField);

        sizeField.setText(nf.format((receive.getSize()) / 1024) + "k");
        fromToTF.setText(receive.getFrom());
        fileTF.setText(receive.getName());
        descriptionArea.setText(receive.getDescription());

    }


    /** "Accept" button pressed */
    protected void doAye() {
        // try really hard to make sure that file will not be overwritten
        // and can be written to

        boolean done = false;
        while( !done ) {

            fileChooser.setSelectedFile(new File(receive.getName()));
            int retval = fileChooser.showSaveDialog(this);
            if(retval == JFileChooser.APPROVE_OPTION) {
                if(fileChooser.getSelectedFile().exists()) {
                    int choice = JOptionPane.showConfirmDialog(this,
                    "File exists - overwrite?","Receive File",JOptionPane.YES_NO_CANCEL_OPTION);
                    if(choice == JOptionPane.CANCEL_OPTION) {
                        doNay();
                        return;
                    } else if(choice == JOptionPane.NO_OPTION) {
                        done = false;
                    } else if(choice == JOptionPane.YES_OPTION) {
                        done = true;
                        if( ! fileChooser.getSelectedFile().canWrite() ) {
                            JOptionPane.showMessageDialog(this,
                            resources.getString( "writingToFileNotPermitted" ),
                            resources.getString( "receiveFileError" ),
                            JOptionPane.ERROR_MESSAGE
                            );
                            done = false;
                        }
                    }
                } else {

                    done = true;
                }
            } else if(retval == JFileChooser.CANCEL_OPTION) {
                doNay();
                return;
            }
        }
        // send confirmation message indicating that we want to receive the file

        statusLabel.setText("Waiting for sender...");
        disableAll();
        new FileReceiveThread().start();
    }

    class FileReceiveThread extends Thread
    {
        public void run()
        {
            dispose();
            FileTransferManager ft = ConnectorThread.getInstance().getFileTransferManager();
            boolean preferIBB = Settings.getInstance().getBoolean("preferIBB");
            if(preferIBB) ft.setPreferredType(FileTransferManager.TYPE_IBB);
            else ft.setPreferredType(-1);
            receive.setFile(fileChooser.getSelectedFile());
            FileProgressListener listener = FileProgressDialog.getInstance().addFile(receive, fileChooser.getSelectedFile().getName(), 1, receive.getSize());
            try {
                receive.addProgressListener(listener);
      
                receive.save(fileChooser.getSelectedFile());
            }
            catch(SocketException ex)
            {
                listener.update(FileTransferManager.Event.CANCELLED, 0, 0);
            }
            catch(Exception ex)
            {
                listener.update(FileTransferManager.Event.ERROR, 0, 0);

            }
            return;
        }
    }

    /** "Reject" button pressed */
    protected void doNay() {
        // we don't want to accept the file so we need to send the IQ error message
        // to the Initiator
        IQ errorPacket = receive.getSI().createConfirmationMessage();
        errorPacket.setType(IQ.Type.ERROR);

        errorPacket.setError(new XMPPError(403,"forbidden"));
        sendPacket( errorPacket );
        dispose();
    }

    /**
     * helper method for making sure that packet will be sent
     * @param packet packet to send
     */
    private void sendPacket(Packet packet) {
        if(BuddyList.getInstance().checkConnection()) {
            BuddyList.getInstance().getConnection().sendPacket( packet );
        }
    }
}
TOP

Related Classes of com.valhalla.jbother.FileReceiveDialog$FileReceiveThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.