package JabberClient;
import java.io.File;
import javax.swing.JOptionPane;
import javax.swing.ProgressMonitorInputStream;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.filetransfer.FileTransferListener;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
public class FileTransfer{
private XMPPConnection xmppconnection;
private ChatWindow chatwindow;
public FileTransfer(XMPPConnection xmppconnection, ChatWindow chatwindow){
this.xmppconnection = xmppconnection;
this.chatwindow = chatwindow;
}
public FileTransfer(XMPPConnection xmppconnection){
this.xmppconnection = xmppconnection;
}
// strJidreceiver muss der Form "test2@192.168.1.143/Smack" entsprechen, also mit / und der Ressource
public void sendFile(String strJidreceiver){
FileTransferManager manager = new FileTransferManager(xmppconnection);
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(strJidreceiver);
OpenDialog opendialogsendfile = new OpenDialog(true);
if (opendialogsendfile.getAbortedStatus() != true){
try {
transfer.sendFile(new File(opendialogsendfile.getCompletePath()),opendialogsendfile.getFileDescription());
new Progress((int) transfer.getFileSize(), chatwindow, transfer);
}
catch (XMPPException e) {
e.printStackTrace();
}
}
else{
System.out.println("Transfer-Abbruch durch den Benutzer!");
}
}
public void receiveFile(){
// der Status muss available sein, sonst ist das Senden nicht möglich
Presence presence = new Presence(Presence.Type.available);
xmppconnection.sendPacket(presence);
FileTransferManager manager = new FileTransferManager(xmppconnection);
manager.addFileTransferListener(new FileTransferListener() {
public void fileTransferRequest(FileTransferRequest request) {
ProgressMonitorInputStream pm = null;
try {
int result = JOptionPane.showConfirmDialog( null, "Möchten Sie einen Dateitransfer von " + request.getRequestor() + " annehmen?","eingehender Dateitransfer", 0);
if(result == 0){
OpenDialog opendialogreceivefile = new OpenDialog(false);
if (opendialogreceivefile.getAbortedStatus() != true){
IncomingFileTransfer transfer = request.accept();
transfer.recieveFile(new File(opendialogreceivefile.getCompletePath() + "\\" + request.getFileName()));
new Progress((int) transfer.getFileSize(), transfer);
}
else{
System.out.println("Transfer-Abbruch durch den Benutzer!");
}
}
}
catch (XMPPException e) {
e.printStackTrace();
}
}
});
}
}