aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service
diff options
context:
space:
mode:
authorhrxi <hrrrxi@gmail.com>2019-06-23 14:51:33 +0200
committerhrxi <hrrrxi@gmail.com>2019-07-09 11:21:40 +0200
commit82e7cf4447d72c24af04c64c05eed35338455f35 (patch)
tree3766c40dc3e030aa6a2c092545af17e5c998aa9c /libdino/src/service
parent877c46628fa2836f9226e24a3d0a84b9a3f821e6 (diff)
downloaddino-82e7cf4447d72c24af04c64c05eed35338455f35.tar.gz
dino-82e7cf4447d72c24af04c64c05eed35338455f35.zip
Add file receiving via Jingle
This currently follows the same rules as HTTP file download for accepting files.
Diffstat (limited to 'libdino/src/service')
-rw-r--r--libdino/src/service/file_manager.vala6
-rw-r--r--libdino/src/service/jingle_file_manager.vala71
2 files changed, 74 insertions, 3 deletions
diff --git a/libdino/src/service/file_manager.vala b/libdino/src/service/file_manager.vala
index 9873539a..049239f7 100644
--- a/libdino/src/service/file_manager.vala
+++ b/libdino/src/service/file_manager.vala
@@ -65,8 +65,8 @@ public class FileManager : StreamInteractionModule, Object {
foreach (FileSender file_sender in file_senders) {
if (file_sender.can_send(conversation, file_transfer)) {
- // TODO(hrxi): Currently, this tries to send the file with every transfer available, but it should probably only select one.
file_sender.send_file(conversation, file_transfer);
+ return;
}
}
received_file(file_transfer, conversation);
@@ -121,7 +121,9 @@ public class FileManager : StreamInteractionModule, Object {
}
public void add_sender(FileSender file_sender) {
- file_senders.add(file_sender);
+ // Order file_senders in reverse order of adding them -- HTTP is added
+ // later than Jingle.
+ file_senders.insert(0, file_sender);
file_sender.upload_available.connect((account) => {
upload_available(account);
});
diff --git a/libdino/src/service/jingle_file_manager.vala b/libdino/src/service/jingle_file_manager.vala
index 055f0758..bd470f0b 100644
--- a/libdino/src/service/jingle_file_manager.vala
+++ b/libdino/src/service/jingle_file_manager.vala
@@ -6,11 +6,13 @@ using Dino.Entities;
namespace Dino {
-public class JingleFileManager : StreamInteractionModule, FileSender, Object {
+public class JingleFileManager : StreamInteractionModule, FileProvider, FileSender, Object {
public static ModuleIdentity<JingleFileManager> IDENTITY = new ModuleIdentity<JingleFileManager>("jingle_files");
public string id { get { return IDENTITY.id; } }
private StreamInteractor stream_interactor;
+ private HashMap<string, Xmpp.Xep.JingleFileTransfer.FileTransfer> file_transfers
+ = new HashMap<string, Xmpp.Xep.JingleFileTransfer.FileTransfer>();
public static void start(StreamInteractor stream_interactor) {
JingleFileManager m = new JingleFileManager(stream_interactor);
@@ -21,6 +23,73 @@ public class JingleFileManager : StreamInteractionModule, FileSender, Object {
this.stream_interactor = stream_interactor;
stream_interactor.get_module(FileManager.IDENTITY).add_sender(this);
+ stream_interactor.get_module(FileManager.IDENTITY).add_provider(this);
+ stream_interactor.stream_negotiated.connect(on_stream_negotiated);
+ }
+
+ private void on_stream_negotiated(Account account, XmppStream stream) {
+ stream_interactor.module_manager.get_module(account, Xmpp.Xep.JingleFileTransfer.Module.IDENTITY).file_incoming.connect((stream, jingle_file_transfer) => {
+ Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(jingle_file_transfer.peer.bare_jid, account);
+ if (conversation == null) {
+ // TODO(hrxi): What to do?
+ return;
+ }
+ string id = random_uuid();
+
+ FileTransfer file_transfer = new FileTransfer();
+ file_transfer.account = account;
+ file_transfer.counterpart = jingle_file_transfer.peer.bare_jid;
+ file_transfer.ourpart = account.bare_jid;
+ file_transfer.encryption = Encryption.NONE;
+ file_transfer.time = new DateTime.now_utc();
+ file_transfer.local_time = new DateTime.now_utc();
+ file_transfer.direction = FileTransfer.DIRECTION_RECEIVED;
+ file_transfer.file_name = jingle_file_transfer.file_name;
+ file_transfer.size = (int)jingle_file_transfer.size; // TODO(hrxi): remove cast
+ file_transfer.state = FileTransfer.State.NOT_STARTED;
+ file_transfer.provider = 0; // TODO(hrxi): what is this?
+ file_transfer.info = id;
+ file_transfers[id] = jingle_file_transfer;
+
+ file_incoming(file_transfer, conversation);
+ });
+ }
+
+ async void get_meta_info(FileTransfer file_transfer) {
+ // TODO(hrxi): what is this function?
+ }
+ async void download(FileTransfer file_transfer, File file_) {
+ // TODO(hrxi) What should happen if `stream == null`?
+ XmppStream? stream = stream_interactor.get_stream(file_transfer.account);
+ Xmpp.Xep.JingleFileTransfer.FileTransfer jingle_file_transfer = file_transfers[file_transfer.info];
+ jingle_file_transfer.accept(stream);
+ file_transfer.input_stream = jingle_file_transfer.stream;
+
+ // TODO(hrxi): BEGIN: Copied from plugins/http-files/src/file_provider.vala
+ foreach (IncomingFileProcessor processor in stream_interactor.get_module(FileManager.IDENTITY).incoming_processors) {
+ if (processor.can_process(file_transfer)) {
+ processor.process(file_transfer);
+ }
+ }
+
+ // TODO(hrxi): should this be an &&?
+ File file = file_;
+ if (file_transfer.encryption == Encryption.PGP || file.get_path().has_suffix(".pgp")) {
+ file = File.new_for_path(file.get_path().substring(0, file.get_path().length - 4));
+ }
+ // TODO(hrxi): END: Copied from plugins/http-files/src/file_provider.vala
+
+ try {
+ OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION);
+ yield os.splice_async(file_transfer.input_stream, OutputStreamSpliceFlags.CLOSE_SOURCE|OutputStreamSpliceFlags.CLOSE_TARGET);
+ file_transfer.path = file.get_basename();
+ file_transfer.input_stream = yield file.read_async();
+
+ file_transfer.state = FileTransfer.State.COMPLETE;
+ } catch (Error e) {
+ file_transfer.state = FileTransfer.State.FAILED;
+ return;
+ }
}
public bool is_upload_available(Conversation conversation) {