I guess I'll release this later today. Is that ok with everyone?
[pidgin-git.git] / pidgin / gtkft.c
blob4c1bc658eab25b5710457682425d59681c04724f
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 *tree;
561 GtkListStore *model;
562 GtkCellRenderer *renderer;
563 GtkTreeViewColumn *column;
564 GtkTreeSelection *selection;
566 /* Build the tree model */
567 /* Transfer type, Progress Bar, Filename, Size, Remaining */
568 model = gtk_list_store_new(NUM_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_INT,
569 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
570 G_TYPE_POINTER);
571 dialog->model = model;
573 /* Create the treeview */
574 dialog->tree = tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
575 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
576 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
577 /* gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE); */
579 gtk_widget_show(tree);
581 g_signal_connect(G_OBJECT(selection), "changed",
582 G_CALLBACK(selection_changed_cb), dialog);
584 g_object_unref(G_OBJECT(model));
587 /* Columns */
589 /* Transfer Type column */
590 renderer = gtk_cell_renderer_pixbuf_new();
591 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
592 "pixbuf", COLUMN_STATUS, NULL);
593 gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(column),
594 GTK_TREE_VIEW_COLUMN_FIXED);
595 gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(column), 25);
596 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
597 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
599 /* Progress bar column */
600 renderer = gtk_cell_renderer_progress_new();
601 column = gtk_tree_view_column_new_with_attributes(_("Progress"), renderer,
602 "value", COLUMN_PROGRESS, NULL);
603 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
604 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
606 /* Filename column */
607 renderer = gtk_cell_renderer_text_new();
608 column = gtk_tree_view_column_new_with_attributes(_("Filename"), renderer,
609 "text", COLUMN_FILENAME, NULL);
610 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
611 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
613 /* File Size column */
614 renderer = gtk_cell_renderer_text_new();
615 column = gtk_tree_view_column_new_with_attributes(_("Size"), renderer,
616 "text", COLUMN_SIZE, NULL);
617 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
618 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
620 /* Bytes Remaining column */
621 renderer = gtk_cell_renderer_text_new();
622 column = gtk_tree_view_column_new_with_attributes(_("Remaining"),
623 renderer, "text", COLUMN_REMAINING, NULL);
624 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
625 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
627 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(tree));
629 gtk_widget_show(tree);
631 return tree;
634 static GtkWidget *
635 make_info_table(PidginXferDialog *dialog)
637 GtkWidget *table;
638 GtkWidget *label;
639 int i;
641 struct
643 GtkWidget **desc_label;
644 GtkWidget **val_label;
645 const char *desc;
647 } labels[] =
649 { &dialog->local_user_desc_label, &dialog->local_user_label, NULL },
650 { &dialog->remote_user_desc_label, &dialog->remote_user_label, NULL },
651 { &label, &dialog->protocol_label, _("Protocol:") },
652 { &label, &dialog->filename_label, _("Filename:") },
653 { &label, &dialog->localfile_label, _("Local File:") },
654 { &label, &dialog->status_label, _("Status:") },
655 { &label, &dialog->speed_label, _("Speed:") },
656 { &label, &dialog->time_elapsed_label, _("Time Elapsed:") },
657 { &label, &dialog->time_remaining_label, _("Time Remaining:") }
660 /* Setup the initial table */
661 dialog->table = table = gtk_table_new(G_N_ELEMENTS(labels) + 1, 2, FALSE);
662 gtk_table_set_row_spacings(GTK_TABLE(table), PIDGIN_HIG_BOX_SPACE);
663 gtk_table_set_col_spacings(GTK_TABLE(table), PIDGIN_HIG_BOX_SPACE);
665 /* Setup the labels */
666 for (i = 0; i < G_N_ELEMENTS(labels); i++) {
667 GtkWidget *label;
668 char buf[256];
670 g_snprintf(buf, sizeof(buf), "<b>%s</b>",
671 labels[i].desc != NULL ? labels[i].desc : "");
673 *labels[i].desc_label = label = gtk_label_new(NULL);
674 gtk_label_set_markup(GTK_LABEL(label), buf);
675 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
676 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
677 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, i + 1,
678 GTK_FILL, 0, 0, 0);
679 gtk_widget_show(label);
681 *labels[i].val_label = label = gtk_label_new(NULL);
682 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
683 gtk_table_attach(GTK_TABLE(table), label, 1, 2, i, i + 1,
684 GTK_FILL | GTK_EXPAND, 0, 0, 0);
685 gtk_widget_show(label);
688 /* Setup the progress bar */
689 dialog->progress = gtk_progress_bar_new();
690 gtk_table_attach(GTK_TABLE(table), dialog->progress,
691 0, 2,
692 G_N_ELEMENTS(labels), G_N_ELEMENTS(labels) + 1,
693 GTK_FILL, GTK_FILL, 0, 0);
694 gtk_widget_show(dialog->progress);
696 return table;
699 PidginXferDialog *
700 pidgin_xfer_dialog_new(void)
702 PidginXferDialog *dialog;
703 GtkWidget *window;
704 GtkWidget *vbox1, *vbox2;
705 GtkWidget *expander;
706 GtkWidget *alignment;
707 GtkWidget *table;
708 GtkWidget *checkbox;
709 GtkWidget *bbox;
711 dialog = g_new0(PidginXferDialog, 1);
712 dialog->keep_open =
713 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/filetransfer/keep_open");
714 dialog->auto_clear =
715 purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/filetransfer/clear_finished");
717 /* Create the window. */
718 dialog->window = window = pidgin_create_window(_("File Transfers"), PIDGIN_HIG_BORDER, "file transfer", TRUE);
719 gtk_window_set_default_size(GTK_WINDOW(window), 450, 250);
721 g_signal_connect(G_OBJECT(window), "delete_event",
722 G_CALLBACK(delete_win_cb), dialog);
724 /* Create the parent vbox for everything. */
725 vbox1 = gtk_vbox_new(FALSE, 0);
726 gtk_widget_show(vbox1);
727 gtk_container_add(GTK_CONTAINER(window), vbox1);
729 /* Create the main vbox for top half of the window. */
730 vbox2 = gtk_vbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
731 gtk_box_pack_start(GTK_BOX(vbox1), vbox2, TRUE, TRUE, 0);
732 gtk_widget_show(vbox2);
734 /* Setup the listbox */
735 gtk_box_pack_start(GTK_BOX(vbox2),
736 pidgin_make_scrollable(setup_tree(dialog), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC, GTK_SHADOW_IN, -1, 140),
737 TRUE, TRUE, 0);
739 /* "Close this window when all transfers finish" */
740 checkbox = gtk_check_button_new_with_mnemonic(
741 _("Close this window when all transfers _finish"));
742 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox),
743 !dialog->keep_open);
744 g_signal_connect(G_OBJECT(checkbox), "toggled",
745 G_CALLBACK(toggle_keep_open_cb), dialog);
746 gtk_box_pack_start(GTK_BOX(vbox2), checkbox, FALSE, FALSE, 0);
747 gtk_widget_show(checkbox);
749 /* "Clear finished transfers" */
750 checkbox = gtk_check_button_new_with_mnemonic(
751 _("C_lear finished transfers"));
752 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox),
753 dialog->auto_clear);
754 g_signal_connect(G_OBJECT(checkbox), "toggled",
755 G_CALLBACK(toggle_clear_finished_cb), dialog);
756 gtk_box_pack_start(GTK_BOX(vbox2), checkbox, FALSE, FALSE, 0);
757 gtk_widget_show(checkbox);
759 /* "Download Details" arrow */
760 expander = gtk_expander_new_with_mnemonic(_("File transfer _details"));
761 dialog->expander = expander;
762 gtk_box_pack_start(GTK_BOX(vbox2), expander, FALSE, FALSE, 0);
763 gtk_widget_show(expander);
765 gtk_widget_set_sensitive(expander, FALSE);
767 /* Small indent make table fall under GtkExpander's label */
768 alignment = gtk_alignment_new(1, 0, 1, 1);
769 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), 0, 0, 20, 0);
770 gtk_container_add(GTK_CONTAINER(expander), alignment);
771 gtk_widget_show(alignment);
773 /* The table of information. */
774 table = make_info_table(dialog);
775 gtk_container_add(GTK_CONTAINER(alignment), table);
776 gtk_widget_show(table);
778 bbox = gtk_hbutton_box_new();
779 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
780 gtk_box_set_spacing(GTK_BOX(bbox), PIDGIN_HIG_BOX_SPACE);
781 gtk_box_pack_end(GTK_BOX(vbox1), bbox, FALSE, TRUE, 0);
782 gtk_widget_show(bbox);
784 #define ADD_BUTTON(b, label, callback, callbackdata) do { \
785 GtkWidget *button = gtk_button_new_from_stock(label); \
786 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0); \
787 g_signal_connect(G_OBJECT(button), "clicked", callback, callbackdata); \
788 gtk_widget_show(button); \
789 b = button; \
790 } while (0)
792 /* Open button */
793 ADD_BUTTON(dialog->open_button, GTK_STOCK_OPEN, G_CALLBACK(open_button_cb), dialog);
794 gtk_widget_set_sensitive(dialog->open_button, FALSE);
796 /* Remove button */
797 ADD_BUTTON(dialog->remove_button, GTK_STOCK_REMOVE, G_CALLBACK(remove_button_cb), dialog);
798 gtk_widget_hide(dialog->remove_button);
800 /* Stop button */
801 ADD_BUTTON(dialog->stop_button, GTK_STOCK_STOP, G_CALLBACK(stop_button_cb), dialog);
802 gtk_widget_set_sensitive(dialog->stop_button, FALSE);
804 /* Close button */
805 ADD_BUTTON(dialog->close_button, GTK_STOCK_CLOSE, G_CALLBACK(close_button_cb), dialog);
807 #undef ADD_BUTTON
809 #ifdef _WIN32
810 g_signal_connect(G_OBJECT(dialog->window), "show",
811 G_CALLBACK(winpidgin_ensure_onscreen), dialog->window);
812 #endif
814 return dialog;
817 void
818 pidgin_xfer_dialog_destroy(PidginXferDialog *dialog)
820 g_return_if_fail(dialog != NULL);
822 purple_notify_close_with_handle(dialog);
824 gtk_widget_destroy(dialog->window);
826 g_free(dialog);
829 void
830 pidgin_xfer_dialog_show(PidginXferDialog *dialog)
832 PidginXferDialog *tmp;
834 if (dialog == NULL) {
835 tmp = pidgin_get_xfer_dialog();
837 if (tmp == NULL) {
838 tmp = pidgin_xfer_dialog_new();
839 pidgin_set_xfer_dialog(tmp);
842 gtk_widget_show(tmp->window);
843 } else {
844 gtk_window_present(GTK_WINDOW(dialog->window));
848 void
849 pidgin_xfer_dialog_hide(PidginXferDialog *dialog)
851 g_return_if_fail(dialog != NULL);
853 purple_notify_close_with_handle(dialog);
855 gtk_widget_hide(dialog->window);
858 void
859 pidgin_xfer_dialog_add_xfer(PidginXferDialog *dialog, PurpleXfer *xfer)
861 PidginXferUiData *data;
862 PurpleXferType type;
863 GdkPixbuf *pixbuf;
864 char *size_str, *remaining_str;
865 char *lfilename, *utf8;
867 g_return_if_fail(dialog != NULL);
868 g_return_if_fail(xfer != NULL);
870 purple_xfer_ref(xfer);
872 data = PIDGINXFER(xfer);
873 data->in_list = TRUE;
875 pidgin_xfer_dialog_show(dialog);
877 data->last_updated_time = 0;
879 type = purple_xfer_get_type(xfer);
881 size_str = purple_str_size_to_units(purple_xfer_get_size(xfer));
882 remaining_str = purple_str_size_to_units(purple_xfer_get_bytes_remaining(xfer));
884 pixbuf = gtk_widget_render_icon(dialog->window,
885 (type == PURPLE_XFER_RECEIVE
886 ? PIDGIN_STOCK_DOWNLOAD
887 : PIDGIN_STOCK_UPLOAD),
888 GTK_ICON_SIZE_MENU, NULL);
890 gtk_list_store_append(dialog->model, &data->iter);
891 lfilename = g_path_get_basename(purple_xfer_get_local_filename(xfer));
892 utf8 = g_filename_to_utf8(lfilename, -1, NULL, NULL, NULL);
893 g_free(lfilename);
894 lfilename = utf8;
895 gtk_list_store_set(dialog->model, &data->iter,
896 COLUMN_STATUS, pixbuf,
897 COLUMN_PROGRESS, 0,
898 COLUMN_FILENAME, (type == PURPLE_XFER_RECEIVE)
899 ? purple_xfer_get_filename(xfer)
900 : lfilename,
901 COLUMN_SIZE, size_str,
902 COLUMN_REMAINING, _("Waiting for transfer to begin"),
903 COLUMN_DATA, xfer,
904 -1);
905 g_free(lfilename);
907 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dialog->tree));
909 g_object_unref(pixbuf);
911 g_free(size_str);
912 g_free(remaining_str);
914 dialog->num_transfers++;
916 ensure_row_selected(dialog);
917 update_title_progress(dialog);
920 void
921 pidgin_xfer_dialog_remove_xfer(PidginXferDialog *dialog,
922 PurpleXfer *xfer)
924 PidginXferUiData *data;
926 g_return_if_fail(dialog != NULL);
927 g_return_if_fail(xfer != NULL);
929 data = PIDGINXFER(xfer);
931 if (data == NULL)
932 return;
934 if (!data->in_list)
935 return;
937 data->in_list = FALSE;
939 gtk_list_store_remove(GTK_LIST_STORE(dialog->model), &data->iter);
941 dialog->num_transfers--;
943 ensure_row_selected(dialog);
945 update_title_progress(dialog);
946 purple_xfer_unref(xfer);
949 void
950 pidgin_xfer_dialog_cancel_xfer(PidginXferDialog *dialog,
951 PurpleXfer *xfer)
953 PidginXferUiData *data;
954 GdkPixbuf *pixbuf;
955 const gchar *status;
957 g_return_if_fail(dialog != NULL);
958 g_return_if_fail(xfer != NULL);
960 data = PIDGINXFER(xfer);
962 if (data == NULL)
963 return;
965 if (!data->in_list)
966 return;
968 if ((purple_xfer_get_status(xfer) == PURPLE_XFER_STATUS_CANCEL_LOCAL) && (dialog->auto_clear)) {
969 pidgin_xfer_dialog_remove_xfer(dialog, xfer);
970 return;
973 data = PIDGINXFER(xfer);
975 update_detailed_info(dialog, xfer);
976 update_title_progress(dialog);
978 pixbuf = gtk_widget_render_icon(dialog->window,
979 PIDGIN_STOCK_FILE_CANCELED,
980 GTK_ICON_SIZE_MENU, NULL);
982 if (purple_xfer_is_canceled(xfer))
983 status = _("Cancelled");
984 else
985 status = _("Failed");
987 gtk_list_store_set(dialog->model, &data->iter,
988 COLUMN_STATUS, pixbuf,
989 COLUMN_REMAINING, status,
990 -1);
992 g_object_unref(pixbuf);
994 update_buttons(dialog, xfer);
997 void
998 pidgin_xfer_dialog_update_xfer(PidginXferDialog *dialog,
999 PurpleXfer *xfer)
1001 PidginXferUiData *data;
1002 char *size_str, *remaining_str;
1003 time_t current_time;
1004 GtkTreeIter iter;
1005 gboolean valid;
1007 g_return_if_fail(dialog != NULL);
1008 g_return_if_fail(xfer != NULL);
1010 if ((data = PIDGINXFER(xfer)) == NULL)
1011 return;
1013 if (data->in_list == FALSE)
1014 return;
1016 current_time = time(NULL);
1017 if (((current_time - data->last_updated_time) == 0) &&
1018 (!purple_xfer_is_completed(xfer)))
1020 /* Don't update the window more than once per second */
1021 return;
1023 data->last_updated_time = current_time;
1025 size_str = purple_str_size_to_units(purple_xfer_get_size(xfer));
1026 remaining_str = purple_str_size_to_units(purple_xfer_get_bytes_remaining(xfer));
1028 gtk_list_store_set(xfer_dialog->model, &data->iter,
1029 COLUMN_PROGRESS, (gint)(purple_xfer_get_progress(xfer) * 100),
1030 COLUMN_SIZE, size_str,
1031 COLUMN_REMAINING, remaining_str,
1032 -1);
1034 g_free(size_str);
1035 g_free(remaining_str);
1037 if (purple_xfer_is_completed(xfer))
1039 GdkPixbuf *pixbuf;
1041 pixbuf = gtk_widget_render_icon(dialog->window,
1042 PIDGIN_STOCK_FILE_DONE,
1043 GTK_ICON_SIZE_MENU, NULL);
1045 gtk_list_store_set(GTK_LIST_STORE(xfer_dialog->model), &data->iter,
1046 COLUMN_STATUS, pixbuf,
1047 COLUMN_REMAINING, _("Finished"),
1048 -1);
1050 g_object_unref(pixbuf);
1053 update_title_progress(dialog);
1054 if (xfer == dialog->selected_xfer)
1055 update_detailed_info(xfer_dialog, xfer);
1057 if (purple_xfer_is_completed(xfer) && dialog->auto_clear)
1058 pidgin_xfer_dialog_remove_xfer(dialog, xfer);
1059 else
1060 update_buttons(dialog, xfer);
1063 * If all transfers are finished, and the pref is set, then
1064 * close the dialog. Otherwise just exit this function.
1066 if (dialog->keep_open)
1067 return;
1069 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog->model), &iter);
1070 while (valid)
1072 GValue val;
1073 PurpleXfer *next;
1075 val.g_type = 0;
1076 gtk_tree_model_get_value(GTK_TREE_MODEL(dialog->model),
1077 &iter, COLUMN_DATA, &val);
1079 next = g_value_get_pointer(&val);
1080 if (!purple_xfer_is_completed(next))
1081 return;
1083 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(dialog->model), &iter);
1086 /* If we got to this point then we know everything is finished */
1087 pidgin_xfer_dialog_hide(dialog);
1090 /**************************************************************************
1091 * File Transfer UI Ops
1092 **************************************************************************/
1093 static void
1094 pidgin_xfer_new_xfer(PurpleXfer *xfer)
1096 PidginXferUiData *data;
1098 /* This is where we're setting xfer->ui_data for the first time. */
1099 data = g_new0(PidginXferUiData, 1);
1100 xfer->ui_data = data;
1103 static void
1104 pidgin_xfer_destroy(PurpleXfer *xfer)
1106 PidginXferUiData *data;
1108 data = PIDGINXFER(xfer);
1109 if (data) {
1110 g_free(data->name);
1111 g_free(data);
1112 xfer->ui_data = NULL;
1116 static void
1117 pidgin_xfer_add_xfer(PurpleXfer *xfer)
1119 if (xfer_dialog == NULL)
1120 xfer_dialog = pidgin_xfer_dialog_new();
1122 pidgin_xfer_dialog_add_xfer(xfer_dialog, xfer);
1125 static void
1126 pidgin_xfer_update_progress(PurpleXfer *xfer, double percent)
1128 pidgin_xfer_dialog_update_xfer(xfer_dialog, xfer);
1131 static void
1132 pidgin_xfer_cancel_local(PurpleXfer *xfer)
1134 if (xfer_dialog)
1135 pidgin_xfer_dialog_cancel_xfer(xfer_dialog, xfer);
1138 static void
1139 pidgin_xfer_cancel_remote(PurpleXfer *xfer)
1141 if (xfer_dialog)
1142 pidgin_xfer_dialog_cancel_xfer(xfer_dialog, xfer);
1145 static void
1146 pidgin_xfer_add_thumbnail(PurpleXfer *xfer, const gchar *formats)
1148 purple_debug_info("ft", "creating thumbnail for transfer\n");
1150 if (purple_xfer_get_size(xfer) <= PIDGIN_XFER_MAX_SIZE_IMAGE_THUMBNAIL) {
1151 GdkPixbuf *thumbnail =
1152 pidgin_pixbuf_new_from_file_at_size(
1153 purple_xfer_get_local_filename(xfer), 128, 128);
1155 if (thumbnail) {
1156 gchar **formats_split = g_strsplit(formats, ",", 0);
1157 gchar *buffer = NULL;
1158 gsize size;
1159 char *option_keys[2] = {NULL, NULL};
1160 char *option_values[2] = {NULL, NULL};
1161 int i;
1162 gchar *format = NULL;
1164 for (i = 0; formats_split[i]; i++) {
1165 if (purple_strequal(formats_split[i], "jpeg")) {
1166 purple_debug_info("ft", "creating JPEG thumbnail\n");
1167 option_keys[0] = "quality";
1168 option_values[0] = "90";
1169 format = "jpeg";
1170 break;
1171 } else if (purple_strequal(formats_split[i], "png")) {
1172 purple_debug_info("ft", "creating PNG thumbnail\n");
1173 option_keys[0] = "compression";
1174 option_values[0] = "9";
1175 format = "png";
1176 break;
1180 /* Try the first format given by the PRPL without options */
1181 if (format == NULL) {
1182 purple_debug_info("ft",
1183 "creating thumbnail of format %s as demanded by PRPL\n",
1184 formats_split[0]);
1185 format = formats_split[0];
1188 gdk_pixbuf_save_to_bufferv(thumbnail, &buffer, &size, format,
1189 option_keys, option_values, NULL);
1191 if (buffer) {
1192 gchar *mimetype = g_strdup_printf("image/%s", format);
1193 purple_debug_info("ft",
1194 "created thumbnail of %" G_GSIZE_FORMAT " bytes\n",
1195 size);
1196 purple_xfer_set_thumbnail(xfer, buffer, size, mimetype);
1197 g_free(buffer);
1198 g_free(mimetype);
1200 g_object_unref(thumbnail);
1201 g_strfreev(formats_split);
1206 static PurpleXferUiOps ops =
1208 pidgin_xfer_new_xfer,
1209 pidgin_xfer_destroy,
1210 pidgin_xfer_add_xfer,
1211 pidgin_xfer_update_progress,
1212 pidgin_xfer_cancel_local,
1213 pidgin_xfer_cancel_remote,
1214 NULL,
1215 NULL,
1216 NULL,
1217 pidgin_xfer_add_thumbnail
1220 /**************************************************************************
1221 * GTK+ File Transfer API
1222 **************************************************************************/
1223 void
1224 pidgin_xfers_init(void)
1226 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/filetransfer");
1227 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/filetransfer/clear_finished", TRUE);
1228 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/filetransfer/keep_open", FALSE);
1231 void
1232 pidgin_xfers_uninit(void)
1234 if (xfer_dialog != NULL)
1235 pidgin_xfer_dialog_destroy(xfer_dialog);
1238 void
1239 pidgin_set_xfer_dialog(PidginXferDialog *dialog)
1241 xfer_dialog = dialog;
1244 PidginXferDialog *
1245 pidgin_get_xfer_dialog(void)
1247 return xfer_dialog;
1250 PurpleXferUiOps *
1251 pidgin_xfers_get_ui_ops(void)
1253 return &ops;