Cast back and forth between int and pointer instead of putting pointers
[pidgin-git.git] / pidgin / gtkft.c
blobdfc91a51a44e90395bfcbdc139c8d84f4455b8c9
1 /**
2 * @file gtkft.c GTK+ File Transfer UI
3 * @ingroup pidgin
4 */
6 /* pidgin
8 * Pidgin is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "internal.h"
27 #include "pidgin.h"
29 #include "debug.h"
30 #include "notify.h"
31 #include "ft.h"
32 #include "prpl.h"
33 #include "util.h"
35 #include "gtkft.h"
36 #include "prefs.h"
37 #include "pidginstock.h"
38 #include "gtkutils.h"
40 #define PIDGINXFER(xfer) \
41 (PidginXferUiData *)(xfer)->ui_data
43 /* the maximum size of files we will try to make a thumbnail for */
44 #define PIDGIN_XFER_MAX_SIZE_IMAGE_THUMBNAIL 10 * 1024 * 1024
46 struct _PidginXferDialog
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 *table;
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 if (xfer->end_time != 0)
120 now = xfer->end_time;
121 else
122 now = time(NULL);
124 kb_sent = purple_xfer_get_bytes_sent(xfer) / 1024.0;
125 kb_rem = purple_xfer_get_bytes_remaining(xfer) / 1024.0;
126 elapsed = (xfer->start_time > 0 ? now - xfer->start_time : 0);
127 kbps = (elapsed > 0 ? (kb_sent / elapsed) : 0);
129 if (kbsec != NULL) {
130 *kbsec = g_strdup_printf(_("%.2f KiB/s"), kbps);
133 if (time_elapsed != NULL)
135 int h, m, s;
136 int secs_elapsed;
138 if (xfer->start_time > 0)
140 secs_elapsed = now - xfer->start_time;
142 h = secs_elapsed / 3600;
143 m = (secs_elapsed % 3600) / 60;
144 s = secs_elapsed % 60;
146 *time_elapsed = g_strdup_printf("%d:%02d:%02d", h, m, s);
148 else
150 *time_elapsed = g_strdup(_("Not started"));
154 if (time_remaining != NULL) {
155 if (purple_xfer_is_completed(xfer)) {
156 *time_remaining = g_strdup(_("Finished"));
158 else if (purple_xfer_is_canceled(xfer)) {
159 *time_remaining = g_strdup(_("Cancelled"));
161 else if (purple_xfer_get_size(xfer) == 0 || (kb_sent > 0 && kbps == 0)) {
162 *time_remaining = g_strdup(_("Unknown"));
164 else if (kb_sent <= 0) {
165 *time_remaining = g_strdup(_("Waiting for transfer to begin"));
167 else {
168 int h, m, s;
169 int secs_remaining;
171 secs_remaining = (int)(kb_rem / kbps);
173 h = secs_remaining / 3600;
174 m = (secs_remaining % 3600) / 60;
175 s = secs_remaining % 60;
177 *time_remaining = g_strdup_printf("%d:%02d:%02d", h, m, s);
182 static void
183 update_title_progress(PidginXferDialog *dialog)
185 gboolean valid;
186 GtkTreeIter iter;
187 int num_active_xfers = 0;
188 guint64 total_bytes_xferred = 0;
189 guint64 total_file_size = 0;
191 if (dialog->window == NULL)
192 return;
194 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog->model), &iter);
196 /* Find all active transfers */
197 while (valid) {
198 GValue val;
199 PurpleXfer *xfer = NULL;
201 val.g_type = 0;
202 gtk_tree_model_get_value(GTK_TREE_MODEL(dialog->model),
203 &iter, COLUMN_DATA, &val);
205 xfer = g_value_get_pointer(&val);
206 if (purple_xfer_get_status(xfer) == PURPLE_XFER_STATUS_STARTED) {
207 num_active_xfers++;
208 total_bytes_xferred += purple_xfer_get_bytes_sent(xfer);
209 total_file_size += purple_xfer_get_size(xfer);
212 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(dialog->model), &iter);
215 /* Update the title */
216 if (num_active_xfers > 0)
218 gchar *title;
219 int total_pct = 0;
221 if (total_file_size > 0) {
222 total_pct = 100 * total_bytes_xferred / total_file_size;
225 title = g_strdup_printf(ngettext("File Transfers - %d%% of %d file",
226 "File Transfers - %d%% of %d files",
227 num_active_xfers),
228 total_pct, num_active_xfers);
229 gtk_window_set_title(GTK_WINDOW(dialog->window), title);
230 g_free(title);
231 } else {
232 gtk_window_set_title(GTK_WINDOW(dialog->window), _("File Transfers"));
236 static void
237 update_detailed_info(PidginXferDialog *dialog, PurpleXfer *xfer)
239 PidginXferUiData *data;
240 char *kbsec, *time_elapsed, *time_remaining;
241 char *status, *utf8;
243 if (dialog == NULL || xfer == NULL)
244 return;
246 data = PIDGINXFER(xfer);
248 get_xfer_info_strings(xfer, &kbsec, &time_elapsed, &time_remaining);
250 status = g_strdup_printf("%d%% (%" G_GSIZE_FORMAT " of %" G_GSIZE_FORMAT " bytes)",
251 (int)(purple_xfer_get_progress(xfer)*100),
252 purple_xfer_get_bytes_sent(xfer),
253 purple_xfer_get_size(xfer));
255 if (purple_xfer_is_completed(xfer)) {
257 GdkPixbuf *pixbuf = NULL;
259 pixbuf = gtk_widget_render_icon(xfer_dialog->window,
260 PIDGIN_STOCK_FILE_DONE,
261 GTK_ICON_SIZE_MENU, NULL);
263 gtk_list_store_set(GTK_LIST_STORE(xfer_dialog->model), &data->iter,
264 COLUMN_STATUS, pixbuf,
265 -1);
267 g_object_unref(pixbuf);
270 if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) {
271 gtk_label_set_markup(GTK_LABEL(dialog->local_user_desc_label),
272 _("<b>Receiving As:</b>"));
273 gtk_label_set_markup(GTK_LABEL(dialog->remote_user_desc_label),
274 _("<b>Receiving From:</b>"));
276 else {
277 gtk_label_set_markup(GTK_LABEL(dialog->remote_user_desc_label),
278 _("<b>Sending To:</b>"));
279 gtk_label_set_markup(GTK_LABEL(dialog->local_user_desc_label),
280 _("<b>Sending As:</b>"));
283 gtk_label_set_text(GTK_LABEL(dialog->local_user_label),
284 purple_account_get_username(xfer->account));
285 gtk_label_set_text(GTK_LABEL(dialog->remote_user_label), xfer->who);
286 gtk_label_set_text(GTK_LABEL(dialog->protocol_label),
287 purple_account_get_protocol_name(xfer->account));
289 if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) {
290 gtk_label_set_text(GTK_LABEL(dialog->filename_label),
291 purple_xfer_get_filename(xfer));
292 } else {
293 char *tmp;
295 tmp = g_path_get_basename(purple_xfer_get_local_filename(xfer));
296 utf8 = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
297 g_free(tmp);
299 gtk_label_set_text(GTK_LABEL(dialog->filename_label), utf8);
300 g_free(utf8);
303 utf8 = g_filename_to_utf8((purple_xfer_get_local_filename(xfer)), -1, NULL, NULL, NULL);
304 gtk_label_set_text(GTK_LABEL(dialog->localfile_label), utf8);
305 g_free(utf8);
307 gtk_label_set_text(GTK_LABEL(dialog->status_label), status);
309 gtk_label_set_text(GTK_LABEL(dialog->speed_label), kbsec);
310 gtk_label_set_text(GTK_LABEL(dialog->time_elapsed_label), time_elapsed);
311 gtk_label_set_text(GTK_LABEL(dialog->time_remaining_label),
312 time_remaining);
314 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dialog->progress),
315 purple_xfer_get_progress(xfer));
317 g_free(kbsec);
318 g_free(time_elapsed);
319 g_free(time_remaining);
320 g_free(status);
323 static void
324 update_buttons(PidginXferDialog *dialog, PurpleXfer *xfer)
326 if (dialog->selected_xfer == NULL) {
327 gtk_widget_set_sensitive(dialog->expander, FALSE);
328 gtk_widget_set_sensitive(dialog->open_button, FALSE);
329 gtk_widget_set_sensitive(dialog->stop_button, FALSE);
331 gtk_widget_show(dialog->stop_button);
332 gtk_widget_hide(dialog->remove_button);
334 return;
337 if (dialog->selected_xfer != xfer)
338 return;
340 if (purple_xfer_is_completed(xfer)) {
341 gtk_widget_hide(dialog->stop_button);
342 gtk_widget_show(dialog->remove_button);
344 #ifdef _WIN32
345 /* If using Win32... */
346 if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) {
347 gtk_widget_set_sensitive(dialog->open_button, TRUE);
348 } else {
349 gtk_widget_set_sensitive(dialog->open_button, FALSE);
351 #else
352 if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) {
353 gtk_widget_set_sensitive(dialog->open_button, TRUE);
354 } else {
355 gtk_widget_set_sensitive (dialog->open_button, FALSE);
357 #endif
359 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
360 } else if (purple_xfer_is_canceled(xfer)) {
361 gtk_widget_hide(dialog->stop_button);
362 gtk_widget_show(dialog->remove_button);
364 gtk_widget_set_sensitive(dialog->open_button, FALSE);
366 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
367 } else {
368 gtk_widget_show(dialog->stop_button);
369 gtk_widget_hide(dialog->remove_button);
371 gtk_widget_set_sensitive(dialog->open_button, FALSE);
372 gtk_widget_set_sensitive(dialog->stop_button, TRUE);
376 static void
377 ensure_row_selected(PidginXferDialog *dialog)
379 GtkTreeIter iter;
380 GtkTreeSelection *selection;
382 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->tree));
384 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
385 return;
387 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog->model), &iter))
388 gtk_tree_selection_select_iter(selection, &iter);
391 /**************************************************************************
392 * Callbacks
393 **************************************************************************/
394 static gint
395 delete_win_cb(GtkWidget *w, GdkEventAny *e, gpointer d)
397 PidginXferDialog *dialog;
399 dialog = (PidginXferDialog *)d;
401 pidgin_xfer_dialog_hide(dialog);
403 return TRUE;
406 static void
407 toggle_keep_open_cb(GtkWidget *w, PidginXferDialog *dialog)
409 dialog->keep_open = !dialog->keep_open;
410 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/filetransfer/keep_open",
411 dialog->keep_open);
414 static void
415 toggle_clear_finished_cb(GtkWidget *w, PidginXferDialog *dialog)
417 dialog->auto_clear = !dialog->auto_clear;
418 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/filetransfer/clear_finished",
419 dialog->auto_clear);
422 static void
423 selection_changed_cb(GtkTreeSelection *selection, PidginXferDialog *dialog)
425 GtkTreeIter iter;
426 PurpleXfer *xfer = NULL;
428 if (gtk_tree_selection_get_selected(selection, NULL, &iter)) {
429 GValue val;
431 gtk_widget_set_sensitive(dialog->expander, TRUE);
433 val.g_type = 0;
434 gtk_tree_model_get_value(GTK_TREE_MODEL(dialog->model),
435 &iter, COLUMN_DATA, &val);
437 xfer = g_value_get_pointer(&val);
439 update_detailed_info(dialog, xfer);
441 dialog->selected_xfer = xfer;
443 else {
444 gtk_expander_set_expanded(GTK_EXPANDER(dialog->expander),
445 FALSE);
447 gtk_widget_set_sensitive(dialog->expander, FALSE);
449 dialog->selected_xfer = NULL;
452 update_buttons(dialog, xfer);
455 static void
456 open_button_cb(GtkButton *button, PidginXferDialog *dialog)
458 #ifdef _WIN32
459 /* If using Win32... */
460 int code;
461 wchar_t *wc_filename = g_utf8_to_utf16(
462 purple_xfer_get_local_filename(
463 dialog->selected_xfer),
464 -1, NULL, NULL, NULL);
466 code = (int) ShellExecuteW(NULL, NULL, wc_filename, NULL, NULL,
467 SW_SHOW);
469 g_free(wc_filename);
471 if (code == SE_ERR_ASSOCINCOMPLETE || code == SE_ERR_NOASSOC)
473 purple_notify_error(dialog, NULL,
474 _("There is no application configured to open this type of file."), NULL);
476 else if (code < 32)
478 purple_notify_error(dialog, NULL,
479 _("An error occurred while opening the file."), NULL);
480 purple_debug_warning("ft", "filename: %s; code: %d\n",
481 purple_xfer_get_local_filename(dialog->selected_xfer), code);
483 #else
484 const char *filename = purple_xfer_get_local_filename(dialog->selected_xfer);
485 char *command = NULL;
486 char *tmp = NULL;
487 GError *error = NULL;
489 if (purple_running_gnome())
491 char *escaped = g_shell_quote(filename);
492 command = g_strdup_printf("gnome-open %s", escaped);
493 g_free(escaped);
495 else if (purple_running_kde())
497 char *escaped = g_shell_quote(filename);
499 if (purple_str_has_suffix(filename, ".desktop"))
500 command = g_strdup_printf("kfmclient openURL %s 'text/plain'", escaped);
501 else
502 command = g_strdup_printf("kfmclient openURL %s", escaped);
503 g_free(escaped);
505 else
507 purple_notify_uri(NULL, filename);
508 return;
511 if (purple_program_is_valid(command))
513 gint exit_status;
514 if (!g_spawn_command_line_sync(command, NULL, NULL, &exit_status, &error))
516 tmp = g_strdup_printf(_("Error launching %s: %s"),
517 purple_xfer_get_local_filename(dialog->selected_xfer),
518 error->message);
519 purple_notify_error(dialog, NULL, _("Unable to open file."), tmp);
520 g_free(tmp);
521 g_error_free(error);
523 if (exit_status != 0)
525 char *primary = g_strdup_printf(_("Error running %s"), command);
526 char *secondary = g_strdup_printf(_("Process returned error code %d"),
527 exit_status);
528 purple_notify_error(dialog, NULL, primary, secondary);
529 g_free(tmp);
532 #endif
535 static void
536 remove_button_cb(GtkButton *button, PidginXferDialog *dialog)
538 pidgin_xfer_dialog_remove_xfer(dialog, dialog->selected_xfer);
541 static void
542 stop_button_cb(GtkButton *button, PidginXferDialog *dialog)
544 purple_xfer_cancel_local(dialog->selected_xfer);
547 static void
548 close_button_cb(GtkButton *button, PidginXferDialog *dialog)
550 pidgin_xfer_dialog_hide(dialog);
554 /**************************************************************************
555 * Dialog Building Functions
556 **************************************************************************/
557 static GtkWidget *
558 setup_tree(PidginXferDialog *dialog)
560 GtkWidget *sw;
561 GtkWidget *tree;
562 GtkListStore *model;
563 GtkCellRenderer *renderer;
564 GtkTreeViewColumn *column;
565 GtkTreeSelection *selection;
567 /* Create the scrolled window. */
568 sw = gtk_scrolled_window_new(0, 0);
569 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
570 GTK_SHADOW_IN);
571 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
572 GTK_POLICY_AUTOMATIC,
573 GTK_POLICY_AUTOMATIC);
574 gtk_widget_show(sw);
576 /* Build the tree model */
577 /* Transfer type, Progress Bar, Filename, Size, Remaining */
578 model = gtk_list_store_new(NUM_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_INT,
579 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
580 G_TYPE_POINTER);
581 dialog->model = model;
583 /* Create the treeview */
584 dialog->tree = tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
585 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
586 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
587 /* gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE); */
589 gtk_widget_show(tree);
591 g_signal_connect(G_OBJECT(selection), "changed",
592 G_CALLBACK(selection_changed_cb), dialog);
594 g_object_unref(G_OBJECT(model));
597 /* Columns */
599 /* Transfer Type column */
600 renderer = gtk_cell_renderer_pixbuf_new();
601 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
602 "pixbuf", COLUMN_STATUS, NULL);
603 gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(column),
604 GTK_TREE_VIEW_COLUMN_FIXED);
605 gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(column), 25);
606 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
607 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
609 /* Progress bar column */
610 renderer = gtk_cell_renderer_progress_new();
611 column = gtk_tree_view_column_new_with_attributes(_("Progress"), renderer,
612 "value", COLUMN_PROGRESS, NULL);
613 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
614 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
616 /* Filename column */
617 renderer = gtk_cell_renderer_text_new();
618 column = gtk_tree_view_column_new_with_attributes(_("Filename"), renderer,
619 "text", COLUMN_FILENAME, NULL);
620 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
621 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
623 /* File Size column */
624 renderer = gtk_cell_renderer_text_new();
625 column = gtk_tree_view_column_new_with_attributes(_("Size"), renderer,
626 "text", COLUMN_SIZE, NULL);
627 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
628 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
630 /* Bytes Remaining column */
631 renderer = gtk_cell_renderer_text_new();
632 column = gtk_tree_view_column_new_with_attributes(_("Remaining"),
633 renderer, "text", COLUMN_REMAINING, NULL);
634 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
635 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
637 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(tree));
639 gtk_container_add(GTK_CONTAINER(sw), tree);
640 gtk_widget_show(tree);
642 return sw;
645 static GtkWidget *
646 make_info_table(PidginXferDialog *dialog)
648 GtkWidget *table;
649 GtkWidget *label;
650 int i;
652 struct
654 GtkWidget **desc_label;
655 GtkWidget **val_label;
656 const char *desc;
658 } labels[] =
660 { &dialog->local_user_desc_label, &dialog->local_user_label, NULL },
661 { &dialog->remote_user_desc_label, &dialog->remote_user_label, NULL },
662 { &label, &dialog->protocol_label, _("Protocol:") },
663 { &label, &dialog->filename_label, _("Filename:") },
664 { &label, &dialog->localfile_label, _("Local File:") },
665 { &label, &dialog->status_label, _("Status:") },
666 { &label, &dialog->speed_label, _("Speed:") },
667 { &label, &dialog->time_elapsed_label, _("Time Elapsed:") },
668 { &label, &dialog->time_remaining_label, _("Time Remaining:") }
671 /* Setup the initial table */
672 dialog->table = table = gtk_table_new(G_N_ELEMENTS(labels) + 1, 2, FALSE);
673 gtk_table_set_row_spacings(GTK_TABLE(table), PIDGIN_HIG_BOX_SPACE);
674 gtk_table_set_col_spacings(GTK_TABLE(table), PIDGIN_HIG_BOX_SPACE);
676 /* Setup the labels */
677 for (i = 0; i < G_N_ELEMENTS(labels); i++) {
678 GtkWidget *label;
679 char buf[256];
681 g_snprintf(buf, sizeof(buf), "<b>%s</b>",
682 labels[i].desc != NULL ? labels[i].desc : "");
684 *labels[i].desc_label = label = gtk_label_new(NULL);
685 gtk_label_set_markup(GTK_LABEL(label), buf);
686 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
687 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
688 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, i + 1,
689 GTK_FILL, 0, 0, 0);
690 gtk_widget_show(label);
692 *labels[i].val_label = label = gtk_label_new(NULL);
693 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
694 gtk_table_attach(GTK_TABLE(table), label, 1, 2, i, i + 1,
695 GTK_FILL | GTK_EXPAND, 0, 0, 0);
696 gtk_widget_show(label);
699 /* Setup the progress bar */
700 dialog->progress = gtk_progress_bar_new();
701 gtk_table_attach(GTK_TABLE(table), dialog->progress,
702 0, 2,
703 G_N_ELEMENTS(labels), G_N_ELEMENTS(labels) + 1,
704 GTK_FILL, GTK_FILL, 0, 0);
705 gtk_widget_show(dialog->progress);
707 return table;
710 PidginXferDialog *
711 pidgin_xfer_dialog_new(void)
713 PidginXferDialog *dialog;
714 GtkWidget *window;
715 GtkWidget *vbox1, *vbox2;
716 GtkWidget *sw;
717 GtkWidget *expander;
718 GtkWidget *alignment;
719 GtkWidget *table;
720 GtkWidget *checkbox;
721 GtkWidget *bbox;
723 dialog = g_new0(PidginXferDialog, 1);
724 dialog->keep_open =
725 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/filetransfer/keep_open");
726 dialog->auto_clear =
727 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/filetransfer/clear_finished");
729 /* Create the window. */
730 dialog->window = window = pidgin_create_window(_("File Transfers"), PIDGIN_HIG_BORDER, "file transfer", TRUE);
731 gtk_window_set_default_size(GTK_WINDOW(window), 450, 250);
733 g_signal_connect(G_OBJECT(window), "delete_event",
734 G_CALLBACK(delete_win_cb), dialog);
736 /* Create the parent vbox for everything. */
737 vbox1 = gtk_vbox_new(FALSE, 0);
738 gtk_widget_show(vbox1);
739 gtk_container_add(GTK_CONTAINER(window), vbox1);
741 /* Create the main vbox for top half of the window. */
742 vbox2 = gtk_vbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
743 gtk_box_pack_start(GTK_BOX(vbox1), vbox2, TRUE, TRUE, 0);
744 gtk_widget_show(vbox2);
746 /* Setup the listbox */
747 sw = setup_tree(dialog);
748 gtk_box_pack_start(GTK_BOX(vbox2), sw, TRUE, TRUE, 0);
749 gtk_widget_set_size_request(sw,-1, 140);
751 /* "Close this window when all transfers finish" */
752 checkbox = gtk_check_button_new_with_mnemonic(
753 _("Close this window when all transfers _finish"));
754 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox),
755 !dialog->keep_open);
756 g_signal_connect(G_OBJECT(checkbox), "toggled",
757 G_CALLBACK(toggle_keep_open_cb), dialog);
758 gtk_box_pack_start(GTK_BOX(vbox2), checkbox, FALSE, FALSE, 0);
759 gtk_widget_show(checkbox);
761 /* "Clear finished transfers" */
762 checkbox = gtk_check_button_new_with_mnemonic(
763 _("C_lear finished transfers"));
764 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox),
765 dialog->auto_clear);
766 g_signal_connect(G_OBJECT(checkbox), "toggled",
767 G_CALLBACK(toggle_clear_finished_cb), dialog);
768 gtk_box_pack_start(GTK_BOX(vbox2), checkbox, FALSE, FALSE, 0);
769 gtk_widget_show(checkbox);
771 /* "Download Details" arrow */
772 expander = gtk_expander_new_with_mnemonic(_("File transfer _details"));
773 dialog->expander = expander;
774 gtk_box_pack_start(GTK_BOX(vbox2), expander, FALSE, FALSE, 0);
775 gtk_widget_show(expander);
777 gtk_widget_set_sensitive(expander, FALSE);
779 /* Small indent make table 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 table of information. */
786 table = make_info_table(dialog);
787 gtk_container_add(GTK_CONTAINER(alignment), table);
788 gtk_widget_show(table);
790 bbox = gtk_hbutton_box_new();
791 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
792 gtk_box_set_spacing(GTK_BOX(bbox), PIDGIN_HIG_BOX_SPACE);
793 gtk_box_pack_end(GTK_BOX(vbox1), bbox, FALSE, TRUE, 0);
794 gtk_widget_show(bbox);
796 #define ADD_BUTTON(b, label, callback, callbackdata) do { \
797 GtkWidget *button = gtk_button_new_from_stock(label); \
798 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0); \
799 g_signal_connect(G_OBJECT(button), "clicked", callback, callbackdata); \
800 gtk_widget_show(button); \
801 b = button; \
802 } while (0)
804 /* Open button */
805 ADD_BUTTON(dialog->open_button, GTK_STOCK_OPEN, G_CALLBACK(open_button_cb), dialog);
806 gtk_widget_set_sensitive(dialog->open_button, FALSE);
808 /* Remove button */
809 ADD_BUTTON(dialog->remove_button, GTK_STOCK_REMOVE, G_CALLBACK(remove_button_cb), dialog);
810 gtk_widget_hide(dialog->remove_button);
812 /* Stop button */
813 ADD_BUTTON(dialog->stop_button, GTK_STOCK_STOP, G_CALLBACK(stop_button_cb), dialog);
814 gtk_widget_set_sensitive(dialog->stop_button, FALSE);
816 /* Close button */
817 ADD_BUTTON(dialog->close_button, GTK_STOCK_CLOSE, G_CALLBACK(close_button_cb), dialog);
819 #undef ADD_BUTTON
821 #ifdef _WIN32
822 g_signal_connect(G_OBJECT(dialog->window), "show",
823 G_CALLBACK(winpidgin_ensure_onscreen), dialog->window);
824 #endif
826 return dialog;
829 void
830 pidgin_xfer_dialog_destroy(PidginXferDialog *dialog)
832 g_return_if_fail(dialog != NULL);
834 purple_notify_close_with_handle(dialog);
836 gtk_widget_destroy(dialog->window);
838 g_free(dialog);
841 void
842 pidgin_xfer_dialog_show(PidginXferDialog *dialog)
844 PidginXferDialog *tmp;
846 if (dialog == NULL) {
847 tmp = pidgin_get_xfer_dialog();
849 if (tmp == NULL) {
850 tmp = pidgin_xfer_dialog_new();
851 pidgin_set_xfer_dialog(tmp);
854 gtk_widget_show(tmp->window);
855 } else {
856 gtk_window_present(GTK_WINDOW(dialog->window));
860 void
861 pidgin_xfer_dialog_hide(PidginXferDialog *dialog)
863 g_return_if_fail(dialog != NULL);
865 purple_notify_close_with_handle(dialog);
867 gtk_widget_hide(dialog->window);
870 void
871 pidgin_xfer_dialog_add_xfer(PidginXferDialog *dialog, PurpleXfer *xfer)
873 PidginXferUiData *data;
874 PurpleXferType type;
875 GdkPixbuf *pixbuf;
876 char *size_str, *remaining_str;
877 char *lfilename, *utf8;
879 g_return_if_fail(dialog != NULL);
880 g_return_if_fail(xfer != NULL);
882 purple_xfer_ref(xfer);
884 data = PIDGINXFER(xfer);
885 data->in_list = TRUE;
887 pidgin_xfer_dialog_show(dialog);
889 data->last_updated_time = 0;
891 type = purple_xfer_get_type(xfer);
893 size_str = purple_str_size_to_units(purple_xfer_get_size(xfer));
894 remaining_str = purple_str_size_to_units(purple_xfer_get_bytes_remaining(xfer));
896 pixbuf = gtk_widget_render_icon(dialog->window,
897 (type == PURPLE_XFER_RECEIVE
898 ? PIDGIN_STOCK_DOWNLOAD
899 : PIDGIN_STOCK_UPLOAD),
900 GTK_ICON_SIZE_MENU, NULL);
902 gtk_list_store_append(dialog->model, &data->iter);
903 lfilename = g_path_get_basename(purple_xfer_get_local_filename(xfer));
904 utf8 = g_filename_to_utf8(lfilename, -1, NULL, NULL, NULL);
905 g_free(lfilename);
906 lfilename = utf8;
907 gtk_list_store_set(dialog->model, &data->iter,
908 COLUMN_STATUS, pixbuf,
909 COLUMN_PROGRESS, 0,
910 COLUMN_FILENAME, (type == PURPLE_XFER_RECEIVE)
911 ? purple_xfer_get_filename(xfer)
912 : lfilename,
913 COLUMN_SIZE, size_str,
914 COLUMN_REMAINING, _("Waiting for transfer to begin"),
915 COLUMN_DATA, xfer,
916 -1);
917 g_free(lfilename);
919 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dialog->tree));
921 g_object_unref(pixbuf);
923 g_free(size_str);
924 g_free(remaining_str);
926 dialog->num_transfers++;
928 ensure_row_selected(dialog);
929 update_title_progress(dialog);
932 void
933 pidgin_xfer_dialog_remove_xfer(PidginXferDialog *dialog,
934 PurpleXfer *xfer)
936 PidginXferUiData *data;
938 g_return_if_fail(dialog != NULL);
939 g_return_if_fail(xfer != NULL);
941 data = PIDGINXFER(xfer);
943 if (data == NULL)
944 return;
946 if (!data->in_list)
947 return;
949 data->in_list = FALSE;
951 gtk_list_store_remove(GTK_LIST_STORE(dialog->model), &data->iter);
953 dialog->num_transfers--;
955 ensure_row_selected(dialog);
957 update_title_progress(dialog);
958 purple_xfer_unref(xfer);
961 void
962 pidgin_xfer_dialog_cancel_xfer(PidginXferDialog *dialog,
963 PurpleXfer *xfer)
965 PidginXferUiData *data;
966 GdkPixbuf *pixbuf;
967 const gchar *status;
969 g_return_if_fail(dialog != NULL);
970 g_return_if_fail(xfer != NULL);
972 data = PIDGINXFER(xfer);
974 if (data == NULL)
975 return;
977 if (!data->in_list)
978 return;
980 if ((purple_xfer_get_status(xfer) == PURPLE_XFER_STATUS_CANCEL_LOCAL) && (dialog->auto_clear)) {
981 pidgin_xfer_dialog_remove_xfer(dialog, xfer);
982 return;
985 data = PIDGINXFER(xfer);
987 update_detailed_info(dialog, xfer);
988 update_title_progress(dialog);
990 pixbuf = gtk_widget_render_icon(dialog->window,
991 PIDGIN_STOCK_FILE_CANCELED,
992 GTK_ICON_SIZE_MENU, NULL);
994 if (purple_xfer_is_canceled(xfer))
995 status = _("Cancelled");
996 else
997 status = _("Failed");
999 gtk_list_store_set(dialog->model, &data->iter,
1000 COLUMN_STATUS, pixbuf,
1001 COLUMN_REMAINING, status,
1002 -1);
1004 g_object_unref(pixbuf);
1006 update_buttons(dialog, xfer);
1009 void
1010 pidgin_xfer_dialog_update_xfer(PidginXferDialog *dialog,
1011 PurpleXfer *xfer)
1013 PidginXferUiData *data;
1014 char *size_str, *remaining_str;
1015 time_t current_time;
1016 GtkTreeIter iter;
1017 gboolean valid;
1019 g_return_if_fail(dialog != NULL);
1020 g_return_if_fail(xfer != NULL);
1022 if ((data = PIDGINXFER(xfer)) == NULL)
1023 return;
1025 if (data->in_list == FALSE)
1026 return;
1028 current_time = time(NULL);
1029 if (((current_time - data->last_updated_time) == 0) &&
1030 (!purple_xfer_is_completed(xfer)))
1032 /* Don't update the window more than once per second */
1033 return;
1035 data->last_updated_time = current_time;
1037 size_str = purple_str_size_to_units(purple_xfer_get_size(xfer));
1038 remaining_str = purple_str_size_to_units(purple_xfer_get_bytes_remaining(xfer));
1040 gtk_list_store_set(xfer_dialog->model, &data->iter,
1041 COLUMN_PROGRESS, (gint)(purple_xfer_get_progress(xfer) * 100),
1042 COLUMN_SIZE, size_str,
1043 COLUMN_REMAINING, remaining_str,
1044 -1);
1046 g_free(size_str);
1047 g_free(remaining_str);
1049 if (purple_xfer_is_completed(xfer))
1051 GdkPixbuf *pixbuf;
1053 pixbuf = gtk_widget_render_icon(dialog->window,
1054 PIDGIN_STOCK_FILE_DONE,
1055 GTK_ICON_SIZE_MENU, NULL);
1057 gtk_list_store_set(GTK_LIST_STORE(xfer_dialog->model), &data->iter,
1058 COLUMN_STATUS, pixbuf,
1059 COLUMN_REMAINING, _("Finished"),
1060 -1);
1062 g_object_unref(pixbuf);
1065 update_title_progress(dialog);
1066 if (xfer == dialog->selected_xfer)
1067 update_detailed_info(xfer_dialog, xfer);
1069 if (purple_xfer_is_completed(xfer) && dialog->auto_clear)
1070 pidgin_xfer_dialog_remove_xfer(dialog, xfer);
1071 else
1072 update_buttons(dialog, xfer);
1075 * If all transfers are finished, and the pref is set, then
1076 * close the dialog. Otherwise just exit this function.
1078 if (dialog->keep_open)
1079 return;
1081 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog->model), &iter);
1082 while (valid)
1084 GValue val;
1085 PurpleXfer *next;
1087 val.g_type = 0;
1088 gtk_tree_model_get_value(GTK_TREE_MODEL(dialog->model),
1089 &iter, COLUMN_DATA, &val);
1091 next = g_value_get_pointer(&val);
1092 if (!purple_xfer_is_completed(next))
1093 return;
1095 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(dialog->model), &iter);
1098 /* If we got to this point then we know everything is finished */
1099 pidgin_xfer_dialog_hide(dialog);
1102 /**************************************************************************
1103 * File Transfer UI Ops
1104 **************************************************************************/
1105 static void
1106 pidgin_xfer_new_xfer(PurpleXfer *xfer)
1108 PidginXferUiData *data;
1110 /* This is where we're setting xfer->ui_data for the first time. */
1111 data = g_new0(PidginXferUiData, 1);
1112 xfer->ui_data = data;
1115 static void
1116 pidgin_xfer_destroy(PurpleXfer *xfer)
1118 PidginXferUiData *data;
1120 data = PIDGINXFER(xfer);
1121 if (data) {
1122 g_free(data->name);
1123 g_free(data);
1124 xfer->ui_data = NULL;
1128 static void
1129 pidgin_xfer_add_xfer(PurpleXfer *xfer)
1131 if (xfer_dialog == NULL)
1132 xfer_dialog = pidgin_xfer_dialog_new();
1134 pidgin_xfer_dialog_add_xfer(xfer_dialog, xfer);
1137 static void
1138 pidgin_xfer_update_progress(PurpleXfer *xfer, double percent)
1140 pidgin_xfer_dialog_update_xfer(xfer_dialog, xfer);
1143 static void
1144 pidgin_xfer_cancel_local(PurpleXfer *xfer)
1146 if (xfer_dialog)
1147 pidgin_xfer_dialog_cancel_xfer(xfer_dialog, xfer);
1150 static void
1151 pidgin_xfer_cancel_remote(PurpleXfer *xfer)
1153 if (xfer_dialog)
1154 pidgin_xfer_dialog_cancel_xfer(xfer_dialog, xfer);
1157 static void
1158 pidgin_xfer_add_thumbnail(PurpleXfer *xfer, const gchar *formats)
1160 purple_debug_info("ft", "creating thumbnail for transfer\n");
1162 if (purple_xfer_get_size(xfer) <= PIDGIN_XFER_MAX_SIZE_IMAGE_THUMBNAIL) {
1163 GdkPixbuf *thumbnail =
1164 gdk_pixbuf_new_from_file_at_size(
1165 purple_xfer_get_local_filename(xfer), 128, 128, NULL);
1167 if (thumbnail) {
1168 gchar **formats_split = g_strsplit(formats, ",", 0);
1169 gchar *buffer = NULL;
1170 gsize size;
1171 char *option_keys[2] = {NULL, NULL};
1172 char *option_values[2] = {NULL, NULL};
1173 int i;
1174 gchar *format = NULL;
1176 for (i = 0; formats_split[i]; i++) {
1177 if (purple_strequal(formats_split[i], "jpeg")) {
1178 purple_debug_info("ft", "creating JPEG thumbnail\n");
1179 option_keys[0] = "quality";
1180 option_values[0] = "90";
1181 format = "jpeg";
1182 break;
1183 } else if (purple_strequal(formats_split[i], "png")) {
1184 purple_debug_info("ft", "creating PNG thumbnail\n");
1185 option_keys[0] = "compression";
1186 option_values[0] = "9";
1187 format = "png";
1188 break;
1192 /* Try the first format given by the PRPL without options */
1193 if (format == NULL) {
1194 purple_debug_info("ft",
1195 "creating thumbnail of format %s as demanded by PRPL\n",
1196 formats_split[0]);
1197 format = formats_split[0];
1200 gdk_pixbuf_save_to_bufferv(thumbnail, &buffer, &size, format,
1201 option_keys, option_values, NULL);
1203 if (buffer) {
1204 gchar *mimetype = g_strdup_printf("image/%s", format);
1205 purple_debug_info("ft",
1206 "created thumbnail of %" G_GSIZE_FORMAT " bytes\n",
1207 size);
1208 purple_xfer_set_thumbnail(xfer, buffer, size, mimetype);
1209 g_free(buffer);
1210 g_free(mimetype);
1212 g_object_unref(thumbnail);
1213 g_strfreev(formats_split);
1218 static PurpleXferUiOps ops =
1220 pidgin_xfer_new_xfer,
1221 pidgin_xfer_destroy,
1222 pidgin_xfer_add_xfer,
1223 pidgin_xfer_update_progress,
1224 pidgin_xfer_cancel_local,
1225 pidgin_xfer_cancel_remote,
1226 NULL,
1227 NULL,
1228 NULL,
1229 pidgin_xfer_add_thumbnail
1232 /**************************************************************************
1233 * GTK+ File Transfer API
1234 **************************************************************************/
1235 void
1236 pidgin_xfers_init(void)
1238 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/filetransfer");
1239 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/filetransfer/clear_finished", TRUE);
1240 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/filetransfer/keep_open", FALSE);
1243 void
1244 pidgin_xfers_uninit(void)
1246 if (xfer_dialog != NULL)
1247 pidgin_xfer_dialog_destroy(xfer_dialog);
1250 void
1251 pidgin_set_xfer_dialog(PidginXferDialog *dialog)
1253 xfer_dialog = dialog;
1256 PidginXferDialog *
1257 pidgin_get_xfer_dialog(void)
1259 return xfer_dialog;
1262 PurpleXferUiOps *
1263 pidgin_xfers_get_ui_ops(void)
1265 return &ops;