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