1 | /****************************************************************************** |
---|
2 | * $Id: tr-core.c 10108 2010-02-06 05:21:25Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <string.h> /* strcmp, strlen */ |
---|
26 | |
---|
27 | #include <gtk/gtk.h> |
---|
28 | #include <glib/gi18n.h> |
---|
29 | #ifdef HAVE_GIO |
---|
30 | #include <gio/gio.h> |
---|
31 | #endif |
---|
32 | #ifdef HAVE_DBUS_GLIB |
---|
33 | #include <dbus/dbus-glib.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | #include <libtransmission/transmission.h> |
---|
37 | #include <libtransmission/bencode.h> |
---|
38 | #include <libtransmission/rpcimpl.h> |
---|
39 | #include <libtransmission/json.h> |
---|
40 | #include <libtransmission/utils.h> /* tr_free */ |
---|
41 | #include <libtransmission/web.h> |
---|
42 | |
---|
43 | #include "conf.h" |
---|
44 | #include "notify.h" |
---|
45 | #include "tr-core.h" |
---|
46 | #ifdef HAVE_DBUS_GLIB |
---|
47 | #include "tr-core-dbus.h" |
---|
48 | #endif |
---|
49 | #include "tr-prefs.h" |
---|
50 | #include "tr-torrent.h" |
---|
51 | #include "util.h" |
---|
52 | #include "actions.h" |
---|
53 | |
---|
54 | static void maybeInhibitHibernation( TrCore * core ); |
---|
55 | |
---|
56 | static gboolean our_instance_adds_remote_torrents = FALSE; |
---|
57 | |
---|
58 | struct TrCorePrivate |
---|
59 | { |
---|
60 | #ifdef HAVE_GIO |
---|
61 | GFileMonitor * monitor; |
---|
62 | gulong monitor_tag; |
---|
63 | char * monitor_path; |
---|
64 | GSList * monitor_files; |
---|
65 | guint monitor_idle_tag; |
---|
66 | #endif |
---|
67 | gboolean adding_from_watch_dir; |
---|
68 | gboolean inhibit_allowed; |
---|
69 | gboolean have_inhibit_cookie; |
---|
70 | gboolean dbus_error; |
---|
71 | guint inhibit_cookie; |
---|
72 | GtkTreeModel * model; |
---|
73 | tr_session * session; |
---|
74 | }; |
---|
75 | |
---|
76 | static int |
---|
77 | isDisposed( const TrCore * core ) |
---|
78 | { |
---|
79 | return !core || !core->priv; |
---|
80 | } |
---|
81 | |
---|
82 | static void |
---|
83 | tr_core_dispose( GObject * obj ) |
---|
84 | { |
---|
85 | TrCore * core = TR_CORE( obj ); |
---|
86 | |
---|
87 | if( !isDisposed( core ) ) |
---|
88 | { |
---|
89 | GObjectClass * parent; |
---|
90 | |
---|
91 | core->priv = NULL; |
---|
92 | |
---|
93 | parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) ); |
---|
94 | parent->dispose( obj ); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | static void |
---|
99 | tr_core_class_init( gpointer g_class, |
---|
100 | gpointer g_class_data UNUSED ) |
---|
101 | { |
---|
102 | GObjectClass * gobject_class; |
---|
103 | TrCoreClass * cc; |
---|
104 | |
---|
105 | g_type_class_add_private( g_class, sizeof( struct TrCorePrivate ) ); |
---|
106 | |
---|
107 | gobject_class = G_OBJECT_CLASS( g_class ); |
---|
108 | gobject_class->dispose = tr_core_dispose; |
---|
109 | |
---|
110 | cc = TR_CORE_CLASS( g_class ); |
---|
111 | |
---|
112 | cc->blocklistSignal = g_signal_new( "blocklist-updated", /* name */ |
---|
113 | G_TYPE_FROM_CLASS( g_class ), /* applies to TrCore */ |
---|
114 | G_SIGNAL_RUN_FIRST, /* when to invoke */ |
---|
115 | 0, NULL, NULL, /* accumulator */ |
---|
116 | g_cclosure_marshal_VOID__INT, /* marshaler */ |
---|
117 | G_TYPE_NONE, /* return type */ |
---|
118 | 1, G_TYPE_INT ); /* signal arguments */ |
---|
119 | |
---|
120 | cc->portSignal = g_signal_new( "port-tested", |
---|
121 | G_TYPE_FROM_CLASS( g_class ), |
---|
122 | G_SIGNAL_RUN_LAST, |
---|
123 | 0, NULL, NULL, |
---|
124 | g_cclosure_marshal_VOID__BOOLEAN, |
---|
125 | G_TYPE_NONE, |
---|
126 | 1, G_TYPE_BOOLEAN ); |
---|
127 | |
---|
128 | cc->errsig = g_signal_new( "error", |
---|
129 | G_TYPE_FROM_CLASS( g_class ), |
---|
130 | G_SIGNAL_RUN_LAST, |
---|
131 | 0, NULL, NULL, |
---|
132 | g_cclosure_marshal_VOID__UINT_POINTER, |
---|
133 | G_TYPE_NONE, |
---|
134 | 2, G_TYPE_UINT, G_TYPE_POINTER ); |
---|
135 | |
---|
136 | cc->promptsig = g_signal_new( "add-torrent-prompt", |
---|
137 | G_TYPE_FROM_CLASS( g_class ), |
---|
138 | G_SIGNAL_RUN_LAST, |
---|
139 | 0, NULL, NULL, |
---|
140 | g_cclosure_marshal_VOID__POINTER, |
---|
141 | G_TYPE_NONE, |
---|
142 | 1, G_TYPE_POINTER ); |
---|
143 | |
---|
144 | cc->quitsig = g_signal_new( "quit", |
---|
145 | G_TYPE_FROM_CLASS( g_class ), |
---|
146 | G_SIGNAL_RUN_LAST, |
---|
147 | 0, NULL, NULL, |
---|
148 | g_cclosure_marshal_VOID__VOID, |
---|
149 | G_TYPE_NONE, |
---|
150 | 0 ); |
---|
151 | |
---|
152 | cc->prefsig = g_signal_new( "prefs-changed", |
---|
153 | G_TYPE_FROM_CLASS( g_class ), |
---|
154 | G_SIGNAL_RUN_LAST, |
---|
155 | 0, NULL, NULL, |
---|
156 | g_cclosure_marshal_VOID__STRING, |
---|
157 | G_TYPE_NONE, |
---|
158 | 1, G_TYPE_STRING ); |
---|
159 | |
---|
160 | #ifdef HAVE_DBUS_GLIB |
---|
161 | { |
---|
162 | DBusGConnection * bus = dbus_g_bus_get( DBUS_BUS_SESSION, NULL ); |
---|
163 | DBusGProxy * bus_proxy = NULL; |
---|
164 | if( bus ) |
---|
165 | bus_proxy = |
---|
166 | dbus_g_proxy_new_for_name( bus, "org.freedesktop.DBus", |
---|
167 | "/org/freedesktop/DBus", |
---|
168 | "org.freedesktop.DBus" ); |
---|
169 | if( bus_proxy ) |
---|
170 | { |
---|
171 | int result = 0; |
---|
172 | dbus_g_proxy_call( bus_proxy, "RequestName", NULL, |
---|
173 | G_TYPE_STRING, |
---|
174 | "com.transmissionbt.Transmission", |
---|
175 | G_TYPE_UINT, 0, |
---|
176 | G_TYPE_INVALID, |
---|
177 | G_TYPE_UINT, &result, |
---|
178 | G_TYPE_INVALID ); |
---|
179 | if( ( our_instance_adds_remote_torrents = result == 1 ) ) |
---|
180 | dbus_g_object_type_install_info( |
---|
181 | TR_CORE_TYPE, |
---|
182 | & |
---|
183 | dbus_glib_tr_core_object_info ); |
---|
184 | } |
---|
185 | } |
---|
186 | #endif |
---|
187 | } |
---|
188 | |
---|
189 | /*** |
---|
190 | **** SORTING |
---|
191 | ***/ |
---|
192 | |
---|
193 | static gboolean |
---|
194 | isValidETA( int t ) |
---|
195 | { |
---|
196 | return ( t != TR_ETA_NOT_AVAIL ) && ( t != TR_ETA_UNKNOWN ); |
---|
197 | } |
---|
198 | |
---|
199 | static int |
---|
200 | compareETA( int a, int b ) |
---|
201 | { |
---|
202 | const gboolean a_valid = isValidETA( a ); |
---|
203 | const gboolean b_valid = isValidETA( b ); |
---|
204 | |
---|
205 | if( !a_valid && !b_valid ) return 0; |
---|
206 | if( !a_valid ) return -1; |
---|
207 | if( !b_valid ) return 1; |
---|
208 | return a < b ? 1 : -1; |
---|
209 | } |
---|
210 | |
---|
211 | static int |
---|
212 | compareDouble( double a, double b ) |
---|
213 | { |
---|
214 | if( a < b ) return -1; |
---|
215 | if( a > b ) return 1; |
---|
216 | return 0; |
---|
217 | } |
---|
218 | |
---|
219 | static int |
---|
220 | compareRatio( double a, double b ) |
---|
221 | { |
---|
222 | if( (int)a == TR_RATIO_INF && (int)b == TR_RATIO_INF ) return 0; |
---|
223 | if( (int)a == TR_RATIO_INF ) return 1; |
---|
224 | if( (int)b == TR_RATIO_INF ) return -1; |
---|
225 | return compareDouble( a, b ); |
---|
226 | } |
---|
227 | |
---|
228 | static int |
---|
229 | compareTime( time_t a, time_t b ) |
---|
230 | { |
---|
231 | if( a < b ) return -1; |
---|
232 | if( a > b ) return 1; |
---|
233 | return 0; |
---|
234 | } |
---|
235 | |
---|
236 | static int |
---|
237 | compareByRatio( GtkTreeModel * model, |
---|
238 | GtkTreeIter * a, |
---|
239 | GtkTreeIter * b, |
---|
240 | gpointer user_data UNUSED ) |
---|
241 | { |
---|
242 | tr_torrent *ta, *tb; |
---|
243 | const tr_stat *sa, *sb; |
---|
244 | |
---|
245 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
246 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
247 | |
---|
248 | sa = tr_torrentStatCached( ta ); |
---|
249 | sb = tr_torrentStatCached( tb ); |
---|
250 | |
---|
251 | return compareRatio( sa->ratio, sb->ratio ); |
---|
252 | } |
---|
253 | |
---|
254 | static int |
---|
255 | compareByActivity( GtkTreeModel * model, |
---|
256 | GtkTreeIter * a, |
---|
257 | GtkTreeIter * b, |
---|
258 | gpointer user_data UNUSED ) |
---|
259 | { |
---|
260 | int i; |
---|
261 | tr_torrent *ta, *tb; |
---|
262 | const tr_stat *sa, *sb; |
---|
263 | double aUp, aDown, bUp, bDown; |
---|
264 | |
---|
265 | gtk_tree_model_get( model, a, MC_SPEED_UP, &aUp, |
---|
266 | MC_SPEED_DOWN, &aDown, |
---|
267 | MC_TORRENT_RAW, &ta, |
---|
268 | -1 ); |
---|
269 | gtk_tree_model_get( model, b, MC_SPEED_UP, &bUp, |
---|
270 | MC_SPEED_DOWN, &bDown, |
---|
271 | MC_TORRENT_RAW, &tb, |
---|
272 | -1 ); |
---|
273 | |
---|
274 | if(( i = compareDouble( aUp+aDown, bUp+bDown ))) |
---|
275 | return i; |
---|
276 | |
---|
277 | sa = tr_torrentStatCached( ta ); |
---|
278 | sb = tr_torrentStatCached( tb ); |
---|
279 | if( sa->uploadedEver != sb->uploadedEver ) |
---|
280 | return sa->uploadedEver < sa->uploadedEver ? -1 : 1; |
---|
281 | |
---|
282 | return 0; |
---|
283 | } |
---|
284 | |
---|
285 | static int |
---|
286 | compareByName( GtkTreeModel * model, |
---|
287 | GtkTreeIter * a, |
---|
288 | GtkTreeIter * b, |
---|
289 | gpointer user_data UNUSED ) |
---|
290 | { |
---|
291 | int ret; |
---|
292 | char *ca, *cb; |
---|
293 | |
---|
294 | gtk_tree_model_get( model, a, MC_NAME_COLLATED, &ca, -1 ); |
---|
295 | gtk_tree_model_get( model, b, MC_NAME_COLLATED, &cb, -1 ); |
---|
296 | ret = strcmp( ca, cb ); |
---|
297 | g_free( cb ); |
---|
298 | g_free( ca ); |
---|
299 | return ret; |
---|
300 | } |
---|
301 | |
---|
302 | static int |
---|
303 | compareByAge( GtkTreeModel * model, |
---|
304 | GtkTreeIter * a, |
---|
305 | GtkTreeIter * b, |
---|
306 | gpointer user_data UNUSED ) |
---|
307 | { |
---|
308 | tr_torrent *ta, *tb; |
---|
309 | |
---|
310 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
311 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
312 | return compareTime( tr_torrentStatCached( ta )->addedDate, |
---|
313 | tr_torrentStatCached( tb )->addedDate ); |
---|
314 | } |
---|
315 | |
---|
316 | static int |
---|
317 | compareBySize( GtkTreeModel * model, |
---|
318 | GtkTreeIter * a, |
---|
319 | GtkTreeIter * b, |
---|
320 | gpointer user_data UNUSED ) |
---|
321 | { |
---|
322 | tr_torrent *t; |
---|
323 | const tr_info *ia, *ib; |
---|
324 | |
---|
325 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &t, -1 ); |
---|
326 | ia = tr_torrentInfo( t ); |
---|
327 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &t, -1 ); |
---|
328 | ib = tr_torrentInfo( t ); |
---|
329 | |
---|
330 | if( ia->totalSize < ib->totalSize ) return 1; |
---|
331 | if( ia->totalSize > ib->totalSize ) return -1; |
---|
332 | return 0; |
---|
333 | } |
---|
334 | |
---|
335 | static int |
---|
336 | compareByProgress( GtkTreeModel * model, |
---|
337 | GtkTreeIter * a, |
---|
338 | GtkTreeIter * b, |
---|
339 | gpointer user_data UNUSED ) |
---|
340 | { |
---|
341 | int ret; |
---|
342 | tr_torrent * t; |
---|
343 | const tr_stat *sa, *sb; |
---|
344 | |
---|
345 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &t, -1 ); |
---|
346 | sa = tr_torrentStatCached( t ); |
---|
347 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &t, -1 ); |
---|
348 | sb = tr_torrentStatCached( t ); |
---|
349 | ret = compareDouble( sa->percentDone, sb->percentDone ); |
---|
350 | if( !ret ) |
---|
351 | ret = compareRatio( sa->ratio, sb->ratio ); |
---|
352 | return ret; |
---|
353 | } |
---|
354 | |
---|
355 | static int |
---|
356 | compareByETA( GtkTreeModel * model, |
---|
357 | GtkTreeIter * a, |
---|
358 | GtkTreeIter * b, |
---|
359 | gpointer user_data UNUSED ) |
---|
360 | { |
---|
361 | tr_torrent *ta, *tb; |
---|
362 | |
---|
363 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
364 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
365 | |
---|
366 | return compareETA( tr_torrentStatCached( ta )->eta, |
---|
367 | tr_torrentStatCached( tb )->eta ); |
---|
368 | } |
---|
369 | |
---|
370 | static int |
---|
371 | compareByState( GtkTreeModel * model, |
---|
372 | GtkTreeIter * a, |
---|
373 | GtkTreeIter * b, |
---|
374 | gpointer user_data ) |
---|
375 | { |
---|
376 | int sa, sb, ret; |
---|
377 | |
---|
378 | /* first by state */ |
---|
379 | gtk_tree_model_get( model, a, MC_ACTIVITY, &sa, -1 ); |
---|
380 | gtk_tree_model_get( model, b, MC_ACTIVITY, &sb, -1 ); |
---|
381 | ret = sa - sb; |
---|
382 | |
---|
383 | /* second by progress */ |
---|
384 | if( !ret ) |
---|
385 | ret = compareByProgress( model, a, b, user_data ); |
---|
386 | |
---|
387 | return ret; |
---|
388 | } |
---|
389 | |
---|
390 | static int |
---|
391 | compareByTracker( GtkTreeModel * model, |
---|
392 | GtkTreeIter * a, |
---|
393 | GtkTreeIter * b, |
---|
394 | gpointer user_data UNUSED ) |
---|
395 | { |
---|
396 | const tr_torrent * ta; |
---|
397 | const tr_torrent * tb; |
---|
398 | const tr_info * aInf; |
---|
399 | const tr_info * bInf; |
---|
400 | const char * aTracker; |
---|
401 | const char * bTracker; |
---|
402 | |
---|
403 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
404 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
405 | |
---|
406 | aInf = tr_torrentInfo( ta ); |
---|
407 | bInf = tr_torrentInfo( tb ); |
---|
408 | aTracker = aInf->trackerCount > 0 ? aInf->trackers[0].announce : NULL; |
---|
409 | bTracker = bInf->trackerCount > 0 ? bInf->trackers[0].announce : NULL; |
---|
410 | |
---|
411 | if( !aTracker && !bTracker ) return 0; |
---|
412 | if( !aTracker ) return -1; |
---|
413 | if( !bTracker ) return 1; |
---|
414 | return strcmp( aTracker, bTracker ); |
---|
415 | } |
---|
416 | |
---|
417 | static void |
---|
418 | setSort( TrCore * core, |
---|
419 | const char * mode, |
---|
420 | gboolean isReversed ) |
---|
421 | { |
---|
422 | const int col = MC_TORRENT_RAW; |
---|
423 | GtkTreeIterCompareFunc sort_func; |
---|
424 | GtkSortType type = |
---|
425 | isReversed ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING; |
---|
426 | GtkTreeSortable * sortable = |
---|
427 | GTK_TREE_SORTABLE( tr_core_model( core ) ); |
---|
428 | |
---|
429 | if( !strcmp( mode, "sort-by-activity" ) ) |
---|
430 | sort_func = compareByActivity; |
---|
431 | else if( !strcmp( mode, "sort-by-age" ) ) |
---|
432 | sort_func = compareByAge; |
---|
433 | else if( !strcmp( mode, "sort-by-progress" ) ) |
---|
434 | sort_func = compareByProgress; |
---|
435 | else if( !strcmp( mode, "sort-by-time-left" ) ) |
---|
436 | sort_func = compareByETA; |
---|
437 | else if( !strcmp( mode, "sort-by-ratio" ) ) |
---|
438 | sort_func = compareByRatio; |
---|
439 | else if( !strcmp( mode, "sort-by-state" ) ) |
---|
440 | sort_func = compareByState; |
---|
441 | else if( !strcmp( mode, "sort-by-tracker" ) ) |
---|
442 | sort_func = compareByTracker; |
---|
443 | else if( !strcmp( mode, "sort-by-size" ) ) |
---|
444 | sort_func = compareBySize; |
---|
445 | else { |
---|
446 | sort_func = compareByName; |
---|
447 | type = isReversed ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING; |
---|
448 | } |
---|
449 | |
---|
450 | gtk_tree_sortable_set_sort_func( sortable, col, sort_func, NULL, NULL ); |
---|
451 | gtk_tree_sortable_set_sort_column_id( sortable, col, type ); |
---|
452 | } |
---|
453 | |
---|
454 | static void |
---|
455 | tr_core_apply_defaults( tr_ctor * ctor ) |
---|
456 | { |
---|
457 | if( tr_ctorGetPaused( ctor, TR_FORCE, NULL ) ) |
---|
458 | tr_ctorSetPaused( ctor, TR_FORCE, !pref_flag_get( PREF_KEY_START ) ); |
---|
459 | |
---|
460 | if( tr_ctorGetDeleteSource( ctor, NULL ) ) |
---|
461 | tr_ctorSetDeleteSource( ctor, |
---|
462 | pref_flag_get( PREF_KEY_TRASH_ORIGINAL ) ); |
---|
463 | |
---|
464 | if( tr_ctorGetPeerLimit( ctor, TR_FORCE, NULL ) ) |
---|
465 | tr_ctorSetPeerLimit( ctor, TR_FORCE, |
---|
466 | pref_int_get( TR_PREFS_KEY_PEER_LIMIT_TORRENT ) ); |
---|
467 | |
---|
468 | if( tr_ctorGetDownloadDir( ctor, TR_FORCE, NULL ) ) |
---|
469 | { |
---|
470 | const char * path = pref_string_get( TR_PREFS_KEY_DOWNLOAD_DIR ); |
---|
471 | tr_ctorSetDownloadDir( ctor, TR_FORCE, path ); |
---|
472 | } |
---|
473 | } |
---|
474 | |
---|
475 | static int |
---|
476 | tr_strcmp( const void * a, |
---|
477 | const void * b ) |
---|
478 | { |
---|
479 | if( a && b ) return strcmp( a, b ); |
---|
480 | if( a ) return 1; |
---|
481 | if( b ) return -1; |
---|
482 | return 0; |
---|
483 | } |
---|
484 | |
---|
485 | #ifdef HAVE_GIO |
---|
486 | |
---|
487 | struct watchdir_file |
---|
488 | { |
---|
489 | char * filename; |
---|
490 | time_t mtime; |
---|
491 | }; |
---|
492 | |
---|
493 | static int |
---|
494 | compare_watchdir_file_to_filename( const void * a, const void * filename ) |
---|
495 | { |
---|
496 | return strcmp( ((const struct watchdir_file*)a)->filename, filename ); |
---|
497 | } |
---|
498 | |
---|
499 | static void |
---|
500 | watchdir_file_update_mtime( struct watchdir_file * file ) |
---|
501 | { |
---|
502 | GFile * gfile = g_file_new_for_path( file->filename ); |
---|
503 | GFileInfo * info = g_file_query_info( gfile, G_FILE_ATTRIBUTE_TIME_MODIFIED, 0, NULL, NULL ); |
---|
504 | |
---|
505 | file->mtime = g_file_info_get_attribute_uint64( info, G_FILE_ATTRIBUTE_TIME_MODIFIED ); |
---|
506 | |
---|
507 | g_object_unref( G_OBJECT( info ) ); |
---|
508 | g_object_unref( G_OBJECT( gfile ) ); |
---|
509 | } |
---|
510 | |
---|
511 | static struct watchdir_file* |
---|
512 | watchdir_file_new( const char * filename ) |
---|
513 | { |
---|
514 | struct watchdir_file * f; |
---|
515 | |
---|
516 | f = g_new( struct watchdir_file, 1 ); |
---|
517 | f->filename = g_strdup( filename ); |
---|
518 | watchdir_file_update_mtime( f ); |
---|
519 | |
---|
520 | return f; |
---|
521 | } |
---|
522 | |
---|
523 | static void |
---|
524 | watchdir_file_free( struct watchdir_file * f ) |
---|
525 | { |
---|
526 | g_free( f->filename ); |
---|
527 | g_free( f ); |
---|
528 | } |
---|
529 | |
---|
530 | static gboolean |
---|
531 | watchFolderIdle( gpointer gcore ) |
---|
532 | { |
---|
533 | GSList * l; |
---|
534 | GSList * addme = NULL; |
---|
535 | GSList * monitor_files = NULL; |
---|
536 | TrCore * core = TR_CORE( gcore ); |
---|
537 | const time_t now = time( NULL ); |
---|
538 | struct TrCorePrivate * p = core->priv; |
---|
539 | |
---|
540 | /* of the monitor_files, make a list of those that haven't |
---|
541 | * changed lately, since they should be ready to add */ |
---|
542 | for( l=p->monitor_files; l!=NULL; l=l->next ) { |
---|
543 | struct watchdir_file * f = l->data; |
---|
544 | watchdir_file_update_mtime( f ); |
---|
545 | if( f->mtime + 2 >= now ) |
---|
546 | monitor_files = g_slist_prepend( monitor_files, f ); |
---|
547 | else { |
---|
548 | addme = g_slist_prepend( addme, g_strdup( f->filename ) ); |
---|
549 | watchdir_file_free( f ); |
---|
550 | } |
---|
551 | } |
---|
552 | |
---|
553 | /* add the torrents from that list */ |
---|
554 | core->priv->adding_from_watch_dir = TRUE; |
---|
555 | tr_core_add_list_defaults( core, addme, TRUE ); |
---|
556 | core->priv->adding_from_watch_dir = FALSE; |
---|
557 | |
---|
558 | /* update the monitor_files list */ |
---|
559 | g_slist_free( p->monitor_files ); |
---|
560 | p->monitor_files = monitor_files; |
---|
561 | |
---|
562 | /* if monitor_files is nonempty, keep checking every second */ |
---|
563 | if( core->priv->monitor_files ) |
---|
564 | return TRUE; |
---|
565 | core->priv->monitor_idle_tag = 0; |
---|
566 | return FALSE; |
---|
567 | |
---|
568 | } |
---|
569 | |
---|
570 | static void |
---|
571 | maybeAddTorrent( TrCore * core, const char * filename ) |
---|
572 | { |
---|
573 | const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" ); |
---|
574 | |
---|
575 | if( isTorrent ) |
---|
576 | { |
---|
577 | struct TrCorePrivate * p = core->priv; |
---|
578 | |
---|
579 | if( !g_slist_find_custom( p->monitor_files, filename, (GCompareFunc)compare_watchdir_file_to_filename ) ) |
---|
580 | p->monitor_files = g_slist_append( p->monitor_files, watchdir_file_new( filename ) ); |
---|
581 | |
---|
582 | if( !p->monitor_idle_tag ) |
---|
583 | p->monitor_idle_tag = gtr_timeout_add_seconds( 1, watchFolderIdle, core ); |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | static void |
---|
588 | watchFolderChanged( GFileMonitor * monitor UNUSED, |
---|
589 | GFile * file, |
---|
590 | GFile * other_type UNUSED, |
---|
591 | GFileMonitorEvent event_type, |
---|
592 | gpointer core ) |
---|
593 | { |
---|
594 | if( event_type == G_FILE_MONITOR_EVENT_CREATED ) |
---|
595 | { |
---|
596 | char * filename = g_file_get_path( file ); |
---|
597 | maybeAddTorrent( core, filename ); |
---|
598 | g_free( filename ); |
---|
599 | } |
---|
600 | } |
---|
601 | |
---|
602 | static void |
---|
603 | scanWatchDir( TrCore * core ) |
---|
604 | { |
---|
605 | const gboolean isEnabled = pref_flag_get( PREF_KEY_DIR_WATCH_ENABLED ); |
---|
606 | |
---|
607 | if( isEnabled ) |
---|
608 | { |
---|
609 | const char * dirname = pref_string_get( PREF_KEY_DIR_WATCH ); |
---|
610 | GDir * dir = g_dir_open( dirname, 0, NULL ); |
---|
611 | |
---|
612 | if( dir != NULL ) |
---|
613 | { |
---|
614 | const char * basename; |
---|
615 | while(( basename = g_dir_read_name( dir ))) |
---|
616 | { |
---|
617 | char * filename = g_build_filename( dirname, basename, NULL ); |
---|
618 | maybeAddTorrent( core, filename ); |
---|
619 | g_free( filename ); |
---|
620 | } |
---|
621 | |
---|
622 | g_dir_close( dir ); |
---|
623 | } |
---|
624 | } |
---|
625 | } |
---|
626 | |
---|
627 | static void |
---|
628 | updateWatchDir( TrCore * core ) |
---|
629 | { |
---|
630 | const char * filename = pref_string_get( PREF_KEY_DIR_WATCH ); |
---|
631 | const gboolean isEnabled = pref_flag_get( |
---|
632 | PREF_KEY_DIR_WATCH_ENABLED ); |
---|
633 | struct TrCorePrivate * p = TR_CORE( core )->priv; |
---|
634 | |
---|
635 | if( p->monitor && ( !isEnabled || tr_strcmp( filename, p->monitor_path ) ) ) |
---|
636 | { |
---|
637 | g_signal_handler_disconnect( p->monitor, p->monitor_tag ); |
---|
638 | g_free( p->monitor_path ); |
---|
639 | g_file_monitor_cancel( p->monitor ); |
---|
640 | g_object_unref( G_OBJECT( p->monitor ) ); |
---|
641 | p->monitor_path = NULL; |
---|
642 | p->monitor = NULL; |
---|
643 | p->monitor_tag = 0; |
---|
644 | } |
---|
645 | |
---|
646 | if( isEnabled && !p->monitor ) |
---|
647 | { |
---|
648 | GFile * file = g_file_new_for_path( filename ); |
---|
649 | GFileMonitor * m = g_file_monitor_directory( file, 0, NULL, NULL ); |
---|
650 | scanWatchDir( core ); |
---|
651 | p->monitor = m; |
---|
652 | p->monitor_path = g_strdup( filename ); |
---|
653 | p->monitor_tag = g_signal_connect( m, "changed", |
---|
654 | G_CALLBACK( |
---|
655 | watchFolderChanged ), core ); |
---|
656 | } |
---|
657 | } |
---|
658 | |
---|
659 | #endif |
---|
660 | |
---|
661 | static void |
---|
662 | prefsChanged( TrCore * core, |
---|
663 | const char * key, |
---|
664 | gpointer data UNUSED ) |
---|
665 | { |
---|
666 | if( !strcmp( key, PREF_KEY_SORT_MODE ) |
---|
667 | || !strcmp( key, PREF_KEY_SORT_REVERSED ) ) |
---|
668 | { |
---|
669 | const char * mode = pref_string_get( PREF_KEY_SORT_MODE ); |
---|
670 | gboolean isReversed = pref_flag_get( PREF_KEY_SORT_REVERSED ); |
---|
671 | setSort( core, mode, isReversed ); |
---|
672 | } |
---|
673 | else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_GLOBAL ) ) |
---|
674 | { |
---|
675 | const uint16_t val = pref_int_get( key ); |
---|
676 | tr_sessionSetPeerLimit( tr_core_session( core ), val ); |
---|
677 | } |
---|
678 | else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_TORRENT ) ) |
---|
679 | { |
---|
680 | const uint16_t val = pref_int_get( key ); |
---|
681 | tr_sessionSetPeerLimitPerTorrent( tr_core_session( core ), val ); |
---|
682 | } |
---|
683 | else if( !strcmp( key, PREF_KEY_INHIBIT_HIBERNATION ) ) |
---|
684 | { |
---|
685 | maybeInhibitHibernation( core ); |
---|
686 | } |
---|
687 | #ifdef HAVE_GIO |
---|
688 | else if( !strcmp( key, PREF_KEY_DIR_WATCH ) |
---|
689 | || !strcmp( key, PREF_KEY_DIR_WATCH_ENABLED ) ) |
---|
690 | { |
---|
691 | updateWatchDir( core ); |
---|
692 | } |
---|
693 | #endif |
---|
694 | } |
---|
695 | |
---|
696 | static void |
---|
697 | tr_core_init( GTypeInstance * instance, |
---|
698 | gpointer g_class UNUSED ) |
---|
699 | { |
---|
700 | GtkListStore * store; |
---|
701 | struct TrCorePrivate * p; |
---|
702 | TrCore * self = (TrCore *) instance; |
---|
703 | |
---|
704 | /* column types for the model used to store torrent information */ |
---|
705 | /* keep this in sync with the enum near the bottom of tr_core.h */ |
---|
706 | GType types[] = { G_TYPE_STRING, /* name */ |
---|
707 | G_TYPE_STRING, /* collated name */ |
---|
708 | TR_TORRENT_TYPE, /* TrTorrent object */ |
---|
709 | G_TYPE_POINTER, /* tr_torrent* */ |
---|
710 | G_TYPE_DOUBLE, /* tr_stat.pieceUploadSpeed */ |
---|
711 | G_TYPE_DOUBLE, /* tr_stat.pieceDownloadSpeed */ |
---|
712 | G_TYPE_INT }; /* tr_stat.status */ |
---|
713 | |
---|
714 | p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE( self, |
---|
715 | TR_CORE_TYPE, |
---|
716 | struct TrCorePrivate ); |
---|
717 | |
---|
718 | /* create the model used to store torrent data */ |
---|
719 | g_assert( G_N_ELEMENTS( types ) == MC_ROW_COUNT ); |
---|
720 | store = gtk_list_store_newv( MC_ROW_COUNT, types ); |
---|
721 | |
---|
722 | p->model = GTK_TREE_MODEL( store ); |
---|
723 | |
---|
724 | #ifdef HAVE_DBUS_GLIB |
---|
725 | if( our_instance_adds_remote_torrents ) |
---|
726 | { |
---|
727 | DBusGConnection * bus = dbus_g_bus_get( DBUS_BUS_SESSION, NULL ); |
---|
728 | if( bus ) |
---|
729 | dbus_g_connection_register_g_object( |
---|
730 | bus, |
---|
731 | "/com/transmissionbt/Transmission", |
---|
732 | G_OBJECT( self ) ); |
---|
733 | } |
---|
734 | #endif |
---|
735 | } |
---|
736 | |
---|
737 | GType |
---|
738 | tr_core_get_type( void ) |
---|
739 | { |
---|
740 | static GType type = 0; |
---|
741 | |
---|
742 | if( !type ) |
---|
743 | { |
---|
744 | static const GTypeInfo info = |
---|
745 | { |
---|
746 | sizeof( TrCoreClass ), |
---|
747 | NULL, /* base_init */ |
---|
748 | NULL, /* base_finalize */ |
---|
749 | tr_core_class_init, /* class_init */ |
---|
750 | NULL, /* class_finalize */ |
---|
751 | NULL, /* class_data */ |
---|
752 | sizeof( TrCore ), |
---|
753 | 0, /* n_preallocs */ |
---|
754 | tr_core_init, /* instance_init */ |
---|
755 | NULL, |
---|
756 | }; |
---|
757 | type = g_type_register_static( G_TYPE_OBJECT, "TrCore", &info, 0 ); |
---|
758 | } |
---|
759 | |
---|
760 | return type; |
---|
761 | } |
---|
762 | |
---|
763 | /** |
---|
764 | *** |
---|
765 | **/ |
---|
766 | |
---|
767 | TrCore * |
---|
768 | tr_core_new( tr_session * session ) |
---|
769 | { |
---|
770 | TrCore * core = TR_CORE( g_object_new( TR_CORE_TYPE, NULL ) ); |
---|
771 | |
---|
772 | core->priv->session = session; |
---|
773 | |
---|
774 | /* init from prefs & listen to pref changes */ |
---|
775 | prefsChanged( core, PREF_KEY_SORT_MODE, NULL ); |
---|
776 | prefsChanged( core, PREF_KEY_SORT_REVERSED, NULL ); |
---|
777 | prefsChanged( core, PREF_KEY_DIR_WATCH_ENABLED, NULL ); |
---|
778 | prefsChanged( core, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, NULL ); |
---|
779 | prefsChanged( core, PREF_KEY_INHIBIT_HIBERNATION, NULL ); |
---|
780 | g_signal_connect( core, "prefs-changed", G_CALLBACK( prefsChanged ), NULL ); |
---|
781 | |
---|
782 | return core; |
---|
783 | } |
---|
784 | |
---|
785 | void |
---|
786 | tr_core_close( TrCore * core ) |
---|
787 | { |
---|
788 | tr_session * session = tr_core_session( core ); |
---|
789 | |
---|
790 | if( session ) |
---|
791 | { |
---|
792 | core->priv->session = NULL; |
---|
793 | pref_save( session ); |
---|
794 | tr_sessionClose( session ); |
---|
795 | } |
---|
796 | } |
---|
797 | |
---|
798 | GtkTreeModel * |
---|
799 | tr_core_model( TrCore * core ) |
---|
800 | { |
---|
801 | return isDisposed( core ) ? NULL : core->priv->model; |
---|
802 | } |
---|
803 | |
---|
804 | tr_session * |
---|
805 | tr_core_session( TrCore * core ) |
---|
806 | { |
---|
807 | return isDisposed( core ) ? NULL : core->priv->session; |
---|
808 | } |
---|
809 | |
---|
810 | static char* |
---|
811 | doCollate( const char * in ) |
---|
812 | { |
---|
813 | char * ret; |
---|
814 | char * casefold; |
---|
815 | const char * end = in ? in + strlen( in ) : NULL; |
---|
816 | |
---|
817 | while( in < end ) |
---|
818 | { |
---|
819 | const gunichar ch = g_utf8_get_char( in ); |
---|
820 | if( !g_unichar_isalnum ( ch ) ) /* eat everything before the first alnum |
---|
821 | */ |
---|
822 | in += g_unichar_to_utf8( ch, NULL ); |
---|
823 | else |
---|
824 | break; |
---|
825 | } |
---|
826 | |
---|
827 | if( in == end ) |
---|
828 | return g_strdup ( "" ); |
---|
829 | |
---|
830 | casefold = g_utf8_casefold( in, end - in ); |
---|
831 | ret = g_utf8_collate_key( casefold, -1 ); |
---|
832 | g_free( casefold ); |
---|
833 | |
---|
834 | return ret; |
---|
835 | } |
---|
836 | |
---|
837 | void |
---|
838 | tr_core_add_torrent( TrCore * self, |
---|
839 | TrTorrent * gtor, |
---|
840 | gboolean doNotify ) |
---|
841 | { |
---|
842 | const tr_info * inf = tr_torrent_info( gtor ); |
---|
843 | const tr_stat * st = tr_torrent_stat( gtor ); |
---|
844 | tr_torrent * tor = tr_torrent_handle( gtor ); |
---|
845 | char * collated = doCollate( inf->name ); |
---|
846 | GtkListStore * store = GTK_LIST_STORE( tr_core_model( self ) ); |
---|
847 | GtkTreeIter unused; |
---|
848 | |
---|
849 | gtk_list_store_insert_with_values( store, &unused, 0, |
---|
850 | MC_NAME, inf->name, |
---|
851 | MC_NAME_COLLATED, collated, |
---|
852 | MC_TORRENT, gtor, |
---|
853 | MC_TORRENT_RAW, tor, |
---|
854 | MC_SPEED_UP, st->pieceUploadSpeed, |
---|
855 | MC_SPEED_DOWN, st->pieceDownloadSpeed, |
---|
856 | MC_ACTIVITY, st->activity, |
---|
857 | -1 ); |
---|
858 | |
---|
859 | if( doNotify ) |
---|
860 | tr_notify_added( inf->name ); |
---|
861 | |
---|
862 | /* cleanup */ |
---|
863 | g_object_unref( G_OBJECT( gtor ) ); |
---|
864 | g_free( collated ); |
---|
865 | } |
---|
866 | |
---|
867 | int |
---|
868 | tr_core_load( TrCore * self, |
---|
869 | gboolean forcePaused ) |
---|
870 | { |
---|
871 | int i; |
---|
872 | int count = 0; |
---|
873 | tr_torrent ** torrents; |
---|
874 | tr_ctor * ctor; |
---|
875 | |
---|
876 | ctor = tr_ctorNew( tr_core_session( self ) ); |
---|
877 | if( forcePaused ) |
---|
878 | tr_ctorSetPaused( ctor, TR_FORCE, TRUE ); |
---|
879 | tr_ctorSetPeerLimit( ctor, TR_FALLBACK, |
---|
880 | pref_int_get( TR_PREFS_KEY_PEER_LIMIT_TORRENT ) ); |
---|
881 | |
---|
882 | torrents = tr_sessionLoadTorrents ( tr_core_session( self ), ctor, &count ); |
---|
883 | for( i = 0; i < count; ++i ) |
---|
884 | tr_core_add_torrent( self, tr_torrent_new_preexisting( torrents[i] ), FALSE ); |
---|
885 | |
---|
886 | tr_free( torrents ); |
---|
887 | tr_ctorFree( ctor ); |
---|
888 | |
---|
889 | return count; |
---|
890 | } |
---|
891 | |
---|
892 | static void |
---|
893 | emitBlocklistUpdated( TrCore * core, int ruleCount ) |
---|
894 | { |
---|
895 | g_signal_emit( core, TR_CORE_GET_CLASS( core )->blocklistSignal, 0, ruleCount ); |
---|
896 | } |
---|
897 | |
---|
898 | static void |
---|
899 | emitPortTested( TrCore * core, gboolean isOpen ) |
---|
900 | { |
---|
901 | g_signal_emit( core, TR_CORE_GET_CLASS( core )->portSignal, 0, isOpen ); |
---|
902 | } |
---|
903 | |
---|
904 | static void |
---|
905 | tr_core_errsig( TrCore * core, |
---|
906 | enum tr_core_err type, |
---|
907 | const char * msg ) |
---|
908 | { |
---|
909 | g_signal_emit( core, TR_CORE_GET_CLASS( core )->errsig, 0, type, msg ); |
---|
910 | } |
---|
911 | |
---|
912 | static int |
---|
913 | add_ctor( TrCore * core, tr_ctor * ctor, gboolean doPrompt, gboolean doNotify ) |
---|
914 | { |
---|
915 | tr_info inf; |
---|
916 | int err = tr_torrentParse( ctor, &inf ); |
---|
917 | |
---|
918 | switch( err ) |
---|
919 | { |
---|
920 | case TR_PARSE_ERR: |
---|
921 | break; |
---|
922 | |
---|
923 | case TR_PARSE_DUPLICATE: |
---|
924 | /* don't complain about .torrent files in the watch directory |
---|
925 | * that have already been added... that gets annoying and we |
---|
926 | * don't want to be nagging users to clean up their watch dirs */ |
---|
927 | if( !tr_ctorGetSourceFile(ctor) || !core->priv->adding_from_watch_dir ) |
---|
928 | tr_core_errsig( core, err, inf.name ); |
---|
929 | tr_metainfoFree( &inf ); |
---|
930 | break; |
---|
931 | |
---|
932 | default: |
---|
933 | if( doPrompt ) |
---|
934 | g_signal_emit( core, TR_CORE_GET_CLASS( core )->promptsig, 0, ctor ); |
---|
935 | else { |
---|
936 | tr_session * session = tr_core_session( core ); |
---|
937 | TrTorrent * gtor = tr_torrent_new_ctor( session, ctor, &err ); |
---|
938 | if( !err ) |
---|
939 | tr_core_add_torrent( core, gtor, doNotify ); |
---|
940 | } |
---|
941 | tr_metainfoFree( &inf ); |
---|
942 | break; |
---|
943 | } |
---|
944 | |
---|
945 | return err; |
---|
946 | } |
---|
947 | |
---|
948 | void |
---|
949 | tr_core_add_ctor( TrCore * core, tr_ctor * ctor ) |
---|
950 | { |
---|
951 | const gboolean doStart = pref_flag_get( PREF_KEY_START ); |
---|
952 | const gboolean doPrompt = pref_flag_get( PREF_KEY_OPTIONS_PROMPT ); |
---|
953 | tr_core_apply_defaults( ctor ); |
---|
954 | add_ctor( core, ctor, doStart, doPrompt ); |
---|
955 | } |
---|
956 | |
---|
957 | /* invoked remotely via dbus. */ |
---|
958 | gboolean |
---|
959 | tr_core_add_metainfo( TrCore * core, |
---|
960 | const char * payload, |
---|
961 | gboolean * setme_handled, |
---|
962 | GError ** gerr UNUSED ) |
---|
963 | { |
---|
964 | tr_session * session = tr_core_session( core ); |
---|
965 | |
---|
966 | if( !session ) |
---|
967 | { |
---|
968 | *setme_handled = FALSE; |
---|
969 | } |
---|
970 | else if( gtr_is_supported_url( payload ) || gtr_is_magnet_link( payload ) ) |
---|
971 | { |
---|
972 | tr_core_add_from_url( core, payload ); |
---|
973 | *setme_handled = TRUE; |
---|
974 | } |
---|
975 | else /* base64-encoded metainfo */ |
---|
976 | { |
---|
977 | int file_length; |
---|
978 | tr_ctor * ctor; |
---|
979 | char * file_contents; |
---|
980 | gboolean do_prompt = pref_flag_get( PREF_KEY_OPTIONS_PROMPT ); |
---|
981 | |
---|
982 | ctor = tr_ctorNew( session ); |
---|
983 | tr_core_apply_defaults( ctor ); |
---|
984 | |
---|
985 | file_contents = tr_base64_decode( payload, -1, &file_length ); |
---|
986 | tr_ctorSetMetainfo( ctor, (const uint8_t*)file_contents, file_length ); |
---|
987 | add_ctor( core, ctor, do_prompt, TRUE ); |
---|
988 | |
---|
989 | tr_free( file_contents ); |
---|
990 | tr_core_torrents_added( core ); |
---|
991 | *setme_handled = TRUE; |
---|
992 | } |
---|
993 | |
---|
994 | return TRUE; |
---|
995 | } |
---|
996 | |
---|
997 | /*** |
---|
998 | **** |
---|
999 | ***/ |
---|
1000 | |
---|
1001 | struct url_dialog_data |
---|
1002 | { |
---|
1003 | TrCore * core; |
---|
1004 | tr_ctor * ctor; |
---|
1005 | GtkDialog * dialog; |
---|
1006 | }; |
---|
1007 | |
---|
1008 | static gboolean |
---|
1009 | onURLDoneIdle( gpointer vdata ) |
---|
1010 | { |
---|
1011 | struct url_dialog_data * data = vdata; |
---|
1012 | tr_core_add_ctor( data->core, data->ctor ); |
---|
1013 | g_free( data ); |
---|
1014 | return FALSE; |
---|
1015 | } |
---|
1016 | |
---|
1017 | static void |
---|
1018 | onURLDone( tr_session * session, |
---|
1019 | long response_code UNUSED, |
---|
1020 | const void * response, |
---|
1021 | size_t response_byte_count, |
---|
1022 | void * vdata ) |
---|
1023 | { |
---|
1024 | struct url_dialog_data * data = vdata; |
---|
1025 | tr_ctor * ctor = tr_ctorNew( session ); |
---|
1026 | |
---|
1027 | /* FIME: error dialog */ |
---|
1028 | |
---|
1029 | if( tr_ctorSetMetainfo( ctor, response, response_byte_count ) ) |
---|
1030 | { |
---|
1031 | tr_ctorFree( ctor ); |
---|
1032 | g_free( data ); |
---|
1033 | } |
---|
1034 | else /* move the work back to the gtk thread */ |
---|
1035 | { |
---|
1036 | data->ctor = ctor; |
---|
1037 | gtr_idle_add( onURLDoneIdle, data ); |
---|
1038 | } |
---|
1039 | } |
---|
1040 | |
---|
1041 | void |
---|
1042 | tr_core_add_from_url( TrCore * core, const char * url ) |
---|
1043 | { |
---|
1044 | tr_session * session = tr_core_session( core ); |
---|
1045 | const gboolean is_magnet_link = gtr_is_magnet_link( url ); |
---|
1046 | |
---|
1047 | if( is_magnet_link || gtr_is_hex_hashcode( url ) ) |
---|
1048 | { |
---|
1049 | int err; |
---|
1050 | char * tmp = NULL; |
---|
1051 | tr_ctor * ctor = tr_ctorNew( session ); |
---|
1052 | |
---|
1053 | if( gtr_is_hex_hashcode( url ) ) |
---|
1054 | url = tmp = g_strdup_printf( "magnet:?xt=urn:btih:%s", url ); |
---|
1055 | |
---|
1056 | err = tr_ctorSetMetainfoFromMagnetLink( ctor, url ); |
---|
1057 | |
---|
1058 | if( err ) |
---|
1059 | { |
---|
1060 | gtr_unrecognized_url_dialog( NULL, url ); |
---|
1061 | tr_ctorFree( ctor ); |
---|
1062 | } |
---|
1063 | else |
---|
1064 | { |
---|
1065 | tr_session * session = tr_core_session( core ); |
---|
1066 | TrTorrent * gtor = tr_torrent_new_ctor( session, ctor, &err ); |
---|
1067 | if( !err ) |
---|
1068 | tr_core_add_torrent( core, gtor, FALSE ); |
---|
1069 | else |
---|
1070 | g_message( "tr_torrent_new_ctor err %d", err ); |
---|
1071 | } |
---|
1072 | |
---|
1073 | g_free( tmp ); |
---|
1074 | } |
---|
1075 | else |
---|
1076 | { |
---|
1077 | struct url_dialog_data * data = g_new( struct url_dialog_data, 1 ); |
---|
1078 | data->core = core; |
---|
1079 | tr_webRun( session, url, NULL, onURLDone, data ); |
---|
1080 | } |
---|
1081 | } |
---|
1082 | |
---|
1083 | /*** |
---|
1084 | **** |
---|
1085 | ***/ |
---|
1086 | |
---|
1087 | static void |
---|
1088 | add_filename( TrCore * core, |
---|
1089 | const char * filename, |
---|
1090 | gboolean doStart, |
---|
1091 | gboolean doPrompt, |
---|
1092 | gboolean doNotify ) |
---|
1093 | { |
---|
1094 | tr_session * session = tr_core_session( core ); |
---|
1095 | |
---|
1096 | if( session == NULL ) |
---|
1097 | return; |
---|
1098 | |
---|
1099 | if( gtr_is_supported_url( filename ) || gtr_is_magnet_link( filename ) ) |
---|
1100 | { |
---|
1101 | tr_core_add_from_url( core, filename ); |
---|
1102 | } |
---|
1103 | else if( g_file_test( filename, G_FILE_TEST_EXISTS ) ) |
---|
1104 | { |
---|
1105 | int err; |
---|
1106 | |
---|
1107 | tr_ctor * ctor = tr_ctorNew( session ); |
---|
1108 | tr_ctorSetMetainfoFromFile( ctor, filename ); |
---|
1109 | tr_core_apply_defaults( ctor ); |
---|
1110 | tr_ctorSetPaused( ctor, TR_FORCE, !doStart ); |
---|
1111 | |
---|
1112 | err = add_ctor( core, ctor, doPrompt, doNotify ); |
---|
1113 | if( err == TR_PARSE_ERR ) |
---|
1114 | tr_core_errsig( core, TR_PARSE_ERR, filename ); |
---|
1115 | } |
---|
1116 | else if( gtr_is_hex_hashcode( filename ) ) |
---|
1117 | { |
---|
1118 | tr_core_add_from_url( core, filename ); |
---|
1119 | } |
---|
1120 | } |
---|
1121 | |
---|
1122 | gboolean |
---|
1123 | tr_core_present_window( TrCore * core UNUSED, |
---|
1124 | gboolean * success, |
---|
1125 | GError ** err UNUSED ) |
---|
1126 | { |
---|
1127 | action_activate( "present-main-window" ); |
---|
1128 | *success = TRUE; |
---|
1129 | return TRUE; |
---|
1130 | } |
---|
1131 | |
---|
1132 | void |
---|
1133 | tr_core_add_list( TrCore * core, |
---|
1134 | GSList * torrentFiles, |
---|
1135 | pref_flag_t start, |
---|
1136 | pref_flag_t prompt, |
---|
1137 | gboolean doNotify ) |
---|
1138 | { |
---|
1139 | const gboolean doStart = pref_flag_eval( start, PREF_KEY_START ); |
---|
1140 | const gboolean doPrompt = pref_flag_eval( prompt, PREF_KEY_OPTIONS_PROMPT ); |
---|
1141 | GSList * l; |
---|
1142 | |
---|
1143 | for( l = torrentFiles; l != NULL; l = l->next ) |
---|
1144 | add_filename( core, l->data, doStart, doPrompt, doNotify ); |
---|
1145 | |
---|
1146 | tr_core_torrents_added( core ); |
---|
1147 | freestrlist( torrentFiles ); |
---|
1148 | } |
---|
1149 | |
---|
1150 | void |
---|
1151 | tr_core_torrents_added( TrCore * self ) |
---|
1152 | { |
---|
1153 | tr_core_update( self ); |
---|
1154 | tr_core_errsig( self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL ); |
---|
1155 | } |
---|
1156 | |
---|
1157 | static gboolean |
---|
1158 | findTorrentInModel( TrCore * core, |
---|
1159 | int id, |
---|
1160 | GtkTreeIter * setme ) |
---|
1161 | { |
---|
1162 | int match = 0; |
---|
1163 | GtkTreeIter iter; |
---|
1164 | GtkTreeModel * model = tr_core_model( core ); |
---|
1165 | |
---|
1166 | if( gtk_tree_model_iter_children( model, &iter, NULL ) ) do |
---|
1167 | { |
---|
1168 | tr_torrent * tor; |
---|
1169 | gtk_tree_model_get( model, &iter, MC_TORRENT_RAW, &tor, -1 ); |
---|
1170 | match = tr_torrentId( tor ) == id; |
---|
1171 | } |
---|
1172 | while( !match && gtk_tree_model_iter_next( model, &iter ) ); |
---|
1173 | |
---|
1174 | if( match ) |
---|
1175 | *setme = iter; |
---|
1176 | |
---|
1177 | return match; |
---|
1178 | } |
---|
1179 | |
---|
1180 | void |
---|
1181 | tr_core_torrent_destroyed( TrCore * core, |
---|
1182 | int id ) |
---|
1183 | { |
---|
1184 | GtkTreeIter iter; |
---|
1185 | |
---|
1186 | if( findTorrentInModel( core, id, &iter ) ) |
---|
1187 | { |
---|
1188 | TrTorrent * gtor; |
---|
1189 | GtkTreeModel * model = tr_core_model( core ); |
---|
1190 | gtk_tree_model_get( model, &iter, MC_TORRENT, >or, -1 ); |
---|
1191 | tr_torrent_clear( gtor ); |
---|
1192 | gtk_list_store_remove( GTK_LIST_STORE( model ), &iter ); |
---|
1193 | g_object_unref( G_OBJECT( gtor ) ); |
---|
1194 | } |
---|
1195 | } |
---|
1196 | |
---|
1197 | void |
---|
1198 | tr_core_remove_torrent( TrCore * core, |
---|
1199 | TrTorrent * gtor, |
---|
1200 | int deleteFiles ) |
---|
1201 | { |
---|
1202 | const tr_torrent * tor = tr_torrent_handle( gtor ); |
---|
1203 | |
---|
1204 | if( tor ) |
---|
1205 | { |
---|
1206 | int id = tr_torrentId( tor ); |
---|
1207 | GtkTreeIter iter; |
---|
1208 | if( findTorrentInModel( core, id, &iter ) ) |
---|
1209 | { |
---|
1210 | GtkTreeModel * model = tr_core_model( core ); |
---|
1211 | |
---|
1212 | /* remove from the gui */ |
---|
1213 | gtk_list_store_remove( GTK_LIST_STORE( model ), &iter ); |
---|
1214 | |
---|
1215 | /* maybe delete the downloaded files */ |
---|
1216 | if( deleteFiles ) |
---|
1217 | tr_torrent_delete_files( gtor ); |
---|
1218 | |
---|
1219 | /* remove the torrent */ |
---|
1220 | tr_torrent_set_remove_flag( gtor, TRUE ); |
---|
1221 | g_object_unref( G_OBJECT( gtor ) ); |
---|
1222 | } |
---|
1223 | } |
---|
1224 | } |
---|
1225 | |
---|
1226 | /*** |
---|
1227 | **** |
---|
1228 | ***/ |
---|
1229 | |
---|
1230 | static gboolean |
---|
1231 | update_foreach( GtkTreeModel * model, |
---|
1232 | GtkTreePath * path UNUSED, |
---|
1233 | GtkTreeIter * iter, |
---|
1234 | gpointer data UNUSED ) |
---|
1235 | { |
---|
1236 | int oldActivity, newActivity; |
---|
1237 | double oldUpSpeed, newUpSpeed; |
---|
1238 | double oldDownSpeed, newDownSpeed; |
---|
1239 | const tr_stat * st; |
---|
1240 | TrTorrent * gtor; |
---|
1241 | |
---|
1242 | /* get the old states */ |
---|
1243 | gtk_tree_model_get( model, iter, |
---|
1244 | MC_TORRENT, >or, |
---|
1245 | MC_ACTIVITY, &oldActivity, |
---|
1246 | MC_SPEED_UP, &oldUpSpeed, |
---|
1247 | MC_SPEED_DOWN, &oldDownSpeed, |
---|
1248 | -1 ); |
---|
1249 | |
---|
1250 | /* get the new states */ |
---|
1251 | st = tr_torrentStat( tr_torrent_handle( gtor ) ); |
---|
1252 | newActivity = st->activity; |
---|
1253 | newUpSpeed = st->pieceUploadSpeed; |
---|
1254 | newDownSpeed = st->pieceDownloadSpeed; |
---|
1255 | |
---|
1256 | /* updating the model triggers off resort/refresh, |
---|
1257 | so don't do it unless something's actually changed... */ |
---|
1258 | if( ( newActivity != oldActivity ) || |
---|
1259 | ( (int)(newUpSpeed*10.0) != (int)(oldUpSpeed*10.0) ) || |
---|
1260 | ( (int)(newDownSpeed*10.0) != (int)(oldDownSpeed*10.0) ) ) |
---|
1261 | { |
---|
1262 | gtk_list_store_set( GTK_LIST_STORE( model ), iter, |
---|
1263 | MC_ACTIVITY, newActivity, |
---|
1264 | MC_SPEED_UP, newUpSpeed, |
---|
1265 | MC_SPEED_DOWN, newDownSpeed, |
---|
1266 | -1 ); |
---|
1267 | } |
---|
1268 | |
---|
1269 | /* cleanup */ |
---|
1270 | g_object_unref( gtor ); |
---|
1271 | return FALSE; |
---|
1272 | } |
---|
1273 | |
---|
1274 | void |
---|
1275 | tr_core_update( TrCore * self ) |
---|
1276 | { |
---|
1277 | int column; |
---|
1278 | GtkSortType order; |
---|
1279 | GtkTreeSortable * sortable; |
---|
1280 | GtkTreeModel * model = tr_core_model( self ); |
---|
1281 | |
---|
1282 | /* pause sorting */ |
---|
1283 | sortable = GTK_TREE_SORTABLE( model ); |
---|
1284 | gtk_tree_sortable_get_sort_column_id( sortable, &column, &order ); |
---|
1285 | gtk_tree_sortable_set_sort_column_id( |
---|
1286 | sortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, order ); |
---|
1287 | |
---|
1288 | /* refresh the model */ |
---|
1289 | gtk_tree_model_foreach( model, update_foreach, NULL ); |
---|
1290 | |
---|
1291 | /* resume sorting */ |
---|
1292 | gtk_tree_sortable_set_sort_column_id( sortable, column, order ); |
---|
1293 | |
---|
1294 | /* maybe inhibit hibernation */ |
---|
1295 | maybeInhibitHibernation( self ); |
---|
1296 | } |
---|
1297 | |
---|
1298 | void |
---|
1299 | tr_core_quit( TrCore * core ) |
---|
1300 | { |
---|
1301 | g_signal_emit( core, TR_CORE_GET_CLASS( core )->quitsig, 0 ); |
---|
1302 | } |
---|
1303 | |
---|
1304 | /** |
---|
1305 | *** Hibernate |
---|
1306 | **/ |
---|
1307 | |
---|
1308 | #ifdef HAVE_DBUS_GLIB |
---|
1309 | |
---|
1310 | static DBusGProxy* |
---|
1311 | get_hibernation_inhibit_proxy( void ) |
---|
1312 | { |
---|
1313 | DBusGConnection * conn; |
---|
1314 | GError * error = NULL; |
---|
1315 | const char * name = "org.gnome.SessionManager"; |
---|
1316 | const char * path = "/org/gnome/SessionManager"; |
---|
1317 | const char * interface = "org.gnome.SessionManager"; |
---|
1318 | |
---|
1319 | conn = dbus_g_bus_get( DBUS_BUS_SESSION, &error ); |
---|
1320 | if( error ) |
---|
1321 | { |
---|
1322 | g_warning ( "DBUS cannot connect : %s", error->message ); |
---|
1323 | g_error_free ( error ); |
---|
1324 | return NULL; |
---|
1325 | } |
---|
1326 | |
---|
1327 | return dbus_g_proxy_new_for_name ( conn, name, path, interface ); |
---|
1328 | } |
---|
1329 | |
---|
1330 | static gboolean |
---|
1331 | gtr_inhibit_hibernation( guint * cookie ) |
---|
1332 | { |
---|
1333 | gboolean success = FALSE; |
---|
1334 | DBusGProxy * proxy = get_hibernation_inhibit_proxy( ); |
---|
1335 | |
---|
1336 | if( proxy ) |
---|
1337 | { |
---|
1338 | GError * error = NULL; |
---|
1339 | const int toplevel_xid = 0; |
---|
1340 | const char * application = _( "Transmission Bittorrent Client" ); |
---|
1341 | const char * reason = _( "BitTorrent Activity" ); |
---|
1342 | const int flags = 4; /* Inhibit suspending the session or computer */ |
---|
1343 | |
---|
1344 | success = dbus_g_proxy_call( proxy, "Inhibit", &error, |
---|
1345 | G_TYPE_STRING, application, |
---|
1346 | G_TYPE_UINT, toplevel_xid, |
---|
1347 | G_TYPE_STRING, reason, |
---|
1348 | G_TYPE_UINT, flags, |
---|
1349 | G_TYPE_INVALID, /* sentinel - end of input args */ |
---|
1350 | G_TYPE_UINT, cookie, |
---|
1351 | G_TYPE_INVALID /* senitnel - end of output args */ ); |
---|
1352 | if( success ) |
---|
1353 | tr_inf( "%s", _( "Disallowing desktop hibernation" ) ); |
---|
1354 | else |
---|
1355 | { |
---|
1356 | tr_err( _( "Couldn't disable desktop hibernation: %s" ), |
---|
1357 | error->message ); |
---|
1358 | g_error_free( error ); |
---|
1359 | } |
---|
1360 | |
---|
1361 | g_object_unref( G_OBJECT( proxy ) ); |
---|
1362 | } |
---|
1363 | |
---|
1364 | return success != 0; |
---|
1365 | } |
---|
1366 | |
---|
1367 | static void |
---|
1368 | gtr_uninhibit_hibernation( guint inhibit_cookie ) |
---|
1369 | { |
---|
1370 | DBusGProxy * proxy = get_hibernation_inhibit_proxy( ); |
---|
1371 | |
---|
1372 | if( proxy ) |
---|
1373 | { |
---|
1374 | GError * error = NULL; |
---|
1375 | gboolean success = dbus_g_proxy_call( proxy, "Uninhibit", &error, |
---|
1376 | G_TYPE_UINT, inhibit_cookie, |
---|
1377 | G_TYPE_INVALID, |
---|
1378 | G_TYPE_INVALID ); |
---|
1379 | if( success ) |
---|
1380 | tr_inf( "%s", _( "Allowing desktop hibernation" ) ); |
---|
1381 | else |
---|
1382 | { |
---|
1383 | g_warning( "Couldn't uninhibit the system from suspending: %s.", |
---|
1384 | error->message ); |
---|
1385 | g_error_free( error ); |
---|
1386 | } |
---|
1387 | |
---|
1388 | g_object_unref( G_OBJECT( proxy ) ); |
---|
1389 | } |
---|
1390 | } |
---|
1391 | |
---|
1392 | #endif |
---|
1393 | |
---|
1394 | static void |
---|
1395 | tr_core_set_hibernation_allowed( TrCore * core, |
---|
1396 | gboolean allowed ) |
---|
1397 | { |
---|
1398 | #ifdef HAVE_DBUS_GLIB |
---|
1399 | g_return_if_fail( core ); |
---|
1400 | g_return_if_fail( core->priv ); |
---|
1401 | |
---|
1402 | core->priv->inhibit_allowed = allowed != 0; |
---|
1403 | |
---|
1404 | if( allowed && core->priv->have_inhibit_cookie ) |
---|
1405 | { |
---|
1406 | gtr_uninhibit_hibernation( core->priv->inhibit_cookie ); |
---|
1407 | core->priv->have_inhibit_cookie = FALSE; |
---|
1408 | } |
---|
1409 | |
---|
1410 | if( !allowed |
---|
1411 | && !core->priv->have_inhibit_cookie |
---|
1412 | && !core->priv->dbus_error ) |
---|
1413 | { |
---|
1414 | if( gtr_inhibit_hibernation( &core->priv->inhibit_cookie ) ) |
---|
1415 | core->priv->have_inhibit_cookie = TRUE; |
---|
1416 | else |
---|
1417 | core->priv->dbus_error = TRUE; |
---|
1418 | } |
---|
1419 | #endif |
---|
1420 | } |
---|
1421 | |
---|
1422 | static void |
---|
1423 | maybeInhibitHibernation( TrCore * core ) |
---|
1424 | { |
---|
1425 | gboolean inhibit = pref_flag_get( PREF_KEY_INHIBIT_HIBERNATION ); |
---|
1426 | |
---|
1427 | /* always allow hibernation when all the torrents are paused */ |
---|
1428 | if( inhibit ) { |
---|
1429 | tr_session * session = tr_core_session( core ); |
---|
1430 | |
---|
1431 | if( tr_sessionGetActiveTorrentCount( session ) == 0 ) |
---|
1432 | inhibit = FALSE; |
---|
1433 | } |
---|
1434 | |
---|
1435 | tr_core_set_hibernation_allowed( core, !inhibit ); |
---|
1436 | } |
---|
1437 | |
---|
1438 | /** |
---|
1439 | *** Prefs |
---|
1440 | **/ |
---|
1441 | |
---|
1442 | static void |
---|
1443 | commitPrefsChange( TrCore * core, |
---|
1444 | const char * key ) |
---|
1445 | { |
---|
1446 | g_signal_emit( core, TR_CORE_GET_CLASS( core )->prefsig, 0, key ); |
---|
1447 | pref_save( tr_core_session( core ) ); |
---|
1448 | } |
---|
1449 | |
---|
1450 | void |
---|
1451 | tr_core_set_pref( TrCore * self, |
---|
1452 | const char * key, |
---|
1453 | const char * newval ) |
---|
1454 | { |
---|
1455 | const char * oldval = pref_string_get( key ); |
---|
1456 | |
---|
1457 | if( tr_strcmp( oldval, newval ) ) |
---|
1458 | { |
---|
1459 | pref_string_set( key, newval ); |
---|
1460 | commitPrefsChange( self, key ); |
---|
1461 | } |
---|
1462 | } |
---|
1463 | |
---|
1464 | void |
---|
1465 | tr_core_set_pref_bool( TrCore * self, |
---|
1466 | const char * key, |
---|
1467 | gboolean newval ) |
---|
1468 | { |
---|
1469 | const gboolean oldval = pref_flag_get( key ); |
---|
1470 | |
---|
1471 | if( oldval != newval ) |
---|
1472 | { |
---|
1473 | pref_flag_set( key, newval ); |
---|
1474 | commitPrefsChange( self, key ); |
---|
1475 | } |
---|
1476 | } |
---|
1477 | |
---|
1478 | void |
---|
1479 | tr_core_set_pref_int( TrCore * self, |
---|
1480 | const char * key, |
---|
1481 | int newval ) |
---|
1482 | { |
---|
1483 | const int oldval = pref_int_get( key ); |
---|
1484 | |
---|
1485 | if( oldval != newval ) |
---|
1486 | { |
---|
1487 | pref_int_set( key, newval ); |
---|
1488 | commitPrefsChange( self, key ); |
---|
1489 | } |
---|
1490 | } |
---|
1491 | |
---|
1492 | void |
---|
1493 | tr_core_set_pref_double( TrCore * self, |
---|
1494 | const char * key, |
---|
1495 | double newval ) |
---|
1496 | { |
---|
1497 | const double oldval = pref_double_get( key ); |
---|
1498 | |
---|
1499 | if( oldval != newval ) |
---|
1500 | { |
---|
1501 | pref_double_set( key, newval ); |
---|
1502 | commitPrefsChange( self, key ); |
---|
1503 | } |
---|
1504 | } |
---|
1505 | |
---|
1506 | /*** |
---|
1507 | **** |
---|
1508 | **** RPC Interface |
---|
1509 | **** |
---|
1510 | ***/ |
---|
1511 | |
---|
1512 | /* #define DEBUG_RPC */ |
---|
1513 | |
---|
1514 | static int nextTag = 1; |
---|
1515 | |
---|
1516 | typedef void ( server_response_func )( TrCore * core, tr_benc * response, gpointer user_data ); |
---|
1517 | |
---|
1518 | struct pending_request_data |
---|
1519 | { |
---|
1520 | TrCore * core; |
---|
1521 | server_response_func * responseFunc; |
---|
1522 | gpointer responseFuncUserData; |
---|
1523 | }; |
---|
1524 | |
---|
1525 | static GHashTable * pendingRequests = NULL; |
---|
1526 | |
---|
1527 | static gboolean |
---|
1528 | readResponseIdle( void * vresponse ) |
---|
1529 | { |
---|
1530 | GByteArray * response; |
---|
1531 | tr_benc top; |
---|
1532 | int64_t intVal; |
---|
1533 | int tag; |
---|
1534 | struct pending_request_data * data; |
---|
1535 | |
---|
1536 | response = vresponse; |
---|
1537 | tr_jsonParse( NULL, response->data, response->len, &top, NULL ); |
---|
1538 | tr_bencDictFindInt( &top, "tag", &intVal ); |
---|
1539 | tag = (int)intVal; |
---|
1540 | |
---|
1541 | data = g_hash_table_lookup( pendingRequests, &tag ); |
---|
1542 | if( data && data->responseFunc ) |
---|
1543 | (*data->responseFunc)(data->core, &top, data->responseFuncUserData ); |
---|
1544 | |
---|
1545 | tr_bencFree( &top ); |
---|
1546 | g_hash_table_remove( pendingRequests, &tag ); |
---|
1547 | g_byte_array_free( response, TRUE ); |
---|
1548 | return FALSE; |
---|
1549 | } |
---|
1550 | |
---|
1551 | static void |
---|
1552 | readResponse( tr_session * session UNUSED, |
---|
1553 | const char * response, |
---|
1554 | size_t response_len, |
---|
1555 | void * unused UNUSED ) |
---|
1556 | { |
---|
1557 | GByteArray * bytes = g_byte_array_new( ); |
---|
1558 | #ifdef DEBUG_RPC |
---|
1559 | g_message( "response: [%*.*s]", (int)response_len, (int)response_len, response ); |
---|
1560 | #endif |
---|
1561 | g_byte_array_append( bytes, (const uint8_t*)response, response_len ); |
---|
1562 | gtr_idle_add( readResponseIdle, bytes ); |
---|
1563 | } |
---|
1564 | |
---|
1565 | static void |
---|
1566 | sendRequest( TrCore * core, const char * json, int tag, |
---|
1567 | server_response_func * responseFunc, void * responseFuncUserData ) |
---|
1568 | { |
---|
1569 | tr_session * session = tr_core_session( core ); |
---|
1570 | |
---|
1571 | if( pendingRequests == NULL ) |
---|
1572 | { |
---|
1573 | pendingRequests = g_hash_table_new_full( g_int_hash, g_int_equal, g_free, g_free ); |
---|
1574 | } |
---|
1575 | |
---|
1576 | if( session == NULL ) |
---|
1577 | { |
---|
1578 | g_error( "GTK+ client doesn't support connections to remote servers yet." ); |
---|
1579 | } |
---|
1580 | else |
---|
1581 | { |
---|
1582 | /* remember this request */ |
---|
1583 | struct pending_request_data * data; |
---|
1584 | data = g_new0( struct pending_request_data, 1 ); |
---|
1585 | data->core = core; |
---|
1586 | data->responseFunc = responseFunc; |
---|
1587 | data->responseFuncUserData = responseFuncUserData; |
---|
1588 | g_hash_table_insert( pendingRequests, g_memdup( &tag, sizeof( int ) ), data ); |
---|
1589 | |
---|
1590 | /* make the request */ |
---|
1591 | #ifdef DEBUG_RPC |
---|
1592 | g_message( "request: [%s]", json ); |
---|
1593 | #endif |
---|
1594 | tr_rpc_request_exec_json( session, json, strlen( json ), readResponse, GINT_TO_POINTER(tag) ); |
---|
1595 | } |
---|
1596 | } |
---|
1597 | |
---|
1598 | /*** |
---|
1599 | **** Sending a test-port request via RPC |
---|
1600 | ***/ |
---|
1601 | |
---|
1602 | static void |
---|
1603 | portTestResponseFunc( TrCore * core, tr_benc * response, gpointer userData UNUSED ) |
---|
1604 | { |
---|
1605 | tr_benc * args; |
---|
1606 | tr_bool isOpen = FALSE; |
---|
1607 | |
---|
1608 | if( tr_bencDictFindDict( response, "arguments", &args ) ) |
---|
1609 | tr_bencDictFindBool( args, "port-is-open", &isOpen ); |
---|
1610 | |
---|
1611 | emitPortTested( core, isOpen ); |
---|
1612 | } |
---|
1613 | |
---|
1614 | void |
---|
1615 | tr_core_port_test( TrCore * core ) |
---|
1616 | { |
---|
1617 | char buf[128]; |
---|
1618 | const int tag = nextTag++; |
---|
1619 | g_snprintf( buf, sizeof( buf ), "{ \"method\": \"port-test\", \"tag\": %d }", tag ); |
---|
1620 | sendRequest( core, buf, tag, portTestResponseFunc, NULL ); |
---|
1621 | } |
---|
1622 | |
---|
1623 | /*** |
---|
1624 | **** Updating a blocklist via RPC |
---|
1625 | ***/ |
---|
1626 | |
---|
1627 | static void |
---|
1628 | blocklistResponseFunc( TrCore * core, tr_benc * response, gpointer userData UNUSED ) |
---|
1629 | { |
---|
1630 | tr_benc * args; |
---|
1631 | int64_t ruleCount = 0; |
---|
1632 | |
---|
1633 | if( tr_bencDictFindDict( response, "arguments", &args ) ) |
---|
1634 | tr_bencDictFindInt( args, "blocklist-size", &ruleCount ); |
---|
1635 | |
---|
1636 | if( ruleCount > 0 ) |
---|
1637 | pref_int_set( "blocklist-date", time( NULL ) ); |
---|
1638 | |
---|
1639 | emitBlocklistUpdated( core, ruleCount ); |
---|
1640 | } |
---|
1641 | |
---|
1642 | void |
---|
1643 | tr_core_blocklist_update( TrCore * core ) |
---|
1644 | { |
---|
1645 | char buf[128]; |
---|
1646 | const int tag = nextTag++; |
---|
1647 | g_snprintf( buf, sizeof( buf ), "{ \"method\": \"blocklist-update\", \"tag\": %d }", tag ); |
---|
1648 | sendRequest( core, buf, tag, blocklistResponseFunc, NULL ); |
---|
1649 | } |
---|
1650 | |
---|
1651 | /*** |
---|
1652 | **** |
---|
1653 | ***/ |
---|
1654 | |
---|
1655 | void |
---|
1656 | tr_core_exec_json( TrCore * core, const char * json ) |
---|
1657 | { |
---|
1658 | const int tag = nextTag++; |
---|
1659 | sendRequest( core, json, tag, NULL, NULL ); |
---|
1660 | } |
---|
1661 | |
---|
1662 | void |
---|
1663 | tr_core_exec( TrCore * core, const tr_benc * top ) |
---|
1664 | { |
---|
1665 | char * json = tr_bencToStr( top, TR_FMT_JSON_LEAN, NULL ); |
---|
1666 | tr_core_exec_json( core, json ); |
---|
1667 | tr_free( json ); |
---|
1668 | } |
---|