1 | /* |
---|
2 | * This file Copyright (C) 2008-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: util.c 14718 2016-03-13 22:11:01Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <ctype.h> /* isxdigit () */ |
---|
11 | #include <errno.h> |
---|
12 | #include <limits.h> /* INT_MAX */ |
---|
13 | #include <stdarg.h> |
---|
14 | #include <string.h> /* strchr (), strrchr (), strlen (), strstr () */ |
---|
15 | |
---|
16 | #include <gtk/gtk.h> |
---|
17 | #include <glib/gi18n.h> |
---|
18 | #include <gio/gio.h> /* g_file_trash () */ |
---|
19 | |
---|
20 | #include <libtransmission/transmission.h> /* TR_RATIO_NA, TR_RATIO_INF */ |
---|
21 | #include <libtransmission/error.h> |
---|
22 | #include <libtransmission/utils.h> /* tr_strratio () */ |
---|
23 | #include <libtransmission/web.h> /* tr_webResponseStr () */ |
---|
24 | #include <libtransmission/version.h> /* SHORT_VERSION_STRING */ |
---|
25 | |
---|
26 | #include "conf.h" |
---|
27 | #include "hig.h" |
---|
28 | #include "tr-core.h" |
---|
29 | #include "tr-prefs.h" |
---|
30 | #include "util.h" |
---|
31 | |
---|
32 | /*** |
---|
33 | **** UNITS |
---|
34 | ***/ |
---|
35 | |
---|
36 | const int mem_K = 1024; |
---|
37 | const char * mem_K_str = N_("KiB"); |
---|
38 | const char * mem_M_str = N_("MiB"); |
---|
39 | const char * mem_G_str = N_("GiB"); |
---|
40 | const char * mem_T_str = N_("TiB"); |
---|
41 | |
---|
42 | const int disk_K = 1000; |
---|
43 | const char * disk_K_str = N_("kB"); |
---|
44 | const char * disk_M_str = N_("MB"); |
---|
45 | const char * disk_G_str = N_("GB"); |
---|
46 | const char * disk_T_str = N_("TB"); |
---|
47 | |
---|
48 | const int speed_K = 1000; |
---|
49 | const char * speed_K_str = N_("kB/s"); |
---|
50 | const char * speed_M_str = N_("MB/s"); |
---|
51 | const char * speed_G_str = N_("GB/s"); |
---|
52 | const char * speed_T_str = N_("TB/s"); |
---|
53 | |
---|
54 | /*** |
---|
55 | **** |
---|
56 | ***/ |
---|
57 | |
---|
58 | const char* |
---|
59 | gtr_get_unicode_string (int i) |
---|
60 | { |
---|
61 | switch (i) |
---|
62 | { |
---|
63 | case GTR_UNICODE_UP: return "\xE2\x96\xB4"; |
---|
64 | case GTR_UNICODE_DOWN: return "\xE2\x96\xBE"; |
---|
65 | case GTR_UNICODE_INF: return "\xE2\x88\x9E"; |
---|
66 | case GTR_UNICODE_BULLET: return "\xE2\x88\x99"; |
---|
67 | default: return "err"; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | char* |
---|
72 | tr_strlratio (char * buf, double ratio, size_t buflen) |
---|
73 | { |
---|
74 | return tr_strratio (buf, buflen, ratio, gtr_get_unicode_string (GTR_UNICODE_INF)); |
---|
75 | } |
---|
76 | |
---|
77 | char* |
---|
78 | tr_strlpercent (char * buf, double x, size_t buflen) |
---|
79 | { |
---|
80 | return tr_strpercent (buf, x, buflen); |
---|
81 | } |
---|
82 | |
---|
83 | char* |
---|
84 | tr_strlsize (char * buf, guint64 bytes, size_t buflen) |
---|
85 | { |
---|
86 | if (!bytes) |
---|
87 | g_strlcpy (buf, Q_("None"), buflen); |
---|
88 | else |
---|
89 | tr_formatter_size_B (buf, bytes, buflen); |
---|
90 | |
---|
91 | return buf; |
---|
92 | } |
---|
93 | |
---|
94 | char* |
---|
95 | tr_strltime (char * buf, int seconds, size_t buflen) |
---|
96 | { |
---|
97 | int days, hours, minutes; |
---|
98 | char d[128], h[128], m[128], s[128]; |
---|
99 | |
---|
100 | if (seconds < 0) |
---|
101 | seconds = 0; |
---|
102 | |
---|
103 | days = seconds / 86400; |
---|
104 | hours = (seconds % 86400) / 3600; |
---|
105 | minutes = (seconds % 3600) / 60; |
---|
106 | seconds = (seconds % 3600) % 60; |
---|
107 | |
---|
108 | g_snprintf (d, sizeof (d), ngettext ("%'d day", "%'d days", days), days); |
---|
109 | g_snprintf (h, sizeof (h), ngettext ("%'d hour", "%'d hours", hours), hours); |
---|
110 | g_snprintf (m, sizeof (m), ngettext ("%'d minute", "%'d minutes", minutes), minutes); |
---|
111 | g_snprintf (s, sizeof (s), ngettext ("%'d second", "%'d seconds", seconds), seconds); |
---|
112 | |
---|
113 | if (days) |
---|
114 | { |
---|
115 | if (days >= 4 || !hours) |
---|
116 | g_strlcpy (buf, d, buflen); |
---|
117 | else |
---|
118 | g_snprintf (buf, buflen, "%s, %s", d, h); |
---|
119 | } |
---|
120 | else if (hours) |
---|
121 | { |
---|
122 | if (hours >= 4 || !minutes) |
---|
123 | g_strlcpy (buf, h, buflen); |
---|
124 | else |
---|
125 | g_snprintf (buf, buflen, "%s, %s", h, m); |
---|
126 | } |
---|
127 | else if (minutes) |
---|
128 | { |
---|
129 | if (minutes >= 4 || !seconds) |
---|
130 | g_strlcpy (buf, m, buflen); |
---|
131 | else |
---|
132 | g_snprintf (buf, buflen, "%s, %s", m, s); |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | g_strlcpy (buf, s, buflen); |
---|
137 | } |
---|
138 | |
---|
139 | return buf; |
---|
140 | } |
---|
141 | |
---|
142 | /* pattern-matching text; ie, legaltorrents.com */ |
---|
143 | void |
---|
144 | gtr_get_host_from_url (char * buf, size_t buflen, const char * url) |
---|
145 | { |
---|
146 | char host[1024]; |
---|
147 | const char * pch; |
---|
148 | |
---|
149 | if ((pch = strstr (url, "://"))) |
---|
150 | { |
---|
151 | const size_t hostlen = strcspn (pch+3, ":/"); |
---|
152 | const size_t copylen = MIN (hostlen, sizeof (host)-1); |
---|
153 | memcpy (host, pch+3, copylen); |
---|
154 | host[copylen] = '\0'; |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | *host = '\0'; |
---|
159 | } |
---|
160 | |
---|
161 | if (tr_addressIsIP (host)) |
---|
162 | { |
---|
163 | g_strlcpy (buf, url, buflen); |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | const char * first_dot = strchr (host, '.'); |
---|
168 | const char * last_dot = strrchr (host, '.'); |
---|
169 | if ((first_dot) && (last_dot) && (first_dot != last_dot)) |
---|
170 | g_strlcpy (buf, first_dot + 1, buflen); |
---|
171 | else |
---|
172 | g_strlcpy (buf, host, buflen); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | static gboolean |
---|
177 | gtr_is_supported_url (const char * str) |
---|
178 | { |
---|
179 | return ((str != NULL) && |
---|
180 | (g_str_has_prefix (str, "ftp://") || |
---|
181 | g_str_has_prefix (str, "http://") || |
---|
182 | g_str_has_prefix (str, "https://"))); |
---|
183 | } |
---|
184 | |
---|
185 | gboolean |
---|
186 | gtr_is_magnet_link (const char * str) |
---|
187 | { |
---|
188 | return (str != NULL) && (g_str_has_prefix (str, "magnet:?")); |
---|
189 | } |
---|
190 | |
---|
191 | gboolean |
---|
192 | gtr_is_hex_hashcode (const char * str) |
---|
193 | { |
---|
194 | int i; |
---|
195 | |
---|
196 | if (!str || (strlen (str) != 40)) |
---|
197 | return FALSE; |
---|
198 | |
---|
199 | for (i=0; i<40; ++i) |
---|
200 | if (!isxdigit (str[i])) |
---|
201 | return FALSE; |
---|
202 | |
---|
203 | return TRUE; |
---|
204 | } |
---|
205 | |
---|
206 | static GtkWindow * |
---|
207 | getWindow (GtkWidget * w) |
---|
208 | { |
---|
209 | if (w == NULL) |
---|
210 | return NULL; |
---|
211 | |
---|
212 | if (GTK_IS_WINDOW (w)) |
---|
213 | return GTK_WINDOW (w); |
---|
214 | |
---|
215 | return GTK_WINDOW (gtk_widget_get_ancestor (w, GTK_TYPE_WINDOW)); |
---|
216 | } |
---|
217 | |
---|
218 | void |
---|
219 | gtr_add_torrent_error_dialog (GtkWidget * child, |
---|
220 | int err, |
---|
221 | tr_torrent * duplicate_torrent, |
---|
222 | const char * filename) |
---|
223 | { |
---|
224 | char * secondary; |
---|
225 | GtkWidget * w; |
---|
226 | GtkWindow * win = getWindow (child); |
---|
227 | |
---|
228 | if (err == TR_PARSE_ERR) |
---|
229 | secondary = g_strdup_printf (_("The torrent file \"%s\" contains invalid data."), filename); |
---|
230 | else if (err == TR_PARSE_DUPLICATE) |
---|
231 | secondary = g_strdup_printf (_("The torrent file \"%s\" is already in use by \"%s.\""), filename, tr_torrentName (duplicate_torrent)); |
---|
232 | else |
---|
233 | secondary = g_strdup_printf (_("The torrent file \"%s\" encountered an unknown error."), filename); |
---|
234 | |
---|
235 | w = gtk_message_dialog_new (win, |
---|
236 | GTK_DIALOG_DESTROY_WITH_PARENT, |
---|
237 | GTK_MESSAGE_ERROR, |
---|
238 | GTK_BUTTONS_CLOSE, |
---|
239 | "%s", _("Error opening torrent")); |
---|
240 | gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (w), |
---|
241 | "%s", secondary); |
---|
242 | g_signal_connect_swapped (w, "response", |
---|
243 | G_CALLBACK (gtk_widget_destroy), w); |
---|
244 | gtk_widget_show_all (w); |
---|
245 | g_free (secondary); |
---|
246 | } |
---|
247 | |
---|
248 | typedef void (PopupFunc)(GtkWidget*, GdkEventButton*); |
---|
249 | |
---|
250 | /* pop up the context menu if a user right-clicks. |
---|
251 | if the row they right-click on isn't selected, select it. */ |
---|
252 | |
---|
253 | gboolean |
---|
254 | on_tree_view_button_pressed (GtkWidget * view, |
---|
255 | GdkEventButton * event, |
---|
256 | gpointer func) |
---|
257 | { |
---|
258 | GtkTreeView * tv = GTK_TREE_VIEW (view); |
---|
259 | |
---|
260 | if (event->type == GDK_BUTTON_PRESS && event->button == 3) |
---|
261 | { |
---|
262 | GtkTreePath * path; |
---|
263 | GtkTreeSelection * selection = gtk_tree_view_get_selection (tv); |
---|
264 | |
---|
265 | if (gtk_tree_view_get_path_at_pos (tv, |
---|
266 | (gint) event->x, |
---|
267 | (gint) event->y, |
---|
268 | &path, NULL, NULL, NULL)) |
---|
269 | { |
---|
270 | if (!gtk_tree_selection_path_is_selected (selection, path)) |
---|
271 | { |
---|
272 | gtk_tree_selection_unselect_all (selection); |
---|
273 | gtk_tree_selection_select_path (selection, path); |
---|
274 | } |
---|
275 | gtk_tree_path_free (path); |
---|
276 | } |
---|
277 | |
---|
278 | if (func != NULL) |
---|
279 | ((PopupFunc*)func)(view, event); |
---|
280 | |
---|
281 | return TRUE; |
---|
282 | } |
---|
283 | |
---|
284 | return FALSE; |
---|
285 | } |
---|
286 | |
---|
287 | /* if the user clicked in an empty area of the list, |
---|
288 | * clear all the selections. */ |
---|
289 | gboolean |
---|
290 | on_tree_view_button_released (GtkWidget * view, |
---|
291 | GdkEventButton * event, |
---|
292 | gpointer unused UNUSED) |
---|
293 | { |
---|
294 | GtkTreeView * tv = GTK_TREE_VIEW (view); |
---|
295 | |
---|
296 | if (!gtk_tree_view_get_path_at_pos (tv, |
---|
297 | (gint) event->x, |
---|
298 | (gint) event->y, |
---|
299 | NULL, NULL, NULL, NULL)) |
---|
300 | { |
---|
301 | GtkTreeSelection * selection = gtk_tree_view_get_selection (tv); |
---|
302 | gtk_tree_selection_unselect_all (selection); |
---|
303 | } |
---|
304 | |
---|
305 | return FALSE; |
---|
306 | } |
---|
307 | |
---|
308 | bool |
---|
309 | gtr_file_trash_or_remove (const char * filename, tr_error ** error) |
---|
310 | { |
---|
311 | GFile * file; |
---|
312 | gboolean trashed = FALSE; |
---|
313 | bool result = true; |
---|
314 | |
---|
315 | g_return_val_if_fail (filename && *filename, false); |
---|
316 | |
---|
317 | file = g_file_new_for_path (filename); |
---|
318 | |
---|
319 | if (gtr_pref_flag_get (TR_KEY_trash_can_enabled)) |
---|
320 | { |
---|
321 | GError * err = NULL; |
---|
322 | trashed = g_file_trash (file, NULL, &err); |
---|
323 | if (err) |
---|
324 | { |
---|
325 | g_message ("Unable to trash file \"%s\": %s", filename, err->message); |
---|
326 | tr_error_set_literal (error, err->code, err->message); |
---|
327 | g_clear_error (&err); |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | if (!trashed) |
---|
332 | { |
---|
333 | GError * err = NULL; |
---|
334 | g_file_delete (file, NULL, &err); |
---|
335 | if (err) |
---|
336 | { |
---|
337 | g_message ("Unable to delete file \"%s\": %s", filename, err->message); |
---|
338 | tr_error_clear (error); |
---|
339 | tr_error_set_literal (error, err->code, err->message); |
---|
340 | g_clear_error (&err); |
---|
341 | result = false; |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | g_object_unref (G_OBJECT (file)); |
---|
346 | return result; |
---|
347 | } |
---|
348 | |
---|
349 | const char* |
---|
350 | gtr_get_help_uri (void) |
---|
351 | { |
---|
352 | static char * uri = NULL; |
---|
353 | |
---|
354 | if (!uri) |
---|
355 | { |
---|
356 | const char * fmt = "http://www.transmissionbt.com/help/gtk/%d.%dx"; |
---|
357 | uri = g_strdup_printf (fmt, MAJOR_VERSION, MINOR_VERSION / 10); |
---|
358 | } |
---|
359 | |
---|
360 | return uri; |
---|
361 | } |
---|
362 | |
---|
363 | void |
---|
364 | gtr_open_file (const char * path) |
---|
365 | { |
---|
366 | GFile * file = g_file_new_for_path (path); |
---|
367 | gchar * uri = g_file_get_uri (file); |
---|
368 | gtr_open_uri (uri); |
---|
369 | g_free (uri); |
---|
370 | g_object_unref (file); |
---|
371 | } |
---|
372 | |
---|
373 | void |
---|
374 | gtr_open_uri (const char * uri) |
---|
375 | { |
---|
376 | if (uri) |
---|
377 | { |
---|
378 | gboolean opened = FALSE; |
---|
379 | |
---|
380 | if (!opened) |
---|
381 | opened = gtk_show_uri (NULL, uri, GDK_CURRENT_TIME, NULL); |
---|
382 | |
---|
383 | if (!opened) |
---|
384 | opened = g_app_info_launch_default_for_uri (uri, NULL, NULL); |
---|
385 | |
---|
386 | if (!opened) |
---|
387 | { |
---|
388 | char * argv[] = { (char*)"xdg-open", (char*)uri, NULL }; |
---|
389 | opened = g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL); |
---|
390 | } |
---|
391 | |
---|
392 | if (!opened) |
---|
393 | g_message ("Unable to open \"%s\"", uri); |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | /*** |
---|
398 | **** |
---|
399 | ***/ |
---|
400 | |
---|
401 | void |
---|
402 | gtr_combo_box_set_active_enum (GtkComboBox * combo_box, int value) |
---|
403 | { |
---|
404 | int i; |
---|
405 | int currentValue; |
---|
406 | const int column = 0; |
---|
407 | GtkTreeIter iter; |
---|
408 | GtkTreeModel * model = gtk_combo_box_get_model (combo_box); |
---|
409 | |
---|
410 | /* do the value and current value match? */ |
---|
411 | if (gtk_combo_box_get_active_iter (combo_box, &iter)) |
---|
412 | { |
---|
413 | gtk_tree_model_get (model, &iter, column, ¤tValue, -1); |
---|
414 | if (currentValue == value) |
---|
415 | return; |
---|
416 | } |
---|
417 | |
---|
418 | /* find the one to select */ |
---|
419 | i = 0; |
---|
420 | while ((gtk_tree_model_iter_nth_child (model, &iter, NULL, i++))) |
---|
421 | { |
---|
422 | gtk_tree_model_get (model, &iter, column, ¤tValue, -1); |
---|
423 | if (currentValue == value) |
---|
424 | { |
---|
425 | gtk_combo_box_set_active_iter (combo_box, &iter); |
---|
426 | return; |
---|
427 | } |
---|
428 | } |
---|
429 | } |
---|
430 | |
---|
431 | |
---|
432 | GtkWidget * |
---|
433 | gtr_combo_box_new_enum (const char * text_1, ...) |
---|
434 | { |
---|
435 | GtkWidget * w; |
---|
436 | GtkCellRenderer * r; |
---|
437 | GtkListStore * store; |
---|
438 | const char * text; |
---|
439 | |
---|
440 | store = gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING); |
---|
441 | |
---|
442 | text = text_1; |
---|
443 | if (text != NULL) |
---|
444 | { |
---|
445 | va_list vl; |
---|
446 | |
---|
447 | va_start (vl, text_1); |
---|
448 | do |
---|
449 | { |
---|
450 | const int val = va_arg (vl, int); |
---|
451 | gtk_list_store_insert_with_values (store, NULL, INT_MAX, 0, val, 1, text, -1); |
---|
452 | text = va_arg (vl, const char *); |
---|
453 | } |
---|
454 | while (text != NULL); |
---|
455 | va_end (vl); |
---|
456 | } |
---|
457 | |
---|
458 | w = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store)); |
---|
459 | r = gtk_cell_renderer_text_new (); |
---|
460 | gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (w), r, TRUE); |
---|
461 | gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (w), r, "text", 1, NULL); |
---|
462 | |
---|
463 | /* cleanup */ |
---|
464 | g_object_unref (store); |
---|
465 | return w; |
---|
466 | } |
---|
467 | |
---|
468 | int |
---|
469 | gtr_combo_box_get_active_enum (GtkComboBox * combo_box) |
---|
470 | { |
---|
471 | int value = 0; |
---|
472 | GtkTreeIter iter; |
---|
473 | |
---|
474 | if (gtk_combo_box_get_active_iter (combo_box, &iter)) |
---|
475 | gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter, 0, &value, -1); |
---|
476 | |
---|
477 | return value; |
---|
478 | } |
---|
479 | |
---|
480 | GtkWidget * |
---|
481 | gtr_priority_combo_new (void) |
---|
482 | { |
---|
483 | return gtr_combo_box_new_enum (_("High"), TR_PRI_HIGH, |
---|
484 | _("Normal"), TR_PRI_NORMAL, |
---|
485 | _("Low"), TR_PRI_LOW, |
---|
486 | NULL); |
---|
487 | } |
---|
488 | |
---|
489 | /*** |
---|
490 | **** |
---|
491 | ***/ |
---|
492 | |
---|
493 | #define GTR_CHILD_HIDDEN "gtr-child-hidden" |
---|
494 | |
---|
495 | void |
---|
496 | gtr_widget_set_visible (GtkWidget * w, gboolean b) |
---|
497 | { |
---|
498 | /* toggle the transient children, too */ |
---|
499 | if (GTK_IS_WINDOW (w)) |
---|
500 | { |
---|
501 | GList * l; |
---|
502 | GList * windows = gtk_window_list_toplevels (); |
---|
503 | GtkWindow * window = GTK_WINDOW (w); |
---|
504 | |
---|
505 | for (l=windows; l!=NULL; l=l->next) |
---|
506 | { |
---|
507 | if (!GTK_IS_WINDOW (l->data)) |
---|
508 | continue; |
---|
509 | if (gtk_window_get_transient_for (GTK_WINDOW (l->data)) != window) |
---|
510 | continue; |
---|
511 | if (gtk_widget_get_visible (GTK_WIDGET (l->data)) == b) |
---|
512 | continue; |
---|
513 | |
---|
514 | if (b && g_object_get_data (G_OBJECT (l->data), GTR_CHILD_HIDDEN) != NULL) |
---|
515 | { |
---|
516 | g_object_steal_data (G_OBJECT (l->data), GTR_CHILD_HIDDEN); |
---|
517 | gtr_widget_set_visible (GTK_WIDGET (l->data), TRUE); |
---|
518 | } |
---|
519 | else if (!b) |
---|
520 | { |
---|
521 | g_object_set_data (G_OBJECT (l->data), GTR_CHILD_HIDDEN, GINT_TO_POINTER (1)); |
---|
522 | gtr_widget_set_visible (GTK_WIDGET (l->data), FALSE); |
---|
523 | } |
---|
524 | } |
---|
525 | |
---|
526 | g_list_free (windows); |
---|
527 | } |
---|
528 | |
---|
529 | gtk_widget_set_visible (w, b); |
---|
530 | } |
---|
531 | |
---|
532 | void |
---|
533 | gtr_dialog_set_content (GtkDialog * dialog, GtkWidget * content) |
---|
534 | { |
---|
535 | GtkWidget * vbox = gtk_dialog_get_content_area (dialog); |
---|
536 | gtk_box_pack_start (GTK_BOX (vbox), content, TRUE, TRUE, 0); |
---|
537 | gtk_widget_show_all (content); |
---|
538 | } |
---|
539 | |
---|
540 | /*** |
---|
541 | **** |
---|
542 | ***/ |
---|
543 | |
---|
544 | void |
---|
545 | gtr_http_failure_dialog (GtkWidget * parent, const char * url, long response_code) |
---|
546 | { |
---|
547 | GtkWindow * window = getWindow (parent); |
---|
548 | |
---|
549 | GtkWidget * w = gtk_message_dialog_new (window, 0, |
---|
550 | GTK_MESSAGE_ERROR, |
---|
551 | GTK_BUTTONS_CLOSE, |
---|
552 | _("Error opening \"%s\""), url); |
---|
553 | |
---|
554 | gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (w), |
---|
555 | _("Server returned \"%1$ld %2$s\""), |
---|
556 | response_code, |
---|
557 | tr_webGetResponseStr (response_code)); |
---|
558 | |
---|
559 | g_signal_connect_swapped (w, "response", G_CALLBACK (gtk_widget_destroy), w); |
---|
560 | gtk_widget_show (w); |
---|
561 | } |
---|
562 | |
---|
563 | void |
---|
564 | gtr_unrecognized_url_dialog (GtkWidget * parent, const char * url) |
---|
565 | { |
---|
566 | const char * xt = "xt=urn:btih"; |
---|
567 | |
---|
568 | GtkWindow * window = getWindow (parent); |
---|
569 | |
---|
570 | GString * gstr = g_string_new (NULL); |
---|
571 | |
---|
572 | GtkWidget * w = gtk_message_dialog_new (window, 0, |
---|
573 | GTK_MESSAGE_ERROR, |
---|
574 | GTK_BUTTONS_CLOSE, |
---|
575 | "%s", _("Unrecognized URL")); |
---|
576 | |
---|
577 | g_string_append_printf (gstr, _("Transmission doesn't know how to use \"%s\""), url); |
---|
578 | |
---|
579 | if (gtr_is_magnet_link (url) && (strstr (url, xt) == NULL)) |
---|
580 | { |
---|
581 | g_string_append_printf (gstr, "\n \n"); |
---|
582 | 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); |
---|
583 | } |
---|
584 | |
---|
585 | gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (w), "%s", gstr->str); |
---|
586 | g_signal_connect_swapped (w, "response", G_CALLBACK (gtk_widget_destroy), w); |
---|
587 | gtk_widget_show (w); |
---|
588 | g_string_free (gstr, TRUE); |
---|
589 | } |
---|
590 | |
---|
591 | /*** |
---|
592 | **** |
---|
593 | ***/ |
---|
594 | |
---|
595 | void |
---|
596 | gtr_paste_clipboard_url_into_entry (GtkWidget * e) |
---|
597 | { |
---|
598 | size_t i; |
---|
599 | |
---|
600 | char * text[] = |
---|
601 | { |
---|
602 | g_strstrip (gtk_clipboard_wait_for_text (gtk_clipboard_get (GDK_SELECTION_PRIMARY))), |
---|
603 | g_strstrip (gtk_clipboard_wait_for_text (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD))) |
---|
604 | }; |
---|
605 | |
---|
606 | for (i=0; i<G_N_ELEMENTS (text); ++i) |
---|
607 | { |
---|
608 | char * s = text[i]; |
---|
609 | if (s && (gtr_is_supported_url (s) || gtr_is_magnet_link (s) || gtr_is_hex_hashcode (s))) |
---|
610 | { |
---|
611 | gtk_entry_set_text (GTK_ENTRY (e), s); |
---|
612 | break; |
---|
613 | } |
---|
614 | } |
---|
615 | |
---|
616 | for (i=0; i<G_N_ELEMENTS (text); ++i) |
---|
617 | g_free (text[i]); |
---|
618 | } |
---|
619 | |
---|
620 | /*** |
---|
621 | **** |
---|
622 | ***/ |
---|
623 | |
---|
624 | void |
---|
625 | gtr_label_set_text (GtkLabel * lb, const char * newstr) |
---|
626 | { |
---|
627 | const char * oldstr = gtk_label_get_text (lb); |
---|
628 | |
---|
629 | if (g_strcmp0 (oldstr, newstr) != 0) |
---|
630 | gtk_label_set_text (lb, newstr); |
---|
631 | } |
---|
632 | |
---|
633 | /*** |
---|
634 | **** |
---|
635 | ***/ |
---|
636 | |
---|
637 | struct freespace_label_data |
---|
638 | { |
---|
639 | guint timer_id; |
---|
640 | TrCore * core; |
---|
641 | GtkLabel * label; |
---|
642 | char * dir; |
---|
643 | }; |
---|
644 | |
---|
645 | static void on_freespace_label_core_destroyed (gpointer gdata, GObject * dead_core); |
---|
646 | static void on_freespace_label_destroyed (gpointer gdata, GObject * dead_label); |
---|
647 | |
---|
648 | static void |
---|
649 | freespace_label_data_free (gpointer gdata) |
---|
650 | { |
---|
651 | struct freespace_label_data * data = gdata; |
---|
652 | |
---|
653 | if (data->core != NULL) |
---|
654 | g_object_weak_unref (G_OBJECT(data->core), on_freespace_label_core_destroyed, data); |
---|
655 | |
---|
656 | if (data->label != NULL) |
---|
657 | g_object_weak_ref (G_OBJECT(data->label), on_freespace_label_destroyed, data); |
---|
658 | |
---|
659 | g_source_remove (data->timer_id); |
---|
660 | g_free (data->dir); |
---|
661 | g_free (data); |
---|
662 | } |
---|
663 | |
---|
664 | static TR_DEFINE_QUARK (freespace_label_data, freespace_label_data) |
---|
665 | |
---|
666 | static void |
---|
667 | on_freespace_label_core_destroyed (gpointer gdata, GObject * dead_core G_GNUC_UNUSED) |
---|
668 | { |
---|
669 | struct freespace_label_data * data = gdata; |
---|
670 | data->core = NULL; |
---|
671 | freespace_label_data_free (data); |
---|
672 | } |
---|
673 | |
---|
674 | static void |
---|
675 | on_freespace_label_destroyed (gpointer gdata, GObject * dead_label G_GNUC_UNUSED) |
---|
676 | { |
---|
677 | struct freespace_label_data * data = gdata; |
---|
678 | data->label = NULL; |
---|
679 | freespace_label_data_free (data); |
---|
680 | } |
---|
681 | |
---|
682 | static gboolean |
---|
683 | on_freespace_timer (gpointer gdata) |
---|
684 | { |
---|
685 | char text[128]; |
---|
686 | char markup[128]; |
---|
687 | int64_t bytes; |
---|
688 | tr_session * session; |
---|
689 | struct freespace_label_data * data = gdata; |
---|
690 | |
---|
691 | session = gtr_core_session (data->core); |
---|
692 | bytes = tr_sessionGetDirFreeSpace (session, data->dir); |
---|
693 | |
---|
694 | if (bytes < 0) |
---|
695 | { |
---|
696 | g_snprintf (text, sizeof(text), _("Error")); |
---|
697 | } |
---|
698 | else |
---|
699 | { |
---|
700 | char size[128]; |
---|
701 | tr_strlsize (size, bytes, sizeof(size)); |
---|
702 | g_snprintf (text, sizeof(text), _("%s free"), size); |
---|
703 | } |
---|
704 | |
---|
705 | g_snprintf (markup, sizeof(markup), "<i>%s</i>", text); |
---|
706 | gtk_label_set_markup (data->label, markup); |
---|
707 | |
---|
708 | return G_SOURCE_CONTINUE; |
---|
709 | } |
---|
710 | |
---|
711 | GtkWidget * |
---|
712 | gtr_freespace_label_new (struct _TrCore * core, const char * dir) |
---|
713 | { |
---|
714 | struct freespace_label_data * data; |
---|
715 | |
---|
716 | data = g_new0 (struct freespace_label_data, 1); |
---|
717 | data->timer_id = g_timeout_add_seconds (3, on_freespace_timer, data); |
---|
718 | data->core = core; |
---|
719 | data->label = GTK_LABEL (gtk_label_new (NULL)); |
---|
720 | data->dir = g_strdup (dir); |
---|
721 | |
---|
722 | /* when either the core or the label is destroyed, stop updating */ |
---|
723 | g_object_weak_ref (G_OBJECT(core), on_freespace_label_core_destroyed, data); |
---|
724 | g_object_weak_ref (G_OBJECT(data->label), on_freespace_label_destroyed, data); |
---|
725 | |
---|
726 | g_object_set_qdata (G_OBJECT(data->label), freespace_label_data_quark (), data); |
---|
727 | on_freespace_timer (data); |
---|
728 | return GTK_WIDGET (data->label); |
---|
729 | } |
---|
730 | |
---|
731 | void |
---|
732 | gtr_freespace_label_set_dir (GtkWidget * label, const char * dir) |
---|
733 | { |
---|
734 | struct freespace_label_data * data; |
---|
735 | |
---|
736 | data = g_object_get_qdata (G_OBJECT(label), freespace_label_data_quark ()); |
---|
737 | |
---|
738 | tr_free (data->dir); |
---|
739 | data->dir = g_strdup (dir); |
---|
740 | on_freespace_timer (data); |
---|
741 | } |
---|