1 | /****************************************************************************** |
---|
2 | * $Id: tr-core.c 5947 2008-05-27 17:47:39Z 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/utils.h> /* tr_free */ |
---|
38 | |
---|
39 | #include "conf.h" |
---|
40 | #include "tr-core.h" |
---|
41 | #ifdef HAVE_DBUS_GLIB |
---|
42 | #include "tr-core-dbus.h" |
---|
43 | #endif |
---|
44 | #include "tr-prefs.h" |
---|
45 | #include "tr-torrent.h" |
---|
46 | #include "util.h" |
---|
47 | |
---|
48 | static void tr_core_set_hibernation_allowed( TrCore * core, gboolean allowed ); |
---|
49 | |
---|
50 | struct TrCorePrivate |
---|
51 | { |
---|
52 | #ifdef HAVE_GIO |
---|
53 | GFileMonitor * monitor; |
---|
54 | gulong monitor_tag; |
---|
55 | char * monitor_path; |
---|
56 | GSList * monitor_files; |
---|
57 | guint monitor_idle_tag; |
---|
58 | #endif |
---|
59 | gboolean inhibit_allowed; |
---|
60 | gboolean have_inhibit_cookie; |
---|
61 | guint inhibit_cookie; |
---|
62 | GtkTreeModel * model; |
---|
63 | tr_handle * handle; |
---|
64 | }; |
---|
65 | |
---|
66 | static void |
---|
67 | tr_core_marshal_err( GClosure * closure, GValue * ret UNUSED, |
---|
68 | guint count, const GValue * vals, |
---|
69 | gpointer hint UNUSED, gpointer marshal ) |
---|
70 | { |
---|
71 | typedef void (*TRMarshalErr) |
---|
72 | ( gpointer, enum tr_core_err, const char *, gpointer ); |
---|
73 | TRMarshalErr callback; |
---|
74 | GCClosure * cclosure = (GCClosure*) closure; |
---|
75 | enum tr_core_err errcode; |
---|
76 | const char * errstr; |
---|
77 | gpointer inst, gdata; |
---|
78 | |
---|
79 | g_return_if_fail( count == 3 ); |
---|
80 | |
---|
81 | inst = g_value_peek_pointer( vals ); |
---|
82 | errcode = g_value_get_int( vals + 1 ); |
---|
83 | errstr = g_value_get_string( vals + 2 ); |
---|
84 | gdata = closure->data; |
---|
85 | |
---|
86 | callback = (TRMarshalErr)( marshal ? marshal : cclosure->callback ); |
---|
87 | callback( inst, errcode, errstr, gdata ); |
---|
88 | } |
---|
89 | |
---|
90 | static void |
---|
91 | tr_core_marshal_prompt( GClosure * closure, GValue * ret UNUSED, |
---|
92 | guint count, const GValue * vals, |
---|
93 | gpointer hint UNUSED, gpointer marshal ) |
---|
94 | { |
---|
95 | typedef void (*TRMarshalPrompt)( gpointer, tr_ctor *, gpointer ); |
---|
96 | TRMarshalPrompt callback; |
---|
97 | GCClosure * cclosure = (GCClosure*) closure; |
---|
98 | gpointer ctor; |
---|
99 | gpointer inst, gdata; |
---|
100 | |
---|
101 | g_return_if_fail( count == 2 ); |
---|
102 | |
---|
103 | inst = g_value_peek_pointer( vals ); |
---|
104 | ctor = g_value_peek_pointer( vals + 1 ); |
---|
105 | gdata = closure->data; |
---|
106 | |
---|
107 | callback = (TRMarshalPrompt)( marshal ? marshal : cclosure->callback ); |
---|
108 | callback( inst, ctor, gdata ); |
---|
109 | } |
---|
110 | |
---|
111 | static int |
---|
112 | isDisposed( const TrCore * core ) |
---|
113 | { |
---|
114 | return !core || !core->priv; |
---|
115 | } |
---|
116 | |
---|
117 | static void |
---|
118 | tr_core_dispose( GObject * obj ) |
---|
119 | { |
---|
120 | TrCore * core = TR_CORE( obj ); |
---|
121 | |
---|
122 | if( !isDisposed( core ) ) |
---|
123 | { |
---|
124 | GObjectClass * parent; |
---|
125 | |
---|
126 | pref_save( NULL ); |
---|
127 | core->priv = NULL; |
---|
128 | |
---|
129 | parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) ); |
---|
130 | parent->dispose( obj ); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | static void |
---|
135 | tr_core_class_init( gpointer g_class, gpointer g_class_data UNUSED ) |
---|
136 | { |
---|
137 | GObjectClass * gobject_class; |
---|
138 | TrCoreClass * core_class; |
---|
139 | |
---|
140 | g_type_class_add_private( g_class, sizeof(struct TrCorePrivate) ); |
---|
141 | |
---|
142 | gobject_class = G_OBJECT_CLASS( g_class ); |
---|
143 | gobject_class->dispose = tr_core_dispose; |
---|
144 | |
---|
145 | |
---|
146 | core_class = TR_CORE_CLASS( g_class ); |
---|
147 | core_class->errsig = g_signal_new( "error", G_TYPE_FROM_CLASS( g_class ), |
---|
148 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
149 | tr_core_marshal_err, G_TYPE_NONE, |
---|
150 | 2, G_TYPE_INT, G_TYPE_STRING ); |
---|
151 | core_class->promptsig = g_signal_new( "add-torrent-prompt", |
---|
152 | G_TYPE_FROM_CLASS( g_class ), |
---|
153 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
154 | tr_core_marshal_prompt, G_TYPE_NONE, |
---|
155 | 1, G_TYPE_POINTER ); |
---|
156 | core_class->quitsig = g_signal_new( "quit", G_TYPE_FROM_CLASS( g_class ), |
---|
157 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
158 | g_cclosure_marshal_VOID__VOID, |
---|
159 | G_TYPE_NONE, 0 ); |
---|
160 | core_class->prefsig = g_signal_new( "prefs-changed", |
---|
161 | G_TYPE_FROM_CLASS( g_class ), |
---|
162 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
---|
163 | g_cclosure_marshal_VOID__STRING, |
---|
164 | G_TYPE_NONE, 1, G_TYPE_STRING ); |
---|
165 | |
---|
166 | #ifdef HAVE_DBUS_GLIB |
---|
167 | { |
---|
168 | DBusGConnection * bus = dbus_g_bus_get( DBUS_BUS_SESSION, NULL ); |
---|
169 | DBusGProxy * bus_proxy = NULL; |
---|
170 | if( bus ) |
---|
171 | bus_proxy = dbus_g_proxy_new_for_name( bus, "org.freedesktop.DBus", |
---|
172 | "/org/freedesktop/DBus", |
---|
173 | "org.freedesktop.DBus" ); |
---|
174 | if( bus_proxy ) { |
---|
175 | int result = 0; |
---|
176 | dbus_g_proxy_call( bus_proxy, "RequestName", NULL, |
---|
177 | G_TYPE_STRING, "com.transmissionbt.Transmission", |
---|
178 | G_TYPE_UINT, 0, |
---|
179 | G_TYPE_INVALID, |
---|
180 | G_TYPE_UINT, &result, |
---|
181 | G_TYPE_INVALID ); |
---|
182 | if( result == 1 ) |
---|
183 | dbus_g_object_type_install_info( TR_CORE_TYPE, |
---|
184 | &dbus_glib_tr_core_object_info ); |
---|
185 | } |
---|
186 | } |
---|
187 | #endif |
---|
188 | } |
---|
189 | |
---|
190 | /*** |
---|
191 | **** SORTING |
---|
192 | ***/ |
---|
193 | |
---|
194 | static int |
---|
195 | compareDouble( double a, double b ) |
---|
196 | { |
---|
197 | if( a < b ) return -1; |
---|
198 | if( a > b ) return 1; |
---|
199 | return 0; |
---|
200 | } |
---|
201 | |
---|
202 | static int |
---|
203 | compareRatio( double a, double b ) |
---|
204 | { |
---|
205 | if( (int)a == TR_RATIO_INF && (int)b == TR_RATIO_INF ) return 0; |
---|
206 | if( (int)a == TR_RATIO_INF ) return 1; |
---|
207 | if( (int)b == TR_RATIO_INF ) return -1; |
---|
208 | return compareDouble( a, b ); |
---|
209 | } |
---|
210 | |
---|
211 | static int |
---|
212 | compareByRatio( GtkTreeModel * model, |
---|
213 | GtkTreeIter * a, |
---|
214 | GtkTreeIter * b, |
---|
215 | gpointer user_data UNUSED ) |
---|
216 | { |
---|
217 | tr_torrent *ta, *tb; |
---|
218 | const tr_stat *sa, *sb; |
---|
219 | |
---|
220 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
221 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
222 | |
---|
223 | sa = tr_torrentStatCached( ta ); |
---|
224 | sb = tr_torrentStatCached( tb ); |
---|
225 | |
---|
226 | return compareRatio( sa->ratio, sb->ratio ); |
---|
227 | } |
---|
228 | |
---|
229 | static int |
---|
230 | compareByActivity( GtkTreeModel * model, |
---|
231 | GtkTreeIter * a, |
---|
232 | GtkTreeIter * b, |
---|
233 | gpointer user_data UNUSED ) |
---|
234 | { |
---|
235 | int i; |
---|
236 | tr_torrent *ta, *tb; |
---|
237 | const tr_stat *sa, *sb; |
---|
238 | |
---|
239 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
240 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
241 | |
---|
242 | sa = tr_torrentStatCached( ta ); |
---|
243 | sb = tr_torrentStatCached( tb ); |
---|
244 | |
---|
245 | if(( i = compareDouble( sa->rateUpload + sa->rateDownload, |
---|
246 | sb->rateUpload + sb->rateDownload ) )) |
---|
247 | return i; |
---|
248 | |
---|
249 | if( sa->uploadedEver != sb->uploadedEver ) |
---|
250 | return sa->uploadedEver < sa->uploadedEver ? -1 : 1; |
---|
251 | |
---|
252 | return 0; |
---|
253 | } |
---|
254 | |
---|
255 | static int |
---|
256 | compareByName( GtkTreeModel * model, |
---|
257 | GtkTreeIter * a, |
---|
258 | GtkTreeIter * b, |
---|
259 | gpointer user_data UNUSED ) |
---|
260 | { |
---|
261 | int ret; |
---|
262 | char *ca, *cb; |
---|
263 | gtk_tree_model_get( model, a, MC_NAME_COLLATED, &ca, -1 ); |
---|
264 | gtk_tree_model_get( model, b, MC_NAME_COLLATED, &cb, -1 ); |
---|
265 | ret = strcmp( ca, cb ); |
---|
266 | g_free( cb ); |
---|
267 | g_free( ca ); |
---|
268 | return ret; |
---|
269 | } |
---|
270 | |
---|
271 | static int |
---|
272 | compareByProgress( GtkTreeModel * model, |
---|
273 | GtkTreeIter * a, |
---|
274 | GtkTreeIter * b, |
---|
275 | gpointer user_data UNUSED ) |
---|
276 | { |
---|
277 | int ret; |
---|
278 | tr_torrent *ta, *tb; |
---|
279 | const tr_stat *sa, *sb; |
---|
280 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
281 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
282 | sa = tr_torrentStatCached( ta ); |
---|
283 | sb = tr_torrentStatCached( tb ); |
---|
284 | ret = compareDouble( sa->percentDone, sb->percentDone ); |
---|
285 | if( !ret ) |
---|
286 | ret = compareRatio( sa->ratio, sb->ratio ); |
---|
287 | return ret; |
---|
288 | } |
---|
289 | |
---|
290 | static int |
---|
291 | compareByState( GtkTreeModel * model, |
---|
292 | GtkTreeIter * a, |
---|
293 | GtkTreeIter * b, |
---|
294 | gpointer user_data ) |
---|
295 | { |
---|
296 | int sa, sb, ret; |
---|
297 | |
---|
298 | /* first by state */ |
---|
299 | gtk_tree_model_get( model, a, MC_STATUS, &sa, -1 ); |
---|
300 | gtk_tree_model_get( model, b, MC_STATUS, &sb, -1 ); |
---|
301 | ret = sa - sb; |
---|
302 | |
---|
303 | /* second by progress */ |
---|
304 | if( !ret ) |
---|
305 | ret = compareByProgress( model, a, b, user_data ); |
---|
306 | |
---|
307 | return ret; |
---|
308 | } |
---|
309 | |
---|
310 | static int |
---|
311 | compareByTracker( GtkTreeModel * model, |
---|
312 | GtkTreeIter * a, |
---|
313 | GtkTreeIter * b, |
---|
314 | gpointer user_data UNUSED ) |
---|
315 | { |
---|
316 | const tr_torrent *ta, *tb; |
---|
317 | gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 ); |
---|
318 | gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 ); |
---|
319 | return strcmp( tr_torrentInfo(ta)->trackers[0].announce, |
---|
320 | tr_torrentInfo(tb)->trackers[0].announce ); |
---|
321 | } |
---|
322 | |
---|
323 | static void |
---|
324 | setSort( TrCore * core, const char * mode, gboolean isReversed ) |
---|
325 | { |
---|
326 | const int col = MC_TORRENT_RAW; |
---|
327 | GtkTreeIterCompareFunc sort_func; |
---|
328 | GtkSortType type = isReversed ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING; |
---|
329 | GtkTreeSortable * sortable = GTK_TREE_SORTABLE( tr_core_model( core ) ); |
---|
330 | |
---|
331 | if( !strcmp( mode, "sort-by-activity" ) ) |
---|
332 | sort_func = compareByActivity; |
---|
333 | else if( !strcmp( mode, "sort-by-progress" ) ) |
---|
334 | sort_func = compareByProgress; |
---|
335 | else if( !strcmp( mode, "sort-by-ratio" ) ) |
---|
336 | sort_func = compareByRatio; |
---|
337 | else if( !strcmp( mode, "sort-by-state" ) ) |
---|
338 | sort_func = compareByState; |
---|
339 | else if( !strcmp( mode, "sort-by-tracker" ) ) |
---|
340 | sort_func = compareByTracker; |
---|
341 | else { |
---|
342 | sort_func = compareByName; |
---|
343 | type = isReversed ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING; |
---|
344 | } |
---|
345 | |
---|
346 | gtk_tree_sortable_set_sort_func( sortable, col, sort_func, NULL, NULL ); |
---|
347 | gtk_tree_sortable_set_sort_column_id( sortable, col, type ); |
---|
348 | } |
---|
349 | |
---|
350 | static void |
---|
351 | tr_core_apply_defaults( tr_ctor * ctor ) |
---|
352 | { |
---|
353 | if( tr_ctorGetPaused( ctor, TR_FORCE, NULL ) ) |
---|
354 | tr_ctorSetPaused( ctor, TR_FORCE, !pref_flag_get( PREF_KEY_START ) ); |
---|
355 | |
---|
356 | if( tr_ctorGetDeleteSource( ctor, NULL ) ) |
---|
357 | tr_ctorSetDeleteSource( ctor, pref_flag_get( PREF_KEY_TRASH_ORIGINAL ) ); |
---|
358 | |
---|
359 | if( tr_ctorGetPeerLimit( ctor, TR_FORCE, NULL ) ) |
---|
360 | tr_ctorSetPeerLimit( ctor, TR_FORCE, |
---|
361 | pref_int_get( PREF_KEY_MAX_PEERS_PER_TORRENT ) ); |
---|
362 | |
---|
363 | if( tr_ctorGetDownloadDir( ctor, TR_FORCE, NULL ) ) { |
---|
364 | char * path = pref_string_get( PREF_KEY_DOWNLOAD_DIR ); |
---|
365 | tr_ctorSetDownloadDir( ctor, TR_FORCE, path ); |
---|
366 | g_free( path ); |
---|
367 | } |
---|
368 | } |
---|
369 | |
---|
370 | #ifdef HAVE_GIO |
---|
371 | static gboolean |
---|
372 | watchFolderIdle( gpointer gcore ) |
---|
373 | { |
---|
374 | TrCore * core = TR_CORE( gcore ); |
---|
375 | tr_core_add_list_defaults( core, core->priv->monitor_files ); |
---|
376 | |
---|
377 | /* cleanup */ |
---|
378 | core->priv->monitor_files = NULL; |
---|
379 | core->priv->monitor_idle_tag = 0; |
---|
380 | return FALSE; |
---|
381 | } |
---|
382 | |
---|
383 | static void |
---|
384 | maybeAddTorrent( TrCore * core, const char * filename ) |
---|
385 | { |
---|
386 | const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" ); |
---|
387 | |
---|
388 | if( isTorrent ) |
---|
389 | { |
---|
390 | struct TrCorePrivate * p = core->priv; |
---|
391 | |
---|
392 | if( !g_slist_find_custom( p->monitor_files, filename, (GCompareFunc)strcmp ) ) |
---|
393 | p->monitor_files = g_slist_append( p->monitor_files, g_strdup( filename ) ); |
---|
394 | if( !p->monitor_idle_tag ) |
---|
395 | p->monitor_idle_tag = g_timeout_add( 1000, watchFolderIdle, core ); |
---|
396 | } |
---|
397 | } |
---|
398 | |
---|
399 | static void |
---|
400 | watchFolderChanged( GFileMonitor * monitor UNUSED, |
---|
401 | GFile * file, |
---|
402 | GFile * other_type UNUSED, |
---|
403 | GFileMonitorEvent event_type, |
---|
404 | gpointer core ) |
---|
405 | { |
---|
406 | if( event_type == G_FILE_MONITOR_EVENT_CREATED ) |
---|
407 | { |
---|
408 | char * filename = g_file_get_path( file ); |
---|
409 | maybeAddTorrent( core, filename ); |
---|
410 | g_free( filename ); |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | static void |
---|
415 | scanWatchDir( TrCore * core ) |
---|
416 | { |
---|
417 | const gboolean isEnabled = pref_flag_get( PREF_KEY_DIR_WATCH_ENABLED ); |
---|
418 | if( isEnabled ) |
---|
419 | { |
---|
420 | char * dirname = pref_string_get( PREF_KEY_DIR_WATCH ); |
---|
421 | GDir * dir = g_dir_open( dirname, 0, NULL ); |
---|
422 | const char * basename; |
---|
423 | while(( basename = g_dir_read_name( dir ))) { |
---|
424 | char * filename = g_build_filename( dirname, basename, NULL ); |
---|
425 | maybeAddTorrent( core, filename ); |
---|
426 | g_free( filename ); |
---|
427 | } |
---|
428 | g_free( dirname ); |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | static void |
---|
433 | updateWatchDir( TrCore * core ) |
---|
434 | { |
---|
435 | char * filename = pref_string_get( PREF_KEY_DIR_WATCH ); |
---|
436 | const gboolean isEnabled = pref_flag_get( PREF_KEY_DIR_WATCH_ENABLED ); |
---|
437 | struct TrCorePrivate * p = TR_CORE( core )->priv; |
---|
438 | |
---|
439 | if( p->monitor && ( !isEnabled || tr_strcmp( filename, p->monitor_path ) ) ) |
---|
440 | { |
---|
441 | g_signal_handler_disconnect( p->monitor, p->monitor_tag ); |
---|
442 | g_free( p->monitor_path ); |
---|
443 | g_file_monitor_cancel( p->monitor ); |
---|
444 | g_object_unref( G_OBJECT( p->monitor ) ); |
---|
445 | p->monitor_path = NULL; |
---|
446 | p->monitor = NULL; |
---|
447 | p->monitor_tag = 0; |
---|
448 | } |
---|
449 | |
---|
450 | if( isEnabled && !p->monitor ) |
---|
451 | { |
---|
452 | GFile * file = g_file_new_for_path( filename ); |
---|
453 | GFileMonitor * m = g_file_monitor_directory( file, 0, NULL, NULL ); |
---|
454 | scanWatchDir( core ); |
---|
455 | p->monitor = m; |
---|
456 | p->monitor_path = g_strdup( filename ); |
---|
457 | p->monitor_tag = g_signal_connect( m, "changed", |
---|
458 | G_CALLBACK( watchFolderChanged ), core ); |
---|
459 | } |
---|
460 | |
---|
461 | g_free( filename ); |
---|
462 | } |
---|
463 | #endif |
---|
464 | |
---|
465 | static void |
---|
466 | prefsChanged( TrCore * core, const char * key, gpointer data UNUSED ) |
---|
467 | { |
---|
468 | if( !strcmp( key, PREF_KEY_SORT_MODE ) || |
---|
469 | !strcmp( key, PREF_KEY_SORT_REVERSED ) ) |
---|
470 | { |
---|
471 | char * mode = pref_string_get( PREF_KEY_SORT_MODE ); |
---|
472 | gboolean isReversed = pref_flag_get( PREF_KEY_SORT_REVERSED ); |
---|
473 | setSort( core, mode, isReversed ); |
---|
474 | g_free( mode ); |
---|
475 | } |
---|
476 | else if( !strcmp( key, PREF_KEY_MAX_PEERS_GLOBAL ) ) |
---|
477 | { |
---|
478 | const uint16_t val = pref_int_get( key ); |
---|
479 | tr_sessionSetPeerLimit( tr_core_handle( core ), val ); |
---|
480 | } |
---|
481 | else if( !strcmp( key, PREF_KEY_ALLOW_HIBERNATION ) ) |
---|
482 | { |
---|
483 | tr_core_set_hibernation_allowed( core, pref_flag_get( key ) ); |
---|
484 | } |
---|
485 | #ifdef HAVE_GIO |
---|
486 | else if( !strcmp( key, PREF_KEY_DIR_WATCH ) || |
---|
487 | !strcmp( key, PREF_KEY_DIR_WATCH_ENABLED ) ) |
---|
488 | { |
---|
489 | updateWatchDir( core ); |
---|
490 | } |
---|
491 | #endif |
---|
492 | } |
---|
493 | |
---|
494 | static void |
---|
495 | tr_core_init( GTypeInstance * instance, gpointer g_class UNUSED ) |
---|
496 | { |
---|
497 | TrCore * self = (TrCore *) instance; |
---|
498 | GtkListStore * store; |
---|
499 | struct TrCorePrivate * p; |
---|
500 | |
---|
501 | /* column types for the model used to store torrent information */ |
---|
502 | /* keep this in sync with the enum near the bottom of tr_core.h */ |
---|
503 | GType types[] = { |
---|
504 | G_TYPE_STRING, /* name */ |
---|
505 | G_TYPE_STRING, /* collated name */ |
---|
506 | G_TYPE_STRING, /* hash string */ |
---|
507 | TR_TORRENT_TYPE, /* TrTorrent object */ |
---|
508 | G_TYPE_POINTER, /* tr_torrent* */ |
---|
509 | G_TYPE_INT /* tr_stat()->status */ |
---|
510 | }; |
---|
511 | |
---|
512 | p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE( self, |
---|
513 | TR_CORE_TYPE, |
---|
514 | struct TrCorePrivate ); |
---|
515 | |
---|
516 | /* create the model used to store torrent data */ |
---|
517 | g_assert( ALEN( types ) == MC_ROW_COUNT ); |
---|
518 | store = gtk_list_store_newv( MC_ROW_COUNT, types ); |
---|
519 | |
---|
520 | p->model = GTK_TREE_MODEL( store ); |
---|
521 | |
---|
522 | #ifdef HAVE_DBUS_GLIB |
---|
523 | { |
---|
524 | DBusGConnection * bus = dbus_g_bus_get( DBUS_BUS_SESSION, NULL ); |
---|
525 | if( bus ) |
---|
526 | dbus_g_connection_register_g_object( bus, |
---|
527 | "/com/transmissionbt/Transmission", |
---|
528 | G_OBJECT( self )); |
---|
529 | } |
---|
530 | #endif |
---|
531 | |
---|
532 | } |
---|
533 | |
---|
534 | GType |
---|
535 | tr_core_get_type( void ) |
---|
536 | { |
---|
537 | static GType type = 0; |
---|
538 | |
---|
539 | if( !type ) |
---|
540 | { |
---|
541 | static const GTypeInfo info = |
---|
542 | { |
---|
543 | sizeof( TrCoreClass ), |
---|
544 | NULL, /* base_init */ |
---|
545 | NULL, /* base_finalize */ |
---|
546 | tr_core_class_init, /* class_init */ |
---|
547 | NULL, /* class_finalize */ |
---|
548 | NULL, /* class_data */ |
---|
549 | sizeof( TrCore ), |
---|
550 | 0, /* n_preallocs */ |
---|
551 | tr_core_init, /* instance_init */ |
---|
552 | NULL, |
---|
553 | }; |
---|
554 | type = g_type_register_static( G_TYPE_OBJECT, "TrCore", &info, 0 ); |
---|
555 | } |
---|
556 | |
---|
557 | return type; |
---|
558 | } |
---|
559 | |
---|
560 | /** |
---|
561 | *** |
---|
562 | **/ |
---|
563 | |
---|
564 | TrCore * |
---|
565 | tr_core_new( tr_handle * h ) |
---|
566 | { |
---|
567 | TrCore * core = TR_CORE( g_object_new( TR_CORE_TYPE, NULL ) ); |
---|
568 | core->priv->handle = h; |
---|
569 | |
---|
570 | /* init from prefs & listen to pref changes */ |
---|
571 | prefsChanged( core, PREF_KEY_SORT_MODE, NULL ); |
---|
572 | prefsChanged( core, PREF_KEY_SORT_REVERSED, NULL ); |
---|
573 | prefsChanged( core, PREF_KEY_DIR_WATCH_ENABLED, NULL ); |
---|
574 | prefsChanged( core, PREF_KEY_MAX_PEERS_GLOBAL, NULL ); |
---|
575 | prefsChanged( core, PREF_KEY_ALLOW_HIBERNATION, NULL ); |
---|
576 | g_signal_connect( core, "prefs-changed", G_CALLBACK(prefsChanged), NULL ); |
---|
577 | |
---|
578 | return core; |
---|
579 | } |
---|
580 | |
---|
581 | void |
---|
582 | tr_core_close( TrCore * core ) |
---|
583 | { |
---|
584 | tr_handle * handle = tr_core_handle( core ); |
---|
585 | if( handle ) |
---|
586 | { |
---|
587 | core->priv->handle = NULL; |
---|
588 | tr_sessionClose( handle ); |
---|
589 | } |
---|
590 | } |
---|
591 | |
---|
592 | GtkTreeModel * |
---|
593 | tr_core_model( TrCore * core ) |
---|
594 | { |
---|
595 | return isDisposed( core ) ? NULL : core->priv->model; |
---|
596 | } |
---|
597 | |
---|
598 | tr_handle * |
---|
599 | tr_core_handle( TrCore * core ) |
---|
600 | { |
---|
601 | return isDisposed( core ) ? NULL : core->priv->handle; |
---|
602 | } |
---|
603 | |
---|
604 | static gboolean |
---|
605 | statsForeach( GtkTreeModel * model, |
---|
606 | GtkTreePath * path UNUSED, |
---|
607 | GtkTreeIter * iter, |
---|
608 | gpointer gstats ) |
---|
609 | { |
---|
610 | tr_torrent * tor; |
---|
611 | struct core_stats * stats = gstats; |
---|
612 | int status; |
---|
613 | |
---|
614 | gtk_tree_model_get( model, iter, MC_TORRENT_RAW, &tor, -1 ); |
---|
615 | status = tr_torrentGetStatus( tor ); |
---|
616 | |
---|
617 | if( status == TR_STATUS_DOWNLOAD ) |
---|
618 | ++stats->downloadCount; |
---|
619 | else if( status == TR_STATUS_SEED ) |
---|
620 | ++stats->seedingCount; |
---|
621 | |
---|
622 | return FALSE; |
---|
623 | } |
---|
624 | |
---|
625 | void |
---|
626 | tr_core_get_stats( const TrCore * core, |
---|
627 | struct core_stats * setme ) |
---|
628 | { |
---|
629 | memset( setme, 0, sizeof( struct core_stats ) ); |
---|
630 | |
---|
631 | if( !isDisposed( core ) ) |
---|
632 | { |
---|
633 | tr_torrentRates( core->priv->handle, |
---|
634 | &setme->clientDownloadSpeed, |
---|
635 | &setme->clientUploadSpeed ); |
---|
636 | |
---|
637 | gtk_tree_model_foreach( core->priv->model, |
---|
638 | statsForeach, |
---|
639 | setme ); |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | static char* |
---|
644 | doCollate( const char * in ) |
---|
645 | { |
---|
646 | const char * end = in + strlen( in ); |
---|
647 | char * casefold; |
---|
648 | char * ret; |
---|
649 | |
---|
650 | while( in < end ) { |
---|
651 | const gunichar ch = g_utf8_get_char( in ); |
---|
652 | if (!g_unichar_isalnum (ch)) /* eat everything before the first alnum */ |
---|
653 | in += g_unichar_to_utf8( ch, NULL ); |
---|
654 | else |
---|
655 | break; |
---|
656 | } |
---|
657 | |
---|
658 | if ( in == end ) |
---|
659 | return g_strdup (""); |
---|
660 | |
---|
661 | casefold = g_utf8_casefold( in, end-in ); |
---|
662 | ret = g_utf8_collate_key( casefold, -1 ); |
---|
663 | g_free( casefold ); |
---|
664 | |
---|
665 | return ret; |
---|
666 | } |
---|
667 | |
---|
668 | void |
---|
669 | tr_core_add_torrent( TrCore * self, TrTorrent * gtor ) |
---|
670 | { |
---|
671 | const tr_info * inf = tr_torrent_info( gtor ); |
---|
672 | const tr_stat * torStat = tr_torrent_stat( gtor ); |
---|
673 | tr_torrent * tor = tr_torrent_handle( gtor ); |
---|
674 | char * collated = doCollate( inf->name ); |
---|
675 | GtkListStore * store = GTK_LIST_STORE( tr_core_model( self ) ); |
---|
676 | GtkTreeIter unused; |
---|
677 | |
---|
678 | gtk_list_store_insert_with_values( store, &unused, 0, |
---|
679 | MC_NAME, inf->name, |
---|
680 | MC_NAME_COLLATED, collated, |
---|
681 | MC_HASH, inf->hashString, |
---|
682 | MC_TORRENT, gtor, |
---|
683 | MC_TORRENT_RAW, tor, |
---|
684 | MC_STATUS, torStat->status, |
---|
685 | -1); |
---|
686 | |
---|
687 | /* cleanup */ |
---|
688 | g_object_unref( G_OBJECT( gtor ) ); |
---|
689 | g_free( collated ); |
---|
690 | } |
---|
691 | |
---|
692 | int |
---|
693 | tr_core_load( TrCore * self, gboolean forcePaused ) |
---|
694 | { |
---|
695 | int i; |
---|
696 | int count = 0; |
---|
697 | tr_torrent ** torrents; |
---|
698 | tr_ctor * ctor; |
---|
699 | |
---|
700 | ctor = tr_ctorNew( tr_core_handle( self ) ); |
---|
701 | if( forcePaused ) |
---|
702 | tr_ctorSetPaused( ctor, TR_FORCE, TRUE ); |
---|
703 | tr_ctorSetPeerLimit( ctor, TR_FALLBACK, |
---|
704 | pref_int_get( PREF_KEY_MAX_PEERS_PER_TORRENT ) ); |
---|
705 | |
---|
706 | torrents = tr_sessionLoadTorrents ( tr_core_handle( self ), ctor, &count ); |
---|
707 | for( i=0; i<count; ++i ) |
---|
708 | tr_core_add_torrent( self, tr_torrent_new_preexisting( torrents[i] ) ); |
---|
709 | |
---|
710 | tr_free( torrents ); |
---|
711 | tr_ctorFree( ctor ); |
---|
712 | |
---|
713 | return count; |
---|
714 | } |
---|
715 | |
---|
716 | static void |
---|
717 | tr_core_errsig( TrCore * core, enum tr_core_err type, const char * msg ) |
---|
718 | { |
---|
719 | g_signal_emit( core, TR_CORE_GET_CLASS(core)->errsig, 0, type, msg ); |
---|
720 | } |
---|
721 | |
---|
722 | void |
---|
723 | tr_core_add_ctor( TrCore * self, tr_ctor * ctor ) |
---|
724 | { |
---|
725 | TrTorrent * tor; |
---|
726 | char * errstr = NULL; |
---|
727 | |
---|
728 | tr_core_apply_defaults( ctor ); |
---|
729 | |
---|
730 | if(( tor = tr_torrent_new_ctor( tr_core_handle( self ), ctor, &errstr ))) |
---|
731 | tr_core_add_torrent( self, tor ); |
---|
732 | else{ |
---|
733 | tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr ); |
---|
734 | g_free( errstr ); |
---|
735 | } |
---|
736 | |
---|
737 | /* cleanup */ |
---|
738 | tr_ctorFree( ctor ); |
---|
739 | } |
---|
740 | |
---|
741 | static void |
---|
742 | add_filename( TrCore * core, |
---|
743 | const char * filename, |
---|
744 | gboolean doStart, |
---|
745 | gboolean doPrompt ) |
---|
746 | { |
---|
747 | tr_handle * handle = tr_core_handle( core ); |
---|
748 | |
---|
749 | if( filename && handle ) |
---|
750 | { |
---|
751 | tr_ctor * ctor = tr_ctorNew( handle ); |
---|
752 | tr_core_apply_defaults( ctor ); |
---|
753 | tr_ctorSetPaused( ctor, TR_FORCE, !doStart ); |
---|
754 | if( tr_ctorSetMetainfoFromFile( ctor, filename ) ) |
---|
755 | tr_ctorFree( ctor ); |
---|
756 | else if( tr_torrentParse( handle, ctor, NULL ) ) |
---|
757 | tr_ctorFree( ctor ); |
---|
758 | else if( doPrompt ) |
---|
759 | g_signal_emit( core, TR_CORE_GET_CLASS(core)->promptsig, 0, ctor ); |
---|
760 | else |
---|
761 | tr_core_add_ctor( core, ctor ); |
---|
762 | } |
---|
763 | } |
---|
764 | |
---|
765 | gboolean |
---|
766 | tr_core_add_file( TrCore * core, |
---|
767 | const char * filename, |
---|
768 | gboolean * success, |
---|
769 | GError ** err UNUSED ) |
---|
770 | { |
---|
771 | add_filename( core, filename, |
---|
772 | pref_flag_get( PREF_KEY_START ), |
---|
773 | pref_flag_get( PREF_KEY_OPTIONS_PROMPT ) ); |
---|
774 | *success = TRUE; |
---|
775 | return TRUE; |
---|
776 | } |
---|
777 | |
---|
778 | void |
---|
779 | tr_core_add_list( TrCore * core, |
---|
780 | GSList * torrentFiles, |
---|
781 | pref_flag_t start, |
---|
782 | pref_flag_t prompt ) |
---|
783 | { |
---|
784 | const gboolean doStart = pref_flag_eval( start, PREF_KEY_START ); |
---|
785 | const gboolean doPrompt = pref_flag_eval( prompt,PREF_KEY_OPTIONS_PROMPT ); |
---|
786 | GSList * l; |
---|
787 | for( l=torrentFiles; l!=NULL; l=l->next ) |
---|
788 | add_filename( core, l->data, doStart, doPrompt ); |
---|
789 | freestrlist( torrentFiles ); |
---|
790 | } |
---|
791 | |
---|
792 | void |
---|
793 | tr_core_torrents_added( TrCore * self ) |
---|
794 | { |
---|
795 | tr_core_update( self ); |
---|
796 | tr_core_errsig( self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL ); |
---|
797 | } |
---|
798 | |
---|
799 | static gboolean |
---|
800 | findTorrentInModel( TrCore * core, const TrTorrent * gtor, GtkTreeIter * setme ) |
---|
801 | { |
---|
802 | int match = 0; |
---|
803 | GtkTreeIter iter; |
---|
804 | GtkTreeModel * model = tr_core_model( core ); |
---|
805 | |
---|
806 | if( gtk_tree_model_iter_children( model, &iter, NULL ) ) do |
---|
807 | { |
---|
808 | TrTorrent * tmp; |
---|
809 | gtk_tree_model_get( model, &iter, MC_TORRENT, &tmp, -1 ); |
---|
810 | match = tmp == gtor; |
---|
811 | g_object_unref( G_OBJECT( tmp ) ); |
---|
812 | } |
---|
813 | while( !match && gtk_tree_model_iter_next( model, &iter ) ); |
---|
814 | |
---|
815 | if( match ) |
---|
816 | *setme = iter; |
---|
817 | |
---|
818 | return match; |
---|
819 | } |
---|
820 | |
---|
821 | void |
---|
822 | tr_core_remove_torrent( TrCore * self, TrTorrent * gtor, int deleteFiles ) |
---|
823 | { |
---|
824 | GtkTreeIter iter; |
---|
825 | GtkTreeModel * model = tr_core_model( self ); |
---|
826 | |
---|
827 | /* remove from the gui */ |
---|
828 | if( findTorrentInModel( self, gtor, &iter ) ) |
---|
829 | gtk_list_store_remove( GTK_LIST_STORE( model ), &iter ); |
---|
830 | |
---|
831 | /* maybe delete the downloaded files */ |
---|
832 | if( deleteFiles ) |
---|
833 | tr_torrent_delete_files( gtor ); |
---|
834 | |
---|
835 | /* remove the torrent */ |
---|
836 | tr_torrent_set_remove_flag( gtor, TRUE ); |
---|
837 | g_object_unref( G_OBJECT( gtor ) ); |
---|
838 | } |
---|
839 | |
---|
840 | |
---|
841 | /*** |
---|
842 | **** |
---|
843 | ***/ |
---|
844 | |
---|
845 | static gboolean |
---|
846 | update_foreach( GtkTreeModel * model, |
---|
847 | GtkTreePath * path UNUSED, |
---|
848 | GtkTreeIter * iter, |
---|
849 | gpointer data UNUSED ) |
---|
850 | { |
---|
851 | int oldStatus; |
---|
852 | int newStatus; |
---|
853 | TrTorrent * gtor; |
---|
854 | |
---|
855 | /* maybe update the status column in the model */ |
---|
856 | gtk_tree_model_get( model, iter, |
---|
857 | MC_TORRENT, >or, |
---|
858 | MC_STATUS, &oldStatus, |
---|
859 | -1 ); |
---|
860 | newStatus = tr_torrentGetStatus( tr_torrent_handle( gtor ) ); |
---|
861 | if( newStatus != oldStatus ) |
---|
862 | gtk_list_store_set( GTK_LIST_STORE( model ), iter, |
---|
863 | MC_STATUS, newStatus, |
---|
864 | -1 ); |
---|
865 | |
---|
866 | /* cleanup */ |
---|
867 | g_object_unref( gtor ); |
---|
868 | return FALSE; |
---|
869 | } |
---|
870 | |
---|
871 | void |
---|
872 | tr_core_update( TrCore * self ) |
---|
873 | { |
---|
874 | int column; |
---|
875 | GtkSortType order; |
---|
876 | GtkTreeSortable * sortable; |
---|
877 | GtkTreeModel * model = tr_core_model( self ); |
---|
878 | |
---|
879 | /* pause sorting */ |
---|
880 | sortable = GTK_TREE_SORTABLE( model ); |
---|
881 | gtk_tree_sortable_get_sort_column_id( sortable, &column, &order ); |
---|
882 | gtk_tree_sortable_set_sort_column_id( sortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, order ); |
---|
883 | |
---|
884 | /* refresh the model */ |
---|
885 | gtk_tree_model_foreach( model, update_foreach, NULL ); |
---|
886 | |
---|
887 | /* resume sorting */ |
---|
888 | gtk_tree_sortable_set_sort_column_id( sortable, column, order ); |
---|
889 | } |
---|
890 | |
---|
891 | void |
---|
892 | tr_core_quit( TrCore * core ) |
---|
893 | { |
---|
894 | g_signal_emit( core, TR_CORE_GET_CLASS(core)->quitsig, 0 ); |
---|
895 | } |
---|
896 | |
---|
897 | /** |
---|
898 | *** Hibernate |
---|
899 | **/ |
---|
900 | |
---|
901 | #ifdef HAVE_DBUS_GLIB |
---|
902 | |
---|
903 | static DBusGProxy* |
---|
904 | get_hibernation_inhibit_proxy( void ) |
---|
905 | { |
---|
906 | GError * error = NULL; |
---|
907 | DBusGConnection * conn; |
---|
908 | |
---|
909 | conn = dbus_g_bus_get( DBUS_BUS_SESSION, &error ); |
---|
910 | if( error ) |
---|
911 | { |
---|
912 | g_warning ("DBUS cannot connect : %s", error->message); |
---|
913 | g_error_free (error); |
---|
914 | return NULL; |
---|
915 | } |
---|
916 | |
---|
917 | return dbus_g_proxy_new_for_name (conn, |
---|
918 | "org.freedesktop.PowerManagement", |
---|
919 | "/org/freedesktop/PowerManagement/Inhibit", |
---|
920 | "org.freedesktop.PowerManagement.Inhibit" ); |
---|
921 | } |
---|
922 | |
---|
923 | static gboolean |
---|
924 | gtr_inhibit_hibernation( guint * cookie ) |
---|
925 | { |
---|
926 | gboolean success = FALSE; |
---|
927 | DBusGProxy * proxy = get_hibernation_inhibit_proxy( ); |
---|
928 | if( proxy ) |
---|
929 | { |
---|
930 | GError * error = NULL; |
---|
931 | const char * application = _( "Transmission Bittorrent Client" ); |
---|
932 | const char * reason = _( "BitTorrent Activity" ); |
---|
933 | success = dbus_g_proxy_call( proxy, "Inhibit", &error, |
---|
934 | G_TYPE_STRING, application, |
---|
935 | G_TYPE_STRING, reason, |
---|
936 | G_TYPE_INVALID, |
---|
937 | G_TYPE_UINT, cookie, |
---|
938 | G_TYPE_INVALID ); |
---|
939 | if( success ) |
---|
940 | tr_inf( _( "Disallowing desktop hibernation" ) ); |
---|
941 | else { |
---|
942 | tr_err( _( "Couldn't disable desktop hibernation: %s" ), error->message ); |
---|
943 | g_error_free( error ); |
---|
944 | } |
---|
945 | |
---|
946 | g_object_unref( G_OBJECT( proxy ) ); |
---|
947 | } |
---|
948 | |
---|
949 | return success != 0; |
---|
950 | } |
---|
951 | |
---|
952 | static void |
---|
953 | gtr_uninhibit_hibernation( guint inhibit_cookie ) |
---|
954 | { |
---|
955 | DBusGProxy * proxy = get_hibernation_inhibit_proxy( ); |
---|
956 | if( proxy ) |
---|
957 | { |
---|
958 | GError * error = NULL; |
---|
959 | gboolean success = dbus_g_proxy_call( proxy, "UnInhibit", &error, |
---|
960 | G_TYPE_UINT, inhibit_cookie, |
---|
961 | G_TYPE_INVALID, |
---|
962 | G_TYPE_INVALID ); |
---|
963 | if( success ) |
---|
964 | tr_inf( _( "Allowing desktop hibernation" ) ); |
---|
965 | else { |
---|
966 | g_warning( "Couldn't uninhibit the system from suspending: %s.", error->message ); |
---|
967 | g_error_free( error ); |
---|
968 | } |
---|
969 | |
---|
970 | g_object_unref( G_OBJECT( proxy ) ); |
---|
971 | } |
---|
972 | } |
---|
973 | |
---|
974 | #endif |
---|
975 | |
---|
976 | |
---|
977 | void |
---|
978 | tr_core_set_hibernation_allowed( TrCore * core, gboolean allowed ) |
---|
979 | { |
---|
980 | #ifdef HAVE_DBUS_GLIB |
---|
981 | g_return_if_fail( core ); |
---|
982 | g_return_if_fail( core->priv ); |
---|
983 | |
---|
984 | core->priv->inhibit_allowed = allowed != 0; |
---|
985 | |
---|
986 | if( allowed && core->priv->have_inhibit_cookie ) |
---|
987 | { |
---|
988 | gtr_uninhibit_hibernation( core->priv->inhibit_cookie ); |
---|
989 | core->priv->have_inhibit_cookie = FALSE; |
---|
990 | } |
---|
991 | |
---|
992 | if( !allowed && !core->priv->have_inhibit_cookie ) |
---|
993 | { |
---|
994 | core->priv->have_inhibit_cookie = gtr_inhibit_hibernation( &core->priv->inhibit_cookie ); |
---|
995 | core->priv->have_inhibit_cookie = TRUE; |
---|
996 | } |
---|
997 | #endif |
---|
998 | } |
---|
999 | |
---|
1000 | /** |
---|
1001 | *** Prefs |
---|
1002 | **/ |
---|
1003 | |
---|
1004 | static void |
---|
1005 | commitPrefsChange( TrCore * core, const char * key ) |
---|
1006 | { |
---|
1007 | pref_save( NULL ); |
---|
1008 | g_signal_emit( core, TR_CORE_GET_CLASS(core)->prefsig, 0, key ); |
---|
1009 | } |
---|
1010 | |
---|
1011 | void |
---|
1012 | tr_core_set_pref( TrCore * self, const char * key, const char * newval ) |
---|
1013 | { |
---|
1014 | char * oldval = pref_string_get( key ); |
---|
1015 | if( tr_strcmp( oldval, newval ) ) |
---|
1016 | { |
---|
1017 | pref_string_set( key, newval ); |
---|
1018 | commitPrefsChange( self, key ); |
---|
1019 | } |
---|
1020 | g_free( oldval ); |
---|
1021 | } |
---|
1022 | |
---|
1023 | void |
---|
1024 | tr_core_set_pref_bool( TrCore * self, const char * key, gboolean newval ) |
---|
1025 | { |
---|
1026 | const gboolean oldval = pref_flag_get( key ); |
---|
1027 | if( oldval != newval ) |
---|
1028 | { |
---|
1029 | pref_flag_set( key, newval ); |
---|
1030 | commitPrefsChange( self, key ); |
---|
1031 | } |
---|
1032 | } |
---|
1033 | |
---|
1034 | void |
---|
1035 | tr_core_set_pref_int( TrCore * self, const char * key, int newval ) |
---|
1036 | { |
---|
1037 | const int oldval = pref_int_get( key ); |
---|
1038 | if( oldval != newval ) |
---|
1039 | { |
---|
1040 | pref_int_set( key, newval ); |
---|
1041 | commitPrefsChange( self, key ); |
---|
1042 | } |
---|
1043 | } |
---|