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