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
)
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
,
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
));
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
);
635 make_info_table(PidginXferDialog
*dialog
)
643 GtkWidget
**desc_label
;
644 GtkWidget
**val_label
;
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
++) {
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,
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
,
692 G_N_ELEMENTS(labels
), G_N_ELEMENTS(labels
) + 1,
693 GTK_FILL
, GTK_FILL
, 0, 0);
694 gtk_widget_show(dialog
->progress
);
700 pidgin_xfer_dialog_new(void)
702 PidginXferDialog
*dialog
;
704 GtkWidget
*vbox1
, *vbox2
;
706 GtkWidget
*alignment
;
711 dialog
= g_new0(PidginXferDialog
, 1);
713 purple_prefs_get_bool(PIDGIN_PREFS_ROOT
"/filetransfer/keep_open");
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),
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
),
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
),
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); \
793 ADD_BUTTON(dialog
->open_button
, GTK_STOCK_OPEN
, G_CALLBACK(open_button_cb
), dialog
);
794 gtk_widget_set_sensitive(dialog
->open_button
, FALSE
);
797 ADD_BUTTON(dialog
->remove_button
, GTK_STOCK_REMOVE
, G_CALLBACK(remove_button_cb
), dialog
);
798 gtk_widget_hide(dialog
->remove_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
);
805 ADD_BUTTON(dialog
->close_button
, GTK_STOCK_CLOSE
, G_CALLBACK(close_button_cb
), dialog
);
810 g_signal_connect(G_OBJECT(dialog
->window
), "show",
811 G_CALLBACK(winpidgin_ensure_onscreen
), dialog
->window
);
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
);
830 pidgin_xfer_dialog_show(PidginXferDialog
*dialog
)
832 PidginXferDialog
*tmp
;
834 if (dialog
== NULL
) {
835 tmp
= pidgin_get_xfer_dialog();
838 tmp
= pidgin_xfer_dialog_new();
839 pidgin_set_xfer_dialog(tmp
);
842 gtk_widget_show(tmp
->window
);
844 gtk_window_present(GTK_WINDOW(dialog
->window
));
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
);
859 pidgin_xfer_dialog_add_xfer(PidginXferDialog
*dialog
, PurpleXfer
*xfer
)
861 PidginXferUiData
*data
;
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
);
895 gtk_list_store_set(dialog
->model
, &data
->iter
,
896 COLUMN_STATUS
, pixbuf
,
898 COLUMN_FILENAME
, (type
== PURPLE_XFER_RECEIVE
)
899 ? purple_xfer_get_filename(xfer
)
901 COLUMN_SIZE
, size_str
,
902 COLUMN_REMAINING
, _("Waiting for transfer to begin"),
907 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(dialog
->tree
));
909 g_object_unref(pixbuf
);
912 g_free(remaining_str
);
914 dialog
->num_transfers
++;
916 ensure_row_selected(dialog
);
917 update_title_progress(dialog
);
921 pidgin_xfer_dialog_remove_xfer(PidginXferDialog
*dialog
,
924 PidginXferUiData
*data
;
926 g_return_if_fail(dialog
!= NULL
);
927 g_return_if_fail(xfer
!= NULL
);
929 data
= PIDGINXFER(xfer
);
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
);
950 pidgin_xfer_dialog_cancel_xfer(PidginXferDialog
*dialog
,
953 PidginXferUiData
*data
;
957 g_return_if_fail(dialog
!= NULL
);
958 g_return_if_fail(xfer
!= NULL
);
960 data
= PIDGINXFER(xfer
);
968 if ((purple_xfer_get_status(xfer
) == PURPLE_XFER_STATUS_CANCEL_LOCAL
) && (dialog
->auto_clear
)) {
969 pidgin_xfer_dialog_remove_xfer(dialog
, xfer
);
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");
985 status
= _("Failed");
987 gtk_list_store_set(dialog
->model
, &data
->iter
,
988 COLUMN_STATUS
, pixbuf
,
989 COLUMN_REMAINING
, status
,
992 g_object_unref(pixbuf
);
994 update_buttons(dialog
, xfer
);
998 pidgin_xfer_dialog_update_xfer(PidginXferDialog
*dialog
,
1001 PidginXferUiData
*data
;
1002 char *size_str
, *remaining_str
;
1003 time_t current_time
;
1007 g_return_if_fail(dialog
!= NULL
);
1008 g_return_if_fail(xfer
!= NULL
);
1010 if ((data
= PIDGINXFER(xfer
)) == NULL
)
1013 if (data
->in_list
== FALSE
)
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 */
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
,
1035 g_free(remaining_str
);
1037 if (purple_xfer_is_completed(xfer
))
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"),
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
);
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
)
1069 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dialog
->model
), &iter
);
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
))
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 **************************************************************************/
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
;
1104 pidgin_xfer_destroy(PurpleXfer
*xfer
)
1106 PidginXferUiData
*data
;
1108 data
= PIDGINXFER(xfer
);
1112 xfer
->ui_data
= NULL
;
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
);
1126 pidgin_xfer_update_progress(PurpleXfer
*xfer
, double percent
)
1128 pidgin_xfer_dialog_update_xfer(xfer_dialog
, xfer
);
1132 pidgin_xfer_cancel_local(PurpleXfer
*xfer
)
1135 pidgin_xfer_dialog_cancel_xfer(xfer_dialog
, xfer
);
1139 pidgin_xfer_cancel_remote(PurpleXfer
*xfer
)
1142 pidgin_xfer_dialog_cancel_xfer(xfer_dialog
, xfer
);
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);
1156 gchar
**formats_split
= g_strsplit(formats
, ",", 0);
1157 gchar
*buffer
= NULL
;
1159 char *option_keys
[2] = {NULL
, NULL
};
1160 char *option_values
[2] = {NULL
, NULL
};
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";
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";
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",
1185 format
= formats_split
[0];
1188 gdk_pixbuf_save_to_bufferv(thumbnail
, &buffer
, &size
, format
,
1189 option_keys
, option_values
, NULL
);
1192 gchar
*mimetype
= g_strdup_printf("image/%s", format
);
1193 purple_debug_info("ft",
1194 "created thumbnail of %" G_GSIZE_FORMAT
" bytes\n",
1196 purple_xfer_set_thumbnail(xfer
, buffer
, size
, 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
,
1217 pidgin_xfer_add_thumbnail
1220 /**************************************************************************
1221 * GTK+ File Transfer API
1222 **************************************************************************/
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
);
1232 pidgin_xfers_uninit(void)
1234 if (xfer_dialog
!= NULL
)
1235 pidgin_xfer_dialog_destroy(xfer_dialog
);
1239 pidgin_set_xfer_dialog(PidginXferDialog
*dialog
)
1241 xfer_dialog
= dialog
;
1245 pidgin_get_xfer_dialog(void)
1251 pidgin_xfers_get_ui_ops(void)