1 | /****************************************************************************** |
---|
2 | * $Id: tr-core.c 5207 2008-03-06 20:55:07Z 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 | |
---|
30 | #include <libtransmission/transmission.h> |
---|
31 | #include <libtransmission/utils.h> /* tr_free */ |
---|
32 | |
---|
33 | #include "conf.h" |
---|
34 | #include "tr-core.h" |
---|
35 | #include "tr-prefs.h" |
---|
36 | #include "tr-torrent.h" |
---|
37 | #include "util.h" |
---|
38 | |
---|
39 | struct TrCorePrivate |
---|
40 | { |
---|
41 | GtkTreeModel * model; |
---|
42 | tr_handle * handle; |
---|
43 | int nextid; |
---|
44 | struct core_stats stats; |
---|
45 | }; |
---|
46 | |
---|
47 | static void |
---|
48 | tr_core_marshal_err( GClosure * closure, GValue * ret UNUSED, |
---|
49 | guint count, const GValue * vals, |
---|
50 | gpointer hint UNUSED, gpointer marshal ) |
---|
51 | { |
---|
52 | typedef void (*TRMarshalErr) |
---|
53 | ( gpointer, enum tr_core_err, const char *, gpointer ); |
---|
54 | TRMarshalErr callback; |
---|
55 | GCClosure * cclosure = (GCClosure*) closure; |
---|
56 | enum tr_core_err errcode; |
---|
57 | const char * errstr; |
---|
58 | gpointer inst, gdata; |
---|
59 | |
---|
60 | g_return_if_fail( count == 3 ); |
---|
61 | |
---|
62 | inst = g_value_peek_pointer( vals ); |
---|
63 | errcode = g_value_get_int( vals + 1 ); |
---|
64 | errstr = g_value_get_string( vals + 2 ); |
---|
65 | gdata = closure->data; |
---|
66 | |
---|
67 | callback = (TRMarshalErr)( marshal ? marshal : cclosure->callback ); |
---|
68 | callback( inst, errcode, errstr, gdata ); |
---|
69 | } |
---|
70 | |
---|
71 | static void |
---|
72 | tr_core_marshal_prompt( GClosure * closure, GValue * ret UNUSED, |
---|
73 | guint count, const GValue * vals, |
---|
74 | gpointer hint UNUSED, gpointer marshal ) |
---|
75 | { |
---|
76 | typedef void (*TRMarshalPrompt)( gpointer, GList *, gpointer, gpointer ); |
---|
77 | TRMarshalPrompt callback; |
---|
78 | GCClosure * cclosure = (GCClosure*) closure; |
---|
79 | GList * paths; |
---|
80 | gpointer ctor; |
---|
81 | gpointer inst, gdata; |
---|
82 | |
---|
83 | g_return_if_fail( count == 3 ); |
---|
84 | |
---|
85 | inst = g_value_peek_pointer( vals ); |
---|
86 | paths = g_value_peek_pointer( vals + 1 ); |
---|
87 | ctor = g_value_peek_pointer( vals + 2 ); |
---|
88 | gdata = closure->data; |
---|
89 | |
---|
90 | callback = (TRMarshalPrompt)( marshal ? marshal : cclosure->callback ); |
---|
91 | callback( inst, paths, ctor, gdata ); |
---|
92 | } |
---|
93 | |
---|
94 | static int |
---|
95 | isDisposed( const TrCore * core ) |
---|
96 | { |
---|
97 | return !core || !core->priv; |
---|
98 | } |
---|
99 | |
---|
100 | static void |
---|
101 | tr_core_dispose( GObject * obj ) |
---|
102 | { |
---|
103 | TrCore * core = TR_CORE( obj ); |
---|
104 | |
---|
105 | if( !isDisposed( core ) ) |
---|
106 | { |
---|
107 | GObjectClass * parent; |
---|
108 | |
---|
109 | pref_save( NULL ); |
---|
110 | core->priv = NULL; |
---|
111 | |
---|
112 | parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) ); |
---|
113 | parent->dispose( obj ); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | static void |
---|
118 | tr_core_class_init( gpointer g_class, gpointer g_class_data UNUSED ) |
---|
119 | { |
---|
120 | GObjectClass * gobject_class; |
---|
121 | TrCoreClass * core_class; |
---|
122 | |
---|
123 | g_type_class_add_private( g_class, sizeof(struct TrCorePrivate) ); |
---|
124 | |
---|
125 | gobject_class = G_OBJECT_CLASS( g_class ); |
---|
126 | gobject_class->dispose = tr_core_dispose; |
---|
127 | |
---|
128 | |
---|
129 | core_class = TR_CORE_CLASS( g_class ); |
---|
130 | core_class->errsig = g_signal_new( "error", G_TYPE_FROM_CLASS( g_class ), |
---|
131 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
132 | tr_core_marshal_err, G_TYPE_NONE, |
---|
133 | 2, G_TYPE_INT, G_TYPE_STRING ); |
---|
134 | core_class->promptsig = g_signal_new( "destination-prompt", |
---|
135 | G_TYPE_FROM_CLASS( g_class ), |
---|
136 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
137 | tr_core_marshal_prompt, G_TYPE_NONE, |
---|
138 | 2, G_TYPE_POINTER, G_TYPE_POINTER ); |
---|
139 | core_class->quitsig = g_signal_new( "quit", G_TYPE_FROM_CLASS( g_class ), |
---|
140 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
141 | g_cclosure_marshal_VOID__VOID, |
---|
142 | G_TYPE_NONE, 0 ); |
---|
143 | core_class->prefsig = g_signal_new( "prefs-changed", |
---|
144 | G_TYPE_FROM_CLASS( g_class ), |
---|
145 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
146 | g_cclosure_marshal_VOID__STRING, |
---|
147 | G_TYPE_NONE, 1, G_TYPE_STRING ); |
---|
148 | } |
---|
149 | |
---|
150 | /*** |
---|
151 | **** SORTING |
---|
152 | ***/ |
---|
153 | |
---|
154 | static int |
---|
155 | compareDouble( double a, double b ) |
---|
156 | { |
---|
157 | if( a < b ) return -1; |
---|
158 | if( a > b ) return 1; |
---|
159 | return 0; |
---|
160 | } |
---|
161 | |
---|
162 | static int |
---|
163 | compareByActivity( GtkTreeModel * model, |
---|
164 | GtkTreeIter * a, |
---|
165 | GtkTreeIter * b, |
---|
166 | gpointer user_data UNUSED ) |
---|
167 | { |
---|
168 | int i; |
---|
169 | tr_torrent *ta, *tb; |
---|
170 | const tr_stat *sa, *sb; |
---|
171 | |
---|
172 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
173 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
174 | |
---|
175 | sa = tr_torrentStatCached( ta ); |
---|
176 | sb = tr_torrentStatCached( tb ); |
---|
177 | |
---|
178 | if(( i = compareDouble( sa->rateUpload + sa->rateDownload, |
---|
179 | sb->rateUpload + sb->rateDownload ) )) |
---|
180 | return i; |
---|
181 | |
---|
182 | if( sa->uploadedEver != sb->uploadedEver ) |
---|
183 | return sa->uploadedEver < sa->uploadedEver ? -1 : 1; |
---|
184 | |
---|
185 | return 0; |
---|
186 | } |
---|
187 | |
---|
188 | static int |
---|
189 | compareByName( GtkTreeModel * model, |
---|
190 | GtkTreeIter * a, |
---|
191 | GtkTreeIter * b, |
---|
192 | gpointer user_data UNUSED ) |
---|
193 | { |
---|
194 | int ret; |
---|
195 | char *ca, *cb; |
---|
196 | gtk_tree_model_get( model, a, MC_NAME_COLLATED, &ca, -1 ); |
---|
197 | gtk_tree_model_get( model, b, MC_NAME_COLLATED, &cb, -1 ); |
---|
198 | ret = strcmp( ca, cb ); |
---|
199 | g_free( cb ); |
---|
200 | g_free( ca ); |
---|
201 | return ret; |
---|
202 | } |
---|
203 | |
---|
204 | static int |
---|
205 | compareByProgress( GtkTreeModel * model, |
---|
206 | GtkTreeIter * a, |
---|
207 | GtkTreeIter * b, |
---|
208 | gpointer user_data UNUSED ) |
---|
209 | { |
---|
210 | int ret; |
---|
211 | tr_torrent *ta, *tb; |
---|
212 | const tr_stat *sa, *sb; |
---|
213 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
214 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
215 | sa = tr_torrentStatCached( ta ); |
---|
216 | sb = tr_torrentStatCached( tb ); |
---|
217 | ret = compareDouble( sa->percentDone, sb->percentDone ); |
---|
218 | if( !ret ) |
---|
219 | ret = compareDouble( sa->ratio, sb->ratio ); |
---|
220 | return ret; |
---|
221 | } |
---|
222 | |
---|
223 | static int |
---|
224 | compareByState( GtkTreeModel * model, |
---|
225 | GtkTreeIter * a, |
---|
226 | GtkTreeIter * b, |
---|
227 | gpointer user_data ) |
---|
228 | { |
---|
229 | int sa, sb, ret; |
---|
230 | |
---|
231 | /* first by state */ |
---|
232 | gtk_tree_model_get( model, a, MC_STATUS, &sa, -1 ); |
---|
233 | gtk_tree_model_get( model, b, MC_STATUS, &sb, -1 ); |
---|
234 | ret = sa - sb; |
---|
235 | |
---|
236 | /* second by progress */ |
---|
237 | if( !ret ) |
---|
238 | ret = compareByProgress( model, a, b, user_data ); |
---|
239 | |
---|
240 | return ret; |
---|
241 | } |
---|
242 | |
---|
243 | static int |
---|
244 | compareByTracker( GtkTreeModel * model, |
---|
245 | GtkTreeIter * a, |
---|
246 | GtkTreeIter * b, |
---|
247 | gpointer user_data UNUSED ) |
---|
248 | { |
---|
249 | const tr_torrent *ta, *tb; |
---|
250 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
251 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
252 | return strcmp( tr_torrentInfo(ta)->primaryAddress, |
---|
253 | tr_torrentInfo(tb)->primaryAddress ); |
---|
254 | } |
---|
255 | |
---|
256 | static void |
---|
257 | setSort( TrCore * core, const char * mode, gboolean isReversed ) |
---|
258 | { |
---|
259 | int col = MC_TORRENT_RAW; |
---|
260 | GtkSortType type = isReversed ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING; |
---|
261 | GtkTreeModel * model = tr_core_model( core ); |
---|
262 | GtkTreeSortable * sortable = GTK_TREE_SORTABLE( model ); |
---|
263 | |
---|
264 | if( !strcmp( mode, "sort-by-activity" ) ) |
---|
265 | gtk_tree_sortable_set_sort_func( sortable, col, compareByActivity, NULL, NULL ); |
---|
266 | else if( !strcmp( mode, "sort-by-progress" ) ) |
---|
267 | gtk_tree_sortable_set_sort_func( sortable, col, compareByProgress, NULL, NULL ); |
---|
268 | else if( !strcmp( mode, "sort-by-state" ) ) |
---|
269 | gtk_tree_sortable_set_sort_func( sortable, col, compareByState, NULL, NULL ); |
---|
270 | else if( !strcmp( mode, "sort-by-tracker" ) ) |
---|
271 | gtk_tree_sortable_set_sort_func( sortable, col, compareByTracker, NULL, NULL ); |
---|
272 | else { |
---|
273 | type = isReversed ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING; |
---|
274 | gtk_tree_sortable_set_sort_func( sortable, col, compareByName, NULL, NULL ); |
---|
275 | } |
---|
276 | |
---|
277 | gtk_tree_sortable_set_sort_column_id( sortable, col, type ); |
---|
278 | } |
---|
279 | |
---|
280 | static void |
---|
281 | prefsChanged( TrCore * core, const char * key, gpointer data UNUSED ) |
---|
282 | { |
---|
283 | if( !strcmp( key, PREF_KEY_SORT_MODE ) || |
---|
284 | !strcmp( key, PREF_KEY_SORT_REVERSED ) ) |
---|
285 | { |
---|
286 | char * mode = pref_string_get( PREF_KEY_SORT_MODE ); |
---|
287 | gboolean isReversed = pref_flag_get( PREF_KEY_SORT_REVERSED ); |
---|
288 | setSort( core, mode, isReversed ); |
---|
289 | g_free( mode ); |
---|
290 | } |
---|
291 | else if( !strcmp( key, PREF_KEY_MAX_PEERS_GLOBAL ) ) |
---|
292 | { |
---|
293 | const uint16_t val = pref_int_get( key ); |
---|
294 | tr_setGlobalPeerLimit( tr_core_handle( core ), val ); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | static void |
---|
299 | tr_core_init( GTypeInstance * instance, gpointer g_class UNUSED ) |
---|
300 | { |
---|
301 | tr_handle * h; |
---|
302 | TrCore * self = (TrCore *) instance; |
---|
303 | GtkListStore * store; |
---|
304 | struct TrCorePrivate * p; |
---|
305 | |
---|
306 | /* column types for the model used to store torrent information */ |
---|
307 | /* keep this in sync with the enum near the bottom of tr_core.h */ |
---|
308 | GType types[] = { |
---|
309 | G_TYPE_STRING, /* name */ |
---|
310 | G_TYPE_STRING, /* collated name */ |
---|
311 | G_TYPE_STRING, /* hash string */ |
---|
312 | TR_TORRENT_TYPE, /* TrTorrent object */ |
---|
313 | G_TYPE_POINTER, /* tr_torrent* */ |
---|
314 | G_TYPE_INT, /* tr_stat()->status */ |
---|
315 | G_TYPE_INT /* ID for IPC */ |
---|
316 | }; |
---|
317 | |
---|
318 | p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE( self, |
---|
319 | TR_CORE_TYPE, |
---|
320 | struct TrCorePrivate ); |
---|
321 | |
---|
322 | |
---|
323 | h = tr_initFull( "gtk", |
---|
324 | pref_flag_get( PREF_KEY_PEX ), |
---|
325 | pref_flag_get( PREF_KEY_NAT ), |
---|
326 | pref_int_get( PREF_KEY_PORT ), |
---|
327 | pref_flag_get( PREF_KEY_ENCRYPTED_ONLY ) |
---|
328 | ? TR_ENCRYPTION_REQUIRED |
---|
329 | : TR_ENCRYPTION_PREFERRED, |
---|
330 | pref_flag_get( PREF_KEY_UL_LIMIT_ENABLED ), |
---|
331 | pref_int_get( PREF_KEY_UL_LIMIT ), |
---|
332 | pref_flag_get( PREF_KEY_DL_LIMIT_ENABLED ), |
---|
333 | pref_int_get( PREF_KEY_DL_LIMIT ), |
---|
334 | pref_int_get( PREF_KEY_MAX_PEERS_GLOBAL ), |
---|
335 | pref_int_get( PREF_KEY_MSGLEVEL ), |
---|
336 | TRUE /* message queueing */ ); |
---|
337 | |
---|
338 | /* create the model used to store torrent data */ |
---|
339 | g_assert( ALEN( types ) == MC_ROW_COUNT ); |
---|
340 | store = gtk_list_store_newv( MC_ROW_COUNT, types ); |
---|
341 | |
---|
342 | p->model = GTK_TREE_MODEL( store ); |
---|
343 | p->handle = h; |
---|
344 | p->nextid = 1; |
---|
345 | } |
---|
346 | |
---|
347 | GType |
---|
348 | tr_core_get_type( void ) |
---|
349 | { |
---|
350 | static GType type = 0; |
---|
351 | |
---|
352 | if( !type ) |
---|
353 | { |
---|
354 | static const GTypeInfo info = |
---|
355 | { |
---|
356 | sizeof( TrCoreClass ), |
---|
357 | NULL, /* base_init */ |
---|
358 | NULL, /* base_finalize */ |
---|
359 | tr_core_class_init, /* class_init */ |
---|
360 | NULL, /* class_finalize */ |
---|
361 | NULL, /* class_data */ |
---|
362 | sizeof( TrCore ), |
---|
363 | 0, /* n_preallocs */ |
---|
364 | tr_core_init, /* instance_init */ |
---|
365 | NULL, |
---|
366 | }; |
---|
367 | type = g_type_register_static( G_TYPE_OBJECT, "TrCore", &info, 0 ); |
---|
368 | } |
---|
369 | |
---|
370 | return type; |
---|
371 | } |
---|
372 | |
---|
373 | /** |
---|
374 | *** |
---|
375 | **/ |
---|
376 | |
---|
377 | TrCore * |
---|
378 | tr_core_new( void ) |
---|
379 | { |
---|
380 | TrCore * core = TR_CORE( g_object_new( TR_CORE_TYPE, NULL ) ); |
---|
381 | |
---|
382 | /* init from prefs & listen to pref changes */ |
---|
383 | prefsChanged( core, PREF_KEY_SORT_MODE, NULL ); |
---|
384 | prefsChanged( core, PREF_KEY_SORT_REVERSED, NULL ); |
---|
385 | prefsChanged( core, PREF_KEY_MAX_PEERS_GLOBAL, NULL ); |
---|
386 | g_signal_connect( core, "prefs-changed", G_CALLBACK(prefsChanged), NULL ); |
---|
387 | |
---|
388 | return core; |
---|
389 | } |
---|
390 | |
---|
391 | GtkTreeModel * |
---|
392 | tr_core_model( TrCore * core ) |
---|
393 | { |
---|
394 | return isDisposed( core ) ? NULL : core->priv->model; |
---|
395 | } |
---|
396 | |
---|
397 | tr_handle * |
---|
398 | tr_core_handle( TrCore * core ) |
---|
399 | { |
---|
400 | return isDisposed( core ) ? NULL : core->priv->handle; |
---|
401 | } |
---|
402 | |
---|
403 | |
---|
404 | const struct core_stats* |
---|
405 | tr_core_get_stats( const TrCore * core ) |
---|
406 | { |
---|
407 | return isDisposed( core ) ? NULL : &core->priv->stats; |
---|
408 | } |
---|
409 | |
---|
410 | static char* |
---|
411 | doCollate( const char * in ) |
---|
412 | { |
---|
413 | const char * end = in + strlen( in ); |
---|
414 | char * casefold; |
---|
415 | char * ret; |
---|
416 | |
---|
417 | while( in < end ) { |
---|
418 | const gunichar ch = g_utf8_get_char( in ); |
---|
419 | if (!g_unichar_isalnum (ch)) // eat everything before the first alnum |
---|
420 | in += g_unichar_to_utf8( ch, NULL ); |
---|
421 | else |
---|
422 | break; |
---|
423 | } |
---|
424 | |
---|
425 | if ( in == end ) |
---|
426 | return g_strdup (""); |
---|
427 | |
---|
428 | casefold = g_utf8_casefold( in, end-in ); |
---|
429 | ret = g_utf8_collate_key( casefold, -1 ); |
---|
430 | g_free( casefold ); |
---|
431 | |
---|
432 | return ret; |
---|
433 | } |
---|
434 | |
---|
435 | void |
---|
436 | tr_core_add_torrent( TrCore * self, TrTorrent * tor ) |
---|
437 | { |
---|
438 | const tr_info * inf = tr_torrent_info( tor ); |
---|
439 | const tr_stat * torStat = tr_torrent_stat( tor ); |
---|
440 | char * collated = doCollate( inf->name ); |
---|
441 | GtkListStore * store = GTK_LIST_STORE( tr_core_model( self ) ); |
---|
442 | GtkTreeIter unused; |
---|
443 | |
---|
444 | gtk_list_store_insert_with_values( store, &unused, 0, |
---|
445 | MC_NAME, inf->name, |
---|
446 | MC_NAME_COLLATED, collated, |
---|
447 | MC_HASH, inf->hashString, |
---|
448 | MC_TORRENT, tor, |
---|
449 | MC_TORRENT_RAW, tr_torrent_handle( tor ), |
---|
450 | MC_STATUS, torStat->status, |
---|
451 | MC_ID, self->priv->nextid, |
---|
452 | -1); |
---|
453 | ++self->priv->nextid; |
---|
454 | |
---|
455 | /* cleanup */ |
---|
456 | g_object_unref( G_OBJECT( tor ) ); |
---|
457 | g_free( collated ); |
---|
458 | } |
---|
459 | |
---|
460 | int |
---|
461 | tr_core_load( TrCore * self, gboolean forcePaused ) |
---|
462 | { |
---|
463 | int i; |
---|
464 | int count = 0; |
---|
465 | tr_torrent ** torrents; |
---|
466 | char * path; |
---|
467 | tr_ctor * ctor; |
---|
468 | |
---|
469 | path = getdownloaddir( ); |
---|
470 | |
---|
471 | ctor = tr_ctorNew( tr_core_handle( self ) ); |
---|
472 | if( forcePaused ) |
---|
473 | tr_ctorSetPaused( ctor, TR_FORCE, TRUE ); |
---|
474 | tr_ctorSetDestination( ctor, TR_FALLBACK, path ); |
---|
475 | tr_ctorSetMaxConnectedPeers( ctor, TR_FALLBACK, |
---|
476 | pref_int_get( PREF_KEY_MAX_PEERS_PER_TORRENT ) ); |
---|
477 | |
---|
478 | torrents = tr_loadTorrents ( tr_core_handle( self ), ctor, &count ); |
---|
479 | for( i=0; i<count; ++i ) |
---|
480 | tr_core_add_torrent( self, tr_torrent_new_preexisting( torrents[i] ) ); |
---|
481 | |
---|
482 | tr_free( torrents ); |
---|
483 | tr_ctorFree( ctor ); |
---|
484 | g_free( path ); |
---|
485 | |
---|
486 | return count; |
---|
487 | } |
---|
488 | |
---|
489 | static void |
---|
490 | tr_core_errsig( TrCore * self, enum tr_core_err type, const char * msg ) |
---|
491 | { |
---|
492 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
493 | g_signal_emit( self, class->errsig, 0, type, msg ); |
---|
494 | } |
---|
495 | |
---|
496 | static void |
---|
497 | tr_core_apply_defaults( tr_ctor * ctor ) |
---|
498 | { |
---|
499 | if( tr_ctorGetPaused( ctor, TR_FORCE, NULL ) ) |
---|
500 | tr_ctorSetPaused( ctor, TR_FORCE, !pref_flag_get( PREF_KEY_START ) ); |
---|
501 | |
---|
502 | if( tr_ctorGetDeleteSource( ctor, NULL ) ) |
---|
503 | tr_ctorSetDeleteSource( ctor, |
---|
504 | pref_flag_get( PREF_KEY_DELETE_ORIGINAL ) ); |
---|
505 | |
---|
506 | if( tr_ctorGetMaxConnectedPeers( ctor, TR_FORCE, NULL ) ) |
---|
507 | tr_ctorSetMaxConnectedPeers( ctor, TR_FORCE, |
---|
508 | pref_int_get( PREF_KEY_MAX_PEERS_PER_TORRENT ) ); |
---|
509 | |
---|
510 | if( tr_ctorGetDestination( ctor, TR_FORCE, NULL ) ) { |
---|
511 | char * path = pref_string_get( PREF_KEY_DIR_DEFAULT ); |
---|
512 | tr_ctorSetDestination( ctor, TR_FORCE, path ); |
---|
513 | g_free( path ); |
---|
514 | } |
---|
515 | } |
---|
516 | |
---|
517 | void |
---|
518 | tr_core_add_ctor( TrCore * self, tr_ctor * ctor ) |
---|
519 | { |
---|
520 | TrTorrent * tor; |
---|
521 | char * errstr = NULL; |
---|
522 | |
---|
523 | tr_core_apply_defaults( ctor ); |
---|
524 | |
---|
525 | if(( tor = tr_torrent_new_ctor( tr_core_handle( self ), ctor, &errstr ))) |
---|
526 | tr_core_add_torrent( self, tor ); |
---|
527 | else{ |
---|
528 | tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr ); |
---|
529 | g_free( errstr ); |
---|
530 | } |
---|
531 | } |
---|
532 | |
---|
533 | void |
---|
534 | tr_core_add_list( TrCore * self, |
---|
535 | GList * paths, |
---|
536 | tr_ctor * ctor ) |
---|
537 | { |
---|
538 | tr_core_apply_defaults( ctor ); |
---|
539 | |
---|
540 | if( pref_flag_get( PREF_KEY_OPTIONS_PROMPT ) ) |
---|
541 | { |
---|
542 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
543 | g_signal_emit( self, class->promptsig, 0, paths, ctor ); |
---|
544 | } |
---|
545 | else |
---|
546 | { |
---|
547 | for( ; paths; paths=paths->next ) |
---|
548 | if( !tr_ctorSetMetainfoFromFile( ctor, paths->data ) ) |
---|
549 | tr_core_add_ctor( self, ctor ); |
---|
550 | tr_ctorFree( ctor ); |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | void |
---|
555 | tr_core_torrents_added( TrCore * self ) |
---|
556 | { |
---|
557 | tr_core_update( self ); |
---|
558 | tr_core_errsig( self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL ); |
---|
559 | } |
---|
560 | |
---|
561 | void |
---|
562 | tr_core_delete_torrent( TrCore * self, GtkTreeIter * iter ) |
---|
563 | { |
---|
564 | TrTorrent * tor; |
---|
565 | GtkTreeModel * model = tr_core_model( self ); |
---|
566 | |
---|
567 | gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 ); |
---|
568 | gtk_list_store_remove( GTK_LIST_STORE( model ), iter ); |
---|
569 | tr_torrentRemoveSaved( tr_torrent_handle( tor ) ); |
---|
570 | |
---|
571 | g_object_unref( G_OBJECT( tor ) ); |
---|
572 | } |
---|
573 | |
---|
574 | static gboolean |
---|
575 | findTorrentInModel( TrCore * core, const TrTorrent * gtor, GtkTreeIter * setme ) |
---|
576 | { |
---|
577 | int match = 0; |
---|
578 | GtkTreeIter iter; |
---|
579 | GtkTreeModel * model = tr_core_model( core ); |
---|
580 | |
---|
581 | if( gtk_tree_model_iter_children( model, &iter, NULL ) ) do |
---|
582 | { |
---|
583 | TrTorrent * tmp; |
---|
584 | gtk_tree_model_get( model, &iter, MC_TORRENT, &tmp, -1 ); |
---|
585 | match = tmp == gtor; |
---|
586 | g_object_unref( G_OBJECT( tmp ) ); |
---|
587 | } |
---|
588 | while( !match && gtk_tree_model_iter_next( model, &iter ) ); |
---|
589 | |
---|
590 | if( match ) |
---|
591 | *setme = iter; |
---|
592 | |
---|
593 | return match; |
---|
594 | } |
---|
595 | |
---|
596 | void |
---|
597 | tr_core_remove_torrent( TrCore * self, TrTorrent * gtor, int deleteFiles ) |
---|
598 | { |
---|
599 | GtkTreeIter iter; |
---|
600 | GtkTreeModel * model = tr_core_model( self ); |
---|
601 | |
---|
602 | /* remove from the gui */ |
---|
603 | if( findTorrentInModel( self, gtor, &iter ) ) |
---|
604 | gtk_list_store_remove( GTK_LIST_STORE( model ), &iter ); |
---|
605 | |
---|
606 | /* maybe delete the downloaded files */ |
---|
607 | if( deleteFiles ) |
---|
608 | tr_torrent_delete_files( gtor ); |
---|
609 | |
---|
610 | /* delete the torrent */ |
---|
611 | tr_torrent_set_delete_flag( gtor, TRUE ); |
---|
612 | g_object_unref( G_OBJECT( gtor ) ); |
---|
613 | } |
---|
614 | |
---|
615 | |
---|
616 | /*** |
---|
617 | **** |
---|
618 | ***/ |
---|
619 | |
---|
620 | static gboolean |
---|
621 | update_foreach( GtkTreeModel * model, |
---|
622 | GtkTreePath * path UNUSED, |
---|
623 | GtkTreeIter * iter, |
---|
624 | gpointer data ) |
---|
625 | { |
---|
626 | TrTorrent * gtor; |
---|
627 | int oldStatus; |
---|
628 | const tr_stat * torStat; |
---|
629 | struct core_stats * stats = data; |
---|
630 | |
---|
631 | gtk_tree_model_get( model, iter, MC_TORRENT, >or, |
---|
632 | MC_STATUS, &oldStatus, |
---|
633 | -1 ); |
---|
634 | |
---|
635 | torStat = tr_torrent_stat( gtor ); |
---|
636 | |
---|
637 | /* sum the torrents' cumulative stats... */ |
---|
638 | if( torStat->status == TR_STATUS_DOWNLOAD ) |
---|
639 | ++stats->downloadCount; |
---|
640 | else if( torStat->status == TR_STATUS_SEED ) |
---|
641 | ++stats->seedingCount; |
---|
642 | stats->clientDownloadSpeed += torStat->rateDownload; |
---|
643 | stats->clientUploadSpeed += torStat->rateUpload; |
---|
644 | |
---|
645 | /* update the model's status if necessary */ |
---|
646 | if( oldStatus != (int) torStat->status ) |
---|
647 | gtk_list_store_set( GTK_LIST_STORE( model ), iter, |
---|
648 | MC_STATUS, torStat->status, |
---|
649 | -1 ); |
---|
650 | |
---|
651 | tr_torrent_check_seeding_cap ( gtor ); |
---|
652 | |
---|
653 | g_object_unref( gtor ); |
---|
654 | return FALSE; |
---|
655 | } |
---|
656 | |
---|
657 | void |
---|
658 | tr_core_update( TrCore * self ) |
---|
659 | { |
---|
660 | int column; |
---|
661 | GtkSortType order; |
---|
662 | GtkTreeSortable * sortable; |
---|
663 | GtkTreeModel * model = tr_core_model( self ); |
---|
664 | |
---|
665 | /* pause sorting */ |
---|
666 | sortable = GTK_TREE_SORTABLE( model ); |
---|
667 | gtk_tree_sortable_get_sort_column_id( sortable, &column, &order ); |
---|
668 | gtk_tree_sortable_set_sort_column_id( sortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, order ); |
---|
669 | |
---|
670 | /* refresh the model */ |
---|
671 | memset( &self->priv->stats, 0, sizeof( struct core_stats ) ); |
---|
672 | gtk_tree_model_foreach( model, update_foreach, &self->priv->stats ); |
---|
673 | |
---|
674 | /* resume sorting */ |
---|
675 | gtk_tree_sortable_set_sort_column_id( sortable, column, order ); |
---|
676 | } |
---|
677 | |
---|
678 | void |
---|
679 | tr_core_quit( TrCore * self ) |
---|
680 | { |
---|
681 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
682 | g_signal_emit( self, class->quitsig, 0 ); |
---|
683 | } |
---|
684 | |
---|
685 | /** |
---|
686 | *** Prefs |
---|
687 | **/ |
---|
688 | |
---|
689 | static void |
---|
690 | commitPrefsChange( TrCore * self, const char * key ) |
---|
691 | { |
---|
692 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
693 | pref_save( NULL ); |
---|
694 | g_signal_emit( self, class->prefsig, 0, key ); |
---|
695 | } |
---|
696 | |
---|
697 | void |
---|
698 | tr_core_set_pref( TrCore * self, const char * key, const char * newval ) |
---|
699 | { |
---|
700 | char * oldval = pref_string_get( key ); |
---|
701 | if( tr_strcmp( oldval, newval ) ) |
---|
702 | { |
---|
703 | pref_string_set( key, newval ); |
---|
704 | commitPrefsChange( self, key ); |
---|
705 | } |
---|
706 | g_free( oldval ); |
---|
707 | } |
---|
708 | |
---|
709 | void |
---|
710 | tr_core_set_pref_bool( TrCore * self, const char * key, gboolean newval ) |
---|
711 | { |
---|
712 | const gboolean oldval = pref_flag_get( key ); |
---|
713 | if( oldval != newval ) |
---|
714 | { |
---|
715 | pref_flag_set( key, newval ); |
---|
716 | commitPrefsChange( self, key ); |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | void |
---|
721 | tr_core_set_pref_int( TrCore * self, const char * key, int newval ) |
---|
722 | { |
---|
723 | const int oldval = pref_int_get( key ); |
---|
724 | if( oldval != newval ) |
---|
725 | { |
---|
726 | pref_int_set( key, newval ); |
---|
727 | commitPrefsChange( self, key ); |
---|
728 | } |
---|
729 | } |
---|