diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java index 57fc29341..b35a75d39 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java @@ -41,6 +41,7 @@ import org.jivesoftware.smackx.jingle.provider.JingleContentProviderManager; import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingFileOfferCallback; import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer; import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild; +import org.jivesoftware.smackx.jingle_filetransfer.handler.SendFileHandler; import org.jivesoftware.smackx.jingle_filetransfer.listener.JingleFileTransferOfferListener; import org.jivesoftware.smackx.jingle_filetransfer.provider.JingleFileTransferProvider; @@ -75,12 +76,14 @@ public final class JingleFileTransferManager extends Manager implements JingleHa return manager; } - public void sendFile(FullJid recipient, File file) + public SendFileHandler sendFile(FullJid recipient, File file) throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { OutgoingJingleFileOffer offer = new OutgoingJingleFileOffer(connection(), recipient); JingleManager.getInstanceFor(connection()).registerJingleSessionHandler(recipient, offer.getSessionId(), offer); + SendFileHandler handler = new SendFileHandler(offer); offer.send(file); + return handler; } public SmackFuture asyncSendFile(FullJid recipient, File file) { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/handler/SendFileHandler.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/handler/SendFileHandler.java new file mode 100644 index 000000000..f5b0048d1 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/handler/SendFileHandler.java @@ -0,0 +1,99 @@ +/** + * + * Copyright 2017 Paul Schaub + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.jingle_filetransfer.handler; + +import java.util.HashSet; + +import org.jivesoftware.smackx.jingle_filetransfer.JingleFileTransferSession; + +/** + * Handler that provides some control over the JingleFileOffer session. + */ +public class SendFileHandler { + + private final JingleFileTransferSession session; + private final HashSet finishedListeners = new HashSet<>(); + private final HashSet acceptedListeners = new HashSet<>(); + + public SendFileHandler(JingleFileTransferSession session) { + this.session = session; + } + + /** + * Cancels the current file transfer. + */ + public void cancel() { + + } + + /** + * Returns true, if the file transfer is finished. + * @return true if transfer finished. + */ + public boolean isFinished() { + return false; + } + + /** + * Add a new FinishedListener. + * @param listener listener + */ + public void addFinishedListener(FinishedListener listener) { + finishedListeners.add(listener); + } + + /** + * Add a new AcceptedListener. + * @param listener listener + */ + public void addAcceptedListener(AcceptedListener listener) { + acceptedListeners.add(listener); + } + + /** + * Notify all registered FinishedListeners that the file transfer has finished. + */ + void notifyFinishedListeners() { + for (FinishedListener f : finishedListeners) { + f.onFinished(); + } + } + + /** + * Notify all registered AcceptedListeners that the file transfer session has been accepted by the remote user. + */ + void notifyAcceptedListeners() { + for (AcceptedListener a : acceptedListeners) { + a.onAccepted(); + } + } + + /** + * A FinishedListener will be notified by the SendFileHandler when the corresponding file transfer is finished. + */ + public interface FinishedListener { + void onFinished(); + } + + /** + * An AcceptedListener will be notified by the SendFileHandler when the corresponding pending session has been + * accepted by the remote user. + */ + public interface AcceptedListener { + void onAccepted(); + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/handler/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/handler/package-info.java new file mode 100644 index 000000000..e90c28e2c --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/handler/package-info.java @@ -0,0 +1,22 @@ +/** + * + * Copyright 2017 Paul Schaub + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Smack's API for XEP-0234: Jingle File Transfer. + * Handlers. + */ +package org.jivesoftware.smackx.jingle_filetransfer.handler; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleSession.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleSession.java index 9bd71d81e..5644d87b9 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleSession.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleSession.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.concurrent.Future; +import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; @@ -171,7 +172,8 @@ public abstract class JingleSession implements JingleSessionHandler { return IQ.createResultIQ(jingle); } } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) { - return null; //TODO: + LOGGER.log(Level.SEVERE, "Caught an Exception: ", e); + return null; //TODO: Handle better? } }