Search in sources :

Example 1 with FileTransferPreference

use of org.jivesoftware.spark.filetransfer.preferences.FileTransferPreference in project Spark by igniterealtime.

the class ReceiveFileTransfer method acceptFileTransfer.

public void acceptFileTransfer(final FileTransferRequest request) {
    String fileName = request.getFileName();
    long fileSize = request.getFileSize();
    String requestor = request.getRequestor();
    String bareJID = XmppStringUtils.parseBareJid(requestor);
    // SPARK-1869
    FileTransferNegotiator.getInstanceFor(SparkManager.getConnection());
    FileTransferNegotiator.IBB_ONLY = SettingsManager.getLocalPreferences().isFileTransferIbbOnly();
    ByteFormat format = new ByteFormat();
    String text = format.format(fileSize);
    fileLabel.setText(fileName + " (" + text + ")");
    ContactList contactList = SparkManager.getWorkspace().getContactList();
    ContactItem contactItem = contactList.getContactItemByJID(bareJID);
    titleLabel.setText(Res.getString("message.user.is.sending.you.a.file", contactItem.getDisplayName()));
    File tempFile = new File(Spark.getSparkUserHome(), "/tmp");
    try {
        tempFile.mkdirs();
        File file = new File(tempFile, fileName);
        file.delete();
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write("a");
        out.close();
        imageLabel.setIcon(GraphicUtils.getIcon(file));
        // Delete temp file when program exits.
        file.delete();
    } catch (IOException e) {
        imageLabel.setIcon(SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32));
        Log.error(e);
    }
    acceptButton.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            try {
                Downloads.checkDownloadDirectory();
                acceptRequest(request);
            } catch (Exception ex) {
                // this means there is a problem with the download directory
                try {
                    request.reject();
                } catch (SmackException.NotConnectedException e1) {
                    Log.warning("Unable to reject the request.", ex);
                }
                setBackground(new Color(239, 245, 250));
                acceptButton.setVisible(false);
                declineButton.setVisible(false);
                if (Downloads.getDownloadDirectory() == null) {
                    fileLabel.setText("");
                } else {
                    ResourceUtils.resLabel(fileLabel, null, Res.getString("label.transfer.download.directory") + " " + Downloads.getDownloadDirectory().getAbsolutePath());
                }
                // option to set a new path for the file-download
                pathButton.setVisible(true);
                pathButton.addMouseListener(new MouseAdapter() {

                    public void mousePressed(MouseEvent e) {
                        Preference p = SparkManager.getPreferenceManager().getPreference(new FileTransferPreference().getNamespace());
                        // retrieve the filetransfer preferences and show the preference menu
                        // to the user
                        SparkManager.getPreferenceManager().showPreferences(p);
                    }
                });
                titleLabel.setText(ex.getMessage());
                titleLabel.setForeground(new Color(65, 139, 179));
                invalidate();
                validate();
                repaint();
            }
        }
    });
    declineButton.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            rejectRequest(request);
        }
    });
}
Also used : MouseEvent(java.awt.event.MouseEvent) ContactItem(org.jivesoftware.spark.ui.ContactItem) FileWriter(java.io.FileWriter) SmackException(org.jivesoftware.smack.SmackException) Color(java.awt.Color) MouseAdapter(java.awt.event.MouseAdapter) ContactList(org.jivesoftware.spark.ui.ContactList) IOException(java.io.IOException) FileTransferPreference(org.jivesoftware.spark.filetransfer.preferences.FileTransferPreference) SmackException(org.jivesoftware.smack.SmackException) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ByteFormat(org.jivesoftware.spark.util.ByteFormat) FileTransferPreference(org.jivesoftware.spark.filetransfer.preferences.FileTransferPreference) Preference(org.jivesoftware.spark.preference.Preference) File(java.io.File)

Aggregations

Color (java.awt.Color)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 SmackException (org.jivesoftware.smack.SmackException)1 FileTransferPreference (org.jivesoftware.spark.filetransfer.preferences.FileTransferPreference)1 Preference (org.jivesoftware.spark.preference.Preference)1 ContactItem (org.jivesoftware.spark.ui.ContactItem)1 ContactList (org.jivesoftware.spark.ui.ContactList)1 ByteFormat (org.jivesoftware.spark.util.ByteFormat)1