2 * @file gtkft.c GTK+ File Transfer UI
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
37 #include "pidginstock.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
53 PurpleXfer
*selected_xfer
;
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
;
78 GtkWidget
*open_button
;
79 GtkWidget
*remove_button
;
80 GtkWidget
*stop_button
;
81 GtkWidget
*close_button
;
87 time_t last_updated_time
;
94 static PidginXferDialog
*xfer_dialog
= NULL
;
108 /**************************************************************************
110 **************************************************************************/
112 get_xfer_info_strings(PurpleXfer
*xfer
, char **kbsec
, char **time_elapsed
,
113 char **time_remaining
)
115 double kb_sent
, kb_rem
;
119 if (xfer
->end_time
!= 0)
120 now
= xfer
->end_time
;
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);
130 *kbsec
= g_strdup_printf(_("%.2f KiB/s"), kbps
);
133 if (time_elapsed
!= NULL
)
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
);
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"));
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
);
183 update_title_progress(PidginXferDialog
*dialog
)
187 int num_active_xfers
= 0;
188 guint64 total_bytes_xferred
= 0;
189 guint64 total_file_size
= 0;
191 if (dialog
->window
== NULL
)
194 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog
->model
), &iter
);
196 /* Find all active transfers */
199 PurpleXfer
*xfer
= NULL
;
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
) {
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)
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",
228 total_pct
, num_active_xfers
);
229 gtk_window_set_title(GTK_WINDOW(dialog
->window
), title
);
232 gtk_window_set_title(GTK_WINDOW(dialog
->window
), _("File Transfers"));
237 update_detailed_info(PidginXferDialog
*dialog
, PurpleXfer
*xfer
)
239 PidginXferUiData
*data
;
240 char *kbsec
, *time_elapsed
, *time_remaining
;
243 if (dialog
== NULL
|| xfer
== NULL
)
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
,
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>"));
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
));
295 tmp
= g_path_get_basename(purple_xfer_get_local_filename(xfer
));
296 utf8
= g_filename_to_utf8(tmp
, -1, NULL
, NULL
, NULL
);
299 gtk_label_set_text(GTK_LABEL(dialog
->filename_label
), 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
);
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
),
314 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dialog
->progress
),
315 purple_xfer_get_progress(xfer
));
318 g_free(time_elapsed
);
319 g_free(time_remaining
);
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
);
337 if (dialog
->selected_xfer
!= xfer
)
340 if (purple_xfer_is_completed(xfer
)) {
341 gtk_widget_hide(dialog
->stop_button
);
342 gtk_widget_show(dialog
->remove_button
);
345 /* If using Win32... */
346 if (purple_xfer_get_type(xfer
) == PURPLE_XFER_RECEIVE
) {
347 gtk_widget_set_sensitive(dialog
->open_button
, TRUE
);
349 gtk_widget_set_sensitive(dialog
->open_button
, FALSE
);
352 if (purple_xfer_get_type(xfer
) == PURPLE_XFER_RECEIVE
) {
353 gtk_widget_set_sensitive(dialog
->open_button
, TRUE
);
355 gtk_widget_set_sensitive (dialog
->open_button
, FALSE
);
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
);
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
);
377 ensure_row_selected(PidginXferDialog
*dialog
)
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
))
387 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog
->model
), &iter
))
388 gtk_tree_selection_select_iter(selection
, &iter
);
391 /**************************************************************************
393 **************************************************************************/
395 delete_win_cb(GtkWidget
*w
, GdkEventAny
*e
, gpointer d
)
397 PidginXferDialog
*dialog
;
399 dialog
= (PidginXferDialog
*)d
;
401 pidgin_xfer_dialog_hide(dialog
);
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",
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",
423 selection_changed_cb(GtkTreeSelection
*selection
, PidginXferDialog
*dialog
)
426 PurpleXfer
*xfer
= NULL
;
428 if (gtk_tree_selection_get_selected(selection
, NULL
, &iter
)) {
431 gtk_widget_set_sensitive(dialog
->expander
, TRUE
);
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
;
444 gtk_expander_set_expanded(GTK_EXPANDER(dialog
->expander
),
447 gtk_widget_set_sensitive(dialog
->expander
, FALSE
);
449 dialog
->selected_xfer
= NULL
;
452 update_buttons(dialog
, xfer
);
456 open_button_cb(GtkButton
*button
, PidginXferDialog
*dialog
)
459 /* If using Win32... */
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
,
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
);
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
);
484 const char *filename
= purple_xfer_get_local_filename(dialog
->selected_xfer
);
485 char *command
= 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
);
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
);
502 command
= g_strdup_printf("kfmclient openURL %s", escaped
);
507 purple_notify_uri(NULL
, filename
);
511 if (purple_program_is_valid(command
))
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
),
519 purple_notify_error(dialog
, NULL
, _("Unable to open file."), tmp
);
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"),
528 purple_notify_error(dialog
, NULL
, primary
, secondary
);
536 remove_button_cb(GtkButton
*button
, PidginXferDialog
*dialog
)
538 pidgin_xfer_dialog_remove_xfer(dialog
, dialog
->selected_xfer
);
542 stop_button_cb(GtkButton
*button
, PidginXferDialog
*dialog
)
544 purple_xfer_cancel_local(dialog
->selected_xfer
);
548 close_button_cb(GtkButton
*button
, PidginXferDialog
*dialog
)
550 pidgin_xfer_dialog_hide(dialog
);
554 /**************************************************************************
555 * Dialog Building Functions
556 **************************************************************************/
558 setup_tree(PidginXferDialog
*dialog
)
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
),
571 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw
),
572 GTK_POLICY_AUTOMATIC
,
573 GTK_POLICY_AUTOMATIC
);
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
,
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
));
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
);
646 make_info_table(PidginXferDialog
*dialog
)
654 GtkWidget
**desc_label
;
655 GtkWidget
**val_label
;
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
++) {
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,
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
,
703 G_N_ELEMENTS(labels
), G_N_ELEMENTS(labels
) + 1,
704 GTK_FILL
, GTK_FILL
, 0, 0);
705 gtk_widget_show(dialog
->progress
);
711 pidgin_xfer_dialog_new(void)
713 PidginXferDialog
*dialog
;
715 GtkWidget
*vbox1
, *vbox2
;
718 GtkWidget
*alignment
;
723 dialog
= g_new0(PidginXferDialog
, 1);
725 purple_prefs_get_bool(PIDGIN_PREFS_ROOT
"/filetransfer/keep_open");
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
),
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
),
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); \
805 ADD_BUTTON(dialog
->open_button
, GTK_STOCK_OPEN
, G_CALLBACK(open_button_cb
), dialog
);
806 gtk_widget_set_sensitive(dialog
->open_button
, FALSE
);
809 ADD_BUTTON(dialog
->remove_button
, GTK_STOCK_REMOVE
, G_CALLBACK(remove_button_cb
), dialog
);
810 gtk_widget_hide(dialog
->remove_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
);
817 ADD_BUTTON(dialog
->close_button
, GTK_STOCK_CLOSE
, G_CALLBACK(close_button_cb
), dialog
);
822 g_signal_connect(G_OBJECT(dialog
->window
), "show",
823 G_CALLBACK(winpidgin_ensure_onscreen
), dialog
->window
);
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
);
842 pidgin_xfer_dialog_show(PidginXferDialog
*dialog
)
844 PidginXferDialog
*tmp
;
846 if (dialog
== NULL
) {
847 tmp
= pidgin_get_xfer_dialog();
850 tmp
= pidgin_xfer_dialog_new();
851 pidgin_set_xfer_dialog(tmp
);
854 gtk_widget_show(tmp
->window
);
856 gtk_window_present(GTK_WINDOW(dialog
->window
));
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
);
871 pidgin_xfer_dialog_add_xfer(PidginXferDialog
*dialog
, PurpleXfer
*xfer
)
873 PidginXferUiData
*data
;
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
);
907 gtk_list_store_set(dialog
->model
, &data
->iter
,
908 COLUMN_STATUS
, pixbuf
,
910 COLUMN_FILENAME
, (type
== PURPLE_XFER_RECEIVE
)
911 ? purple_xfer_get_filename(xfer
)
913 COLUMN_SIZE
, size_str
,
914 COLUMN_REMAINING
, _("Waiting for transfer to begin"),
919 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dialog
->tree
));
921 g_object_unref(pixbuf
);
924 g_free(remaining_str
);
926 dialog
->num_transfers
++;
928 ensure_row_selected(dialog
);
929 update_title_progress(dialog
);
933 pidgin_xfer_dialog_remove_xfer(PidginXferDialog
*dialog
,
936 PidginXferUiData
*data
;
938 g_return_if_fail(dialog
!= NULL
);
939 g_return_if_fail(xfer
!= NULL
);
941 data
= PIDGINXFER(xfer
);
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
);
962 pidgin_xfer_dialog_cancel_xfer(PidginXferDialog
*dialog
,
965 PidginXferUiData
*data
;
969 g_return_if_fail(dialog
!= NULL
);
970 g_return_if_fail(xfer
!= NULL
);
972 data
= PIDGINXFER(xfer
);
980 if ((purple_xfer_get_status(xfer
) == PURPLE_XFER_STATUS_CANCEL_LOCAL
) && (dialog
->auto_clear
)) {
981 pidgin_xfer_dialog_remove_xfer(dialog
, xfer
);
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");
997 status
= _("Failed");
999 gtk_list_store_set(dialog
->model
, &data
->iter
,
1000 COLUMN_STATUS
, pixbuf
,
1001 COLUMN_REMAINING
, status
,
1004 g_object_unref(pixbuf
);
1006 update_buttons(dialog
, xfer
);
1010 pidgin_xfer_dialog_update_xfer(PidginXferDialog
*dialog
,
1013 PidginXferUiData
*data
;
1014 char *size_str
, *remaining_str
;
1015 time_t current_time
;
1019 g_return_if_fail(dialog
!= NULL
);
1020 g_return_if_fail(xfer
!= NULL
);
1022 if ((data
= PIDGINXFER(xfer
)) == NULL
)
1025 if (data
->in_list
== FALSE
)
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 */
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
,
1047 g_free(remaining_str
);
1049 if (purple_xfer_is_completed(xfer
))
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"),
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
);
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
)
1081 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog
->model
), &iter
);
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
))
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 **************************************************************************/
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
;
1116 pidgin_xfer_destroy(PurpleXfer
*xfer
)
1118 PidginXferUiData
*data
;
1120 data
= PIDGINXFER(xfer
);
1124 xfer
->ui_data
= NULL
;
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
);
1138 pidgin_xfer_update_progress(PurpleXfer
*xfer
, double percent
)
1140 pidgin_xfer_dialog_update_xfer(xfer_dialog
, xfer
);
1144 pidgin_xfer_cancel_local(PurpleXfer
*xfer
)
1147 pidgin_xfer_dialog_cancel_xfer(xfer_dialog
, xfer
);
1151 pidgin_xfer_cancel_remote(PurpleXfer
*xfer
)
1154 pidgin_xfer_dialog_cancel_xfer(xfer_dialog
, xfer
);
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
);
1168 gchar
**formats_split
= g_strsplit(formats
, ",", 0);
1169 gchar
*buffer
= NULL
;
1171 char *option_keys
[2] = {NULL
, NULL
};
1172 char *option_values
[2] = {NULL
, NULL
};
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";
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";
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",
1197 format
= formats_split
[0];
1200 gdk_pixbuf_save_to_bufferv(thumbnail
, &buffer
, &size
, format
,
1201 option_keys
, option_values
, NULL
);
1204 gchar
*mimetype
= g_strdup_printf("image/%s", format
);
1205 purple_debug_info("ft",
1206 "created thumbnail of %" G_GSIZE_FORMAT
" bytes\n",
1208 purple_xfer_set_thumbnail(xfer
, buffer
, size
, 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
,
1229 pidgin_xfer_add_thumbnail
1232 /**************************************************************************
1233 * GTK+ File Transfer API
1234 **************************************************************************/
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
);
1244 pidgin_xfers_uninit(void)
1246 if (xfer_dialog
!= NULL
)
1247 pidgin_xfer_dialog_destroy(xfer_dialog
);
1251 pidgin_set_xfer_dialog(PidginXferDialog
*dialog
)
1253 xfer_dialog
= dialog
;
1257 pidgin_get_xfer_dialog(void)
1263 pidgin_xfers_get_ui_ops(void)