use of org.jivesoftware.sparkimpl.plugin.filetransfer.transfer.ui.SendFileTransfer in project Spark by igniterealtime.
the class SparkTransferManager method sendFile.
/**
* Send a file to a user.
*
* @param file the file to send.
* @param jid the jid of the user to send the file to.
* @return the ChatRoom of the user.
*/
public ChatRoom sendFile(File file, String jid) {
long maxsize = Long.parseLong(Default.getString(Default.FILE_TRANSFER_MAXIMUM_SIZE));
long warningsize = Long.parseLong(Default.getString(Default.FILE_TRANSFER_WARNING_SIZE));
if (file.length() >= maxsize && maxsize != -1) {
String maxsizeString = TransferUtils.getAppropriateByteWithSuffix(maxsize);
String yoursizeString = TransferUtils.getAppropriateByteWithSuffix(file.length());
String output = Res.getString("message.file.transfer.file.too.big.error", maxsizeString, yoursizeString);
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(null, output, Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
return null;
}
if (file.length() >= warningsize && warningsize != -1) {
int result = JOptionPane.showConfirmDialog(null, Res.getString("message.file.transfer.file.too.big.warning"), Res.getString("title.error"), JOptionPane.YES_NO_OPTION);
if (result != 0) {
return null;
}
}
final ContactList contactList = SparkManager.getWorkspace().getContactList();
String bareJID = XmppStringUtils.parseBareJid(jid);
String fullJID = PresenceManager.getFullyQualifiedJID(jid);
if (!PresenceManager.isOnline(jid)) {
ArrayList<File> list = waitMap.get(jid);
if (list == null) {
list = new ArrayList<>();
}
list.add(file);
waitMap.put(jid, list);
ChatRoom chatRoom;
ContactItem contactItem = contactList.getContactItemByJID(jid);
if (contactItem != null) {
chatRoom = SparkManager.getChatManager().createChatRoom(jid, contactItem.getDisplayName(), contactItem.getDisplayName());
} else {
chatRoom = SparkManager.getChatManager().createChatRoom(jid, jid, jid);
}
chatRoom.getTranscriptWindow().insertNotificationMessage("The user is offline. Will auto-send \"" + file.getName() + "\" when user comes back online.", ChatManager.ERROR_COLOR);
return null;
}
// Create the outgoing file transfer
final OutgoingFileTransfer transfer = transferManager.createOutgoingFileTransfer(fullJID);
ContactItem contactItem = contactList.getContactItemByJID(bareJID);
ChatRoom chatRoom;
if (contactItem != null) {
chatRoom = SparkManager.getChatManager().createChatRoom(bareJID, contactItem.getDisplayName(), contactItem.getDisplayName());
} else {
chatRoom = SparkManager.getChatManager().createChatRoom(bareJID, bareJID, bareJID);
}
TranscriptWindow transcriptWindow = chatRoom.getTranscriptWindow();
SendFileTransfer sendingUI = new SendFileTransfer();
try {
transfer.sendFile(file, "Sending file");
} catch (SmackException e) {
Log.error(e);
}
// Add listener to cancel transfer is sending file to user who just went offline.
AndFilter presenceFilter = new AndFilter(new StanzaTypeFilter(Presence.class), FromMatchesFilter.createBare(bareJID));
final StanzaListener packetListener = stanza -> {
Presence presence = (Presence) stanza;
if (!presence.isAvailable()) {
if (transfer != null) {
transfer.cancel();
}
}
};
// Add presence listener to check if user is offline and cancel sending.
SparkManager.getConnection().addAsyncStanzaListener(packetListener, presenceFilter);
chatRoom.addClosingListener(() -> {
SparkManager.getConnection().removeAsyncStanzaListener(packetListener);
if (!transfer.isDone()) {
transfer.cancel();
}
});
try {
sendingUI.sendFile(transfer, transferManager, fullJID, contactItem.getDisplayName());
} catch (NullPointerException e) {
Log.error(e);
}
transcriptWindow.addComponent(sendingUI);
chatRoom.scrollToBottom();
return chatRoom;
}
Aggregations