summaryrefslogtreecommitdiff
path: root/src/upload.c
diff options
context:
space:
mode:
authorGravatar Alan F <ajf@eth0.org.uk>2014-03-10 09:10:24 +0000
committerGravatar Alan F <ajf@eth0.org.uk>2014-03-10 09:10:24 +0000
commit1a5447c0138fd25e3f6d78f78593f5b70be5614a (patch)
tree9468304a4688a63788363bcf2457b1767aa513a9 /src/upload.c
parentdd0c7d8317dd199b6dd9d612ebb863c4da6cf242 (diff)
parent118b702bec50d9872699357a379413f69e9b176e (diff)
Merge branch 'rss-viewer'. This is the biggest feature I've attempted to add for quite a long time. Quite a lot of refactoring of the HTTP client and threadpool has been done (to make it not send session tokens etc), uploads (was too much code duplication), and the add dialog was done to implement this. Don't expect this to be some fully automatable RSS client, use something like flexget for that, this is just a simple view on RSS feeds. It uses the rss-glib library, which you will probably need to install yourself to optionally use this feature. There are probably bugs still, so report them.
Diffstat (limited to 'src/upload.c')
-rw-r--r--src/upload.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/upload.c b/src/upload.c
new file mode 100644
index 0000000..2f16c51
--- /dev/null
+++ b/src/upload.c
@@ -0,0 +1,103 @@
+#include "protocol-constants.h"
+#include "requests.h"
+#include "trg-client.h"
+#include "util.h"
+#include "trg-main-window.h"
+#include "json.h"
+#include "upload.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);
+ g_free(upload->uid);
+ g_free(upload->file_wanted);
+ g_free(upload->file_priorities);
+ trg_response_free(upload->upload_response);
+ g_free(upload);
+}
+
+static void add_priorities(JsonObject *args, gint* priorities, gint n_files)
+{
+ gint i;
+ for (i = 0; i < n_files; i++) {
+ gint priority = priorities[i];
+ if (priority == TR_PRI_LOW)
+ add_file_id_to_array(args, FIELD_FILES_PRIORITY_LOW, i);
+ else if (priority == TR_PRI_HIGH)
+ add_file_id_to_array(args, FIELD_FILES_PRIORITY_HIGH, i);
+ else
+ add_file_id_to_array(args, FIELD_FILES_PRIORITY_NORMAL, i);
+ }
+}
+
+static void add_wanteds(JsonObject *args, gint* wanteds, gint n_files) {
+ gint i;
+ for (i = 0; i < n_files; i++) {
+ if (wanteds[i])
+ add_file_id_to_array(args, FIELD_FILES_WANTED, i);
+ else
+ add_file_id_to_array(args, FIELD_FILES_UNWANTED, i);
+ }
+}
+
+static void next_upload(trg_upload *upload) {
+ JsonNode *req = NULL;
+
+ if (upload->upload_response && upload->progress_index < 1)
+ req = torrent_add_from_response(upload->upload_response, upload->flags);
+ else if (upload->list && upload->progress_index < g_slist_length(upload->list))
+ req = torrent_add_from_file((gchar*)g_slist_nth_data(upload->list, upload->progress_index), upload->flags);
+
+ if (req) {
+ JsonObject *args = node_get_arguments(req);
+
+ if (upload->extra_args)
+ add_set_common_args(args, upload->priority, upload->dir);
+
+ if (upload->file_wanted)
+ add_wanteds(args, upload->file_wanted, upload->n_files);
+
+ if (upload->file_priorities)
+ add_priorities(args, upload->file_priorities, upload->n_files);
+
+ 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;
+
+ if (upload->callback)
+ upload->callback(data);
+
+ /* the callback we're delegating to will destroy the response */
+
+ if (upload->main_window)
+ on_generic_interactive_action(upload->main_window, response);
+ else
+ trg_response_free(response);
+
+ next_upload(upload);
+
+ return FALSE;
+}
+
+void trg_do_upload(trg_upload *upload)
+{
+ next_upload(upload);
+}