1 | /****************************************************************************** |
---|
2 | * $Id: tr_core.c 4284 2007-12-22 16:34:47Z 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 | compareDouble( double a, double b ) |
---|
175 | { |
---|
176 | if( a < b ) return -1; |
---|
177 | if( b > a ) return 1; |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
181 | static int |
---|
182 | compareByActivity( GtkTreeModel * model, |
---|
183 | GtkTreeIter * a, |
---|
184 | GtkTreeIter * b, |
---|
185 | gpointer user_data UNUSED ) |
---|
186 | { |
---|
187 | int i; |
---|
188 | tr_torrent *ta, *tb; |
---|
189 | const tr_stat *sa, *sb; |
---|
190 | |
---|
191 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
192 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
193 | |
---|
194 | sa = tr_torrentStatCached( ta ); |
---|
195 | sb = tr_torrentStatCached( tb ); |
---|
196 | |
---|
197 | if(( i = compareDouble( sa->rateUpload + sa->rateDownload, |
---|
198 | sb->rateUpload + sb->rateDownload ) )) |
---|
199 | return i; |
---|
200 | |
---|
201 | if( sa->uploadedEver != sb->uploadedEver ) |
---|
202 | return sa->uploadedEver < sa->uploadedEver ? -1 : 1; |
---|
203 | |
---|
204 | return 0; |
---|
205 | } |
---|
206 | |
---|
207 | static int |
---|
208 | compareByDateAdded( GtkTreeModel * model UNUSED, |
---|
209 | GtkTreeIter * a UNUSED, |
---|
210 | GtkTreeIter * b UNUSED, |
---|
211 | gpointer user_data UNUSED ) |
---|
212 | { |
---|
213 | return 0; /* FIXME */ |
---|
214 | } |
---|
215 | |
---|
216 | static int |
---|
217 | compareByName( GtkTreeModel * model, |
---|
218 | GtkTreeIter * a, |
---|
219 | GtkTreeIter * b, |
---|
220 | gpointer user_data UNUSED ) |
---|
221 | { |
---|
222 | int ret; |
---|
223 | char *ca, *cb; |
---|
224 | gtk_tree_model_get( model, a, MC_NAME_COLLATED, &ca, -1 ); |
---|
225 | gtk_tree_model_get( model, b, MC_NAME_COLLATED, &cb, -1 ); |
---|
226 | ret = strcmp( ca, cb ); |
---|
227 | g_free( cb ); |
---|
228 | g_free( ca ); |
---|
229 | return ret; |
---|
230 | } |
---|
231 | |
---|
232 | static int |
---|
233 | compareByProgress( GtkTreeModel * model, |
---|
234 | GtkTreeIter * a, |
---|
235 | GtkTreeIter * b, |
---|
236 | gpointer user_data UNUSED ) |
---|
237 | { |
---|
238 | int ret; |
---|
239 | tr_torrent *ta, *tb; |
---|
240 | const tr_stat *sa, *sb; |
---|
241 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
242 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
243 | sa = tr_torrentStatCached( ta ); |
---|
244 | sb = tr_torrentStatCached( tb ); |
---|
245 | ret = compareDouble( sa->percentDone, sb->percentDone ); |
---|
246 | if( !ret ) |
---|
247 | ret = compareDouble( sa->ratio, sa->ratio ); |
---|
248 | return ret; |
---|
249 | } |
---|
250 | |
---|
251 | static int |
---|
252 | compareByState( GtkTreeModel * model, |
---|
253 | GtkTreeIter * a, |
---|
254 | GtkTreeIter * b, |
---|
255 | gpointer user_data UNUSED ) |
---|
256 | { |
---|
257 | tr_torrent *ta, *tb; |
---|
258 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
259 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
260 | return tr_torrentStatCached(ta)->status - tr_torrentStatCached(tb)->status; |
---|
261 | } |
---|
262 | |
---|
263 | static int |
---|
264 | compareByTracker( GtkTreeModel * model, |
---|
265 | GtkTreeIter * a, |
---|
266 | GtkTreeIter * b, |
---|
267 | gpointer user_data UNUSED ) |
---|
268 | { |
---|
269 | const tr_torrent *ta, *tb; |
---|
270 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
271 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
272 | return strcmp( tr_torrentInfo(ta)->primaryAddress, |
---|
273 | tr_torrentInfo(tb)->primaryAddress ); |
---|
274 | } |
---|
275 | |
---|
276 | /*** |
---|
277 | **** |
---|
278 | ***/ |
---|
279 | |
---|
280 | |
---|
281 | static void |
---|
282 | setSort( TrCore * core, const char * mode, gboolean isReversed ) |
---|
283 | { |
---|
284 | int col = MC_TORRENT_RAW; |
---|
285 | GtkSortType type = isReversed ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING; |
---|
286 | GtkTreeSortable * sortable = GTK_TREE_SORTABLE( core->model ); |
---|
287 | |
---|
288 | if( !strcmp( mode, "sort-by-activity" ) ) |
---|
289 | gtk_tree_sortable_set_sort_func( sortable, col, compareByActivity, NULL, NULL ); |
---|
290 | else if( !strcmp( mode, "sort-by-date-added" ) ) |
---|
291 | gtk_tree_sortable_set_sort_func( sortable, col, compareByDateAdded, NULL, NULL ); |
---|
292 | else if( !strcmp( mode, "sort-by-progress" ) ) |
---|
293 | gtk_tree_sortable_set_sort_func( sortable, col, compareByProgress, NULL, NULL ); |
---|
294 | else if( !strcmp( mode, "sort-by-state" ) ) |
---|
295 | gtk_tree_sortable_set_sort_func( sortable, col, compareByState, NULL, NULL ); |
---|
296 | else if( !strcmp( mode, "sort-by-tracker" ) ) |
---|
297 | gtk_tree_sortable_set_sort_func( sortable, col, compareByTracker, NULL, NULL ); |
---|
298 | else { |
---|
299 | type = isReversed ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING; |
---|
300 | gtk_tree_sortable_set_sort_func( sortable, col, compareByName, NULL, NULL ); |
---|
301 | } |
---|
302 | |
---|
303 | gtk_tree_sortable_set_sort_column_id( sortable, col, type ); |
---|
304 | } |
---|
305 | |
---|
306 | static void |
---|
307 | prefsChanged( TrCore * core, const char * key, gpointer data UNUSED ) |
---|
308 | { |
---|
309 | if( !strcmp( key, PREF_KEY_SORT_MODE ) || !strcmp( key, PREF_KEY_SORT_REVERSED ) ) |
---|
310 | { |
---|
311 | char * mode = pref_string_get( PREF_KEY_SORT_MODE ); |
---|
312 | gboolean isReversed = pref_flag_get( PREF_KEY_SORT_REVERSED ); |
---|
313 | setSort( core, mode, isReversed ); |
---|
314 | g_free( mode ); |
---|
315 | } |
---|
316 | else if( !strcmp( key, PREF_KEY_MAX_PEERS_GLOBAL ) ) |
---|
317 | { |
---|
318 | const uint16_t val = pref_int_get( key ); |
---|
319 | tr_setGlobalPeerLimit( core->handle, val ); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | static void |
---|
324 | tr_core_init( GTypeInstance * instance, gpointer g_class SHUTUP ) |
---|
325 | { |
---|
326 | TrCore * self = (TrCore *) instance; |
---|
327 | GtkListStore * store; |
---|
328 | |
---|
329 | /* column types for the model used to store torrent information */ |
---|
330 | /* keep this in sync with the enum near the bottom of tr_core.h */ |
---|
331 | GType types[] = { |
---|
332 | G_TYPE_STRING, /* name */ |
---|
333 | G_TYPE_STRING, /* collated name */ |
---|
334 | G_TYPE_STRING, /* hash string */ |
---|
335 | TR_TORRENT_TYPE, /* TrTorrent object */ |
---|
336 | G_TYPE_POINTER, /* tr_torrent* */ |
---|
337 | G_TYPE_INT /* ID for IPC */ |
---|
338 | }; |
---|
339 | |
---|
340 | /* create the model used to store torrent data */ |
---|
341 | g_assert( ALEN( types ) == MC_ROW_COUNT ); |
---|
342 | store = gtk_list_store_newv( MC_ROW_COUNT, types ); |
---|
343 | |
---|
344 | self->model = GTK_TREE_MODEL( store ); |
---|
345 | self->handle = tr_init( "gtk" ); |
---|
346 | self->nextid = 1; |
---|
347 | self->quitting = FALSE; |
---|
348 | self->disposed = FALSE; |
---|
349 | } |
---|
350 | |
---|
351 | GType |
---|
352 | tr_core_get_type( void ) |
---|
353 | { |
---|
354 | static GType type = 0; |
---|
355 | |
---|
356 | if( !type ) |
---|
357 | { |
---|
358 | static const GTypeInfo info = |
---|
359 | { |
---|
360 | sizeof( TrCoreClass ), |
---|
361 | NULL, /* base_init */ |
---|
362 | NULL, /* base_finalize */ |
---|
363 | tr_core_class_init, /* class_init */ |
---|
364 | NULL, /* class_finalize */ |
---|
365 | NULL, /* class_data */ |
---|
366 | sizeof( TrCore ), |
---|
367 | 0, /* n_preallocs */ |
---|
368 | tr_core_init, /* instance_init */ |
---|
369 | NULL, |
---|
370 | }; |
---|
371 | type = g_type_register_static( G_TYPE_OBJECT, "TrCore", &info, 0 ); |
---|
372 | } |
---|
373 | |
---|
374 | return type; |
---|
375 | } |
---|
376 | |
---|
377 | /** |
---|
378 | *** |
---|
379 | **/ |
---|
380 | |
---|
381 | TrCore * |
---|
382 | tr_core_new( void ) |
---|
383 | { |
---|
384 | TrCore * core = TR_CORE( g_object_new( TR_CORE_TYPE, NULL ) ); |
---|
385 | |
---|
386 | /* init from prefs & listen to pref changes */ |
---|
387 | prefsChanged( core, PREF_KEY_SORT_MODE, NULL ); |
---|
388 | prefsChanged( core, PREF_KEY_SORT_REVERSED, NULL ); |
---|
389 | prefsChanged( core, PREF_KEY_MAX_PEERS_GLOBAL, NULL ); |
---|
390 | g_signal_connect( core, "prefs-changed", G_CALLBACK(prefsChanged), NULL ); |
---|
391 | |
---|
392 | return core; |
---|
393 | } |
---|
394 | |
---|
395 | GtkTreeModel * |
---|
396 | tr_core_model( TrCore * self ) |
---|
397 | { |
---|
398 | g_return_val_if_fail (TR_IS_CORE(self), NULL); |
---|
399 | |
---|
400 | return self->disposed ? NULL : self->model; |
---|
401 | } |
---|
402 | |
---|
403 | tr_handle * |
---|
404 | tr_core_handle( TrCore * self ) |
---|
405 | { |
---|
406 | g_return_val_if_fail (TR_IS_CORE(self), NULL); |
---|
407 | |
---|
408 | return self->disposed ? NULL : self->handle; |
---|
409 | } |
---|
410 | |
---|
411 | static char* |
---|
412 | doCollate( const char * in ) |
---|
413 | { |
---|
414 | const char * end = in + strlen( in ); |
---|
415 | char * casefold; |
---|
416 | char * ret; |
---|
417 | |
---|
418 | while( in < end ) { |
---|
419 | const gunichar ch = g_utf8_get_char( in ); |
---|
420 | if (!g_unichar_isalnum (ch)) // eat everything before the first alnum |
---|
421 | in += g_unichar_to_utf8( ch, NULL ); |
---|
422 | else |
---|
423 | break; |
---|
424 | } |
---|
425 | |
---|
426 | if ( in == end ) |
---|
427 | return g_strdup (""); |
---|
428 | |
---|
429 | casefold = g_utf8_casefold( in, end-in ); |
---|
430 | ret = g_utf8_collate_key( casefold, -1 ); |
---|
431 | g_free( casefold ); |
---|
432 | return ret; |
---|
433 | } |
---|
434 | |
---|
435 | static void |
---|
436 | tr_core_insert( TrCore * self, TrTorrent * tor ) |
---|
437 | { |
---|
438 | const tr_info * inf = tr_torrent_info( tor ); |
---|
439 | char * collated = doCollate( inf->name ); |
---|
440 | GtkTreeIter unused; |
---|
441 | gtk_list_store_insert_with_values( GTK_LIST_STORE( self->model ), &unused, 0, |
---|
442 | MC_NAME, inf->name, |
---|
443 | MC_NAME_COLLATED, collated, |
---|
444 | MC_HASH, inf->hashString, |
---|
445 | MC_TORRENT, tor, |
---|
446 | MC_TORRENT_RAW, tor->handle, |
---|
447 | MC_ID, self->nextid, |
---|
448 | -1); |
---|
449 | self->nextid++; |
---|
450 | g_object_unref( tor ); |
---|
451 | g_free( collated ); |
---|
452 | } |
---|
453 | |
---|
454 | int |
---|
455 | tr_core_load( TrCore * self, gboolean paused ) |
---|
456 | { |
---|
457 | int i; |
---|
458 | int count = 0; |
---|
459 | tr_torrent ** torrents; |
---|
460 | char * path; |
---|
461 | tr_ctor * ctor; |
---|
462 | |
---|
463 | TR_IS_CORE( self ); |
---|
464 | |
---|
465 | path = getdownloaddir( ); |
---|
466 | |
---|
467 | ctor = tr_ctorNew( self->handle ); |
---|
468 | tr_ctorSetPaused( ctor, TR_FORCE, paused ); |
---|
469 | tr_ctorSetDestination( ctor, TR_FALLBACK, path ); |
---|
470 | tr_ctorSetMaxConnectedPeers( ctor, TR_FALLBACK, pref_int_get( PREF_KEY_MAX_PEERS_PER_TORRENT ) ); |
---|
471 | |
---|
472 | torrents = tr_loadTorrents ( self->handle, ctor, &count ); |
---|
473 | for( i=0; i<count; ++i ) |
---|
474 | tr_core_insert( self, tr_torrent_new_preexisting( torrents[i] ) ); |
---|
475 | |
---|
476 | tr_free( torrents ); |
---|
477 | tr_ctorFree( ctor ); |
---|
478 | g_free( path ); |
---|
479 | |
---|
480 | return count; |
---|
481 | } |
---|
482 | |
---|
483 | gboolean |
---|
484 | tr_core_add( TrCore * self, const char * path, enum tr_torrent_action act, |
---|
485 | gboolean paused ) |
---|
486 | { |
---|
487 | GList * list; |
---|
488 | int ret; |
---|
489 | |
---|
490 | TR_IS_CORE( self ); |
---|
491 | |
---|
492 | list = g_list_append( NULL, (void *) path ); |
---|
493 | ret = tr_core_add_list( self, list, act, paused ); |
---|
494 | g_list_free( list ); |
---|
495 | |
---|
496 | return 1 == ret; |
---|
497 | } |
---|
498 | |
---|
499 | static void |
---|
500 | tr_core_errsig( TrCore * self, enum tr_core_err type, const char * msg ) |
---|
501 | { |
---|
502 | TrCoreClass * class; |
---|
503 | |
---|
504 | class = g_type_class_peek( TR_CORE_TYPE ); |
---|
505 | g_signal_emit( self, class->errsig, 0, type, msg ); |
---|
506 | } |
---|
507 | |
---|
508 | gboolean |
---|
509 | tr_core_add_dir( TrCore * self, const char * path, const char * dir, |
---|
510 | enum tr_torrent_action act, gboolean paused ) |
---|
511 | { |
---|
512 | TrTorrent * tor; |
---|
513 | char * errstr; |
---|
514 | |
---|
515 | TR_IS_CORE( self ); |
---|
516 | |
---|
517 | errstr = NULL; |
---|
518 | tor = tr_torrent_new( self->handle, path, dir, act, paused, &errstr ); |
---|
519 | if( NULL == tor ) |
---|
520 | { |
---|
521 | tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr ); |
---|
522 | g_free( errstr ); |
---|
523 | return FALSE; |
---|
524 | } |
---|
525 | g_assert( NULL == errstr ); |
---|
526 | |
---|
527 | tr_core_insert( self, tor ); |
---|
528 | |
---|
529 | return TRUE; |
---|
530 | } |
---|
531 | |
---|
532 | int |
---|
533 | tr_core_add_list( TrCore * self, GList * paths, enum tr_torrent_action act, |
---|
534 | gboolean paused ) |
---|
535 | { |
---|
536 | char * dir; |
---|
537 | int count; |
---|
538 | |
---|
539 | TR_IS_CORE( self ); |
---|
540 | |
---|
541 | if( pref_flag_get( PREF_KEY_DIR_ASK ) ) |
---|
542 | { |
---|
543 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
544 | g_signal_emit( self, class->promptsig, 0, paths, act, paused ); |
---|
545 | return 0; |
---|
546 | } |
---|
547 | |
---|
548 | dir = getdownloaddir(); |
---|
549 | count = 0; |
---|
550 | for( ; paths; paths=paths->next ) |
---|
551 | if( tr_core_add_dir( self, paths->data, dir, act, paused ) ) |
---|
552 | count++; |
---|
553 | |
---|
554 | g_free( dir ); |
---|
555 | return count; |
---|
556 | } |
---|
557 | |
---|
558 | gboolean |
---|
559 | tr_core_add_data( TrCore * self, uint8_t * data, size_t size, gboolean paused ) |
---|
560 | { |
---|
561 | gboolean ret; |
---|
562 | char * path; |
---|
563 | TR_IS_CORE( self ); |
---|
564 | |
---|
565 | if( pref_flag_get( PREF_KEY_DIR_ASK ) ) |
---|
566 | { |
---|
567 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
568 | g_signal_emit( self, class->promptdatasig, 0, data, size, paused ); |
---|
569 | return FALSE; |
---|
570 | } |
---|
571 | |
---|
572 | path = getdownloaddir( ); |
---|
573 | ret = tr_core_add_data_dir( self, data, size, path, paused ); |
---|
574 | g_free( path ); |
---|
575 | return ret; |
---|
576 | } |
---|
577 | |
---|
578 | gboolean |
---|
579 | tr_core_add_data_dir( TrCore * self, uint8_t * data, size_t size, |
---|
580 | const char * dir, gboolean paused ) |
---|
581 | { |
---|
582 | TrTorrent * tor; |
---|
583 | char * errstr = NULL; |
---|
584 | |
---|
585 | TR_IS_CORE( self ); |
---|
586 | |
---|
587 | tor = tr_torrent_new_with_data( self->handle, data, size, dir, |
---|
588 | paused, &errstr ); |
---|
589 | if( NULL == tor ) |
---|
590 | { |
---|
591 | tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr ); |
---|
592 | g_free( errstr ); |
---|
593 | return FALSE; |
---|
594 | } |
---|
595 | g_assert( NULL == errstr ); |
---|
596 | |
---|
597 | tr_core_insert( self, tor ); |
---|
598 | |
---|
599 | return TRUE; |
---|
600 | } |
---|
601 | |
---|
602 | void |
---|
603 | tr_core_torrents_added( TrCore * self ) |
---|
604 | { |
---|
605 | TR_IS_CORE( self ); |
---|
606 | |
---|
607 | tr_core_update( self ); |
---|
608 | tr_core_errsig( self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL ); |
---|
609 | } |
---|
610 | |
---|
611 | void |
---|
612 | tr_core_delete_torrent( TrCore * self, GtkTreeIter * iter ) |
---|
613 | { |
---|
614 | TrTorrent * tor; |
---|
615 | |
---|
616 | TR_IS_CORE( self ); |
---|
617 | |
---|
618 | gtk_tree_model_get( self->model, iter, MC_TORRENT, &tor, -1 ); |
---|
619 | gtk_list_store_remove( GTK_LIST_STORE( self->model ), iter ); |
---|
620 | tr_torrentRemoveSaved( tr_torrent_handle( tor ) ); |
---|
621 | |
---|
622 | tr_torrent_sever( tor ); |
---|
623 | } |
---|
624 | |
---|
625 | static gboolean |
---|
626 | update_foreach( GtkTreeModel * model, |
---|
627 | GtkTreePath * path UNUSED, |
---|
628 | GtkTreeIter * iter, |
---|
629 | gpointer data UNUSED) |
---|
630 | { |
---|
631 | TrTorrent * tor; |
---|
632 | |
---|
633 | gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 ); |
---|
634 | tr_torrent_check_seeding_cap ( tor ); |
---|
635 | g_object_unref( tor ); |
---|
636 | |
---|
637 | return FALSE; |
---|
638 | } |
---|
639 | |
---|
640 | void |
---|
641 | tr_core_update( TrCore * self ) |
---|
642 | { |
---|
643 | int column; |
---|
644 | GtkSortType order; |
---|
645 | GtkTreeSortable * sortable; |
---|
646 | |
---|
647 | /* pause sorting */ |
---|
648 | sortable = GTK_TREE_SORTABLE( self->model ); |
---|
649 | gtk_tree_sortable_get_sort_column_id( sortable, &column, &order ); |
---|
650 | gtk_tree_sortable_set_sort_column_id( sortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, order ); |
---|
651 | |
---|
652 | /* refresh the model */ |
---|
653 | gtk_tree_model_foreach( self->model, update_foreach, NULL ); |
---|
654 | |
---|
655 | /* resume sorting */ |
---|
656 | gtk_tree_sortable_set_sort_column_id( sortable, column, order ); |
---|
657 | } |
---|
658 | |
---|
659 | void |
---|
660 | tr_core_quit( TrCore * self ) |
---|
661 | { |
---|
662 | TrCoreClass * class; |
---|
663 | |
---|
664 | TR_IS_CORE( self ); |
---|
665 | |
---|
666 | class = g_type_class_peek( TR_CORE_TYPE ); |
---|
667 | g_signal_emit( self, class->quitsig, 0 ); |
---|
668 | } |
---|
669 | |
---|
670 | /** |
---|
671 | *** Prefs |
---|
672 | **/ |
---|
673 | |
---|
674 | static void |
---|
675 | commitPrefsChange( TrCore * self, const char * key ) |
---|
676 | { |
---|
677 | TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE ); |
---|
678 | pref_save( NULL ); |
---|
679 | g_signal_emit( self, class->prefsig, 0, key ); |
---|
680 | } |
---|
681 | |
---|
682 | void |
---|
683 | tr_core_set_pref( TrCore * self, const char * key, const char * newval ) |
---|
684 | { |
---|
685 | char * oldval = pref_string_get( key ); |
---|
686 | if( tr_strcmp( oldval, newval ) ) |
---|
687 | { |
---|
688 | pref_string_set( key, newval ); |
---|
689 | commitPrefsChange( self, key ); |
---|
690 | } |
---|
691 | g_free( oldval ); |
---|
692 | } |
---|
693 | |
---|
694 | void |
---|
695 | tr_core_set_pref_bool( TrCore * self, const char * key, gboolean newval ) |
---|
696 | { |
---|
697 | const gboolean oldval = pref_flag_get( key ); |
---|
698 | if( oldval != newval ) |
---|
699 | { |
---|
700 | pref_flag_set( key, newval ); |
---|
701 | commitPrefsChange( self, key ); |
---|
702 | } |
---|
703 | } |
---|
704 | |
---|
705 | void |
---|
706 | tr_core_set_pref_int( TrCore * self, const char * key, int newval ) |
---|
707 | { |
---|
708 | const int oldval = pref_int_get( key ); |
---|
709 | if( oldval != newval ) |
---|
710 | { |
---|
711 | pref_int_set( key, newval ); |
---|
712 | commitPrefsChange( self, key ); |
---|
713 | } |
---|
714 | } |
---|