1 | /****************************************************************************** |
---|
2 | * $Id: tr_core.c 4050 2007-12-03 19:43:21Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007 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 <inttypes.h> |
---|
26 | #include <stdio.h> |
---|
27 | #include <string.h> |
---|
28 | |
---|
29 | #include <gtk/gtk.h> |
---|
30 | #include <glib/gi18n.h> |
---|
31 | |
---|
32 | #include <libtransmission/transmission.h> |
---|
33 | #include <libtransmission/utils.h> |
---|
34 | |
---|
35 | #include "conf.h" |
---|
36 | #include "tr_core.h" |
---|
37 | #include "tr_prefs.h" |
---|
38 | #include "tr_torrent.h" |
---|
39 | #include "util.h" |
---|
40 | |
---|
41 | static void |
---|
42 | tr_core_marshal_err( GClosure * closure, GValue * ret SHUTUP, guint count, |
---|
43 | const GValue * vals, gpointer hint SHUTUP, |
---|
44 | gpointer marshal ) |
---|
45 | { |
---|
46 | typedef void (*TRMarshalErr) |
---|
47 | ( gpointer, enum tr_core_err, const char *, gpointer ); |
---|
48 | TRMarshalErr callback; |
---|
49 | GCClosure * cclosure = (GCClosure*) closure; |
---|
50 | enum tr_core_err errcode; |
---|
51 | const char * errstr; |
---|
52 | gpointer inst, gdata; |
---|
53 | |
---|
54 | g_return_if_fail( 3 == count ); |
---|
55 | |
---|
56 | inst = g_value_peek_pointer( vals ); |
---|
57 | errcode = g_value_get_int( vals + 1 ); |
---|
58 | errstr = g_value_get_string( vals + 2 ); |
---|
59 | gdata = closure->data; |
---|
60 | |
---|
61 | callback = (TRMarshalErr) ( NULL == marshal ? |
---|
62 | cclosure->callback : marshal ); |
---|
63 | callback( inst, errcode, errstr, gdata ); |
---|
64 | } |
---|
65 | |
---|
66 | static void |
---|
67 | tr_core_marshal_prompt( GClosure * closure, GValue * ret SHUTUP, guint count, |
---|
68 | const GValue * vals, gpointer hint SHUTUP, |
---|
69 | gpointer marshal ) |
---|
70 | { |
---|
71 | typedef void (*TRMarshalPrompt) |
---|
72 | ( gpointer, GList *, enum tr_torrent_action, gboolean, gpointer ); |
---|
73 | TRMarshalPrompt callback; |
---|
74 | GCClosure * cclosure = (GCClosure*) closure; |
---|
75 | GList * paths; |
---|
76 | enum tr_torrent_action action; |
---|
77 | gboolean paused; |
---|
78 | gpointer inst, gdata; |
---|
79 | |
---|
80 | g_return_if_fail( 4 == count ); |
---|
81 | |
---|
82 | inst = g_value_peek_pointer( vals ); |
---|
83 | paths = g_value_get_pointer( vals + 1 ); |
---|
84 | action = g_value_get_int( vals + 2 ); |
---|
85 | paused = g_value_get_boolean( vals + 3 ); |
---|
86 | gdata = closure->data; |
---|
87 | |
---|
88 | callback = (TRMarshalPrompt) ( NULL == marshal ? |
---|
89 | cclosure->callback : marshal ); |
---|
90 | callback( inst, paths, action, paused, gdata ); |
---|
91 | } |
---|
92 | |
---|
93 | static void |
---|
94 | tr_core_marshal_data( GClosure * closure, GValue * ret SHUTUP, guint count, |
---|
95 | const GValue * vals, gpointer hint SHUTUP, |
---|
96 | gpointer marshal ) |
---|
97 | { |
---|
98 | typedef void (*TRMarshalPrompt) |
---|
99 | ( gpointer, uint8_t *, size_t, gboolean, gpointer ); |
---|
100 | TRMarshalPrompt callback; |
---|
101 | GCClosure * cclosure = (GCClosure*) closure; |
---|
102 | uint8_t * data; |
---|
103 | size_t size; |
---|
104 | gboolean paused; |
---|
105 | gpointer inst, gdata; |
---|
106 | |
---|
107 | g_return_if_fail( 4 == count ); |
---|
108 | |
---|
109 | inst = g_value_peek_pointer( vals ); |
---|
110 | data = (uint8_t *) g_value_get_string( vals + 1 ); |
---|
111 | size = g_value_get_uint( vals + 2 ); |
---|
112 | paused = g_value_get_boolean( vals + 3 ); |
---|
113 | gdata = closure->data; |
---|
114 | |
---|
115 | callback = (TRMarshalPrompt) ( NULL == marshal ? |
---|
116 | cclosure->callback : marshal ); |
---|
117 | callback( inst, data, size, paused, gdata ); |
---|
118 | } |
---|
119 | |
---|
120 | static void |
---|
121 | tr_core_dispose( GObject * obj ) |
---|
122 | { |
---|
123 | TrCore * self = (TrCore *) obj; |
---|
124 | GObjectClass * parent; |
---|
125 | |
---|
126 | if( self->disposed ) |
---|
127 | return; |
---|
128 | |
---|
129 | self->disposed = TRUE; |
---|
130 | pref_save( NULL ); |
---|
131 | parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) ); |
---|
132 | parent->dispose( obj ); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | static void |
---|
137 | tr_core_class_init( gpointer g_class, gpointer g_class_data SHUTUP ) |
---|
138 | { |
---|
139 | GObjectClass * gobject_class; |
---|
140 | TrCoreClass * core_class; |
---|
141 | |
---|
142 | gobject_class = G_OBJECT_CLASS( g_class ); |
---|
143 | gobject_class->dispose = tr_core_dispose; |
---|
144 | |
---|
145 | core_class = TR_CORE_CLASS( g_class ); |
---|
146 | core_class->errsig = g_signal_new( "error", G_TYPE_FROM_CLASS( g_class ), |
---|
147 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
148 | tr_core_marshal_err, G_TYPE_NONE, |
---|
149 | 2, G_TYPE_INT, G_TYPE_STRING ); |
---|
150 | core_class->promptsig = g_signal_new( "directory-prompt", |
---|
151 | G_TYPE_FROM_CLASS( g_class ), |
---|
152 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
153 | tr_core_marshal_prompt, G_TYPE_NONE, |
---|
154 | 3, G_TYPE_POINTER, G_TYPE_INT, |
---|
155 | G_TYPE_BOOLEAN ); |
---|
156 | core_class->promptdatasig = g_signal_new( "directory-prompt-data", |
---|
157 | G_TYPE_FROM_CLASS( g_class ), |
---|
158 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
159 | tr_core_marshal_data, |
---|
160 | G_TYPE_NONE, 3, G_TYPE_STRING, |
---|
161 | G_TYPE_UINT, G_TYPE_BOOLEAN ); |
---|
162 | core_class->quitsig = g_signal_new( "quit", G_TYPE_FROM_CLASS( g_class ), |
---|
163 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
164 | g_cclosure_marshal_VOID__VOID, |
---|
165 | G_TYPE_NONE, 0 ); |
---|
166 | core_class->prefsig = g_signal_new( "prefs-changed", |
---|
167 | G_TYPE_FROM_CLASS( g_class ), |
---|
168 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
169 | g_cclosure_marshal_VOID__STRING, |
---|
170 | G_TYPE_NONE, 1, G_TYPE_STRING ); |
---|
171 | } |
---|
172 | |
---|
173 | static int |
---|
174 | compareProgress( GtkTreeModel * model, |
---|
175 | GtkTreeIter * a, |
---|
176 | GtkTreeIter * b, |
---|
177 | gpointer user_data UNUSED ) |
---|
178 | { |
---|
179 | int ia, ib; |
---|
180 | gfloat rateUpA, rateUpB; |
---|
181 | gfloat rateDownA, rateDownB; |
---|
182 | gfloat percentDoneA, percentDoneB; |
---|
183 | guint64 uploadedEverA, uploadedEverB; |
---|
184 | |
---|
185 | gtk_tree_model_get( model, a, MC_PROG_D, &percentDoneA, |
---|
186 | MC_DRATE, &rateDownA, |
---|
187 | MC_URATE, &rateUpA, |
---|
188 | MC_UP, &uploadedEverA, |
---|
189 | -1 ); |
---|
190 | gtk_tree_model_get( model, b, MC_PROG_D, &percentDoneB, |
---|
191 | MC_DRATE, &rateDownB, |
---|
192 | MC_URATE, &rateUpB, |
---|
193 | MC_UP, &uploadedEverB, |
---|
194 | -1 ); |
---|
195 | |
---|
196 | ia = (int)( rateUpA + rateDownA ); |
---|
197 | ib = (int)( rateUpB + rateDownB ); |
---|
198 | if( ia != ib ) |
---|
199 | return ia - ib; |
---|
200 | |
---|
201 | ia = (int)( 100.0 * percentDoneA ); |
---|
202 | ib = (int)( 100.0 * percentDoneB ); |
---|
203 | if( ia != ib ) |
---|
204 | return ia - ib; |
---|
205 | |
---|
206 | if( uploadedEverA != uploadedEverB ) |
---|
207 | return uploadedEverA < uploadedEverB ? -1 : 1; |
---|
208 | |
---|
209 | return 0; |
---|
210 | } |
---|
211 | |
---|
212 | #define STR_REVERSE "reverse-" |
---|
213 | #define STR_PROGRESS "progress" |
---|
214 | #define STR_NAME "name" |
---|
215 | |
---|
216 | static void |
---|
217 | onSortColumnChanged( GtkTreeSortable * sortable, gpointer unused UNUSED ) |
---|
218 | { |
---|
219 | int column; |
---|
220 | GtkSortType order; |
---|
221 | if( gtk_tree_sortable_get_sort_column_id( sortable, &column, &order ) ) |
---|
222 | { |
---|
223 | GString * gstr = g_string_new( NULL ); |
---|
224 | switch( column ) { |
---|
225 | case MC_PROG_D: g_string_assign( gstr, STR_PROGRESS ); break; |
---|
226 | default: g_string_assign( gstr, STR_NAME ); break; |
---|
227 | } |
---|
228 | if( order == GTK_SORT_DESCENDING ) |
---|
229 | g_string_prepend( gstr, STR_REVERSE ); |
---|
230 | pref_string_set( PREF_KEY_SORT_COLUMN, gstr->str ); |
---|
231 | g_string_free( gstr, TRUE ); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | void |
---|
236 | tr_core_set_sort_column_from_prefs( TrCore * core ) |
---|
237 | { |
---|
238 | char * val = pref_string_get( PREF_KEY_SORT_COLUMN ); |
---|
239 | char * freeme = val; |
---|
240 | gint column; |
---|
241 | GtkSortType order = GTK_SORT_ASCENDING; |
---|
242 | if( g_str_has_prefix( val, STR_REVERSE ) ) { |
---|
243 | order = GTK_SORT_DESCENDING; |
---|
244 | val += strlen( STR_REVERSE ); |
---|
245 | } |
---|
246 | if( !strcmp( val, STR_PROGRESS ) ) |
---|
247 | column = MC_PROG_D; |
---|
248 | else /* default */ |
---|
249 | column = MC_NAME; |
---|
250 | gtk_tree_sortable_set_sort_column_id ( GTK_TREE_SORTABLE( core->model ), column, order ); |
---|
251 | g_free( freeme ); |
---|
252 | } |
---|
253 | |
---|
254 | static void |
---|
255 | tr_core_init( GTypeInstance * instance, gpointer g_class SHUTUP ) |
---|
256 | { |
---|
257 | TrCore * self = (TrCore *) instance; |
---|
258 | GtkListStore * store; |
---|
259 | |
---|
260 | /* column types for the model used to store torrent information */ |
---|
261 | /* keep this in sync with the enum near the bottom of tr_core.h */ |
---|
262 | GType types[] = |
---|
263 | { |
---|
264 | /* info->name, info->totalSize, info->hashString, status, */ |
---|
265 | G_TYPE_STRING, G_TYPE_UINT64, G_TYPE_STRING, G_TYPE_INT, |
---|
266 | /* error, errorString, percentComplete, percentDone, rateDownload, rateUpload, */ |
---|
267 | G_TYPE_INT, G_TYPE_STRING, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT, |
---|
268 | /* eta, peersConnected, peersUploading, peersDownloading, seeders, */ |
---|
269 | G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, |
---|
270 | /* leechers, completedFromTracker, downloaded, uploaded */ |
---|
271 | G_TYPE_INT, G_TYPE_INT, G_TYPE_UINT64, G_TYPE_UINT64, |
---|
272 | /* ratio, left, TrTorrent object, ID for IPC */ |
---|
273 | G_TYPE_FLOAT, G_TYPE_UINT64, TR_TORRENT_TYPE, G_TYPE_INT, |
---|
274 | }; |
---|
275 | |
---|
276 | #ifdef REFDBG |
---|
277 | fprintf( stderr, "core %p init\n", self ); |
---|
278 | #endif |
---|
279 | |
---|
280 | /* create the model used to store torrent data */ |
---|
281 | g_assert( ALEN( types ) == MC_ROW_COUNT ); |
---|
282 | store = gtk_list_store_newv( MC_ROW_COUNT, types ); |
---|
283 | g_signal_connect( store, "sort-column-changed", G_CALLBACK(onSortColumnChanged), NULL ); |
---|
284 | |
---|
285 | gtk_tree_sortable_set_sort_func( GTK_TREE_SORTABLE(store), |
---|
286 | MC_PROG_D, |
---|
287 | compareProgress, |
---|
288 | NULL, NULL ); |
---|
289 | |
---|
290 | self->model = GTK_TREE_MODEL( store ); |
---|
291 | self->handle = tr_init( "gtk" ); |
---|
292 | self->nextid = 1; |
---|
293 | self->quitting = FALSE; |
---|
294 | self->disposed = FALSE; |
---|
295 | } |
---|
296 | |
---|
297 | GType |
---|
298 | tr_core_get_type( void ) |
---|
299 | { |
---|
300 | static GType type = 0; |
---|
301 | |
---|
302 | if( 0 == type ) |
---|
303 | { |
---|
304 | static const GTypeInfo info = |
---|
305 | { |
---|
306 | sizeof( TrCoreClass ), |
---|
307 | NULL, /* base_init */ |
---|
308 | NULL, /* base_finalize */ |
---|
309 | tr_core_class_init, /* class_init */ |
---|
310 | NULL, /* class_finalize */ |
---|
311 | NULL, /* class_data */ |
---|
312 | sizeof( TrCore ), |
---|
313 | 0, /* n_preallocs */ |
---|
314 | tr_core_init, /* instance_init */ |
---|
315 | NULL, |
---|
316 | }; |
---|
317 | type = g_type_register_static( G_TYPE_OBJECT, "TrCore", &info, 0 ); |
---|
318 | } |
---|
319 | |
---|
320 | return type; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | *** |
---|
325 | **/ |
---|
326 | |
---|
327 | TrCore * |
---|
328 | tr_core_new( void ) |
---|
329 | { |
---|
330 | return g_object_new( TR_CORE_TYPE, NULL ); |
---|
331 | } |
---|
332 | |
---|
333 | GtkTreeModel * |
---|
334 | tr_core_model( TrCore * self ) |
---|
335 | { |
---|
336 | g_return_val_if_fail (TR_IS_CORE(self), NULL); |
---|
337 | |
---|
338 | return self->disposed ? NULL : self->model; |
---|
339 | } |
---|
340 | |
---|
341 | tr_handle * |
---|
342 | tr_core_handle( TrCore * self ) |
---|
343 | { |
---|
344 | g_return_val_if_fail (TR_IS_CORE(self), NULL); |
---|
345 | |
---|
346 | return self->disposed ? NULL : self->handle; |
---|
347 | } |
---|
348 | |
---|
349 | static void |
---|
350 | tr_core_insert( TrCore * self, TrTorrent * tor ) |
---|
351 | { |
---|
352 | const tr_info * inf = tr_torrent_info( tor ); |
---|
353 | GtkTreeIter unused; |
---|
354 | gtk_list_store_insert_with_values( GTK_LIST_STORE( self->model ), &unused, 0, |
---|
355 | MC_NAME, inf->name, |
---|
356 | MC_SIZE, inf->totalSize, |
---|
357 | MC_HASH, inf->hashString, |
---|
358 | MC_TORRENT, tor, |
---|
359 | MC_ID, self->nextid, |
---|
360 | -1); |
---|
361 | g_object_unref( tor ); |
---|
362 | self->nextid++; |
---|
363 | } |
---|
364 | |
---|
365 | int |
---|
366 | tr_core_load( TrCore * self, gboolean paused ) |
---|
367 | { |
---|
368 | int i; |
---|
369 | int count = 0; |
---|
370 | tr_torrent ** torrents; |
---|
371 | char * path; |
---|
372 | |
---|
373 | TR_IS_CORE( self ); |
---|
374 | |
---|
375 | path = getdownloaddir( ); |
---|
376 | |
---|
377 | torrents = tr_loadTorrents ( self->handle, path, paused, &count ); |
---|
378 | for( i=0; i<count; ++i ) |
---|
379 | tr_core_insert( self, tr_torrent_new_preexisting( torrents[i] ) ); |
---|
380 | tr_free( torrents ); |
---|
381 | |
---|
382 | g_free( path ); |
---|
383 | return count; |
---|
384 | } |
---|
385 | |
---|
386 | gboolean |
---|
387 | tr_core_add( TrCore * self, const char * path, enum tr_torrent_action act, |
---|
388 | gboolean paused ) |
---|
389 | { |
---|
390 | GList * list; |
---|
391 | int ret; |
---|
392 | |
---|
393 | TR_IS_CORE( self ); |
---|
394 | |
---|
395 | list = g_list_append( NULL, (void *) path ); |
---|
396 | ret = tr_core_add_list( self, list, act, paused ); |
---|
397 | g_list_free( list ); |
---|
398 | |
---|
399 | return 1 == ret; |
---|
400 | } |
---|
401 | |
---|
402 | static void |
---|
403 | tr_core_errsig( TrCore * self, enum tr_core_err type, const char * msg ) |
---|
404 | { |
---|
405 | TrCoreClass * class; |
---|
406 | |
---|
407 | class = g_type_class_peek( TR_CORE_TYPE ); |
---|
408 | g_signal_emit( self, class->errsig, 0, type, msg ); |
---|
409 | } |
---|
410 | |
---|
411 | gboolean |
---|
412 | tr_core_add_dir( TrCore * self, const char * path, const char * dir, |
---|
413 | enum tr_torrent_action act, gboolean paused ) |
---|
414 | { |
---|
415 | TrTorrent * tor; |
---|
416 | char * errstr; |
---|
417 | |
---|
418 | TR_IS_CORE( self ); |
---|
419 | |
---|
420 | errstr = NULL; |
---|
421 | tor = tr_torrent_new( self->handle, path, dir, act, paused, &errstr ); |
---|
422 | if( NULL == tor ) |
---|
423 | { |
---|
424 | tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr ); |
---|
425 | g_free( errstr ); |
---|
426 | return FALSE; |
---|
427 | } |
---|
428 | g_assert( NULL == errstr ); |
---|
429 | |
---|
430 | tr_core_insert( self, tor ); |
---|
431 | |
---|
432 | return TRUE; |
---|
433 | } |
---|
434 | |
---|
435 | int |
---|
436 | tr_core_add_list( TrCore * self, GList * paths, enum tr_torrent_action act, |
---|
437 | gboolean paused ) |
---|
438 | { |
---|
439 | char * dir; |
---|
440 | int count; |
---|
441 | |
---|
442 | TR_IS_CORE( self ); |
---|
443 | |
---|
444 | if( pref_flag_get( PREF_KEY_DIR_ASK ) ) |
---|
445 | { |
---|
446 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
447 | g_signal_emit( self, class->promptsig, 0, paths, act, paused ); |
---|
448 | return 0; |
---|
449 | } |
---|
450 | |
---|
451 | dir = getdownloaddir(); |
---|
452 | count = 0; |
---|
453 | for( ; paths; paths=paths->next ) |
---|
454 | if( tr_core_add_dir( self, paths->data, dir, act, paused ) ) |
---|
455 | count++; |
---|
456 | |
---|
457 | g_free( dir ); |
---|
458 | return count; |
---|
459 | } |
---|
460 | |
---|
461 | gboolean |
---|
462 | tr_core_add_data( TrCore * self, uint8_t * data, size_t size, gboolean paused ) |
---|
463 | { |
---|
464 | gboolean ret; |
---|
465 | char * path; |
---|
466 | TR_IS_CORE( self ); |
---|
467 | |
---|
468 | if( pref_flag_get( PREF_KEY_DIR_ASK ) ) |
---|
469 | { |
---|
470 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
471 | g_signal_emit( self, class->promptdatasig, 0, data, size, paused ); |
---|
472 | return FALSE; |
---|
473 | } |
---|
474 | |
---|
475 | path = getdownloaddir( ); |
---|
476 | ret = tr_core_add_data_dir( self, data, size, path, paused ); |
---|
477 | g_free( path ); |
---|
478 | return ret; |
---|
479 | } |
---|
480 | |
---|
481 | gboolean |
---|
482 | tr_core_add_data_dir( TrCore * self, uint8_t * data, size_t size, |
---|
483 | const char * dir, gboolean paused ) |
---|
484 | { |
---|
485 | TrTorrent * tor; |
---|
486 | char * errstr = NULL; |
---|
487 | |
---|
488 | TR_IS_CORE( self ); |
---|
489 | |
---|
490 | tor = tr_torrent_new_with_data( self->handle, data, size, dir, |
---|
491 | paused, &errstr ); |
---|
492 | if( NULL == tor ) |
---|
493 | { |
---|
494 | tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr ); |
---|
495 | g_free( errstr ); |
---|
496 | return FALSE; |
---|
497 | } |
---|
498 | g_assert( NULL == errstr ); |
---|
499 | |
---|
500 | tr_core_insert( self, tor ); |
---|
501 | |
---|
502 | return TRUE; |
---|
503 | } |
---|
504 | |
---|
505 | void |
---|
506 | tr_core_torrents_added( TrCore * self ) |
---|
507 | { |
---|
508 | TR_IS_CORE( self ); |
---|
509 | |
---|
510 | tr_core_update( self ); |
---|
511 | tr_core_errsig( self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL ); |
---|
512 | } |
---|
513 | |
---|
514 | void |
---|
515 | tr_core_delete_torrent( TrCore * self, GtkTreeIter * iter ) |
---|
516 | { |
---|
517 | TrTorrent * tor; |
---|
518 | |
---|
519 | TR_IS_CORE( self ); |
---|
520 | |
---|
521 | gtk_tree_model_get( self->model, iter, MC_TORRENT, &tor, -1 ); |
---|
522 | gtk_list_store_remove( GTK_LIST_STORE( self->model ), iter ); |
---|
523 | tr_torrentRemoveSaved( tr_torrent_handle( tor ) ); |
---|
524 | |
---|
525 | tr_torrent_sever( tor ); |
---|
526 | } |
---|
527 | |
---|
528 | static gboolean |
---|
529 | update_foreach( GtkTreeModel * model, |
---|
530 | GtkTreePath * path UNUSED, |
---|
531 | GtkTreeIter * iter, |
---|
532 | gpointer data UNUSED) |
---|
533 | { |
---|
534 | TrTorrent * tor; |
---|
535 | const tr_stat * st; |
---|
536 | |
---|
537 | gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 ); |
---|
538 | st = tr_torrent_stat( tor ); |
---|
539 | tr_torrent_check_seeding_cap ( tor ); |
---|
540 | g_object_unref( tor ); |
---|
541 | |
---|
542 | gtk_list_store_set( GTK_LIST_STORE( model ), iter, |
---|
543 | MC_STAT, st->status, |
---|
544 | MC_ERR, st->error, |
---|
545 | MC_TERR, st->errorString, |
---|
546 | MC_PROG_C, st->percentComplete, |
---|
547 | MC_PROG_D, st->percentDone, |
---|
548 | MC_DRATE, st->rateDownload, |
---|
549 | MC_URATE, st->rateUpload, |
---|
550 | MC_ETA, st->eta, |
---|
551 | MC_PEERS, st->peersConnected, |
---|
552 | MC_UPEERS, st->peersGettingFromUs, |
---|
553 | MC_DPEERS, st->peersSendingToUs, |
---|
554 | MC_SEED, st->seeders, |
---|
555 | MC_LEECH, st->leechers, |
---|
556 | MC_DONE, st->completedFromTracker, |
---|
557 | MC_DOWN, st->downloadedEver, |
---|
558 | MC_UP, st->uploadedEver, |
---|
559 | MC_RATIO, st->ratio, |
---|
560 | MC_LEFT, st->leftUntilDone, |
---|
561 | -1 ); |
---|
562 | |
---|
563 | return FALSE; |
---|
564 | } |
---|
565 | |
---|
566 | void |
---|
567 | tr_core_update( TrCore * self ) |
---|
568 | { |
---|
569 | int column; |
---|
570 | GtkSortType order; |
---|
571 | GtkTreeSortable * sortable; |
---|
572 | |
---|
573 | /* pause sorting */ |
---|
574 | sortable = GTK_TREE_SORTABLE( self->model ); |
---|
575 | gtk_tree_sortable_get_sort_column_id( sortable, &column, &order ); |
---|
576 | gtk_tree_sortable_set_sort_column_id( sortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, order ); |
---|
577 | |
---|
578 | /* refresh the model */ |
---|
579 | gtk_tree_model_foreach( self->model, update_foreach, NULL ); |
---|
580 | |
---|
581 | /* resume sorting */ |
---|
582 | gtk_tree_sortable_set_sort_column_id( sortable, column, order ); |
---|
583 | } |
---|
584 | |
---|
585 | void |
---|
586 | tr_core_quit( TrCore * self ) |
---|
587 | { |
---|
588 | TrCoreClass * class; |
---|
589 | |
---|
590 | TR_IS_CORE( self ); |
---|
591 | |
---|
592 | class = g_type_class_peek( TR_CORE_TYPE ); |
---|
593 | g_signal_emit( self, class->quitsig, 0 ); |
---|
594 | } |
---|
595 | |
---|
596 | /** |
---|
597 | *** Prefs |
---|
598 | **/ |
---|
599 | |
---|
600 | static void |
---|
601 | commitPrefsChange( TrCore * self, const char * key ) |
---|
602 | { |
---|
603 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
604 | pref_save( NULL ); |
---|
605 | g_signal_emit( self, class->prefsig, 0, key ); |
---|
606 | } |
---|
607 | |
---|
608 | void |
---|
609 | tr_core_set_pref( TrCore * self, const char * key, const char * newval ) |
---|
610 | { |
---|
611 | char * oldval = pref_string_get( key ); |
---|
612 | if( tr_strcmp( oldval, newval ) ) |
---|
613 | { |
---|
614 | pref_string_set( key, newval ); |
---|
615 | commitPrefsChange( self, key ); |
---|
616 | } |
---|
617 | g_free( oldval ); |
---|
618 | } |
---|
619 | |
---|
620 | void |
---|
621 | tr_core_set_pref_bool( TrCore * self, const char * key, gboolean newval ) |
---|
622 | { |
---|
623 | const gboolean oldval = pref_flag_get( key ); |
---|
624 | if( oldval != newval ) |
---|
625 | { |
---|
626 | pref_flag_set( key, newval ); |
---|
627 | commitPrefsChange( self, key ); |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | void |
---|
632 | tr_core_set_pref_int( TrCore * self, const char * key, int newval ) |
---|
633 | { |
---|
634 | const int oldval = pref_int_get( key ); |
---|
635 | if( oldval != newval ) |
---|
636 | { |
---|
637 | pref_int_set( key, newval ); |
---|
638 | commitPrefsChange( self, key ); |
---|
639 | } |
---|
640 | } |
---|