1 | /****************************************************************************** |
---|
2 | * $Id: tr-core.c 11067 2010-07-30 22:19:20Z 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 int |
---|
546 | tr_strcmp( const void * a, |
---|
547 | const void * b ) |
---|
548 | { |
---|
549 | if( a && b ) return strcmp( a, b ); |
---|
550 | if( a ) return 1; |
---|
551 | if( b ) return -1; |
---|
552 | return 0; |
---|
553 | } |
---|
554 | |
---|
555 | #ifdef HAVE_GIO |
---|
556 | |
---|
557 | struct watchdir_file |
---|
558 | { |
---|
559 | char * filename; |
---|
560 | time_t mtime; |
---|
561 | }; |
---|
562 | |
---|
563 | static int |
---|
564 | compare_watchdir_file_to_filename( const void * a, const void * filename ) |
---|
565 | { |
---|
566 | return strcmp( ((const struct watchdir_file*)a)->filename, filename ); |
---|
567 | } |
---|
568 | |
---|
569 | static void |
---|
570 | watchdir_file_update_mtime( struct watchdir_file * file ) |
---|
571 | { |
---|
572 | GFile * gfile = g_file_new_for_path( file->filename ); |
---|
573 | GFileInfo * info = g_file_query_info( gfile, G_FILE_ATTRIBUTE_TIME_MODIFIED, 0, NULL, NULL ); |
---|
574 | |
---|
575 | file->mtime = g_file_info_get_attribute_uint64( info, G_FILE_ATTRIBUTE_TIME_MODIFIED ); |
---|
576 | |
---|
577 | g_object_unref( G_OBJECT( info ) ); |
---|
578 | g_object_unref( G_OBJECT( gfile ) ); |
---|
579 | } |
---|
580 | |
---|
581 | static struct watchdir_file* |
---|
582 | watchdir_file_new( const char * filename ) |
---|
583 | { |
---|
584 | struct watchdir_file * f; |
---|
585 | |
---|
586 | f = g_new( struct watchdir_file, 1 ); |
---|
587 | f->filename = g_strdup( filename ); |
---|
588 | watchdir_file_update_mtime( f ); |
---|
589 | |
---|
590 | return f; |
---|
591 | } |
---|
592 | |
---|
593 | static void |
---|
594 | watchdir_file_free( struct watchdir_file * f ) |
---|
595 | { |
---|
596 | g_free( f->filename ); |
---|
597 | g_free( f ); |
---|
598 | } |
---|
599 | |
---|
600 | static gboolean |
---|
601 | watchFolderIdle( gpointer gcore ) |
---|
602 | { |
---|
603 | GSList * l; |
---|
604 | GSList * addme = NULL; |
---|
605 | GSList * monitor_files = NULL; |
---|
606 | TrCore * core = TR_CORE( gcore ); |
---|
607 | const time_t now = time( NULL ); |
---|
608 | struct TrCorePrivate * p = core->priv; |
---|
609 | |
---|
610 | /* of the monitor_files, make a list of those that haven't |
---|
611 | * changed lately, since they should be ready to add */ |
---|
612 | for( l=p->monitor_files; l!=NULL; l=l->next ) { |
---|
613 | struct watchdir_file * f = l->data; |
---|
614 | watchdir_file_update_mtime( f ); |
---|
615 | if( f->mtime + 2 >= now ) |
---|
616 | monitor_files = g_slist_prepend( monitor_files, f ); |
---|
617 | else { |
---|
618 | addme = g_slist_prepend( addme, g_strdup( f->filename ) ); |
---|
619 | watchdir_file_free( f ); |
---|
620 | } |
---|
621 | } |
---|
622 | |
---|
623 | /* add the torrents from that list */ |
---|
624 | core->priv->adding_from_watch_dir = TRUE; |
---|
625 | tr_core_add_list_defaults( core, addme, TRUE ); |
---|
626 | core->priv->adding_from_watch_dir = FALSE; |
---|
627 | |
---|
628 | /* update the monitor_files list */ |
---|
629 | g_slist_free( p->monitor_files ); |
---|
630 | p->monitor_files = monitor_files; |
---|
631 | |
---|
632 | /* if monitor_files is nonempty, keep checking every second */ |
---|
633 | if( core->priv->monitor_files ) |
---|
634 | return TRUE; |
---|
635 | core->priv->monitor_idle_tag = 0; |
---|
636 | return FALSE; |
---|
637 | |
---|
638 | } |
---|
639 | |
---|
640 | static void |
---|
641 | maybeAddTorrent( TrCore * core, const char * filename ) |
---|
642 | { |
---|
643 | const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" ); |
---|
644 | |
---|
645 | if( isTorrent ) |
---|
646 | { |
---|
647 | struct TrCorePrivate * p = core->priv; |
---|
648 | |
---|
649 | if( !g_slist_find_custom( p->monitor_files, filename, (GCompareFunc)compare_watchdir_file_to_filename ) ) |
---|
650 | p->monitor_files = g_slist_append( p->monitor_files, watchdir_file_new( filename ) ); |
---|
651 | |
---|
652 | if( !p->monitor_idle_tag ) |
---|
653 | p->monitor_idle_tag = gtr_timeout_add_seconds( 1, watchFolderIdle, core ); |
---|
654 | } |
---|
655 | } |
---|
656 | |
---|
657 | static void |
---|
658 | watchFolderChanged( GFileMonitor * monitor UNUSED, |
---|
659 | GFile * file, |
---|
660 | GFile * other_type UNUSED, |
---|
661 | GFileMonitorEvent event_type, |
---|
662 | gpointer core ) |
---|
663 | { |
---|
664 | if( event_type == G_FILE_MONITOR_EVENT_CREATED ) |
---|
665 | { |
---|
666 | char * filename = g_file_get_path( file ); |
---|
667 | maybeAddTorrent( core, filename ); |
---|
668 | g_free( filename ); |
---|
669 | } |
---|
670 | } |
---|
671 | |
---|
672 | static void |
---|
673 | scanWatchDir( TrCore * core ) |
---|
674 | { |
---|
675 | const gboolean isEnabled = pref_flag_get( PREF_KEY_DIR_WATCH_ENABLED ); |
---|
676 | |
---|
677 | if( isEnabled ) |
---|
678 | { |
---|
679 | const char * dirname = pref_string_get( PREF_KEY_DIR_WATCH ); |
---|
680 | GDir * dir = g_dir_open( dirname, 0, NULL ); |
---|
681 | |
---|
682 | if( dir != NULL ) |
---|
683 | { |
---|
684 | const char * basename; |
---|
685 | while(( basename = g_dir_read_name( dir ))) |
---|
686 | { |
---|
687 | char * filename = g_build_filename( dirname, basename, NULL ); |
---|
688 | maybeAddTorrent( core, filename ); |
---|
689 | g_free( filename ); |
---|
690 | } |
---|
691 | |
---|
692 | g_dir_close( dir ); |
---|
693 | } |
---|
694 | } |
---|
695 | } |
---|
696 | |
---|
697 | static void |
---|
698 | updateWatchDir( TrCore * core ) |
---|
699 | { |
---|
700 | const char * filename = pref_string_get( PREF_KEY_DIR_WATCH ); |
---|
701 | const gboolean isEnabled = pref_flag_get( |
---|
702 | PREF_KEY_DIR_WATCH_ENABLED ); |
---|
703 | struct TrCorePrivate * p = TR_CORE( core )->priv; |
---|
704 | |
---|
705 | if( p->monitor && ( !isEnabled || tr_strcmp( filename, p->monitor_path ) ) ) |
---|
706 | { |
---|
707 | g_signal_handler_disconnect( p->monitor, p->monitor_tag ); |
---|
708 | g_free( p->monitor_path ); |
---|
709 | g_file_monitor_cancel( p->monitor ); |
---|
710 | g_object_unref( G_OBJECT( p->monitor ) ); |
---|
711 | p->monitor_path = NULL; |
---|
712 | p->monitor = NULL; |
---|
713 | p->monitor_tag = 0; |
---|
714 | } |
---|
715 | |
---|
716 | if( isEnabled && !p->monitor ) |
---|
717 | { |
---|
718 | GFile * file = g_file_new_for_path( filename ); |
---|
719 | GFileMonitor * m = g_file_monitor_directory( file, 0, NULL, NULL ); |
---|
720 | scanWatchDir( core ); |
---|
721 | p->monitor = m; |
---|
722 | p->monitor_path = g_strdup( filename ); |
---|
723 | p->monitor_tag = g_signal_connect( m, "changed", |
---|
724 | G_CALLBACK( |
---|
725 | watchFolderChanged ), core ); |
---|
726 | } |
---|
727 | } |
---|
728 | |
---|
729 | #endif |
---|
730 | |
---|
731 | static void |
---|
732 | prefsChanged( TrCore * core, |
---|
733 | const char * key, |
---|
734 | gpointer data UNUSED ) |
---|
735 | { |
---|
736 | if( !strcmp( key, PREF_KEY_SORT_MODE ) |
---|
737 | || !strcmp( key, PREF_KEY_SORT_REVERSED ) ) |
---|
738 | { |
---|
739 | const char * mode = pref_string_get( PREF_KEY_SORT_MODE ); |
---|
740 | gboolean isReversed = pref_flag_get( PREF_KEY_SORT_REVERSED ); |
---|
741 | setSort( core, mode, isReversed ); |
---|
742 | } |
---|
743 | else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_GLOBAL ) ) |
---|
744 | { |
---|
745 | const uint16_t val = pref_int_get( key ); |
---|
746 | tr_sessionSetPeerLimit( tr_core_session( core ), val ); |
---|
747 | } |
---|
748 | else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_TORRENT ) ) |
---|
749 | { |
---|
750 | const uint16_t val = pref_int_get( key ); |
---|
751 | tr_sessionSetPeerLimitPerTorrent( tr_core_session( core ), val ); |
---|
752 | } |
---|
753 | else if( !strcmp( key, PREF_KEY_INHIBIT_HIBERNATION ) ) |
---|
754 | { |
---|
755 | maybeInhibitHibernation( core ); |
---|
756 | } |
---|
757 | #ifdef HAVE_GIO |
---|
758 | else if( !strcmp( key, PREF_KEY_DIR_WATCH ) |
---|
759 | || !strcmp( key, PREF_KEY_DIR_WATCH_ENABLED ) ) |
---|
760 | { |
---|
761 | updateWatchDir( core ); |
---|
762 | } |
---|
763 | #endif |
---|
764 | } |
---|
765 | |
---|
766 | static void |
---|
767 | tr_core_init( GTypeInstance * instance, |
---|
768 | gpointer g_class UNUSED ) |
---|
769 | { |
---|
770 | GtkListStore * store; |
---|
771 | struct TrCorePrivate * p; |
---|
772 | TrCore * self = (TrCore *) instance; |
---|
773 | |
---|
774 | /* column types for the model used to store torrent information */ |
---|
775 | /* keep this in sync with the enum near the bottom of tr_core.h */ |
---|
776 | GType types[] = { G_TYPE_STRING, /* name */ |
---|
777 | G_TYPE_STRING, /* collated name */ |
---|
778 | TR_TORRENT_TYPE, /* TrTorrent object */ |
---|
779 | G_TYPE_POINTER, /* tr_torrent* */ |
---|
780 | G_TYPE_DOUBLE, /* tr_stat.pieceUploadSpeed_KBps */ |
---|
781 | G_TYPE_DOUBLE, /* tr_stat.pieceDownloadSpeed_KBps */ |
---|
782 | G_TYPE_INT }; /* tr_stat.status */ |
---|
783 | |
---|
784 | p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE( self, |
---|
785 | TR_CORE_TYPE, |
---|
786 | struct TrCorePrivate ); |
---|
787 | |
---|
788 | /* create the model used to store torrent data */ |
---|
789 | g_assert( G_N_ELEMENTS( types ) == MC_ROW_COUNT ); |
---|
790 | store = gtk_list_store_newv( MC_ROW_COUNT, types ); |
---|
791 | |
---|
792 | p->model = GTK_TREE_MODEL( store ); |
---|
793 | |
---|
794 | #ifdef HAVE_DBUS_GLIB |
---|
795 | if( our_instance_adds_remote_torrents ) |
---|
796 | { |
---|
797 | DBusGConnection * bus = dbus_g_bus_get( DBUS_BUS_SESSION, NULL ); |
---|
798 | if( bus ) |
---|
799 | dbus_g_connection_register_g_object( |
---|
800 | bus, |
---|
801 | "/com/transmissionbt/Transmission", |
---|
802 | G_OBJECT( self ) ); |
---|
803 | } |
---|
804 | #endif |
---|
805 | } |
---|
806 | |
---|
807 | GType |
---|
808 | tr_core_get_type( void ) |
---|
809 | { |
---|
810 | static GType type = 0; |
---|
811 | |
---|
812 | if( !type ) |
---|
813 | { |
---|
814 | static const GTypeInfo info = |
---|
815 | { |
---|
816 | sizeof( TrCoreClass ), |
---|
817 | NULL, /* base_init */ |
---|
818 | NULL, /* base_finalize */ |
---|
819 | tr_core_class_init, /* class_init */ |
---|
820 | NULL, /* class_finalize */ |
---|
821 | NULL, /* class_data */ |
---|
822 | sizeof( TrCore ), |
---|
823 | 0, /* n_preallocs */ |
---|
824 | tr_core_init, /* instance_init */ |
---|
825 | NULL, |
---|
826 | }; |
---|
827 | type = g_type_register_static( G_TYPE_OBJECT, "TrCore", &info, 0 ); |
---|
828 | } |
---|
829 | |
---|
830 | return type; |
---|
831 | } |
---|
832 | |
---|
833 | /** |
---|
834 | *** |
---|
835 | **/ |
---|
836 | |
---|
837 | TrCore * |
---|
838 | tr_core_new( tr_session * session ) |
---|
839 | { |
---|
840 | TrCore * core = TR_CORE( g_object_new( TR_CORE_TYPE, NULL ) ); |
---|
841 | |
---|
842 | core->priv->session = session; |
---|
843 | |
---|
844 | /* init from prefs & listen to pref changes */ |
---|
845 | prefsChanged( core, PREF_KEY_SORT_MODE, NULL ); |
---|
846 | prefsChanged( core, PREF_KEY_SORT_REVERSED, NULL ); |
---|
847 | prefsChanged( core, PREF_KEY_DIR_WATCH_ENABLED, NULL ); |
---|
848 | prefsChanged( core, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, NULL ); |
---|
849 | prefsChanged( core, PREF_KEY_INHIBIT_HIBERNATION, NULL ); |
---|
850 | g_signal_connect( core, "prefs-changed", G_CALLBACK( prefsChanged ), NULL ); |
---|
851 | |
---|
852 | return core; |
---|
853 | } |
---|
854 | |
---|
855 | void |
---|
856 | tr_core_close( TrCore * core ) |
---|
857 | { |
---|
858 | tr_session * session = tr_core_session( core ); |
---|
859 | |
---|
860 | if( session ) |
---|
861 | { |
---|
862 | core->priv->session = NULL; |
---|
863 | pref_save( session ); |
---|
864 | tr_sessionClose( session ); |
---|
865 | } |
---|
866 | } |
---|
867 | |
---|
868 | GtkTreeModel * |
---|
869 | tr_core_model( TrCore * core ) |
---|
870 | { |
---|
871 | return isDisposed( core ) ? NULL : core->priv->model; |
---|
872 | } |
---|
873 | |
---|
874 | tr_session * |
---|
875 | tr_core_session( TrCore * core ) |
---|
876 | { |
---|
877 | return isDisposed( core ) ? NULL : core->priv->session; |
---|
878 | } |
---|
879 | |
---|
880 | void |
---|
881 | tr_core_add_torrent( TrCore * self, |
---|
882 | TrTorrent * gtor, |
---|
883 | gboolean doNotify ) |
---|
884 | { |
---|
885 | const tr_info * inf = tr_torrent_info( gtor ); |
---|
886 | const tr_stat * st = tr_torrent_stat( gtor ); |
---|
887 | tr_torrent * tor = tr_torrent_handle( gtor ); |
---|
888 | char * collated = g_utf8_strdown( inf->name, -1 ); |
---|
889 | GtkListStore * store = GTK_LIST_STORE( tr_core_model( self ) ); |
---|
890 | GtkTreeIter unused; |
---|
891 | |
---|
892 | gtk_list_store_insert_with_values( store, &unused, 0, |
---|
893 | MC_NAME, inf->name, |
---|
894 | MC_NAME_COLLATED, collated, |
---|
895 | MC_TORRENT, gtor, |
---|
896 | MC_TORRENT_RAW, tor, |
---|
897 | MC_SPEED_UP, st->pieceUploadSpeed_KBps, |
---|
898 | MC_SPEED_DOWN, st->pieceDownloadSpeed_KBps, |
---|
899 | MC_ACTIVITY, st->activity, |
---|
900 | -1 ); |
---|
901 | |
---|
902 | if( doNotify ) |
---|
903 | tr_notify_added( inf->name ); |
---|
904 | |
---|
905 | /* cleanup */ |
---|
906 | g_object_unref( G_OBJECT( gtor ) ); |
---|
907 | g_free( collated ); |
---|
908 | } |
---|
909 | |
---|
910 | int |
---|
911 | tr_core_load( TrCore * self, gboolean forcePaused ) |
---|
912 | { |
---|
913 | int i; |
---|
914 | int count = 0; |
---|
915 | tr_torrent ** torrents; |
---|
916 | tr_ctor * ctor; |
---|
917 | |
---|
918 | ctor = tr_ctorNew( tr_core_session( self ) ); |
---|
919 | if( forcePaused ) |
---|
920 | tr_ctorSetPaused( ctor, TR_FORCE, TRUE ); |
---|
921 | tr_ctorSetPeerLimit( ctor, TR_FALLBACK, |
---|
922 | pref_int_get( TR_PREFS_KEY_PEER_LIMIT_TORRENT ) ); |
---|
923 | |
---|
924 | torrents = tr_sessionLoadTorrents ( tr_core_session( self ), ctor, &count ); |
---|
925 | for( i = 0; i < count; ++i ) |
---|
926 | tr_core_add_torrent( self, tr_torrent_new_preexisting( torrents[i] ), FALSE ); |
---|
927 | |
---|
928 | tr_free( torrents ); |
---|
929 | tr_ctorFree( ctor ); |
---|
930 | |
---|
931 | return count; |
---|
932 | } |
---|
933 | |
---|
934 | /*** |
---|
935 | **** |
---|
936 | ***/ |
---|
937 | |
---|
938 | static void |
---|
939 | emitBlocklistUpdated( TrCore * core, int ruleCount ) |
---|
940 | { |
---|
941 | g_signal_emit( core, core_signals[BLOCKLIST_SIGNAL], 0, ruleCount ); |
---|
942 | } |
---|
943 | |
---|
944 | static void |
---|
945 | emitPortTested( TrCore * core, gboolean isOpen ) |
---|
946 | { |
---|
947 | g_signal_emit( core, core_signals[PORT_SIGNAL], 0, isOpen ); |
---|
948 | } |
---|
949 | |
---|
950 | static void |
---|
951 | tr_core_errsig( TrCore * core, enum tr_core_err type, const char * msg ) |
---|
952 | { |
---|
953 | g_signal_emit( core, core_signals[ADD_ERROR_SIGNAL], 0, type, msg ); |
---|
954 | } |
---|
955 | |
---|
956 | static int |
---|
957 | add_ctor( TrCore * core, tr_ctor * ctor, gboolean doPrompt, gboolean doNotify ) |
---|
958 | { |
---|
959 | tr_info inf; |
---|
960 | int err = tr_torrentParse( ctor, &inf ); |
---|
961 | |
---|
962 | switch( err ) |
---|
963 | { |
---|
964 | case TR_PARSE_ERR: |
---|
965 | break; |
---|
966 | |
---|
967 | case TR_PARSE_DUPLICATE: |
---|
968 | /* don't complain about .torrent files in the watch directory |
---|
969 | * that have already been added... that gets annoying and we |
---|
970 | * don't want to be nagging users to clean up their watch dirs */ |
---|
971 | if( !tr_ctorGetSourceFile(ctor) || !core->priv->adding_from_watch_dir ) |
---|
972 | tr_core_errsig( core, err, inf.name ); |
---|
973 | tr_metainfoFree( &inf ); |
---|
974 | break; |
---|
975 | |
---|
976 | default: |
---|
977 | if( doPrompt ) |
---|
978 | g_signal_emit( core, core_signals[ADD_PROMPT_SIGNAL], 0, ctor ); |
---|
979 | else { |
---|
980 | tr_session * session = tr_core_session( core ); |
---|
981 | TrTorrent * gtor = tr_torrent_new_ctor( session, ctor, &err ); |
---|
982 | if( !err ) |
---|
983 | tr_core_add_torrent( core, gtor, doNotify ); |
---|
984 | } |
---|
985 | tr_metainfoFree( &inf ); |
---|
986 | break; |
---|
987 | } |
---|
988 | |
---|
989 | return err; |
---|
990 | } |
---|
991 | |
---|
992 | void |
---|
993 | tr_core_add_ctor( TrCore * core, tr_ctor * ctor ) |
---|
994 | { |
---|
995 | const gboolean doPrompt = pref_flag_get( PREF_KEY_OPTIONS_PROMPT ); |
---|
996 | const gboolean doNotify = FALSE; |
---|
997 | tr_core_apply_defaults( ctor ); |
---|
998 | add_ctor( core, ctor, doPrompt, doNotify ); |
---|
999 | } |
---|
1000 | |
---|
1001 | /* invoked remotely via dbus. */ |
---|
1002 | gboolean |
---|
1003 | tr_core_add_metainfo( TrCore * core, |
---|
1004 | const char * payload, |
---|
1005 | const char * filename, |
---|
1006 | gboolean * setme_handled, |
---|
1007 | GError ** gerr UNUSED ) |
---|
1008 | { |
---|
1009 | tr_session * session = tr_core_session( core ); |
---|
1010 | |
---|
1011 | if( !session ) |
---|
1012 | { |
---|
1013 | *setme_handled = FALSE; |
---|
1014 | } |
---|
1015 | else if( gtr_is_supported_url( payload ) || gtr_is_magnet_link( payload ) ) |
---|
1016 | { |
---|
1017 | tr_core_add_from_url( core, payload ); |
---|
1018 | *setme_handled = TRUE; |
---|
1019 | } |
---|
1020 | else |
---|
1021 | { |
---|
1022 | tr_ctor * ctor; |
---|
1023 | gboolean has_metainfo = FALSE; |
---|
1024 | const gboolean do_prompt = pref_flag_get( PREF_KEY_OPTIONS_PROMPT ); |
---|
1025 | |
---|
1026 | /* create the constructor */ |
---|
1027 | ctor = tr_ctorNew( session ); |
---|
1028 | tr_core_apply_defaults( ctor ); |
---|
1029 | |
---|
1030 | if( !has_metainfo && g_file_test( filename, G_FILE_TEST_IS_REGULAR ) ) |
---|
1031 | { |
---|
1032 | /* set the metainfo from a local file */ |
---|
1033 | has_metainfo = !tr_ctorSetMetainfoFromFile( ctor, filename ); |
---|
1034 | } |
---|
1035 | |
---|
1036 | if( !has_metainfo ) |
---|
1037 | { |
---|
1038 | /* base64-encoded metainfo */ |
---|
1039 | int file_length; |
---|
1040 | char * file_contents = tr_base64_decode( payload, -1, &file_length ); |
---|
1041 | has_metainfo = !tr_ctorSetMetainfo( ctor, (const uint8_t*)file_contents, file_length ); |
---|
1042 | tr_free( file_contents ); |
---|
1043 | } |
---|
1044 | |
---|
1045 | if( has_metainfo ) |
---|
1046 | { |
---|
1047 | add_ctor( core, ctor, do_prompt, TRUE ); |
---|
1048 | tr_core_torrents_added( core ); |
---|
1049 | } |
---|
1050 | |
---|
1051 | *setme_handled = has_metainfo; |
---|
1052 | } |
---|
1053 | |
---|
1054 | return TRUE; |
---|
1055 | } |
---|
1056 | |
---|
1057 | /*** |
---|
1058 | **** |
---|
1059 | ***/ |
---|
1060 | |
---|
1061 | struct url_dialog_data |
---|
1062 | { |
---|
1063 | TrCore * core; |
---|
1064 | tr_ctor * ctor; |
---|
1065 | char * url; |
---|
1066 | long response_code; |
---|
1067 | }; |
---|
1068 | |
---|
1069 | static gboolean |
---|
1070 | onURLDoneIdle( gpointer vdata ) |
---|
1071 | { |
---|
1072 | struct url_dialog_data * data = vdata; |
---|
1073 | |
---|
1074 | if( data->response_code != 200 ) |
---|
1075 | { |
---|
1076 | gtr_http_failure_dialog( NULL, data->url, data->response_code ); |
---|
1077 | } |
---|
1078 | else |
---|
1079 | { |
---|
1080 | const gboolean doPrompt = pref_flag_get( PREF_KEY_OPTIONS_PROMPT ); |
---|
1081 | const gboolean doNotify = FALSE; |
---|
1082 | const int err = add_ctor( data->core, data->ctor, doPrompt, doNotify ); |
---|
1083 | |
---|
1084 | if( err == TR_PARSE_ERR ) |
---|
1085 | tr_core_errsig( data->core, TR_PARSE_ERR, data->url ); |
---|
1086 | |
---|
1087 | tr_core_torrents_added( data->core ); |
---|
1088 | } |
---|
1089 | |
---|
1090 | /* cleanup */ |
---|
1091 | coreDecBusy( data->core ); |
---|
1092 | g_free( data->url ); |
---|
1093 | g_free( data ); |
---|
1094 | return FALSE; |
---|
1095 | } |
---|
1096 | |
---|
1097 | static void |
---|
1098 | onURLDone( tr_session * session, |
---|
1099 | long response_code, |
---|
1100 | const void * response, |
---|
1101 | size_t response_byte_count, |
---|
1102 | void * vdata ) |
---|
1103 | { |
---|
1104 | struct url_dialog_data * data = vdata; |
---|
1105 | |
---|
1106 | data->response_code = response_code; |
---|
1107 | data->ctor = tr_ctorNew( session ); |
---|
1108 | tr_core_apply_defaults( data->ctor ); |
---|
1109 | tr_ctorSetMetainfo( data->ctor, response, response_byte_count ); |
---|
1110 | |
---|
1111 | gtr_idle_add( onURLDoneIdle, data ); |
---|
1112 | } |
---|
1113 | |
---|
1114 | void |
---|
1115 | tr_core_add_from_url( TrCore * core, const char * url ) |
---|
1116 | { |
---|
1117 | tr_session * session = tr_core_session( core ); |
---|
1118 | const gboolean is_magnet_link = gtr_is_magnet_link( url ); |
---|
1119 | |
---|
1120 | if( is_magnet_link || gtr_is_hex_hashcode( url ) ) |
---|
1121 | { |
---|
1122 | int err; |
---|
1123 | char * tmp = NULL; |
---|
1124 | tr_ctor * ctor = tr_ctorNew( session ); |
---|
1125 | |
---|
1126 | if( gtr_is_hex_hashcode( url ) ) |
---|
1127 | url = tmp = g_strdup_printf( "magnet:?xt=urn:btih:%s", url ); |
---|
1128 | |
---|
1129 | err = tr_ctorSetMetainfoFromMagnetLink( ctor, url ); |
---|
1130 | |
---|
1131 | if( !err ) |
---|
1132 | tr_core_add_ctor( core, ctor ); |
---|
1133 | else { |
---|
1134 | gtr_unrecognized_url_dialog( NULL, url ); |
---|
1135 | tr_ctorFree( ctor ); |
---|
1136 | } |
---|
1137 | |
---|
1138 | g_free( tmp ); |
---|
1139 | } |
---|
1140 | else |
---|
1141 | { |
---|
1142 | struct url_dialog_data * data = g_new( struct url_dialog_data, 1 ); |
---|
1143 | data->core = core; |
---|
1144 | data->url = g_strdup( url ); |
---|
1145 | coreIncBusy( data->core ); |
---|
1146 | tr_webRun( session, url, NULL, onURLDone, data ); |
---|
1147 | } |
---|
1148 | } |
---|
1149 | |
---|
1150 | /*** |
---|
1151 | **** |
---|
1152 | ***/ |
---|
1153 | |
---|
1154 | static void |
---|
1155 | add_filename( TrCore * core, |
---|
1156 | const char * filename, |
---|
1157 | gboolean doStart, |
---|
1158 | gboolean doPrompt, |
---|
1159 | gboolean doNotify ) |
---|
1160 | { |
---|
1161 | tr_session * session = tr_core_session( core ); |
---|
1162 | |
---|
1163 | if( session == NULL ) |
---|
1164 | return; |
---|
1165 | |
---|
1166 | if( gtr_is_supported_url( filename ) || gtr_is_magnet_link( filename ) ) |
---|
1167 | { |
---|
1168 | tr_core_add_from_url( core, filename ); |
---|
1169 | } |
---|
1170 | else if( g_file_test( filename, G_FILE_TEST_EXISTS ) ) |
---|
1171 | { |
---|
1172 | int err; |
---|
1173 | |
---|
1174 | tr_ctor * ctor = tr_ctorNew( session ); |
---|
1175 | tr_ctorSetMetainfoFromFile( ctor, filename ); |
---|
1176 | tr_core_apply_defaults( ctor ); |
---|
1177 | tr_ctorSetPaused( ctor, TR_FORCE, !doStart ); |
---|
1178 | |
---|
1179 | err = add_ctor( core, ctor, doPrompt, doNotify ); |
---|
1180 | if( err == TR_PARSE_ERR ) |
---|
1181 | tr_core_errsig( core, TR_PARSE_ERR, filename ); |
---|
1182 | } |
---|
1183 | else if( gtr_is_hex_hashcode( filename ) ) |
---|
1184 | { |
---|
1185 | tr_core_add_from_url( core, filename ); |
---|
1186 | } |
---|
1187 | } |
---|
1188 | |
---|
1189 | gboolean |
---|
1190 | tr_core_present_window( TrCore * core UNUSED, |
---|
1191 | gboolean * success, |
---|
1192 | GError ** err UNUSED ) |
---|
1193 | { |
---|
1194 | /* Setting the toggle-main-window GtkCheckMenuItem to |
---|
1195 | make sure its state is correctly set */ |
---|
1196 | action_toggle( "toggle-main-window", TRUE); |
---|
1197 | |
---|
1198 | *success = TRUE; |
---|
1199 | return TRUE; |
---|
1200 | } |
---|
1201 | |
---|
1202 | void |
---|
1203 | tr_core_add_list( TrCore * core, |
---|
1204 | GSList * torrentFiles, |
---|
1205 | pref_flag_t start, |
---|
1206 | pref_flag_t prompt, |
---|
1207 | gboolean doNotify ) |
---|
1208 | { |
---|
1209 | const gboolean doStart = pref_flag_eval( start, TR_PREFS_KEY_START ); |
---|
1210 | const gboolean doPrompt = pref_flag_eval( prompt, PREF_KEY_OPTIONS_PROMPT ); |
---|
1211 | GSList * l; |
---|
1212 | |
---|
1213 | for( l = torrentFiles; l != NULL; l = l->next ) |
---|
1214 | add_filename( core, l->data, doStart, doPrompt, doNotify ); |
---|
1215 | |
---|
1216 | tr_core_torrents_added( core ); |
---|
1217 | freestrlist( torrentFiles ); |
---|
1218 | } |
---|
1219 | |
---|
1220 | void |
---|
1221 | tr_core_torrents_added( TrCore * self ) |
---|
1222 | { |
---|
1223 | tr_core_update( self ); |
---|
1224 | tr_core_errsig( self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL ); |
---|
1225 | } |
---|
1226 | |
---|
1227 | static gboolean |
---|
1228 | findTorrentInModel( TrCore * core, |
---|
1229 | int id, |
---|
1230 | GtkTreeIter * setme ) |
---|
1231 | { |
---|
1232 | int match = 0; |
---|
1233 | GtkTreeIter iter; |
---|
1234 | GtkTreeModel * model = tr_core_model( core ); |
---|
1235 | |
---|
1236 | if( gtk_tree_model_iter_children( model, &iter, NULL ) ) do |
---|
1237 | { |
---|
1238 | tr_torrent * tor; |
---|
1239 | gtk_tree_model_get( model, &iter, MC_TORRENT_RAW, &tor, -1 ); |
---|
1240 | match = tr_torrentId( tor ) == id; |
---|
1241 | } |
---|
1242 | while( !match && gtk_tree_model_iter_next( model, &iter ) ); |
---|
1243 | |
---|
1244 | if( match ) |
---|
1245 | *setme = iter; |
---|
1246 | |
---|
1247 | return match; |
---|
1248 | } |
---|
1249 | |
---|
1250 | void |
---|
1251 | tr_core_remove_torrent( TrCore * core, TrTorrent * gtor, gboolean deleteFiles ) |
---|
1252 | { |
---|
1253 | const tr_torrent * tor = tr_torrent_handle( gtor ); |
---|
1254 | |
---|
1255 | if( tor != NULL ) |
---|
1256 | tr_core_remove_torrent_from_id( core, tr_torrentId( tor ), deleteFiles ); |
---|
1257 | } |
---|
1258 | |
---|
1259 | void |
---|
1260 | tr_core_remove_torrent_from_id( TrCore * core, int id, gboolean deleteFiles ) |
---|
1261 | { |
---|
1262 | GtkTreeIter iter; |
---|
1263 | |
---|
1264 | if( findTorrentInModel( core, id, &iter ) ) |
---|
1265 | { |
---|
1266 | TrTorrent * gtor = NULL; |
---|
1267 | tr_torrent * tor = NULL; |
---|
1268 | GtkTreeModel * model = tr_core_model( core ); |
---|
1269 | |
---|
1270 | gtk_tree_model_get( model, &iter, MC_TORRENT, >or, |
---|
1271 | MC_TORRENT_RAW, &tor, |
---|
1272 | -1 ); |
---|
1273 | |
---|
1274 | /* remove from the gui */ |
---|
1275 | gtk_list_store_remove( GTK_LIST_STORE( model ), &iter ); |
---|
1276 | |
---|
1277 | /* maybe delete the downloaded files */ |
---|
1278 | if( deleteFiles ) |
---|
1279 | tr_torrentDeleteLocalData( tor, gtr_file_trash_or_remove ); |
---|
1280 | |
---|
1281 | /* remove the torrent */ |
---|
1282 | tr_torrent_set_remove_flag( gtor, TRUE ); |
---|
1283 | gtr_warn_if_fail( G_OBJECT( gtor )->ref_count == 1 ); |
---|
1284 | g_object_unref( G_OBJECT( gtor ) ); /* remove the last refcount */ |
---|
1285 | } |
---|
1286 | } |
---|
1287 | |
---|
1288 | /*** |
---|
1289 | **** |
---|
1290 | ***/ |
---|
1291 | |
---|
1292 | static gboolean |
---|
1293 | update_foreach( GtkTreeModel * model, |
---|
1294 | GtkTreePath * path UNUSED, |
---|
1295 | GtkTreeIter * iter, |
---|
1296 | gpointer data UNUSED ) |
---|
1297 | { |
---|
1298 | int oldActivity, newActivity; |
---|
1299 | double oldUpSpeed, newUpSpeed; |
---|
1300 | double oldDownSpeed, newDownSpeed; |
---|
1301 | const tr_stat * st; |
---|
1302 | TrTorrent * gtor; |
---|
1303 | |
---|
1304 | /* get the old states */ |
---|
1305 | gtk_tree_model_get( model, iter, |
---|
1306 | MC_TORRENT, >or, |
---|
1307 | MC_ACTIVITY, &oldActivity, |
---|
1308 | MC_SPEED_UP, &oldUpSpeed, |
---|
1309 | MC_SPEED_DOWN, &oldDownSpeed, |
---|
1310 | -1 ); |
---|
1311 | |
---|
1312 | /* get the new states */ |
---|
1313 | st = tr_torrentStat( tr_torrent_handle( gtor ) ); |
---|
1314 | newActivity = st->activity; |
---|
1315 | newUpSpeed = st->pieceUploadSpeed_KBps; |
---|
1316 | newDownSpeed = st->pieceDownloadSpeed_KBps; |
---|
1317 | |
---|
1318 | /* updating the model triggers off resort/refresh, |
---|
1319 | so don't do it unless something's actually changed... */ |
---|
1320 | if( ( newActivity != oldActivity ) |
---|
1321 | || gtr_compare_double( newUpSpeed, oldUpSpeed, 3 ) |
---|
1322 | || gtr_compare_double( newDownSpeed, oldDownSpeed, 3 ) ) |
---|
1323 | { |
---|
1324 | gtk_list_store_set( GTK_LIST_STORE( model ), iter, |
---|
1325 | MC_ACTIVITY, newActivity, |
---|
1326 | MC_SPEED_UP, newUpSpeed, |
---|
1327 | MC_SPEED_DOWN, newDownSpeed, |
---|
1328 | -1 ); |
---|
1329 | } |
---|
1330 | |
---|
1331 | /* cleanup */ |
---|
1332 | g_object_unref( gtor ); |
---|
1333 | return FALSE; |
---|
1334 | } |
---|
1335 | |
---|
1336 | void |
---|
1337 | tr_core_update( TrCore * self ) |
---|
1338 | { |
---|
1339 | int column; |
---|
1340 | GtkSortType order; |
---|
1341 | GtkTreeSortable * sortable; |
---|
1342 | GtkTreeModel * model = tr_core_model( self ); |
---|
1343 | |
---|
1344 | /* pause sorting */ |
---|
1345 | sortable = GTK_TREE_SORTABLE( model ); |
---|
1346 | gtk_tree_sortable_get_sort_column_id( sortable, &column, &order ); |
---|
1347 | gtk_tree_sortable_set_sort_column_id( |
---|
1348 | sortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, order ); |
---|
1349 | |
---|
1350 | /* refresh the model */ |
---|
1351 | gtk_tree_model_foreach( model, update_foreach, NULL ); |
---|
1352 | |
---|
1353 | /* resume sorting */ |
---|
1354 | gtk_tree_sortable_set_sort_column_id( sortable, column, order ); |
---|
1355 | |
---|
1356 | /* maybe inhibit hibernation */ |
---|
1357 | maybeInhibitHibernation( self ); |
---|
1358 | } |
---|
1359 | |
---|
1360 | void |
---|
1361 | tr_core_quit( TrCore * core ) |
---|
1362 | { |
---|
1363 | g_signal_emit( core, core_signals[QUIT_SIGNAL], 0 ); |
---|
1364 | } |
---|
1365 | |
---|
1366 | /** |
---|
1367 | *** Hibernate |
---|
1368 | **/ |
---|
1369 | |
---|
1370 | #ifdef HAVE_DBUS_GLIB |
---|
1371 | |
---|
1372 | static DBusGProxy* |
---|
1373 | get_hibernation_inhibit_proxy( void ) |
---|
1374 | { |
---|
1375 | DBusGConnection * conn; |
---|
1376 | GError * error = NULL; |
---|
1377 | const char * name = "org.gnome.SessionManager"; |
---|
1378 | const char * path = "/org/gnome/SessionManager"; |
---|
1379 | const char * interface = "org.gnome.SessionManager"; |
---|
1380 | |
---|
1381 | conn = dbus_g_bus_get( DBUS_BUS_SESSION, &error ); |
---|
1382 | if( error ) |
---|
1383 | { |
---|
1384 | g_warning ( "DBUS cannot connect : %s", error->message ); |
---|
1385 | g_error_free ( error ); |
---|
1386 | return NULL; |
---|
1387 | } |
---|
1388 | |
---|
1389 | return dbus_g_proxy_new_for_name ( conn, name, path, interface ); |
---|
1390 | } |
---|
1391 | |
---|
1392 | static gboolean |
---|
1393 | gtr_inhibit_hibernation( guint * cookie ) |
---|
1394 | { |
---|
1395 | gboolean success = FALSE; |
---|
1396 | DBusGProxy * proxy = get_hibernation_inhibit_proxy( ); |
---|
1397 | |
---|
1398 | if( proxy ) |
---|
1399 | { |
---|
1400 | GError * error = NULL; |
---|
1401 | const int toplevel_xid = 0; |
---|
1402 | const char * application = _( "Transmission Bittorrent Client" ); |
---|
1403 | const char * reason = _( "BitTorrent Activity" ); |
---|
1404 | const int flags = 4; /* Inhibit suspending the session or computer */ |
---|
1405 | |
---|
1406 | success = dbus_g_proxy_call( proxy, "Inhibit", &error, |
---|
1407 | G_TYPE_STRING, application, |
---|
1408 | G_TYPE_UINT, toplevel_xid, |
---|
1409 | G_TYPE_STRING, reason, |
---|
1410 | G_TYPE_UINT, flags, |
---|
1411 | G_TYPE_INVALID, /* sentinel - end of input args */ |
---|
1412 | G_TYPE_UINT, cookie, |
---|
1413 | G_TYPE_INVALID /* senitnel - end of output args */ ); |
---|
1414 | if( success ) |
---|
1415 | tr_inf( "%s", _( "Disallowing desktop hibernation" ) ); |
---|
1416 | else |
---|
1417 | { |
---|
1418 | tr_err( _( "Couldn't disable desktop hibernation: %s" ), |
---|
1419 | error->message ); |
---|
1420 | g_error_free( error ); |
---|
1421 | } |
---|
1422 | |
---|
1423 | g_object_unref( G_OBJECT( proxy ) ); |
---|
1424 | } |
---|
1425 | |
---|
1426 | return success != 0; |
---|
1427 | } |
---|
1428 | |
---|
1429 | static void |
---|
1430 | gtr_uninhibit_hibernation( guint inhibit_cookie ) |
---|
1431 | { |
---|
1432 | DBusGProxy * proxy = get_hibernation_inhibit_proxy( ); |
---|
1433 | |
---|
1434 | if( proxy ) |
---|
1435 | { |
---|
1436 | GError * error = NULL; |
---|
1437 | gboolean success = dbus_g_proxy_call( proxy, "Uninhibit", &error, |
---|
1438 | G_TYPE_UINT, inhibit_cookie, |
---|
1439 | G_TYPE_INVALID, |
---|
1440 | G_TYPE_INVALID ); |
---|
1441 | if( success ) |
---|
1442 | tr_inf( "%s", _( "Allowing desktop hibernation" ) ); |
---|
1443 | else |
---|
1444 | { |
---|
1445 | g_warning( "Couldn't uninhibit the system from suspending: %s.", |
---|
1446 | error->message ); |
---|
1447 | g_error_free( error ); |
---|
1448 | } |
---|
1449 | |
---|
1450 | g_object_unref( G_OBJECT( proxy ) ); |
---|
1451 | } |
---|
1452 | } |
---|
1453 | |
---|
1454 | #endif |
---|
1455 | |
---|
1456 | static void |
---|
1457 | tr_core_set_hibernation_allowed( TrCore * core, |
---|
1458 | gboolean allowed ) |
---|
1459 | { |
---|
1460 | #ifdef HAVE_DBUS_GLIB |
---|
1461 | g_return_if_fail( core ); |
---|
1462 | g_return_if_fail( core->priv ); |
---|
1463 | |
---|
1464 | core->priv->inhibit_allowed = allowed != 0; |
---|
1465 | |
---|
1466 | if( allowed && core->priv->have_inhibit_cookie ) |
---|
1467 | { |
---|
1468 | gtr_uninhibit_hibernation( core->priv->inhibit_cookie ); |
---|
1469 | core->priv->have_inhibit_cookie = FALSE; |
---|
1470 | } |
---|
1471 | |
---|
1472 | if( !allowed |
---|
1473 | && !core->priv->have_inhibit_cookie |
---|
1474 | && !core->priv->dbus_error ) |
---|
1475 | { |
---|
1476 | if( gtr_inhibit_hibernation( &core->priv->inhibit_cookie ) ) |
---|
1477 | core->priv->have_inhibit_cookie = TRUE; |
---|
1478 | else |
---|
1479 | core->priv->dbus_error = TRUE; |
---|
1480 | } |
---|
1481 | #endif |
---|
1482 | } |
---|
1483 | |
---|
1484 | static void |
---|
1485 | maybeInhibitHibernation( TrCore * core ) |
---|
1486 | { |
---|
1487 | /* inhibit if it's enabled *AND* all the torrents are paused */ |
---|
1488 | const gboolean inhibit = pref_flag_get( PREF_KEY_INHIBIT_HIBERNATION ) |
---|
1489 | && ( tr_core_get_active_torrent_count( core ) == 0 ); |
---|
1490 | |
---|
1491 | tr_core_set_hibernation_allowed( core, !inhibit ); |
---|
1492 | } |
---|
1493 | |
---|
1494 | /** |
---|
1495 | *** Prefs |
---|
1496 | **/ |
---|
1497 | |
---|
1498 | static void |
---|
1499 | commitPrefsChange( TrCore * core, const char * key ) |
---|
1500 | { |
---|
1501 | g_signal_emit( core, core_signals[PREFS_SIGNAL], 0, key ); |
---|
1502 | pref_save( tr_core_session( core ) ); |
---|
1503 | } |
---|
1504 | |
---|
1505 | void |
---|
1506 | tr_core_set_pref( TrCore * self, const char * key, const char * newval ) |
---|
1507 | { |
---|
1508 | const char * oldval = pref_string_get( key ); |
---|
1509 | |
---|
1510 | if( tr_strcmp( oldval, newval ) ) |
---|
1511 | { |
---|
1512 | pref_string_set( key, newval ); |
---|
1513 | commitPrefsChange( self, key ); |
---|
1514 | } |
---|
1515 | } |
---|
1516 | |
---|
1517 | void |
---|
1518 | tr_core_set_pref_bool( TrCore * self, |
---|
1519 | const char * key, |
---|
1520 | gboolean newval ) |
---|
1521 | { |
---|
1522 | const gboolean oldval = pref_flag_get( key ); |
---|
1523 | |
---|
1524 | if( oldval != newval ) |
---|
1525 | { |
---|
1526 | pref_flag_set( key, newval ); |
---|
1527 | commitPrefsChange( self, key ); |
---|
1528 | } |
---|
1529 | } |
---|
1530 | |
---|
1531 | void |
---|
1532 | tr_core_set_pref_int( TrCore * self, |
---|
1533 | const char * key, |
---|
1534 | int newval ) |
---|
1535 | { |
---|
1536 | const int oldval = pref_int_get( key ); |
---|
1537 | |
---|
1538 | if( oldval != newval ) |
---|
1539 | { |
---|
1540 | pref_int_set( key, newval ); |
---|
1541 | commitPrefsChange( self, key ); |
---|
1542 | } |
---|
1543 | } |
---|
1544 | |
---|
1545 | void |
---|
1546 | tr_core_set_pref_double( TrCore * self, |
---|
1547 | const char * key, |
---|
1548 | double newval ) |
---|
1549 | { |
---|
1550 | const double oldval = pref_double_get( key ); |
---|
1551 | |
---|
1552 | if( gtr_compare_double( oldval, newval, 4 ) ) |
---|
1553 | { |
---|
1554 | pref_double_set( key, newval ); |
---|
1555 | commitPrefsChange( self, key ); |
---|
1556 | } |
---|
1557 | } |
---|
1558 | |
---|
1559 | /*** |
---|
1560 | **** |
---|
1561 | **** RPC Interface |
---|
1562 | **** |
---|
1563 | ***/ |
---|
1564 | |
---|
1565 | /* #define DEBUG_RPC */ |
---|
1566 | |
---|
1567 | static int nextTag = 1; |
---|
1568 | |
---|
1569 | typedef void ( server_response_func )( TrCore * core, tr_benc * response, gpointer user_data ); |
---|
1570 | |
---|
1571 | struct pending_request_data |
---|
1572 | { |
---|
1573 | TrCore * core; |
---|
1574 | server_response_func * responseFunc; |
---|
1575 | gpointer responseFuncUserData; |
---|
1576 | }; |
---|
1577 | |
---|
1578 | static GHashTable * pendingRequests = NULL; |
---|
1579 | |
---|
1580 | static gboolean |
---|
1581 | readResponseIdle( void * vresponse ) |
---|
1582 | { |
---|
1583 | GByteArray * response; |
---|
1584 | tr_benc top; |
---|
1585 | int64_t intVal; |
---|
1586 | int tag; |
---|
1587 | struct pending_request_data * data; |
---|
1588 | |
---|
1589 | response = vresponse; |
---|
1590 | tr_jsonParse( NULL, response->data, response->len, &top, NULL ); |
---|
1591 | tr_bencDictFindInt( &top, "tag", &intVal ); |
---|
1592 | tag = (int)intVal; |
---|
1593 | |
---|
1594 | data = g_hash_table_lookup( pendingRequests, &tag ); |
---|
1595 | if( data && data->responseFunc ) |
---|
1596 | (*data->responseFunc)(data->core, &top, data->responseFuncUserData ); |
---|
1597 | |
---|
1598 | tr_bencFree( &top ); |
---|
1599 | g_hash_table_remove( pendingRequests, &tag ); |
---|
1600 | g_byte_array_free( response, TRUE ); |
---|
1601 | return FALSE; |
---|
1602 | } |
---|
1603 | |
---|
1604 | static void |
---|
1605 | readResponse( tr_session * session UNUSED, |
---|
1606 | const char * response, |
---|
1607 | size_t response_len, |
---|
1608 | void * unused UNUSED ) |
---|
1609 | { |
---|
1610 | GByteArray * bytes = g_byte_array_new( ); |
---|
1611 | #ifdef DEBUG_RPC |
---|
1612 | g_message( "response: [%*.*s]", (int)response_len, (int)response_len, response ); |
---|
1613 | #endif |
---|
1614 | g_byte_array_append( bytes, (const uint8_t*)response, response_len ); |
---|
1615 | gtr_idle_add( readResponseIdle, bytes ); |
---|
1616 | } |
---|
1617 | |
---|
1618 | static void |
---|
1619 | sendRequest( TrCore * core, const char * json, int tag, |
---|
1620 | server_response_func * responseFunc, void * responseFuncUserData ) |
---|
1621 | { |
---|
1622 | tr_session * session = tr_core_session( core ); |
---|
1623 | |
---|
1624 | if( pendingRequests == NULL ) |
---|
1625 | { |
---|
1626 | pendingRequests = g_hash_table_new_full( g_int_hash, g_int_equal, g_free, g_free ); |
---|
1627 | } |
---|
1628 | |
---|
1629 | if( session == NULL ) |
---|
1630 | { |
---|
1631 | g_error( "GTK+ client doesn't support connections to remote servers yet." ); |
---|
1632 | } |
---|
1633 | else |
---|
1634 | { |
---|
1635 | /* remember this request */ |
---|
1636 | struct pending_request_data * data; |
---|
1637 | data = g_new0( struct pending_request_data, 1 ); |
---|
1638 | data->core = core; |
---|
1639 | data->responseFunc = responseFunc; |
---|
1640 | data->responseFuncUserData = responseFuncUserData; |
---|
1641 | g_hash_table_insert( pendingRequests, g_memdup( &tag, sizeof( int ) ), data ); |
---|
1642 | |
---|
1643 | /* make the request */ |
---|
1644 | #ifdef DEBUG_RPC |
---|
1645 | g_message( "request: [%s]", json ); |
---|
1646 | #endif |
---|
1647 | tr_rpc_request_exec_json( session, json, strlen( json ), readResponse, GINT_TO_POINTER(tag) ); |
---|
1648 | } |
---|
1649 | } |
---|
1650 | |
---|
1651 | /*** |
---|
1652 | **** Sending a test-port request via RPC |
---|
1653 | ***/ |
---|
1654 | |
---|
1655 | static void |
---|
1656 | portTestResponseFunc( TrCore * core, tr_benc * response, gpointer userData UNUSED ) |
---|
1657 | { |
---|
1658 | tr_benc * args; |
---|
1659 | tr_bool isOpen = FALSE; |
---|
1660 | |
---|
1661 | if( tr_bencDictFindDict( response, "arguments", &args ) ) |
---|
1662 | tr_bencDictFindBool( args, "port-is-open", &isOpen ); |
---|
1663 | |
---|
1664 | emitPortTested( core, isOpen ); |
---|
1665 | } |
---|
1666 | |
---|
1667 | void |
---|
1668 | tr_core_port_test( TrCore * core ) |
---|
1669 | { |
---|
1670 | char buf[128]; |
---|
1671 | const int tag = nextTag++; |
---|
1672 | g_snprintf( buf, sizeof( buf ), "{ \"method\": \"port-test\", \"tag\": %d }", tag ); |
---|
1673 | sendRequest( core, buf, tag, portTestResponseFunc, NULL ); |
---|
1674 | } |
---|
1675 | |
---|
1676 | /*** |
---|
1677 | **** Updating a blocklist via RPC |
---|
1678 | ***/ |
---|
1679 | |
---|
1680 | static void |
---|
1681 | blocklistResponseFunc( TrCore * core, tr_benc * response, gpointer userData UNUSED ) |
---|
1682 | { |
---|
1683 | tr_benc * args; |
---|
1684 | int64_t ruleCount = 0; |
---|
1685 | |
---|
1686 | if( tr_bencDictFindDict( response, "arguments", &args ) ) |
---|
1687 | tr_bencDictFindInt( args, "blocklist-size", &ruleCount ); |
---|
1688 | |
---|
1689 | if( ruleCount > 0 ) |
---|
1690 | pref_int_set( "blocklist-date", time( NULL ) ); |
---|
1691 | |
---|
1692 | emitBlocklistUpdated( core, ruleCount ); |
---|
1693 | } |
---|
1694 | |
---|
1695 | void |
---|
1696 | tr_core_blocklist_update( TrCore * core ) |
---|
1697 | { |
---|
1698 | char buf[128]; |
---|
1699 | const int tag = nextTag++; |
---|
1700 | g_snprintf( buf, sizeof( buf ), "{ \"method\": \"blocklist-update\", \"tag\": %d }", tag ); |
---|
1701 | sendRequest( core, buf, tag, blocklistResponseFunc, NULL ); |
---|
1702 | } |
---|
1703 | |
---|
1704 | /*** |
---|
1705 | **** |
---|
1706 | ***/ |
---|
1707 | |
---|
1708 | void |
---|
1709 | tr_core_exec_json( TrCore * core, const char * json ) |
---|
1710 | { |
---|
1711 | const int tag = nextTag++; |
---|
1712 | sendRequest( core, json, tag, NULL, NULL ); |
---|
1713 | } |
---|
1714 | |
---|
1715 | void |
---|
1716 | tr_core_exec( TrCore * core, const tr_benc * top ) |
---|
1717 | { |
---|
1718 | char * json = tr_bencToStr( top, TR_FMT_JSON_LEAN, NULL ); |
---|
1719 | tr_core_exec_json( core, json ); |
---|
1720 | tr_free( json ); |
---|
1721 | } |
---|
1722 | |
---|
1723 | /*** |
---|
1724 | **** |
---|
1725 | ***/ |
---|
1726 | |
---|
1727 | void |
---|
1728 | tr_core_torrent_changed( TrCore * core, int id ) |
---|
1729 | { |
---|
1730 | GtkTreeIter iter; |
---|
1731 | GtkTreeModel * model = tr_core_model( core ); |
---|
1732 | |
---|
1733 | if( gtk_tree_model_get_iter_first( model, &iter ) ) do |
---|
1734 | { |
---|
1735 | tr_torrent * tor; |
---|
1736 | gtk_tree_model_get( model, &iter, MC_TORRENT_RAW, &tor, -1 ); |
---|
1737 | if( tr_torrentId( tor ) == id ) |
---|
1738 | { |
---|
1739 | GtkTreePath * path = gtk_tree_model_get_path( model, &iter ); |
---|
1740 | gtk_tree_model_row_changed( model, path, &iter ); |
---|
1741 | gtk_tree_path_free( path ); |
---|
1742 | break; |
---|
1743 | } |
---|
1744 | } |
---|
1745 | while( gtk_tree_model_iter_next( model, &iter ) ); |
---|
1746 | } |
---|
1747 | |
---|
1748 | int |
---|
1749 | tr_core_get_torrent_count( TrCore * core ) |
---|
1750 | { |
---|
1751 | return gtk_tree_model_iter_n_children( tr_core_model( core ), NULL ); |
---|
1752 | } |
---|
1753 | |
---|
1754 | int |
---|
1755 | tr_core_get_active_torrent_count( TrCore * core ) |
---|
1756 | { |
---|
1757 | GtkTreeIter iter; |
---|
1758 | GtkTreeModel * model = tr_core_model( core ); |
---|
1759 | int activeCount = 0; |
---|
1760 | |
---|
1761 | if( gtk_tree_model_get_iter_first( model, &iter ) ) do |
---|
1762 | { |
---|
1763 | int activity; |
---|
1764 | gtk_tree_model_get( model, &iter, MC_ACTIVITY, &activity, -1 ); |
---|
1765 | |
---|
1766 | if( activity != TR_STATUS_STOPPED ) |
---|
1767 | ++activeCount; |
---|
1768 | } |
---|
1769 | while( gtk_tree_model_iter_next( model, &iter ) ); |
---|
1770 | |
---|
1771 | return activeCount; |
---|
1772 | } |
---|
1773 | |
---|