1 | /* |
---|
2 | * This file Copyright (C) 2008-2010 Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: util.c 10101 2010-02-05 13:59:44Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <ctype.h> /* isxdigit() */ |
---|
14 | #include <errno.h> |
---|
15 | #include <stdlib.h> /* free() */ |
---|
16 | #include <string.h> /* strcmp() */ |
---|
17 | |
---|
18 | #include <sys/types.h> /* for gtr_lockfile()'s open() */ |
---|
19 | #include <sys/stat.h> /* for gtr_lockfile()'s open() */ |
---|
20 | #include <fcntl.h> /* for gtr_lockfile()'s open() */ |
---|
21 | |
---|
22 | #include <gtk/gtk.h> |
---|
23 | #include <glib/gi18n.h> |
---|
24 | #include <glib/gstdio.h> /* g_unlink() */ |
---|
25 | #ifdef HAVE_GIO |
---|
26 | #include <gio/gio.h> /* g_file_trash() */ |
---|
27 | #endif |
---|
28 | #ifdef HAVE_DBUS_GLIB |
---|
29 | #include <dbus/dbus-glib.h> |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <evhttp.h> |
---|
33 | |
---|
34 | #include <libtransmission/transmission.h> /* TR_RATIO_NA, TR_RATIO_INF */ |
---|
35 | #include <libtransmission/utils.h> /* tr_inf */ |
---|
36 | #include <libtransmission/version.h> /* tr_inf */ |
---|
37 | |
---|
38 | #include "conf.h" |
---|
39 | #include "hig.h" |
---|
40 | #include "tr-prefs.h" |
---|
41 | #include "util.h" |
---|
42 | |
---|
43 | gtr_lockfile_state_t |
---|
44 | gtr_lockfile( const char * filename ) |
---|
45 | { |
---|
46 | gtr_lockfile_state_t ret; |
---|
47 | |
---|
48 | #ifdef WIN32 |
---|
49 | |
---|
50 | HANDLE file = CreateFile( filename, |
---|
51 | GENERIC_READ | GENERIC_WRITE, |
---|
52 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
---|
53 | NULL, |
---|
54 | OPEN_ALWAYS, |
---|
55 | FILE_ATTRIBUTE_NORMAL, |
---|
56 | NULL ); |
---|
57 | if( file == INVALID_HANDLE_VALUE ) |
---|
58 | ret = GTR_LOCKFILE_EOPEN; |
---|
59 | else if( !LockFile( file, 0, 0, 1, 1 ) ) |
---|
60 | ret = GTR_LOCKFILE_ELOCK; |
---|
61 | else |
---|
62 | ret = GTR_LOCKFILE_SUCCESS; |
---|
63 | |
---|
64 | #else |
---|
65 | |
---|
66 | int fd = open( filename, O_RDWR | O_CREAT, 0666 ); |
---|
67 | if( fd < 0 ) |
---|
68 | ret = GTR_LOCKFILE_EOPEN; |
---|
69 | else { |
---|
70 | struct flock lk; |
---|
71 | memset( &lk, 0, sizeof( lk ) ); |
---|
72 | lk.l_start = 0; |
---|
73 | lk.l_len = 0; |
---|
74 | lk.l_type = F_WRLCK; |
---|
75 | lk.l_whence = SEEK_SET; |
---|
76 | if( -1 == fcntl( fd, F_SETLK, &lk ) ) |
---|
77 | ret = GTR_LOCKFILE_ELOCK; |
---|
78 | else |
---|
79 | ret = GTR_LOCKFILE_SUCCESS; |
---|
80 | } |
---|
81 | |
---|
82 | #endif |
---|
83 | |
---|
84 | return ret; |
---|
85 | } |
---|
86 | |
---|
87 | /*** |
---|
88 | **** |
---|
89 | ***/ |
---|
90 | |
---|
91 | |
---|
92 | char* |
---|
93 | tr_strlratio( char * buf, double ratio, size_t buflen ) |
---|
94 | { |
---|
95 | return tr_strratio( buf, buflen, ratio, "\xE2\x88\x9E" ); |
---|
96 | } |
---|
97 | |
---|
98 | #define KILOBYTE_FACTOR 1024.0 |
---|
99 | #define MEGABYTE_FACTOR ( 1024.0 * 1024.0 ) |
---|
100 | #define GIGABYTE_FACTOR ( 1024.0 * 1024.0 * 1024.0 ) |
---|
101 | |
---|
102 | char* |
---|
103 | tr_strlsize( char * buf, |
---|
104 | guint64 size, |
---|
105 | size_t buflen ) |
---|
106 | { |
---|
107 | if( !size ) |
---|
108 | g_strlcpy( buf, _( "None" ), buflen ); |
---|
109 | #if GLIB_CHECK_VERSION( 2, 16, 0 ) |
---|
110 | else |
---|
111 | { |
---|
112 | char * tmp = g_format_size_for_display( size ); |
---|
113 | g_strlcpy( buf, tmp, buflen ); |
---|
114 | g_free( tmp ); |
---|
115 | } |
---|
116 | #else |
---|
117 | else if( size < (guint64)KILOBYTE_FACTOR ) |
---|
118 | g_snprintf( buf, buflen, |
---|
119 | ngettext( "%'u byte", "%'u bytes", |
---|
120 | (guint)size ), (guint)size ); |
---|
121 | else |
---|
122 | { |
---|
123 | gdouble displayed_size; |
---|
124 | if( size < (guint64)MEGABYTE_FACTOR ) |
---|
125 | { |
---|
126 | displayed_size = (gdouble) size / KILOBYTE_FACTOR; |
---|
127 | g_snprintf( buf, buflen, _( "%'.1f KB" ), displayed_size ); |
---|
128 | } |
---|
129 | else if( size < (guint64)GIGABYTE_FACTOR ) |
---|
130 | { |
---|
131 | displayed_size = (gdouble) size / MEGABYTE_FACTOR; |
---|
132 | g_snprintf( buf, buflen, _( "%'.1f MB" ), displayed_size ); |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | displayed_size = (gdouble) size / GIGABYTE_FACTOR; |
---|
137 | g_snprintf( buf, buflen, _( "%'.1f GB" ), displayed_size ); |
---|
138 | } |
---|
139 | } |
---|
140 | #endif |
---|
141 | return buf; |
---|
142 | } |
---|
143 | |
---|
144 | char* |
---|
145 | tr_strlspeed( char * buf, |
---|
146 | double kb_sec, |
---|
147 | size_t buflen ) |
---|
148 | { |
---|
149 | const double speed = kb_sec; |
---|
150 | |
---|
151 | if( speed < 1000.0 ) /* 0.0 KB to 999.9 KB */ |
---|
152 | g_snprintf( buf, buflen, _( "%'.1f KB/s" ), speed ); |
---|
153 | else if( speed < 102400.0 ) /* 0.98 MB to 99.99 MB */ |
---|
154 | g_snprintf( buf, buflen, _( "%'.2f MB/s" ), ( speed / KILOBYTE_FACTOR ) ); |
---|
155 | else if( speed < 1024000.0 ) /* 100.0 MB to 999.9 MB */ |
---|
156 | g_snprintf( buf, buflen, _( "%'.1f MB/s" ), ( speed / MEGABYTE_FACTOR ) ); |
---|
157 | else /* insane speeds */ |
---|
158 | g_snprintf( buf, buflen, _( "%'.2f GB/s" ), ( speed / GIGABYTE_FACTOR ) ); |
---|
159 | |
---|
160 | return buf; |
---|
161 | } |
---|
162 | |
---|
163 | char* |
---|
164 | tr_strltime( char * buf, |
---|
165 | int seconds, |
---|
166 | size_t buflen ) |
---|
167 | { |
---|
168 | int days, hours, minutes; |
---|
169 | char d[128], h[128], m[128], s[128]; |
---|
170 | |
---|
171 | if( seconds < 0 ) |
---|
172 | seconds = 0; |
---|
173 | |
---|
174 | days = seconds / 86400; |
---|
175 | hours = ( seconds % 86400 ) / 3600; |
---|
176 | minutes = ( seconds % 3600 ) / 60; |
---|
177 | seconds = ( seconds % 3600 ) % 60; |
---|
178 | |
---|
179 | g_snprintf( d, sizeof( d ), ngettext( "%'d day", "%'d days", |
---|
180 | days ), days ); |
---|
181 | g_snprintf( h, sizeof( h ), ngettext( "%'d hour", "%'d hours", |
---|
182 | hours ), hours ); |
---|
183 | g_snprintf( m, sizeof( m ), |
---|
184 | ngettext( "%'d minute", "%'d minutes", minutes ), minutes ); |
---|
185 | g_snprintf( s, sizeof( s ), |
---|
186 | ngettext( "%'d second", "%'d seconds", seconds ), seconds ); |
---|
187 | |
---|
188 | if( days ) |
---|
189 | { |
---|
190 | if( days >= 4 || !hours ) |
---|
191 | { |
---|
192 | g_strlcpy( buf, d, buflen ); |
---|
193 | } |
---|
194 | else |
---|
195 | { |
---|
196 | g_snprintf( buf, buflen, "%s, %s", d, h ); |
---|
197 | } |
---|
198 | } |
---|
199 | else if( hours ) |
---|
200 | { |
---|
201 | if( hours >= 4 || !minutes ) |
---|
202 | { |
---|
203 | g_strlcpy( buf, h, buflen ); |
---|
204 | } |
---|
205 | else |
---|
206 | { |
---|
207 | g_snprintf( buf, buflen, "%s, %s", h, m ); |
---|
208 | } |
---|
209 | } |
---|
210 | else if( minutes ) |
---|
211 | { |
---|
212 | if( minutes >= 4 || !seconds ) |
---|
213 | { |
---|
214 | g_strlcpy( buf, m, buflen ); |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | g_snprintf( buf, buflen, "%s, %s", m, s ); |
---|
219 | } |
---|
220 | } |
---|
221 | else |
---|
222 | { |
---|
223 | g_strlcpy( buf, s, buflen ); |
---|
224 | } |
---|
225 | |
---|
226 | return buf; |
---|
227 | } |
---|
228 | |
---|
229 | char * |
---|
230 | gtr_localtime( time_t time ) |
---|
231 | { |
---|
232 | const struct tm tm = *localtime( &time ); |
---|
233 | char buf[256], *eoln; |
---|
234 | |
---|
235 | g_strlcpy( buf, asctime( &tm ), sizeof( buf ) ); |
---|
236 | if( ( eoln = strchr( buf, '\n' ) ) ) |
---|
237 | *eoln = '\0'; |
---|
238 | |
---|
239 | return g_locale_to_utf8( buf, -1, NULL, NULL, NULL ); |
---|
240 | } |
---|
241 | |
---|
242 | char * |
---|
243 | gtr_localtime2( char * buf, time_t time, size_t buflen ) |
---|
244 | { |
---|
245 | char * tmp = gtr_localtime( time ); |
---|
246 | g_strlcpy( buf, tmp, buflen ); |
---|
247 | g_free( tmp ); |
---|
248 | return buf; |
---|
249 | } |
---|
250 | |
---|
251 | int |
---|
252 | gtr_mkdir_with_parents( const char * path, int mode ) |
---|
253 | { |
---|
254 | #if GLIB_CHECK_VERSION( 2, 8, 0 ) |
---|
255 | return !g_mkdir_with_parents( path, mode ); |
---|
256 | #else |
---|
257 | return !tr_mkdirp( path, mode ); |
---|
258 | #endif |
---|
259 | } |
---|
260 | |
---|
261 | GSList * |
---|
262 | dupstrlist( GSList * l ) |
---|
263 | { |
---|
264 | GSList * ret = NULL; |
---|
265 | |
---|
266 | for( ; l != NULL; l = l->next ) |
---|
267 | ret = g_slist_prepend( ret, g_strdup( l->data ) ); |
---|
268 | return g_slist_reverse( ret ); |
---|
269 | } |
---|
270 | |
---|
271 | char * |
---|
272 | joinstrlist( GSList *list, |
---|
273 | char * sep ) |
---|
274 | { |
---|
275 | GSList * l; |
---|
276 | GString *gstr = g_string_new ( NULL ); |
---|
277 | |
---|
278 | for( l = list; l != NULL; l = l->next ) |
---|
279 | { |
---|
280 | g_string_append ( gstr, (char*)l->data ); |
---|
281 | if( l->next != NULL ) |
---|
282 | g_string_append ( gstr, ( sep ) ); |
---|
283 | } |
---|
284 | return g_string_free ( gstr, FALSE ); |
---|
285 | } |
---|
286 | |
---|
287 | void |
---|
288 | freestrlist( GSList *list ) |
---|
289 | { |
---|
290 | g_slist_foreach ( list, (GFunc)g_free, NULL ); |
---|
291 | g_slist_free ( list ); |
---|
292 | } |
---|
293 | |
---|
294 | char * |
---|
295 | decode_uri( const char * uri ) |
---|
296 | { |
---|
297 | gboolean in_query = FALSE; |
---|
298 | char * ret = g_new( char, strlen( uri ) + 1 ); |
---|
299 | char * out = ret; |
---|
300 | |
---|
301 | for( ; uri && *uri; ) |
---|
302 | { |
---|
303 | char ch = *uri; |
---|
304 | if( ch == '?' ) |
---|
305 | in_query = TRUE; |
---|
306 | else if( ch == '+' && in_query ) |
---|
307 | ch = ' '; |
---|
308 | else if( ch == '%' && isxdigit( (unsigned char)uri[1] ) |
---|
309 | && isxdigit( (unsigned char)uri[2] ) ) |
---|
310 | { |
---|
311 | char buf[3] = { uri[1], uri[2], '\0' }; |
---|
312 | ch = (char) g_ascii_strtoull( buf, NULL, 16 ); |
---|
313 | uri += 2; |
---|
314 | } |
---|
315 | |
---|
316 | ++uri; |
---|
317 | *out++ = ch; |
---|
318 | } |
---|
319 | |
---|
320 | *out = '\0'; |
---|
321 | return ret; |
---|
322 | } |
---|
323 | |
---|
324 | |
---|
325 | gboolean |
---|
326 | gtr_is_supported_url( const char * str ) |
---|
327 | { |
---|
328 | return !strncmp( str, "ftp://", 6 ) |
---|
329 | || !strncmp( str, "http://", 7 ) |
---|
330 | || !strncmp( str, "https://", 8 ); |
---|
331 | } |
---|
332 | |
---|
333 | gboolean |
---|
334 | gtr_is_magnet_link( const char * str ) |
---|
335 | { |
---|
336 | return !strncmp( str, "magnet:?", 8 ); |
---|
337 | } |
---|
338 | |
---|
339 | gboolean |
---|
340 | gtr_is_hex_hashcode( const char * str ) |
---|
341 | { |
---|
342 | int i; |
---|
343 | |
---|
344 | if( !str || ( strlen( str ) != 40 ) ) |
---|
345 | return FALSE; |
---|
346 | |
---|
347 | for( i=0; i<40; ++i ) |
---|
348 | if( !isxdigit( str[i] ) ) |
---|
349 | return FALSE; |
---|
350 | |
---|
351 | return TRUE; |
---|
352 | } |
---|
353 | |
---|
354 | static GtkWindow * |
---|
355 | getWindow( GtkWidget * w ) |
---|
356 | { |
---|
357 | if( w == NULL ) |
---|
358 | return NULL; |
---|
359 | |
---|
360 | if( GTK_IS_WINDOW( w ) ) |
---|
361 | return GTK_WINDOW( w ); |
---|
362 | |
---|
363 | return GTK_WINDOW( gtk_widget_get_ancestor( w, GTK_TYPE_WINDOW ) ); |
---|
364 | } |
---|
365 | |
---|
366 | void |
---|
367 | addTorrentErrorDialog( GtkWidget * child, |
---|
368 | int err, |
---|
369 | const char * filename ) |
---|
370 | { |
---|
371 | char * secondary; |
---|
372 | const char * fmt; |
---|
373 | GtkWidget * w; |
---|
374 | GtkWindow * win = getWindow( child ); |
---|
375 | |
---|
376 | switch( err ) |
---|
377 | { |
---|
378 | case TR_PARSE_ERR: fmt = _( "The torrent file \"%s\" contains invalid data." ); break; |
---|
379 | case TR_PARSE_DUPLICATE: fmt = _( "The torrent file \"%s\" is already in use." ); break; |
---|
380 | default: fmt = _( "The torrent file \"%s\" encountered an unknown error." ); break; |
---|
381 | } |
---|
382 | secondary = g_strdup_printf( fmt, filename ); |
---|
383 | |
---|
384 | w = gtk_message_dialog_new( win, |
---|
385 | GTK_DIALOG_DESTROY_WITH_PARENT, |
---|
386 | GTK_MESSAGE_ERROR, |
---|
387 | GTK_BUTTONS_CLOSE, |
---|
388 | "%s", _( "Error opening torrent" ) ); |
---|
389 | gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( w ), |
---|
390 | "%s", secondary ); |
---|
391 | g_signal_connect_swapped( w, "response", |
---|
392 | G_CALLBACK( gtk_widget_destroy ), w ); |
---|
393 | gtk_widget_show_all( w ); |
---|
394 | g_free( secondary ); |
---|
395 | } |
---|
396 | |
---|
397 | typedef void ( PopupFunc )( GtkWidget*, GdkEventButton* ); |
---|
398 | |
---|
399 | /* pop up the context menu if a user right-clicks. |
---|
400 | if the row they right-click on isn't selected, select it. */ |
---|
401 | |
---|
402 | gboolean |
---|
403 | on_tree_view_button_pressed( GtkWidget * view, |
---|
404 | GdkEventButton * event, |
---|
405 | gpointer func ) |
---|
406 | { |
---|
407 | GtkTreeView * tv = GTK_TREE_VIEW( view ); |
---|
408 | |
---|
409 | if( event->type == GDK_BUTTON_PRESS && event->button == 3 ) |
---|
410 | { |
---|
411 | GtkTreeSelection * selection = gtk_tree_view_get_selection( tv ); |
---|
412 | GtkTreePath * path; |
---|
413 | if( gtk_tree_view_get_path_at_pos ( tv, |
---|
414 | (gint) event->x, |
---|
415 | (gint) event->y, |
---|
416 | &path, NULL, NULL, NULL ) ) |
---|
417 | { |
---|
418 | if( !gtk_tree_selection_path_is_selected ( selection, path ) ) |
---|
419 | { |
---|
420 | gtk_tree_selection_unselect_all ( selection ); |
---|
421 | gtk_tree_selection_select_path ( selection, path ); |
---|
422 | } |
---|
423 | gtk_tree_path_free( path ); |
---|
424 | } |
---|
425 | |
---|
426 | if( func != NULL ) |
---|
427 | ( (PopupFunc*)func )( view, event ); |
---|
428 | |
---|
429 | return TRUE; |
---|
430 | } |
---|
431 | |
---|
432 | return FALSE; |
---|
433 | } |
---|
434 | |
---|
435 | /* if the user clicked in an empty area of the list, |
---|
436 | * clear all the selections. */ |
---|
437 | gboolean |
---|
438 | on_tree_view_button_released( GtkWidget * view, |
---|
439 | GdkEventButton * event, |
---|
440 | gpointer unused UNUSED ) |
---|
441 | { |
---|
442 | GtkTreeView * tv = GTK_TREE_VIEW( view ); |
---|
443 | |
---|
444 | if( !gtk_tree_view_get_path_at_pos ( tv, |
---|
445 | (gint) event->x, |
---|
446 | (gint) event->y, |
---|
447 | NULL, NULL, NULL, NULL ) ) |
---|
448 | { |
---|
449 | GtkTreeSelection * selection = gtk_tree_view_get_selection( tv ); |
---|
450 | gtk_tree_selection_unselect_all ( selection ); |
---|
451 | } |
---|
452 | |
---|
453 | return FALSE; |
---|
454 | } |
---|
455 | |
---|
456 | gpointer |
---|
457 | tr_object_ref_sink( gpointer object ) |
---|
458 | { |
---|
459 | #if GLIB_CHECK_VERSION( 2, 10, 0 ) |
---|
460 | g_object_ref_sink( object ); |
---|
461 | #else |
---|
462 | g_object_ref( object ); |
---|
463 | gtk_object_sink( GTK_OBJECT( object ) ); |
---|
464 | #endif |
---|
465 | return object; |
---|
466 | } |
---|
467 | |
---|
468 | int |
---|
469 | tr_file_trash_or_remove( const char * filename ) |
---|
470 | { |
---|
471 | if( filename && *filename ) |
---|
472 | { |
---|
473 | gboolean trashed = FALSE; |
---|
474 | #ifdef HAVE_GIO |
---|
475 | GError * err = NULL; |
---|
476 | GFile * file = g_file_new_for_path( filename ); |
---|
477 | trashed = g_file_trash( file, NULL, &err ); |
---|
478 | if( err ) |
---|
479 | g_message( "Unable to trash file \"%s\": %s", filename, err->message ); |
---|
480 | g_clear_error( &err ); |
---|
481 | g_object_unref( G_OBJECT( file ) ); |
---|
482 | #endif |
---|
483 | |
---|
484 | if( !trashed && g_remove( filename ) ) |
---|
485 | { |
---|
486 | const int err = errno; |
---|
487 | g_message( "Unable to remove file \"%s\": %s", filename, g_strerror( err ) ); |
---|
488 | } |
---|
489 | } |
---|
490 | |
---|
491 | return 0; |
---|
492 | } |
---|
493 | |
---|
494 | char* |
---|
495 | gtr_get_help_url( void ) |
---|
496 | { |
---|
497 | const char * fmt = "http://www.transmissionbt.com/help/gtk/%d.%dx"; |
---|
498 | int major, minor; |
---|
499 | |
---|
500 | sscanf( SHORT_VERSION_STRING, "%d.%d", &major, &minor ); |
---|
501 | return g_strdup_printf( fmt, major, minor / 10 ); |
---|
502 | } |
---|
503 | |
---|
504 | void |
---|
505 | gtr_open_file( const char * path ) |
---|
506 | { |
---|
507 | if( path ) |
---|
508 | { |
---|
509 | gboolean opened = FALSE; |
---|
510 | #ifdef HAVE_GIO |
---|
511 | if( !opened ) |
---|
512 | { |
---|
513 | GFile * file = g_file_new_for_path( path ); |
---|
514 | char * uri = g_file_get_uri( file ); |
---|
515 | opened = g_app_info_launch_default_for_uri( uri, NULL, NULL ); |
---|
516 | g_free( uri ); |
---|
517 | g_object_unref( G_OBJECT( file ) ); |
---|
518 | } |
---|
519 | #endif |
---|
520 | if( !opened ) |
---|
521 | { |
---|
522 | char * argv[] = { (char*)"xdg-open", (char*)path, NULL }; |
---|
523 | opened = g_spawn_async( NULL, argv, NULL, G_SPAWN_SEARCH_PATH, |
---|
524 | NULL, NULL, NULL, NULL ); |
---|
525 | } |
---|
526 | |
---|
527 | if( !opened ) |
---|
528 | { |
---|
529 | g_message( "Unable to open \"%s\"", path ); |
---|
530 | } |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | #define VALUE_SERVICE_NAME "com.transmissionbt.Transmission" |
---|
535 | #define VALUE_SERVICE_OBJECT_PATH "/com/transmissionbt/Transmission" |
---|
536 | #define VALUE_SERVICE_INTERFACE "com.transmissionbt.Transmission" |
---|
537 | |
---|
538 | gboolean |
---|
539 | gtr_dbus_add_torrent( const char * filename ) |
---|
540 | { |
---|
541 | /* FIXME: why is this static? */ |
---|
542 | static gboolean handled = FALSE; |
---|
543 | |
---|
544 | #ifdef HAVE_DBUS_GLIB |
---|
545 | char * payload; |
---|
546 | gsize file_length; |
---|
547 | char * file_contents = NULL; |
---|
548 | |
---|
549 | /* If it's a file, load its contents and send them over the wire... |
---|
550 | * it might be a temporary file that's going to disappear. */ |
---|
551 | if( g_file_get_contents( filename, &file_contents, &file_length, NULL ) ) |
---|
552 | payload = tr_base64_encode( file_contents, file_length, NULL ); |
---|
553 | else if( gtr_is_supported_url( filename ) || gtr_is_magnet_link( filename ) ) |
---|
554 | payload = tr_strdup( filename ); |
---|
555 | else |
---|
556 | payload = NULL; |
---|
557 | |
---|
558 | if( payload != NULL ) |
---|
559 | { |
---|
560 | GError * err = NULL; |
---|
561 | DBusGConnection * conn; |
---|
562 | DBusGProxy * proxy = NULL; |
---|
563 | |
---|
564 | if(( conn = dbus_g_bus_get( DBUS_BUS_SESSION, &err ))) |
---|
565 | proxy = dbus_g_proxy_new_for_name (conn, VALUE_SERVICE_NAME, |
---|
566 | VALUE_SERVICE_OBJECT_PATH, |
---|
567 | VALUE_SERVICE_INTERFACE ); |
---|
568 | else if( err ) |
---|
569 | g_message( "err: %s", err->message ); |
---|
570 | |
---|
571 | if( proxy ) |
---|
572 | dbus_g_proxy_call( proxy, "AddMetainfo", &err, |
---|
573 | G_TYPE_STRING, payload, |
---|
574 | G_TYPE_INVALID, |
---|
575 | G_TYPE_BOOLEAN, &handled, |
---|
576 | G_TYPE_INVALID ); |
---|
577 | if( err ) |
---|
578 | g_message( "err: %s", err->message ); |
---|
579 | |
---|
580 | if( proxy ) |
---|
581 | g_object_unref( proxy ); |
---|
582 | if( conn ) |
---|
583 | dbus_g_connection_unref( conn ); |
---|
584 | |
---|
585 | tr_free( payload ); |
---|
586 | } |
---|
587 | |
---|
588 | g_free( file_contents ); |
---|
589 | |
---|
590 | #endif |
---|
591 | return handled; |
---|
592 | } |
---|
593 | |
---|
594 | gboolean |
---|
595 | gtr_dbus_present_window( void ) |
---|
596 | { |
---|
597 | static gboolean success = FALSE; |
---|
598 | |
---|
599 | #ifdef HAVE_DBUS_GLIB |
---|
600 | DBusGProxy * proxy = NULL; |
---|
601 | GError * err = NULL; |
---|
602 | DBusGConnection * conn; |
---|
603 | if( ( conn = dbus_g_bus_get( DBUS_BUS_SESSION, &err ) ) ) |
---|
604 | proxy = dbus_g_proxy_new_for_name ( conn, VALUE_SERVICE_NAME, |
---|
605 | VALUE_SERVICE_OBJECT_PATH, |
---|
606 | VALUE_SERVICE_INTERFACE ); |
---|
607 | else if( err ) |
---|
608 | g_message( "err: %s", err->message ); |
---|
609 | if( proxy ) |
---|
610 | dbus_g_proxy_call( proxy, "PresentWindow", &err, |
---|
611 | G_TYPE_INVALID, |
---|
612 | G_TYPE_BOOLEAN, &success, |
---|
613 | G_TYPE_INVALID ); |
---|
614 | if( err ) |
---|
615 | g_message( "err: %s", err->message ); |
---|
616 | |
---|
617 | g_object_unref( proxy ); |
---|
618 | dbus_g_connection_unref( conn ); |
---|
619 | #endif |
---|
620 | return success; |
---|
621 | } |
---|
622 | |
---|
623 | GtkWidget * |
---|
624 | gtr_button_new_from_stock( const char * stock, |
---|
625 | const char * mnemonic ) |
---|
626 | { |
---|
627 | GtkWidget * image = gtk_image_new_from_stock( stock, |
---|
628 | GTK_ICON_SIZE_BUTTON ); |
---|
629 | GtkWidget * button = gtk_button_new_with_mnemonic( mnemonic ); |
---|
630 | |
---|
631 | gtk_button_set_image( GTK_BUTTON( button ), image ); |
---|
632 | return button; |
---|
633 | } |
---|
634 | |
---|
635 | /*** |
---|
636 | **** |
---|
637 | ***/ |
---|
638 | |
---|
639 | void |
---|
640 | gtr_priority_combo_set_value( GtkWidget * w, tr_priority_t value ) |
---|
641 | { |
---|
642 | int i; |
---|
643 | int currentValue; |
---|
644 | const int column = 0; |
---|
645 | GtkTreeIter iter; |
---|
646 | GtkComboBox * combobox = GTK_COMBO_BOX( w ); |
---|
647 | GtkTreeModel * model = gtk_combo_box_get_model( combobox ); |
---|
648 | |
---|
649 | /* do the value and current value match? */ |
---|
650 | if( gtk_combo_box_get_active_iter( combobox, &iter ) ) { |
---|
651 | gtk_tree_model_get( model, &iter, column, ¤tValue, -1 ); |
---|
652 | if( currentValue == value ) |
---|
653 | return; |
---|
654 | } |
---|
655 | |
---|
656 | /* find the one to select */ |
---|
657 | i = 0; |
---|
658 | while(( gtk_tree_model_iter_nth_child( model, &iter, NULL, i++ ))) { |
---|
659 | gtk_tree_model_get( model, &iter, column, ¤tValue, -1 ); |
---|
660 | if( currentValue == value ) { |
---|
661 | gtk_combo_box_set_active_iter( combobox, &iter ); |
---|
662 | return; |
---|
663 | } |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | tr_priority_t |
---|
668 | gtr_priority_combo_get_value( GtkWidget * w ) |
---|
669 | { |
---|
670 | int value = 0; |
---|
671 | GtkTreeIter iter; |
---|
672 | GtkComboBox * combo_box = GTK_COMBO_BOX( w ); |
---|
673 | |
---|
674 | if( gtk_combo_box_get_active_iter( combo_box, &iter ) ) |
---|
675 | gtk_tree_model_get( gtk_combo_box_get_model( combo_box ), &iter, 0, &value, -1 ); |
---|
676 | |
---|
677 | return value; |
---|
678 | } |
---|
679 | |
---|
680 | GtkWidget * |
---|
681 | gtr_priority_combo_new( void ) |
---|
682 | { |
---|
683 | int i; |
---|
684 | GtkWidget * w; |
---|
685 | GtkCellRenderer * r; |
---|
686 | GtkListStore * store; |
---|
687 | const struct { |
---|
688 | int value; |
---|
689 | const char * text; |
---|
690 | } items[] = { |
---|
691 | { TR_PRI_HIGH, N_( "High" ) }, |
---|
692 | { TR_PRI_NORMAL, N_( "Normal" ) }, |
---|
693 | { TR_PRI_LOW, N_( "Low" ) } |
---|
694 | }; |
---|
695 | |
---|
696 | store = gtk_list_store_new( 2, G_TYPE_INT, G_TYPE_STRING ); |
---|
697 | for( i=0; i<(int)G_N_ELEMENTS(items); ++i ) { |
---|
698 | GtkTreeIter iter; |
---|
699 | gtk_list_store_append( store, &iter ); |
---|
700 | gtk_list_store_set( store, &iter, 0, items[i].value, |
---|
701 | 1, _( items[i].text ), |
---|
702 | -1 ); |
---|
703 | } |
---|
704 | |
---|
705 | w = gtk_combo_box_new_with_model( GTK_TREE_MODEL( store ) ); |
---|
706 | r = gtk_cell_renderer_text_new( ); |
---|
707 | gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( w ), r, TRUE ); |
---|
708 | gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( w ), r, "text", 1, NULL ); |
---|
709 | |
---|
710 | /* cleanup */ |
---|
711 | g_object_unref( store ); |
---|
712 | return w; |
---|
713 | } |
---|
714 | |
---|
715 | /*** |
---|
716 | **** |
---|
717 | ***/ |
---|
718 | |
---|
719 | void |
---|
720 | gtr_widget_set_tooltip_text( GtkWidget * w, const char * tip ) |
---|
721 | { |
---|
722 | #if GTK_CHECK_VERSION( 2,12,0 ) |
---|
723 | gtk_widget_set_tooltip_text( w, tip ); |
---|
724 | #else |
---|
725 | static GtkTooltips * tips = NULL; |
---|
726 | if( tips == NULL ) |
---|
727 | tips = gtk_tooltips_new( ); |
---|
728 | gtk_tooltips_set_tip( tips, w, tip, NULL ); |
---|
729 | #endif |
---|
730 | } |
---|
731 | |
---|
732 | void |
---|
733 | gtr_toolbar_set_orientation( GtkToolbar * toolbar, |
---|
734 | GtkOrientation orientation ) |
---|
735 | { |
---|
736 | #if GTK_CHECK_VERSION( 2,16,0 ) |
---|
737 | gtk_orientable_set_orientation( GTK_ORIENTABLE( toolbar ), orientation ); |
---|
738 | #else |
---|
739 | gtk_toolbar_set_orientation( toolbar, orientation ); |
---|
740 | #endif |
---|
741 | } |
---|
742 | |
---|
743 | /*** |
---|
744 | **** |
---|
745 | ***/ |
---|
746 | |
---|
747 | #if !GTK_CHECK_VERSION( 2,12,0 ) |
---|
748 | struct gtr_func_data |
---|
749 | { |
---|
750 | GSourceFunc function; |
---|
751 | gpointer data; |
---|
752 | }; |
---|
753 | |
---|
754 | static void |
---|
755 | gtr_func_data_free( gpointer data ) |
---|
756 | { |
---|
757 | #if GTK_CHECK_VERSION( 2,10,0 ) |
---|
758 | g_slice_free( struct gtr_func_data, data ); |
---|
759 | #else |
---|
760 | g_free( data ); |
---|
761 | #endif |
---|
762 | } |
---|
763 | |
---|
764 | static struct gtr_func_data * |
---|
765 | gtr_func_data_new( GSourceFunc function, gpointer data ) |
---|
766 | { |
---|
767 | #if GTK_CHECK_VERSION( 2,10,0 ) |
---|
768 | struct gtr_func_data * d = g_slice_new( struct gtr_func_data ); |
---|
769 | #else |
---|
770 | struct gtr_func_data * d = g_new( struct gtr_func_data, 1 ); |
---|
771 | #endif |
---|
772 | d->function = function; |
---|
773 | d->data = data; |
---|
774 | return d; |
---|
775 | } |
---|
776 | |
---|
777 | static gboolean |
---|
778 | gtr_thread_func( gpointer data ) |
---|
779 | { |
---|
780 | gboolean more; |
---|
781 | struct gtr_func_data * idle_data = data; |
---|
782 | |
---|
783 | gdk_threads_enter( ); |
---|
784 | more = idle_data->function( idle_data->data ); |
---|
785 | gdk_threads_leave( ); |
---|
786 | |
---|
787 | return more; |
---|
788 | } |
---|
789 | #endif |
---|
790 | |
---|
791 | void |
---|
792 | gtr_idle_add( GSourceFunc function, gpointer data ) |
---|
793 | { |
---|
794 | #if GTK_CHECK_VERSION( 2,12,0 ) |
---|
795 | gdk_threads_add_idle( function, data ); |
---|
796 | #else |
---|
797 | g_idle_add_full( G_PRIORITY_DEFAULT, |
---|
798 | gtr_thread_func, |
---|
799 | gtr_func_data_new( function, data ), |
---|
800 | gtr_func_data_free ); |
---|
801 | #endif |
---|
802 | } |
---|
803 | |
---|
804 | guint |
---|
805 | gtr_timeout_add_seconds( guint seconds, GSourceFunc function, gpointer data ) |
---|
806 | { |
---|
807 | #if GTK_CHECK_VERSION( 2,14,0 ) |
---|
808 | return gdk_threads_add_timeout_seconds( seconds, function, data ); |
---|
809 | #elif GTK_CHECK_VERSION( 2,12,0 ) |
---|
810 | return gdk_threads_add_timeout( seconds*1000, function, data ); |
---|
811 | #else |
---|
812 | return g_timeout_add_full( G_PRIORITY_DEFAULT, |
---|
813 | seconds * 1000, |
---|
814 | gtr_thread_func, |
---|
815 | gtr_func_data_new( function, data ), |
---|
816 | gtr_func_data_free ); |
---|
817 | #endif |
---|
818 | } |
---|
819 | |
---|
820 | void |
---|
821 | gtr_unrecognized_url_dialog( GtkWidget * parent, const char * url ) |
---|
822 | { |
---|
823 | const char * xt = "xt=urn:btih"; |
---|
824 | |
---|
825 | GtkWindow * window = getWindow( parent ); |
---|
826 | |
---|
827 | GString * gstr = g_string_new( NULL ); |
---|
828 | |
---|
829 | GtkWidget * w = gtk_message_dialog_new( window, 0, |
---|
830 | GTK_MESSAGE_ERROR, |
---|
831 | GTK_BUTTONS_CLOSE, |
---|
832 | "%s", _( "Unrecognized URL" ) ); |
---|
833 | |
---|
834 | g_string_append_printf( gstr, _( "Transmission doesn't know how to use \"%s\"" ), url ); |
---|
835 | |
---|
836 | if( gtr_is_magnet_link( url ) && ( strstr( url, xt ) == NULL ) ) |
---|
837 | { |
---|
838 | g_string_append_printf( gstr, "\n \n" ); |
---|
839 | g_string_append_printf( gstr, _( "This magnet link appears to be intended for something other than BitTorrent. BitTorrent magnet links have a section containing \"%s\"." ), xt ); |
---|
840 | } |
---|
841 | |
---|
842 | gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( w ), "%s", gstr->str ); |
---|
843 | g_signal_connect_swapped( w, "response", G_CALLBACK( gtk_widget_destroy ), w ); |
---|
844 | gtk_widget_show( w ); |
---|
845 | g_string_free( gstr, TRUE ); |
---|
846 | } |
---|