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