Merged pidgin/main into default
[pidgin-git.git] / pidgin / gtkxfer.c
blob280a74738e385101570640dd8c81e71a75ad1c9e
1 /* pidgin
3 * Pidgin is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "pidgin.h"
24 #include "debug.h"
25 #include "notify.h"
26 #include "xfer.h"
27 #include "protocol.h"
28 #include "util.h"
30 #include "gtkxfer.h"
31 #include "prefs.h"
32 #include "pidginstock.h"
33 #include "gtkutils.h"
35 #include "gtk3compat.h"
37 #ifdef _WIN32
38 # include <shellapi.h>
39 #endif
41 /* the maximum size of files we will try to make a thumbnail for */
42 #define PIDGIN_XFER_MAX_SIZE_IMAGE_THUMBNAIL 10 * 1024 * 1024
44 struct _PidginXferDialog
46 gint box_count;
48 gboolean keep_open;
49 gboolean auto_clear;
51 gint num_transfers;
53 PurpleXfer *selected_xfer;
55 GtkWidget *window;
56 GtkWidget *tree;
57 GtkListStore *model;
59 GtkWidget *expander;
61 GtkWidget *grid;
63 GtkWidget *local_user_desc_label;
64 GtkWidget *local_user_label;
65 GtkWidget *remote_user_desc_label;
66 GtkWidget *remote_user_label;
67 GtkWidget *protocol_label;
68 GtkWidget *filename_label;
69 GtkWidget *localfile_label;
70 GtkWidget *status_label;
71 GtkWidget *speed_label;
72 GtkWidget *time_elapsed_label;
73 GtkWidget *time_remaining_label;
75 GtkWidget *progress;
77 /* Buttons */
78 GtkWidget *open_button;
79 GtkWidget *remove_button;
80 GtkWidget *stop_button;
81 GtkWidget *close_button;
84 typedef struct
86 GtkTreeIter iter;
87 time_t last_updated_time;
88 gboolean in_list;
90 char *name;
92 } PidginXferUiData;
94 static PidginXferDialog *xfer_dialog = NULL;
96 enum
98 COLUMN_STATUS = 0,
99 COLUMN_PROGRESS,
100 COLUMN_FILENAME,
101 COLUMN_SIZE,
102 COLUMN_REMAINING,
103 COLUMN_DATA,
104 NUM_COLUMNS
108 /**************************************************************************
109 * Utility Functions
110 **************************************************************************/
111 static void
112 get_xfer_info_strings(PurpleXfer *xfer, char **kbsec, char **time_elapsed,
113 char **time_remaining)
115 double kb_sent, kb_rem;
116 double kbps = 0.0;
117 time_t elapsed, now;
119 now = purple_xfer_get_end_time(xfer);
120 if (now == 0)
121 now = time(NULL);
123 kb_sent = purple_xfer_get_bytes_sent(xfer) / 1024.0;
124 kb_rem = purple_xfer_get_bytes_remaining(xfer) / 1024.0;
125 elapsed = purple_xfer_get_start_time(xfer);
126 if (elapsed > 0)
127 elapsed = now - elapsed;
128 else
129 elapsed = 0;
130 kbps = (elapsed > 0 ? (kb_sent / elapsed) : 0);
132 if (kbsec != NULL) {
133 *kbsec = g_strdup_printf(_("%.2f KiB/s"), kbps);
136 if (time_elapsed != NULL)
138 int h, m, s;
139 int secs_elapsed;
141 if (purple_xfer_get_start_time(xfer) > 0)
143 secs_elapsed = now - purple_xfer_get_start_time(xfer);
145 h = secs_elapsed / 3600;
146 m = (secs_elapsed % 3600) / 60;
147 s = secs_elapsed % 60;
149 *time_elapsed = g_strdup_printf("%d:%02d:%02d", h, m, s);
151 else
153 *time_elapsed = g_strdup(_("Not started"));
157 if (time_remaining != NULL) {
158 if (purple_xfer_is_completed(xfer)) {
159 *time_remaining = g_strdup(_("Finished"));
161 else if (purple_xfer_is_cancelled(xfer)) {
162 *time_remaining = g_strdup(_("Cancelled"));
164 else if (purple_xfer_get_size(xfer) == 0 || (kb_sent > 0 && kbps < 0.001)) {
165 *time_remaining = g_strdup(_("Unknown"));
167 else if (kb_sent <= 0) {
168 *time_remaining = g_strdup(_("Waiting for transfer to begin"));
170 else {
171 int h, m, s;
172 int secs_remaining;
174 secs_remaining = (int)(kb_rem / kbps);
176 h = secs_remaining / 3600;
177 m = (secs_remaining % 3600) / 60;
178 s = secs_remaining % 60;
180 *time_remaining = g_strdup_printf("%d:%02d:%02d", h, m, s);
185 static void
186 update_title_progress(PidginXferDialog *dialog)
188 gboolean valid;
189 GtkTreeIter iter;
190 int num_active_xfers = 0;
191 guint64 total_bytes_xferred = 0;
192 guint64 total_file_size = 0;
194 if (dialog->window == NULL)
195 return;
197 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog->model), &iter);
199 /* Find all active transfers */
200 while (valid) {
201 GValue val;
202 PurpleXfer *xfer = NULL;
204 val.g_type = 0;
205 gtk_tree_model_get_value(GTK_TREE_MODEL(dialog->model),
206 &iter, COLUMN_DATA, &val);
208 xfer = g_value_get_pointer(&val);
209 if (purple_xfer_get_status(xfer) == PURPLE_XFER_STATUS_STARTED) {
210 num_active_xfers++;
211 total_bytes_xferred += purple_xfer_get_bytes_sent(xfer);
212 total_file_size += purple_xfer_get_size(xfer);
215 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(dialog->model), &iter);
218 /* Update the title */
219 if (num_active_xfers > 0)
221 gchar *title;
222 int total_pct = 0;
224 if (total_file_size > 0) {
225 total_pct = 100 * total_bytes_xferred / total_file_size;
228 title = g_strdup_printf(ngettext("File Transfers - %d%% of %d file",
229 "File Transfers - %d%% of %d files",
230 num_active_xfers),
231 total_pct, num_active_xfers);
232 gtk_window_set_title(GTK_WINDOW(dialog->window), title);
233 g_free(title);
234 } else {
235 gtk_window_set_title(GTK_WINDOW(dialog->window), _("File Transfers"));
239 static void
240 update_detailed_info(PidginXferDialog *dialog, PurpleXfer *xfer)
242 PidginXferUiData *data;
243 char *kbsec, *time_elapsed, *time_remaining;
244 char *status, *utf8;
246 if (dialog == NULL || xfer == NULL)
247 return;
249 data = purple_xfer_get_ui_data(xfer);
251 get_xfer_info_strings(xfer, &kbsec, &time_elapsed, &time_remaining);
253 status = g_strdup_printf("%d%% (%" G_GOFFSET_FORMAT " of %" G_GOFFSET_FORMAT " bytes)",
254 (int)(purple_xfer_get_progress(xfer)*100),
255 purple_xfer_get_bytes_sent(xfer),
256 purple_xfer_get_size(xfer));
258 if (purple_xfer_is_completed(xfer)) {
260 GdkPixbuf *pixbuf = NULL;
262 pixbuf = gtk_widget_render_icon(xfer_dialog->window,
263 PIDGIN_STOCK_FILE_DONE,
264 GTK_ICON_SIZE_MENU, NULL);
266 gtk_list_store_set(GTK_LIST_STORE(xfer_dialog->model), &data->iter,
267 COLUMN_STATUS, pixbuf,
268 -1);
270 g_object_unref(pixbuf);
273 if (purple_xfer_get_xfer_type(xfer) == PURPLE_XFER_TYPE_RECEIVE) {
274 gtk_label_set_markup(GTK_LABEL(dialog->local_user_desc_label),
275 _("<b>Receiving As:</b>"));
276 gtk_label_set_markup(GTK_LABEL(dialog->remote_user_desc_label),
277 _("<b>Receiving From:</b>"));
279 else {
280 gtk_label_set_markup(GTK_LABEL(dialog->remote_user_desc_label),
281 _("<b>Sending To:</b>"));
282 gtk_label_set_markup(GTK_LABEL(dialog->local_user_desc_label),
283 _("<b>Sending As:</b>"));
286 gtk_label_set_text(GTK_LABEL(dialog->local_user_label),
287 purple_account_get_username(purple_xfer_get_account(xfer)));
288 gtk_label_set_text(GTK_LABEL(dialog->remote_user_label), purple_xfer_get_remote_user(xfer));
289 gtk_label_set_text(GTK_LABEL(dialog->protocol_label),
290 purple_account_get_protocol_name(purple_xfer_get_account(xfer)));
292 if (purple_xfer_get_xfer_type(xfer) == PURPLE_XFER_TYPE_RECEIVE) {
293 gtk_label_set_text(GTK_LABEL(dialog->filename_label),
294 purple_xfer_get_filename(xfer));
295 } else {
296 char *tmp;
298 tmp = g_path_get_basename(purple_xfer_get_local_filename(xfer));
299 utf8 = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
300 g_free(tmp);
302 gtk_label_set_text(GTK_LABEL(dialog->filename_label), utf8);
303 g_free(utf8);
306 utf8 = g_filename_to_utf8((purple_xfer_get_local_filename(xfer)), -1, NULL, NULL, NULL);
307 gtk_label_set_text(GTK_LABEL(dialog->localfile_label), utf8);
308 g_free(utf8);
310 gtk_label_set_text(GTK_LABEL(dialog->status_label), status);
312 gtk_label_set_text(GTK_LABEL(dialog->speed_label), kbsec);
313 gtk_label_set_text(GTK_LABEL(dialog->time_elapsed_label), time_elapsed);
314 gtk_label_set_text(GTK_LABEL(dialog->time_remaining_label),
315 time_remaining);
317 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dialog->progress),
318 purple_xfer_get_progress(xfer));
320 g_free(kbsec);
321 g_free(time_elapsed);
322 g_free(time_remaining);
323 g_free(status);
326 static void
327 update_buttons(PidginXferDialog *dialog, PurpleXfer *xfer)
329 if (dialog->selected_xfer == NULL) {
330 gtk_widget_set_sensitive(dialog->expander, FALSE);
331 gtk_widget_set_sensitive(dialog->open_button, FALSE);
332 gtk_widget_set_sensitive(dialog->stop_button, FALSE);
334 gtk_widget_show(dialog->stop_button);
335 gtk_widget_hide(dialog->remove_button);
337 return;
340 if (dialog->selected_xfer != xfer)
341 return;
343 if (purple_xfer_is_completed(xfer)) {
344 gtk_widget_hide(dialog->stop_button);
345 gtk_widget_show(dialog->remove_button);
347 #ifdef _WIN32
348 /* If using Win32... */
349 if (purple_xfer_get_xfer_type(xfer) == PURPLE_XFER_TYPE_RECEIVE) {
350 gtk_widget_set_sensitive(dialog->open_button, TRUE);
351 } else {
352 gtk_widget_set_sensitive(dialog->open_button, FALSE);
354 #else
355 if (purple_xfer_get_xfer_type(xfer) == PURPLE_XFER_TYPE_RECEIVE) {
356 gtk_widget_set_sensitive(dialog->open_button, TRUE);
357 } else {
358 gtk_widget_set_sensitive (dialog->open_button, FALSE);
360 #endif
362 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
363 } else if (purple_xfer_is_cancelled(xfer)) {
364 gtk_widget_hide(dialog->stop_button);
365 gtk_widget_show(dialog->remove_button);
367 gtk_widget_set_sensitive(dialog->open_button, FALSE);
369 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
370 } else {
371 gtk_widget_show(dialog->stop_button);
372 gtk_widget_hide(dialog->remove_button);
374 gtk_widget_set_sensitive(dialog->open_button, FALSE);
375 gtk_widget_set_sensitive(dialog->stop_button, TRUE);
379 static void
380 ensure_row_selected(PidginXferDialog *dialog)
382 GtkTreeIter iter;
383 GtkTreeSelection *selection;
385 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->tree));
387 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
388 return;
390 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog->model), &iter))
391 gtk_tree_selection_select_iter(selection, &iter);
394 /**************************************************************************
395 * Callbacks
396 **************************************************************************/
397 static gint
398 delete_win_cb(GtkWidget *w, GdkEventAny *e, gpointer d)
400 PidginXferDialog *dialog;
402 dialog = (PidginXferDialog *)d;
404 pidgin_xfer_dialog_hide(dialog);
406 return TRUE;
409 static void
410 toggle_keep_open_cb(GtkWidget *w, PidginXferDialog *dialog)
412 dialog->keep_open = !dialog->keep_open;
413 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/filetransfer/keep_open",
414 dialog->keep_open);
417 static void
418 toggle_clear_finished_cb(GtkWidget *w, PidginXferDialog *dialog)
420 dialog->auto_clear = !dialog->auto_clear;
421 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/filetransfer/clear_finished",
422 dialog->auto_clear);
425 static void
426 selection_changed_cb(GtkTreeSelection *selection, PidginXferDialog *dialog)
428 GtkTreeIter iter;
429 PurpleXfer *xfer = NULL;
431 if (gtk_tree_selection_get_selected(selection, NULL, &iter)) {
432 GValue val;
434 gtk_widget_set_sensitive(dialog->expander, TRUE);
436 val.g_type = 0;
437 gtk_tree_model_get_value(GTK_TREE_MODEL(dialog->model),
438 &iter, COLUMN_DATA, &val);
440 xfer = g_value_get_pointer(&val);
442 update_detailed_info(dialog, xfer);
444 dialog->selected_xfer = xfer;
446 else {
447 gtk_expander_set_expanded(GTK_EXPANDER(dialog->expander),
448 FALSE);
450 gtk_widget_set_sensitive(dialog->expander, FALSE);
452 dialog->selected_xfer = NULL;
455 update_buttons(dialog, xfer);
458 static void
459 open_button_cb(GtkButton *button, PidginXferDialog *dialog)
461 #ifdef _WIN32
462 /* If using Win32... */
463 int code;
464 wchar_t *wc_filename = g_utf8_to_utf16(
465 purple_xfer_get_local_filename(
466 dialog->selected_xfer),
467 -1, NULL, NULL, NULL);
469 code = (int) ShellExecuteW(NULL, NULL, wc_filename, NULL, NULL,
470 SW_SHOW);
472 g_free(wc_filename);
474 if (code == SE_ERR_ASSOCINCOMPLETE || code == SE_ERR_NOASSOC)
476 purple_notify_error(dialog, NULL,
477 _("There is no application configured to open this type of file."),
478 NULL, NULL);
480 else if (code < 32)
482 purple_notify_error(dialog, NULL,
483 _("An error occurred while opening the file."), NULL, NULL);
484 purple_debug_warning("xfer", "filename: %s; code: %d\n",
485 purple_xfer_get_local_filename(dialog->selected_xfer), code);
487 #else
488 const char *filename = purple_xfer_get_local_filename(dialog->selected_xfer);
489 char *command = NULL;
490 char *tmp = NULL;
491 GError *error = NULL;
493 if (purple_running_gnome())
495 char *escaped = g_shell_quote(filename);
496 command = g_strdup_printf("gnome-open %s", escaped);
497 g_free(escaped);
499 else if (purple_running_kde())
501 char *escaped = g_shell_quote(filename);
503 if (purple_str_has_suffix(filename, ".desktop"))
504 command = g_strdup_printf("kfmclient openURL %s 'text/plain'", escaped);
505 else
506 command = g_strdup_printf("kfmclient openURL %s", escaped);
507 g_free(escaped);
509 else
511 gchar *uri = g_strdup_printf("file://%s", filename);
512 purple_notify_uri(NULL, uri);
513 g_free(uri);
514 return;
517 if (purple_program_is_valid(command))
519 gint exit_status;
520 if (!g_spawn_command_line_sync(command, NULL, NULL, &exit_status, &error))
522 tmp = g_strdup_printf(_("Error launching %s: %s"),
523 purple_xfer_get_local_filename(dialog->selected_xfer),
524 error->message);
525 purple_notify_error(dialog, NULL, _("Unable to open file."), tmp, NULL);
526 g_free(tmp);
527 g_error_free(error);
529 if (exit_status != 0)
531 char *primary = g_strdup_printf(_("Error running %s"), command);
532 char *secondary = g_strdup_printf(_("Process returned error code %d"),
533 exit_status);
534 purple_notify_error(dialog, NULL, primary, secondary, NULL);
535 g_free(tmp);
538 #endif
541 static void
542 remove_button_cb(GtkButton *button, PidginXferDialog *dialog)
544 pidgin_xfer_dialog_remove_xfer(dialog, dialog->selected_xfer);
547 static void
548 stop_button_cb(GtkButton *button, PidginXferDialog *dialog)
550 purple_xfer_cancel_local(dialog->selected_xfer);
553 static void
554 close_button_cb(GtkButton *button, PidginXferDialog *dialog)
556 pidgin_xfer_dialog_hide(dialog);
560 /**************************************************************************
561 * Dialog Building Functions
562 **************************************************************************/
563 static GtkWidget *
564 setup_tree(PidginXferDialog *dialog)
566 GtkWidget *tree;
567 GtkListStore *model;
568 GtkCellRenderer *renderer;
569 GtkTreeViewColumn *column;
570 GtkTreeSelection *selection;
572 /* Build the tree model */
573 /* Transfer type, Progress Bar, Filename, Size, Remaining */
574 model = gtk_list_store_new(NUM_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_INT,
575 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
576 G_TYPE_POINTER);
577 dialog->model = model;
579 /* Create the treeview */
580 dialog->tree = tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
581 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
582 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
583 /* gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE); */
585 gtk_widget_show(tree);
587 g_signal_connect(G_OBJECT(selection), "changed",
588 G_CALLBACK(selection_changed_cb), dialog);
590 g_object_unref(G_OBJECT(model));
593 /* Columns */
595 /* Transfer Type column */
596 renderer = gtk_cell_renderer_pixbuf_new();
597 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
598 "pixbuf", COLUMN_STATUS, NULL);
599 gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(column),
600 GTK_TREE_VIEW_COLUMN_FIXED);
601 gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(column), 25);
602 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
603 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
605 /* Progress bar column */
606 renderer = gtk_cell_renderer_progress_new();
607 column = gtk_tree_view_column_new_with_attributes(_("Progress"), renderer,
608 "value", COLUMN_PROGRESS, NULL);
609 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
610 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
612 /* Filename column */
613 renderer = gtk_cell_renderer_text_new();
614 column = gtk_tree_view_column_new_with_attributes(_("Filename"), renderer,
615 "text", COLUMN_FILENAME, NULL);
616 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
617 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
619 /* File Size column */
620 renderer = gtk_cell_renderer_text_new();
621 column = gtk_tree_view_column_new_with_attributes(_("Size"), renderer,
622 "text", COLUMN_SIZE, NULL);
623 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
624 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
626 /* Bytes Remaining column */
627 renderer = gtk_cell_renderer_text_new();
628 column = gtk_tree_view_column_new_with_attributes(_("Remaining"),
629 renderer, "text", COLUMN_REMAINING, NULL);
630 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
631 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
633 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(tree));
635 gtk_widget_show(tree);
637 return tree;
640 static GtkWidget *
641 make_info_grid(PidginXferDialog *dialog)
643 GtkWidget *grid;
644 GtkWidget *label;
645 gsize i;
647 struct
649 GtkWidget **desc_label;
650 GtkWidget **val_label;
651 const char *desc;
653 } labels[] =
655 { &dialog->local_user_desc_label, &dialog->local_user_label, NULL },
656 { &dialog->remote_user_desc_label, &dialog->remote_user_label, NULL },
657 { &label, &dialog->protocol_label, _("Protocol:") },
658 { &label, &dialog->filename_label, _("Filename:") },
659 { &label, &dialog->localfile_label, _("Local File:") },
660 { &label, &dialog->status_label, _("Status:") },
661 { &label, &dialog->speed_label, _("Speed:") },
662 { &label, &dialog->time_elapsed_label, _("Time Elapsed:") },
663 { &label, &dialog->time_remaining_label, _("Time Remaining:") }
666 /* Setup the initial grid */
667 dialog->grid = grid = gtk_grid_new();
668 gtk_grid_set_row_spacing(GTK_GRID(grid), PIDGIN_HIG_BOX_SPACE);
669 gtk_grid_set_column_spacing(GTK_GRID(grid), PIDGIN_HIG_BOX_SPACE);
671 /* Setup the labels */
672 for (i = 0; i < G_N_ELEMENTS(labels); i++) {
673 GtkWidget *label;
674 char buf[256];
676 g_snprintf(buf, sizeof(buf), "<b>%s</b>",
677 labels[i].desc != NULL ? labels[i].desc : "");
679 *labels[i].desc_label = label = gtk_label_new(NULL);
680 gtk_label_set_markup(GTK_LABEL(label), buf);
681 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
682 gtk_label_set_xalign(GTK_LABEL(label), 0);
683 gtk_grid_attach(GTK_GRID(grid), label, 0, i, 1, 1);
684 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
686 gtk_widget_show(label);
688 *labels[i].val_label = label = gtk_label_new(NULL);
689 gtk_label_set_xalign(GTK_LABEL(label), 0);
690 gtk_grid_attach(GTK_GRID(grid), label, 1, i, 1, 1);
691 gtk_widget_set_hexpand(label, TRUE);
692 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
694 gtk_widget_show(label);
697 /* Setup the progress bar */
698 dialog->progress = gtk_progress_bar_new();
699 gtk_grid_attach(GTK_GRID(grid), dialog->progress,
700 0, G_N_ELEMENTS(labels), 2, 1);
702 gtk_widget_show(dialog->progress);
704 return grid;
707 PidginXferDialog *
708 pidgin_xfer_dialog_new(void)
710 PidginXferDialog *dialog;
711 GtkWidget *window;
712 GtkWidget *vbox;
713 GtkWidget *expander;
714 #if !GTK_CHECK_VERSION(3,14,0)
715 GtkWidget *alignment;
716 #endif
717 GtkWidget *grid;
718 GtkWidget *checkbox;
719 GtkWidget *bbox;
721 dialog = g_new0(PidginXferDialog, 1);
722 dialog->keep_open =
723 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/filetransfer/keep_open");
724 dialog->auto_clear =
725 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/filetransfer/clear_finished");
727 /* Create the window. */
728 dialog->window = window = pidgin_create_dialog(_("File Transfers"), 0, "file transfer", TRUE);
729 gtk_window_set_default_size(GTK_WINDOW(window), 450, 250);
731 g_signal_connect(G_OBJECT(window), "delete_event",
732 G_CALLBACK(delete_win_cb), dialog);
734 /* Create the main vbox for top half of the window. */
735 vbox = pidgin_dialog_get_vbox_with_properties(GTK_DIALOG(window), FALSE, PIDGIN_HIG_BORDER);
737 /* Setup the listbox */
738 gtk_box_pack_start(GTK_BOX(vbox),
739 pidgin_make_scrollable(setup_tree(dialog), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC, GTK_SHADOW_IN, -1, 140),
740 TRUE, TRUE, 0);
742 /* "Close this window when all transfers finish" */
743 checkbox = gtk_check_button_new_with_mnemonic(
744 _("Close this window when all transfers _finish"));
745 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox),
746 !dialog->keep_open);
747 g_signal_connect(G_OBJECT(checkbox), "toggled",
748 G_CALLBACK(toggle_keep_open_cb), dialog);
749 gtk_box_pack_start(GTK_BOX(vbox), checkbox, FALSE, FALSE, 0);
750 gtk_widget_show(checkbox);
752 /* "Clear finished transfers" */
753 checkbox = gtk_check_button_new_with_mnemonic(
754 _("C_lear finished transfers"));
755 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox),
756 dialog->auto_clear);
757 g_signal_connect(G_OBJECT(checkbox), "toggled",
758 G_CALLBACK(toggle_clear_finished_cb), dialog);
759 gtk_box_pack_start(GTK_BOX(vbox), checkbox, FALSE, FALSE, 0);
760 gtk_widget_show(checkbox);
762 /* "Download Details" arrow */
763 expander = gtk_expander_new_with_mnemonic(_("File transfer _details"));
764 dialog->expander = expander;
765 gtk_box_pack_start(GTK_BOX(vbox), expander, FALSE, FALSE, 0);
766 gtk_widget_show(expander);
768 gtk_widget_set_sensitive(expander, FALSE);
770 #if GTK_CHECK_VERSION(3,14,0)
771 /* The grid of information. */
772 grid = make_info_grid(dialog);
773 gtk_container_add(GTK_CONTAINER(expander), grid);
774 gtk_widget_show(grid);
776 /* Small indent make grid fall under GtkExpander's label */
777 gtk_widget_set_margin_start(grid, 20);
778 #else
779 /* Small indent make grid fall under GtkExpander's label */
780 alignment = gtk_alignment_new(1, 0, 1, 1);
781 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), 0, 0, 20, 0);
782 gtk_container_add(GTK_CONTAINER(expander), alignment);
783 gtk_widget_show(alignment);
785 /* The grid of information. */
786 grid = make_info_grid(dialog);
787 gtk_container_add(GTK_CONTAINER(alignment), grid);
788 gtk_widget_show(grid);
789 #endif
791 bbox = pidgin_dialog_get_action_area(GTK_DIALOG(window));
793 #define ADD_BUTTON(b, label, callback, callbackdata) do { \
794 GtkWidget *button = gtk_button_new_from_stock(label); \
795 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0); \
796 g_signal_connect(G_OBJECT(button), "clicked", callback, callbackdata); \
797 gtk_widget_show(button); \
798 b = button; \
799 } while (0)
801 /* Open button */
802 ADD_BUTTON(dialog->open_button, GTK_STOCK_OPEN, G_CALLBACK(open_button_cb), dialog);
803 gtk_widget_set_sensitive(dialog->open_button, FALSE);
805 /* Remove button */
806 ADD_BUTTON(dialog->remove_button, GTK_STOCK_REMOVE, G_CALLBACK(remove_button_cb), dialog);
807 gtk_widget_hide(dialog->remove_button);
809 /* Stop button */
810 ADD_BUTTON(dialog->stop_button, GTK_STOCK_STOP, G_CALLBACK(stop_button_cb), dialog);
811 gtk_widget_set_sensitive(dialog->stop_button, FALSE);
813 /* Close button */
814 ADD_BUTTON(dialog->close_button, GTK_STOCK_CLOSE, G_CALLBACK(close_button_cb), dialog);
816 #undef ADD_BUTTON
818 #ifdef _WIN32
819 g_signal_connect(G_OBJECT(dialog->window), "show",
820 G_CALLBACK(winpidgin_ensure_onscreen), dialog->window);
821 #endif
823 return dialog;
826 void
827 pidgin_xfer_dialog_destroy(PidginXferDialog *dialog)
829 g_return_if_fail(dialog != NULL);
831 purple_notify_close_with_handle(dialog);
833 gtk_widget_destroy(dialog->window);
835 g_free(dialog);
838 void
839 pidgin_xfer_dialog_show(PidginXferDialog *dialog)
841 PidginXferDialog *tmp;
843 if (dialog == NULL) {
844 tmp = pidgin_get_xfer_dialog();
846 if (tmp == NULL) {
847 tmp = pidgin_xfer_dialog_new();
848 pidgin_set_xfer_dialog(tmp);
851 gtk_widget_show(tmp->window);
852 } else {
853 gtk_window_present(GTK_WINDOW(dialog->window));
857 void
858 pidgin_xfer_dialog_hide(PidginXferDialog *dialog)
860 g_return_if_fail(dialog != NULL);
862 purple_notify_close_with_handle(dialog);
864 gtk_widget_hide(dialog->window);
867 void
868 pidgin_xfer_dialog_add_xfer(PidginXferDialog *dialog, PurpleXfer *xfer)
870 PidginXferUiData *data;
871 PurpleXferType type;
872 GdkPixbuf *pixbuf;
873 char *size_str, *remaining_str;
874 char *lfilename, *utf8;
876 g_return_if_fail(dialog != NULL);
877 g_return_if_fail(xfer != NULL);
879 g_object_ref(xfer);
881 data = purple_xfer_get_ui_data(xfer);
882 data->in_list = TRUE;
884 pidgin_xfer_dialog_show(dialog);
886 data->last_updated_time = 0;
888 type = purple_xfer_get_xfer_type(xfer);
890 size_str = purple_str_size_to_units(purple_xfer_get_size(xfer));
891 remaining_str = purple_str_size_to_units(purple_xfer_get_bytes_remaining(xfer));
893 pixbuf = gtk_widget_render_icon(dialog->window,
894 (type == PURPLE_XFER_TYPE_RECEIVE
895 ? PIDGIN_STOCK_DOWNLOAD
896 : PIDGIN_STOCK_UPLOAD),
897 GTK_ICON_SIZE_MENU, NULL);
899 gtk_list_store_append(dialog->model, &data->iter);
900 lfilename = g_path_get_basename(purple_xfer_get_local_filename(xfer));
901 utf8 = g_filename_to_utf8(lfilename, -1, NULL, NULL, NULL);
902 g_free(lfilename);
903 lfilename = utf8;
904 gtk_list_store_set(dialog->model, &data->iter,
905 COLUMN_STATUS, pixbuf,
906 COLUMN_PROGRESS, 0,
907 COLUMN_FILENAME, (type == PURPLE_XFER_TYPE_RECEIVE)
908 ? purple_xfer_get_filename(xfer)
909 : lfilename,
910 COLUMN_SIZE, size_str,
911 COLUMN_REMAINING, _("Waiting for transfer to begin"),
912 COLUMN_DATA, xfer,
913 -1);
914 g_free(lfilename);
916 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dialog->tree));
918 g_object_unref(pixbuf);
920 g_free(size_str);
921 g_free(remaining_str);
923 dialog->num_transfers++;
925 ensure_row_selected(dialog);
926 update_title_progress(dialog);
929 void
930 pidgin_xfer_dialog_remove_xfer(PidginXferDialog *dialog,
931 PurpleXfer *xfer)
933 PidginXferUiData *data;
935 g_return_if_fail(dialog != NULL);
936 g_return_if_fail(xfer != NULL);
938 data = purple_xfer_get_ui_data(xfer);
940 if (data == NULL)
941 return;
943 if (!data->in_list)
944 return;
946 data->in_list = FALSE;
948 gtk_list_store_remove(GTK_LIST_STORE(dialog->model), &data->iter);
950 dialog->num_transfers--;
952 ensure_row_selected(dialog);
954 update_title_progress(dialog);
955 g_object_unref(xfer);
958 void
959 pidgin_xfer_dialog_cancel_xfer(PidginXferDialog *dialog,
960 PurpleXfer *xfer)
962 PidginXferUiData *data;
963 GdkPixbuf *pixbuf;
964 const gchar *status;
966 g_return_if_fail(dialog != NULL);
967 g_return_if_fail(xfer != NULL);
969 data = purple_xfer_get_ui_data(xfer);
971 if (data == NULL)
972 return;
974 if (!data->in_list)
975 return;
977 if ((purple_xfer_get_status(xfer) == PURPLE_XFER_STATUS_CANCEL_LOCAL) && (dialog->auto_clear)) {
978 pidgin_xfer_dialog_remove_xfer(dialog, xfer);
979 return;
982 data = purple_xfer_get_ui_data(xfer);
984 update_detailed_info(dialog, xfer);
985 update_title_progress(dialog);
987 pixbuf = gtk_widget_render_icon(dialog->window,
988 PIDGIN_STOCK_FILE_CANCELLED,
989 GTK_ICON_SIZE_MENU, NULL);
991 if (purple_xfer_is_cancelled(xfer))
992 status = _("Cancelled");
993 else
994 status = _("Failed");
996 gtk_list_store_set(dialog->model, &data->iter,
997 COLUMN_STATUS, pixbuf,
998 COLUMN_REMAINING, status,
999 -1);
1001 g_object_unref(pixbuf);
1003 update_buttons(dialog, xfer);
1006 void
1007 pidgin_xfer_dialog_update_xfer(PidginXferDialog *dialog,
1008 PurpleXfer *xfer)
1010 PidginXferUiData *data;
1011 char *size_str, *remaining_str;
1012 time_t current_time;
1013 GtkTreeIter iter;
1014 gboolean valid;
1016 g_return_if_fail(dialog != NULL);
1017 g_return_if_fail(xfer != NULL);
1019 if ((data = purple_xfer_get_ui_data(xfer)) == NULL)
1020 return;
1022 if (data->in_list == FALSE)
1023 return;
1025 current_time = time(NULL);
1026 if (((current_time - data->last_updated_time) == 0) &&
1027 (!purple_xfer_is_completed(xfer)))
1029 /* Don't update the window more than once per second */
1030 return;
1032 data->last_updated_time = current_time;
1034 size_str = purple_str_size_to_units(purple_xfer_get_size(xfer));
1035 remaining_str = purple_str_size_to_units(purple_xfer_get_bytes_remaining(xfer));
1037 gtk_list_store_set(xfer_dialog->model, &data->iter,
1038 COLUMN_PROGRESS, (gint)(purple_xfer_get_progress(xfer) * 100),
1039 COLUMN_SIZE, size_str,
1040 COLUMN_REMAINING, remaining_str,
1041 -1);
1043 g_free(size_str);
1044 g_free(remaining_str);
1046 if (purple_xfer_is_completed(xfer))
1048 GdkPixbuf *pixbuf;
1050 pixbuf = gtk_widget_render_icon(dialog->window,
1051 PIDGIN_STOCK_FILE_DONE,
1052 GTK_ICON_SIZE_MENU, NULL);
1054 gtk_list_store_set(GTK_LIST_STORE(xfer_dialog->model), &data->iter,
1055 COLUMN_STATUS, pixbuf,
1056 COLUMN_REMAINING, _("Finished"),
1057 -1);
1059 g_object_unref(pixbuf);
1062 update_title_progress(dialog);
1063 if (xfer == dialog->selected_xfer)
1064 update_detailed_info(xfer_dialog, xfer);
1066 if (purple_xfer_is_completed(xfer) && dialog->auto_clear)
1067 pidgin_xfer_dialog_remove_xfer(dialog, xfer);
1068 else
1069 update_buttons(dialog, xfer);
1072 * If all transfers are finished, and the pref is set, then
1073 * close the dialog. Otherwise just exit this function.
1075 if (dialog->keep_open)
1076 return;
1078 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog->model), &iter);
1079 while (valid)
1081 GValue val;
1082 PurpleXfer *next;
1084 val.g_type = 0;
1085 gtk_tree_model_get_value(GTK_TREE_MODEL(dialog->model),
1086 &iter, COLUMN_DATA, &val);
1088 next = g_value_get_pointer(&val);
1089 if (!purple_xfer_is_completed(next))
1090 return;
1092 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(dialog->model), &iter);
1095 /* If we got to this point then we know everything is finished */
1096 pidgin_xfer_dialog_hide(dialog);
1099 /**************************************************************************
1100 * PidginXferDialog GBoxed code
1101 **************************************************************************/
1102 static PidginXferDialog *
1103 pidgin_xfer_dialog_ref(PidginXferDialog *dialog)
1105 g_return_val_if_fail(dialog != NULL, NULL);
1107 dialog->box_count++;
1109 return dialog;
1112 static void
1113 pidgin_xfer_dialog_unref(PidginXferDialog *dialog)
1115 g_return_if_fail(dialog != NULL);
1116 g_return_if_fail(dialog->box_count >= 0);
1118 if (!dialog->box_count--)
1119 pidgin_xfer_dialog_destroy(dialog);
1122 GType
1123 pidgin_xfer_dialog_get_type(void)
1125 static GType type = 0;
1127 if (type == 0) {
1128 type = g_boxed_type_register_static("PidginXferDialog",
1129 (GBoxedCopyFunc)pidgin_xfer_dialog_ref,
1130 (GBoxedFreeFunc)pidgin_xfer_dialog_unref);
1133 return type;
1136 /**************************************************************************
1137 * File Transfer UI Ops
1138 **************************************************************************/
1139 static void
1140 pidgin_xfer_new_xfer(PurpleXfer *xfer)
1142 PidginXferUiData *data;
1144 /* This is where we're setting xfer's "ui_data" for the first time. */
1145 data = g_new0(PidginXferUiData, 1);
1146 purple_xfer_set_ui_data(xfer, data);
1149 static void
1150 pidgin_xfer_destroy(PurpleXfer *xfer)
1152 PidginXferUiData *data;
1154 data = purple_xfer_get_ui_data(xfer);
1155 if (data) {
1156 g_free(data->name);
1157 g_free(data);
1158 purple_xfer_set_ui_data(xfer, NULL);
1162 static void
1163 pidgin_xfer_add_xfer(PurpleXfer *xfer)
1165 if (xfer_dialog == NULL)
1166 xfer_dialog = pidgin_xfer_dialog_new();
1168 pidgin_xfer_dialog_add_xfer(xfer_dialog, xfer);
1171 static void
1172 pidgin_xfer_update_progress(PurpleXfer *xfer, double percent)
1174 pidgin_xfer_dialog_update_xfer(xfer_dialog, xfer);
1177 static void
1178 pidgin_xfer_cancel_local(PurpleXfer *xfer)
1180 if (xfer_dialog)
1181 pidgin_xfer_dialog_cancel_xfer(xfer_dialog, xfer);
1184 static void
1185 pidgin_xfer_cancel_remote(PurpleXfer *xfer)
1187 if (xfer_dialog)
1188 pidgin_xfer_dialog_cancel_xfer(xfer_dialog, xfer);
1191 static void
1192 pidgin_xfer_add_thumbnail(PurpleXfer *xfer, const gchar *formats)
1194 purple_debug_info("xfer", "creating thumbnail for transfer\n");
1196 if (purple_xfer_get_size(xfer) <= PIDGIN_XFER_MAX_SIZE_IMAGE_THUMBNAIL) {
1197 GdkPixbuf *thumbnail =
1198 pidgin_pixbuf_new_from_file_at_size(
1199 purple_xfer_get_local_filename(xfer), 128, 128);
1201 if (thumbnail) {
1202 gchar **formats_split = g_strsplit(formats, ",", 0);
1203 gchar *buffer = NULL;
1204 gsize size;
1205 char *option_keys[2] = {NULL, NULL};
1206 char *option_values[2] = {NULL, NULL};
1207 int i;
1208 gchar *format = NULL;
1210 for (i = 0; formats_split[i]; i++) {
1211 if (purple_strequal(formats_split[i], "jpeg")) {
1212 purple_debug_info("xfer", "creating JPEG thumbnail\n");
1213 option_keys[0] = "quality";
1214 option_values[0] = "90";
1215 format = "jpeg";
1216 break;
1217 } else if (purple_strequal(formats_split[i], "png")) {
1218 purple_debug_info("xfer", "creating PNG thumbnail\n");
1219 option_keys[0] = "compression";
1220 option_values[0] = "9";
1221 format = "png";
1222 break;
1226 /* Try the first format given by the protocol without options */
1227 if (format == NULL) {
1228 purple_debug_info("xfer",
1229 "creating thumbnail of format %s as demanded by protocol\n",
1230 formats_split[0]);
1231 format = formats_split[0];
1234 gdk_pixbuf_save_to_bufferv(thumbnail, &buffer, &size, format,
1235 option_keys, option_values, NULL);
1237 if (buffer) {
1238 gchar *mimetype = g_strdup_printf("image/%s", format);
1239 purple_debug_info("xfer",
1240 "created thumbnail of %" G_GSIZE_FORMAT " bytes\n",
1241 size);
1242 purple_xfer_set_thumbnail(xfer, buffer, size, mimetype);
1243 g_free(buffer);
1244 g_free(mimetype);
1246 g_object_unref(thumbnail);
1247 g_strfreev(formats_split);
1252 static PurpleXferUiOps ops =
1254 pidgin_xfer_new_xfer,
1255 pidgin_xfer_destroy,
1256 pidgin_xfer_add_xfer,
1257 pidgin_xfer_update_progress,
1258 pidgin_xfer_cancel_local,
1259 pidgin_xfer_cancel_remote,
1260 NULL,
1261 NULL,
1262 NULL,
1263 pidgin_xfer_add_thumbnail
1266 /**************************************************************************
1267 * GTK+ File Transfer API
1268 **************************************************************************/
1269 void
1270 pidgin_xfers_init(void)
1272 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/filetransfer");
1273 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/filetransfer/clear_finished", TRUE);
1274 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/filetransfer/keep_open", FALSE);
1277 void
1278 pidgin_xfers_uninit(void)
1280 if (xfer_dialog != NULL)
1281 pidgin_xfer_dialog_destroy(xfer_dialog);
1284 void
1285 pidgin_set_xfer_dialog(PidginXferDialog *dialog)
1287 xfer_dialog = dialog;
1290 PidginXferDialog *
1291 pidgin_get_xfer_dialog(void)
1293 return xfer_dialog;
1296 PurpleXferUiOps *
1297 pidgin_xfers_get_ui_ops(void)
1299 return &ops;