Package net.sphene.goim.rcp.xmpp.util

Source Code of net.sphene.goim.rcp.xmpp.util.FileTransferUtil

/*
* File    : FileTransferUtil.java
* Created : 18.03.2006
* By      : kahless
*
* GOIM - Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul
*              (JabberId: kahless@sphene.net / Email: herbert.poul@gmail.com)
* http://goim.sphene.net
*
* 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 2 of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package net.sphene.goim.rcp.xmpp.util;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import net.sphene.goim.rcp.beans.GOIMAccount;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.jivesoftware.smackx.filetransfer.FileTransfer;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
import org.jivesoftware.smackx.filetransfer.FileTransfer.Status;

public class FileTransferUtil {
 
  /**
   * Trys to send a file to user.
   * It first asks the user which file to send, and prompt for a description.
   * Afterwards trys to negotiate a file transfer.
   * @param parent parent shell - used for dialogs
   * @param account account on which connection to send
   * @param user destination of the file.
   * @return true if file started being sent ... false if user cancelled prior sending could start (e.g. not selecting file, etc.)
   */
  public static boolean sendFileTo(Shell parent, GOIMAccount account, final String user) {
   
    FileDialog fileDialog = new FileDialog(parent,SWT.SINGLE|SWT.OPEN);
    fileDialog.setText("Choose file to send to " + user);
    String file = fileDialog.open();
    if(file == null) return false;
   
    FileTransferManager manager = new FileTransferManager(account.xmpp.getConnection());
    final OutgoingFileTransfer outgoingFileTransfer = manager.createOutgoingFileTransfer(user);
   
    final File f = new File(file);
    InputDialog descriptionInput = new InputDialog(parent,
        "Sending file to " + user,
        "Please Provide a description of the file (" + f.getName() + ") " +
          "you want to send to " + user,
        "",null);
    if(descriptionInput.open() != InputDialog.OK) return false;
    try {
      outgoingFileTransfer.sendFile(f,descriptionInput.getValue());
     
      final String task = "Sending file " + f.getName() + " to " + user;
      showProgressMonitor(parent, outgoingFileTransfer, task);
    } catch (Exception e) {
      throw new RuntimeException("Error while trying to send file.",e);
    }
   
   
    return true;
  }

  public static void showProgressMonitor(final Shell parent, final FileTransfer fileTransfer, final String task) throws InvocationTargetException, InterruptedException {
    new NonModalProgressMonitor(parent).run(true,true,new IRunnableWithProgress() {
      public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
        monitor.beginTask(task, 1000);
        int lastwork = 0;
        while(!fileTransfer.isDone()) {
          if(monitor.isCanceled()) break;
          monitor.setTaskName(task + " (" + fileTransfer.getAmountWritten() + "/" + fileTransfer.getFileSize() + ")");
          int currwork = (int)(fileTransfer.getProgress() * 1000);
          monitor.worked(currwork - lastwork);
          lastwork = currwork;
          Thread.sleep(100);
        }
        monitor.done();
        parent.getDisplay().asyncExec(new Runnable() {
          public void run() {
            if(fileTransfer.getStatus() == Status.ERROR) {
              MessageDialog.openError(parent,"Error while trying to send file.","Error while trying to send file: " + (fileTransfer.getError() == null ? "Unknown ?!" : fileTransfer.getError().getMessage()));
            }
          }
        });
      } });
  }
}
TOP

Related Classes of net.sphene.goim.rcp.xmpp.util.FileTransferUtil

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.