/* * transmission-remote-gtk - A GTK RPC client to Transmission * Copyright (C) 2011 Alan Fitton * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include #include "protocol-constants.h" #include "base64.h" #include "json.h" #include "requests.h" static JsonNode *base_request(gchar * method); JsonNode *generic_request(gchar * method, JsonArray * ids) { JsonNode *root = base_request(method); if (ids != NULL) json_object_set_array_member(node_get_arguments(root), PARAM_IDS, ids); return root; } JsonNode *session_stats(void) { return generic_request(METHOD_SESSION_STATS, NULL); } JsonNode *blocklist_update(void) { return generic_request(METHOD_BLOCKLIST_UPDATE, NULL); } JsonNode *port_test(void) { return generic_request(METHOD_PORT_TEST, NULL); } JsonNode *session_get(void) { return generic_request(METHOD_SESSION_GET, NULL); } JsonNode *torrent_set_location(JsonArray * array, gchar * location, gboolean move) { JsonNode *req = generic_request(METHOD_TORRENT_SET_LOCATION, array); JsonObject *args = node_get_arguments(req); json_object_set_boolean_member(args, FIELD_MOVE, move); json_object_set_string_member(args, FIELD_LOCATION, location); return req; } JsonNode *torrent_start(JsonArray * array) { return generic_request(METHOD_TORRENT_START, array); } JsonNode *torrent_pause(JsonArray * array) { return generic_request(METHOD_TORRENT_STOP, array); } JsonNode *torrent_reannounce(JsonArray * array) { return generic_request(METHOD_TORRENT_REANNOUNCE, array); } JsonNode *torrent_verify(JsonArray * array) { return generic_request(METHOD_TORRENT_VERIFY, array); } JsonNode *session_set(void) { return generic_request(METHOD_SESSION_SET, NULL); } JsonNode *torrent_set(JsonArray * array) { return generic_request(METHOD_TORRENT_SET, array); } JsonNode *torrent_remove(JsonArray * array, gboolean removeData) { JsonNode *root = base_request(METHOD_TORRENT_REMOVE); JsonObject *args = node_get_arguments(root); json_object_set_array_member(args, PARAM_IDS, array); json_object_set_boolean_member(args, PARAM_DELETE_LOCAL_DATA, removeData); return root; } JsonNode *torrent_get(gint64 id) { JsonNode *root = base_request(METHOD_TORRENT_GET); JsonObject *args = node_get_arguments(root); JsonArray *fields = json_array_new(); if (id == -2) { json_object_set_string_member(args, PARAM_IDS, FIELD_RECENTLY_ACTIVE); } else if (id >= 0) { JsonArray *ids = json_array_new(); json_array_add_int_element(ids, id); json_object_set_array_member(args, PARAM_IDS, ids); } json_array_add_string_element(fields, FIELD_ETA); json_array_add_string_element(fields, FIELD_PEERS); json_array_add_string_element(fields, FIELD_FILES); json_array_add_string_element(fields, FIELD_HAVEVALID); json_array_add_string_element(fields, FIELD_HAVEUNCHECKED); json_array_add_string_element(fields, FIELD_RATEUPLOAD); json_array_add_string_element(fields, FIELD_RATEDOWNLOAD); json_array_add_string_element(fields, FIELD_STATUS); json_array_add_string_element(fields, FIELD_ADDED_DATE); json_array_add_string_element(fields, FIELD_UPLOADEDEVER); json_array_add_string_element(fields, FIELD_SIZEWHENDONE); json_array_add_string_element(fields, FIELD_ID); json_array_add_string_element(fields, FIELD_NAME); json_array_add_string_element(fields, FIELD_PERCENTDONE); json_array_add_string_element(fields, FIELD_COMMENT); json_array_add_string_element(fields, FIELD_TOTAL_SIZE); json_array_add_string_element(fields, FIELD_LEFT_UNTIL_DONE); json_array_add_string_element(fields, FIELD_ANNOUNCE_URL); json_array_add_string_element(fields, FIELD_ERROR_STRING); json_array_add_string_element(fields, FIELD_SWARM_SPEED); json_array_add_string_element(fields, FIELD_TRACKERS); json_array_add_string_element(fields, FIELD_DOWNLOAD_DIR); json_array_add_string_element(fields, FIELD_HASH_STRING); json_array_add_string_element(fields, FIELD_DONE_DATE); json_array_add_string_element(fields, FIELD_HONORS_SESSION_LIMITS); json_array_add_string_element(fields, FIELD_UPLOAD_LIMIT); json_array_add_string_element(fields, FIELD_UPLOAD_LIMITED); json_array_add_string_element(fields, FIELD_DOWNLOAD_LIMIT); json_array_add_string_element(fields, FIELD_DOWNLOAD_LIMITED); json_array_add_string_element(fields, FIELD_BANDWIDTH_PRIORITY); json_array_add_string_element(fields, FIELD_SEED_RATIO_LIMIT); json_array_add_string_element(fields, FIELD_SEED_RATIO_MODE); json_array_add_string_element(fields, FIELD_PEER_LIMIT); json_array_add_string_element(fields, FIELD_ERRORSTR); json_array_add_string_element(fields, FIELD_WANTED); json_array_add_string_element(fields, FIELD_PRIORITIES); json_object_set_array_member(args, PARAM_FIELDS, fields); return root; } JsonNode *torrent_add_url(const gchar * url, gboolean paused) { JsonNode *root = base_request(METHOD_TORRENT_ADD); JsonObject *args = node_get_arguments(root); json_object_set_string_member(args, PARAM_FILENAME, url); json_object_set_boolean_member(args, PARAM_PAUSED, paused); return root; } JsonNode *torrent_add(gchar * filename, gboolean paused) { JsonNode *root = base_request(METHOD_TORRENT_ADD); JsonObject *args = node_get_arguments(root); gchar *encodedFile = base64encode(filename); if (encodedFile) json_object_set_string_member(args, PARAM_METAINFO, encodedFile); json_object_set_boolean_member(args, PARAM_PAUSED, paused); g_free(encodedFile); return root; } static JsonNode *base_request(gchar * method) { JsonNode *root = json_node_new(JSON_NODE_OBJECT); JsonObject *object = json_object_new(); JsonObject *args = json_object_new(); json_object_set_string_member(object, PARAM_METHOD, method); json_object_set_object_member(object, PARAM_ARGUMENTS, args); json_node_take_object(root, object); return root; } void request_set_tag(JsonNode * req, gint64 tag) { json_object_set_int_member(json_node_get_object(req), PARAM_TAG, tag); } void request_set_tag_from_ids(JsonNode * req, JsonArray * ids) { gint64 id = json_array_get_length(ids) == 1 ? json_array_get_int_element(ids, 0) : -1; request_set_tag(req, id); }