summaryrefslogtreecommitdiff
path: root/src/upload.c
diff options
context:
space:
mode:
authorGravatar Alan F <ajf@eth0.org.uk>2014-02-19 08:54:10 +0000
committerGravatar Alan F <ajf@eth0.org.uk>2014-02-19 08:54:10 +0000
commit1d6f77d4c4fafd0a9cafddd2797249557e601dba (patch)
tree33dee51882a0475aff63ec6c6d9095c92c9ac5d4 /src/upload.c
parentadf06453574270bf467b5f37e632c04c153ee90a (diff)
refactor the torrent upload code (it's called from a few places so is probably worth it), and make it easier to use a HTTP response as the input.
Diffstat (limited to 'src/upload.c')
-rw-r--r--src/upload.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/upload.c b/src/upload.c
new file mode 100644
index 0000000..5bac869
--- /dev/null
+++ b/src/upload.c
@@ -0,0 +1,62 @@
+#include "protocol-constants.h"
+#include "requests.h"
+#include "trg-client.h"
+#include "upload.h"
+#include "util.h"
+#include "trg-main-window.h"
+
+static gboolean upload_complete_callback(gpointer data);
+static void next_upload(trg_upload *upload);
+
+static void
+add_set_common_args(JsonObject * args, gint priority, gchar * dir)
+{
+ json_object_set_string_member(args, FIELD_FILE_DOWNLOAD_DIR, dir);
+ json_object_set_int_member(args, FIELD_BANDWIDTH_PRIORITY,
+ (gint64) priority);
+}
+
+void trg_upload_free(trg_upload *upload) {
+ g_str_slist_free(upload->list);
+ g_free(upload->dir);
+ trg_response_free(upload->upload_response);
+ g_free(upload);
+}
+
+static void next_upload(trg_upload *upload) {
+ if (upload->upload_response && upload->progress_index < 1) {
+ JsonNode *req = torrent_add_from_response(upload->upload_response, 0);
+ /*JsonObject *args =
+ if (upload->extra_args)
+ add_set_common_args()*/
+ upload->progress_index++;
+ dispatch_async(upload->client, req, upload_complete_callback, upload);
+ } else if (upload->list && upload->progress_index < g_slist_length(upload->list)) {
+ JsonNode *req = torrent_add_from_file((gchar*)g_slist_nth_data(upload->list, upload->progress_index), 0);
+ upload->progress_index++;
+ dispatch_async(upload->client, req, upload_complete_callback, upload);
+ } else {
+ trg_upload_free(upload);
+ }
+}
+
+static gboolean upload_complete_callback(gpointer data) {
+ trg_response *response = (trg_response*)data;
+ trg_upload *upload = (trg_upload*)response->cb_data;
+
+ next_upload(upload);
+
+ /* the callback we're delegating to will destroy the response */
+
+ if (upload->main_window)
+ on_generic_interactive_action(upload->main_window, response);
+ else /* otherwise need to do it here */
+ trg_response_free(response);
+
+ return FALSE;
+}
+
+void trg_do_upload(trg_upload *upload)
+{
+ next_upload(upload);
+}