1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*
* 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 <glib-object.h>
#include <gtk/gtk.h>
#include <json-glib/json-glib.h>
#include "torrent.h"
#include "protocol-constants.h"
JsonArray *torrent_get_peers(JsonObject * t)
{
return json_object_get_array_member(t, FIELD_PEERS);
}
JsonArray *torrent_get_wanted(JsonObject * t)
{
return json_object_get_array_member(t, FIELD_WANTED);
}
JsonArray *torrent_get_priorities(JsonObject * t)
{
return json_object_get_array_member(t, FIELD_PRIORITIES);
}
JsonArray *torrent_get_trackers(JsonObject * t)
{
return json_object_get_array_member(t, FIELD_TRACKERS);
}
gint64 torrent_get_id(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_ID);
}
const gchar *torrent_get_download_dir(JsonObject * t)
{
return json_object_get_string_member(t, FIELD_DOWNLOAD_DIR);
}
const gchar *torrent_get_name(JsonObject * t)
{
return json_object_get_string_member(t, FIELD_NAME);
}
gboolean torrent_get_honors_session_limits(JsonObject * t)
{
return json_object_get_boolean_member(t, FIELD_HONORS_SESSION_LIMITS);
}
gint64 torrent_get_bandwidth_priority(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_BANDWIDTH_PRIORITY);
}
gint64 torrent_get_upload_limit(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_UPLOAD_LIMIT);
}
gint64 torrent_get_peer_limit(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_PEER_LIMIT);
}
gboolean torrent_get_upload_limited(JsonObject * t)
{
return json_object_get_boolean_member(t, FIELD_UPLOAD_LIMITED);
}
gint64 torrent_get_seed_ratio_mode(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_SEED_RATIO_MODE);
}
gdouble torrent_get_seed_ratio_limit(JsonObject * t)
{
return json_object_get_double_member(t, FIELD_SEED_RATIO_LIMIT);
}
gint64 torrent_get_download_limit(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_DOWNLOAD_LIMIT);
}
gboolean torrent_get_download_limited(JsonObject * t)
{
return json_object_get_boolean_member(t, FIELD_DOWNLOAD_LIMITED);
}
gint64 torrent_get_size(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_SIZEWHENDONE);
}
gint64 torrent_get_rate_down(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_RATEDOWNLOAD);
}
gint64 torrent_get_rate_up(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_RATEUPLOAD);
}
gint64 torrent_get_eta(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_ETA);
}
gint64 torrent_get_uploaded(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_UPLOADEDEVER);
}
gint64 torrent_get_have_valid(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_HAVEVALID);
}
gint64 torrent_get_have_unchecked(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_HAVEUNCHECKED);
}
gint64 torrent_get_downloaded(JsonObject * t)
{
return torrent_get_have_valid(t) + torrent_get_have_unchecked(t);
}
gint64 torrent_get_status(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_STATUS);
}
gboolean torrent_get_is_finished(JsonObject * t)
{
return torrent_get_left_until_done(t) <= 0;
}
gdouble torrent_get_percent_done(JsonObject * t)
{
JsonNode *percentDone = json_object_get_member(t, FIELD_PERCENTDONE);
GValue a = { 0 };
json_node_get_value(percentDone, &a);
switch (G_VALUE_TYPE(&a)) {
case G_TYPE_INT64:
return (gdouble) g_value_get_int64(&a) * 100.0;
case G_TYPE_DOUBLE:
return g_value_get_double(&a) * 100.0;
default:
return 0.0;
}
}
gchar *torrent_get_status_icon(guint flags)
{
if (flags & TORRENT_FLAG_ERROR)
return g_strdup(GTK_STOCK_DIALOG_WARNING);
else if (flags & TORRENT_FLAG_DOWNLOADING)
return g_strdup(GTK_STOCK_GO_DOWN);
else if (flags & TORRENT_FLAG_PAUSED)
return g_strdup(GTK_STOCK_MEDIA_PAUSE);
else if (flags & TORRENT_FLAG_SEEDING)
return g_strdup(GTK_STOCK_GO_UP);
else if (flags & TORRENT_FLAG_CHECKING)
return g_strdup(GTK_STOCK_REFRESH);
else
return g_strdup(GTK_STOCK_DIALOG_QUESTION);
}
const gchar *torrent_get_errorstr(JsonObject * t)
{
return json_object_get_string_member(t, FIELD_ERRORSTR);
}
gchar *torrent_get_status_string(gint64 value)
{
switch (value) {
case STATUS_DOWNLOADING:
return g_strdup("Downloading");
case STATUS_PAUSED:
return g_strdup("Paused");
case STATUS_SEEDING:
return g_strdup("Seeding");
case STATUS_CHECKING:
return g_strdup("Checking");
case STATUS_WAITING_TO_CHECK:
return g_strdup("Waiting To Check");
default:
return g_strdup("Unknown");
}
}
gint64 tracker_get_id(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_ID);
}
gint64 tracker_get_tier(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_TIER);
}
gint64 torrent_get_left_until_done(JsonObject * t)
{
return json_object_get_int_member(t, FIELD_LEFTUNTILDONE);
}
const gchar *tracker_get_announce(JsonObject * t)
{
return json_object_get_string_member(t, FIELD_ANNOUNCE);
}
const gchar *tracker_get_scrape(JsonObject * t)
{
return json_object_get_string_member(t, FIELD_SCRAPE);
}
JsonArray *get_torrents(JsonObject * response)
{
return json_object_get_array_member(response, FIELD_TORRENTS);
}
JsonArray *torrent_get_files(JsonObject * args)
{
return json_object_get_array_member(args, FIELD_FILES);
}
|