3 * Finch is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include <gntbutton.h>
27 #include <gntcheckbox.h>
49 GntWidget
*remove_button
;
50 GntWidget
*stop_button
;
51 GntWidget
*close_button
;
52 } PurpleGntXferDialog
;
54 static PurpleGntXferDialog
*xfer_dialog
= NULL
;
58 gint64 last_updated_time
;
62 gboolean notified
; /* Has the completion of the transfer been notified? */
64 } PurpleGntXferUiData
;
78 /**************************************************************************
80 **************************************************************************/
83 update_title_progress(void)
86 int num_active_xfers
= 0;
87 guint64 total_bytes_xferred
= 0;
88 guint64 total_file_size
= 0;
90 if (xfer_dialog
== NULL
|| xfer_dialog
->window
== NULL
)
93 /* Find all active transfers */
94 for (list
= gnt_tree_get_rows(GNT_TREE(xfer_dialog
->tree
)); list
; list
= list
->next
) {
95 PurpleXfer
*xfer
= (PurpleXfer
*)list
->data
;
97 if (purple_xfer_get_status(xfer
) == PURPLE_XFER_STATUS_STARTED
) {
99 total_bytes_xferred
+= purple_xfer_get_bytes_sent(xfer
);
100 total_file_size
+= purple_xfer_get_size(xfer
);
104 /* Update the title */
105 if (num_active_xfers
> 0) {
109 if (total_file_size
> 0) {
110 total_pct
= 100 * total_bytes_xferred
/ total_file_size
;
113 title
= g_strdup_printf(ngettext("File Transfers - %d%% of %d file",
114 "File Transfers - %d%% of %d files",
116 total_pct
, num_active_xfers
);
117 gnt_screen_rename_widget((xfer_dialog
->window
), title
);
120 gnt_screen_rename_widget((xfer_dialog
->window
), _("File Transfers"));
125 /**************************************************************************
127 **************************************************************************/
129 toggle_keep_open_cb(GntWidget
*w
)
131 xfer_dialog
->keep_open
= !xfer_dialog
->keep_open
;
132 purple_prefs_set_bool("/finch/filetransfer/keep_open",
133 xfer_dialog
->keep_open
);
137 toggle_clear_finished_cb(GntWidget
*w
)
139 xfer_dialog
->auto_clear
= !xfer_dialog
->auto_clear
;
140 purple_prefs_set_bool("/finch/filetransfer/clear_finished",
141 xfer_dialog
->auto_clear
);
142 if (xfer_dialog
->auto_clear
) {
143 GList
*iter
= purple_xfers_get_all();
145 PurpleXfer
*xfer
= iter
->data
;
147 if (purple_xfer_is_completed(xfer
) || purple_xfer_is_cancelled(xfer
))
148 finch_xfer_dialog_remove_xfer(xfer
);
154 remove_button_cb(GntButton
*button
)
156 PurpleXfer
*selected_xfer
= gnt_tree_get_selection_data(GNT_TREE(xfer_dialog
->tree
));
157 if (selected_xfer
&& (purple_xfer_is_completed(selected_xfer
) ||
158 purple_xfer_is_cancelled(selected_xfer
))) {
159 finch_xfer_dialog_remove_xfer(selected_xfer
);
164 stop_button_cb(GntButton
*button
)
166 PurpleXfer
*selected_xfer
= gnt_tree_get_selection_data(GNT_TREE(xfer_dialog
->tree
));
167 PurpleXferStatus status
;
172 status
= purple_xfer_get_status(selected_xfer
);
173 if (status
!= PURPLE_XFER_STATUS_CANCEL_LOCAL
&&
174 status
!= PURPLE_XFER_STATUS_CANCEL_REMOTE
&&
175 status
!= PURPLE_XFER_STATUS_DONE
)
176 purple_xfer_cancel_local(selected_xfer
);
179 /**************************************************************************
180 * Dialog Building Functions
181 **************************************************************************/
185 finch_xfer_dialog_new(void)
193 int widths
[] = {8, 12, 8, 8, 8, 8, -1};
196 xfer_dialog
= g_new0(PurpleGntXferDialog
, 1);
198 xfer_dialog
->keep_open
=
199 purple_prefs_get_bool("/finch/filetransfer/keep_open");
200 xfer_dialog
->auto_clear
=
201 purple_prefs_get_bool("/finch/filetransfer/clear_finished");
203 /* Create the window. */
204 xfer_dialog
->window
= window
= gnt_vbox_new(FALSE
);
205 g_signal_connect(G_OBJECT(window
), "destroy", G_CALLBACK(finch_xfer_dialog_destroy
), NULL
);
206 gnt_box_set_toplevel(GNT_BOX(window
), TRUE
);
207 gnt_box_set_title(GNT_BOX(window
), _("File Transfers"));
208 gnt_box_set_fill(GNT_BOX(window
), TRUE
);
209 gnt_box_set_alignment(GNT_BOX(window
), GNT_ALIGN_MID
);
211 xfer_dialog
->tree
= tree
= gnt_tree_new_with_columns(NUM_COLUMNS
);
212 gnt_tree_set_column_titles(GNT_TREE(tree
), _("Progress"), _("Filename"), _("Size"), _("Speed"), _("Remaining"), _("Status"));
213 gnt_tree_set_column_width_ratio(GNT_TREE(tree
), widths
);
214 gnt_tree_set_column_resizable(GNT_TREE(tree
), COLUMN_PROGRESS
, FALSE
);
215 gnt_tree_set_column_resizable(GNT_TREE(tree
), COLUMN_SIZE
, FALSE
);
216 gnt_tree_set_column_resizable(GNT_TREE(tree
), COLUMN_SPEED
, FALSE
);
217 gnt_tree_set_column_resizable(GNT_TREE(tree
), COLUMN_REMAINING
, FALSE
);
218 gnt_widget_set_size(tree
, 70, -1);
219 gnt_tree_set_show_title(GNT_TREE(tree
), TRUE
);
220 gnt_box_add_widget(GNT_BOX(window
), tree
);
222 checkbox
= gnt_check_box_new( _("Close this window when all transfers finish"));
223 gnt_check_box_set_checked(GNT_CHECK_BOX(checkbox
),
224 !xfer_dialog
->keep_open
);
225 g_signal_connect(G_OBJECT(checkbox
), "toggled",
226 G_CALLBACK(toggle_keep_open_cb
), NULL
);
227 gnt_box_add_widget(GNT_BOX(window
), checkbox
);
229 checkbox
= gnt_check_box_new(_("Clear finished transfers"));
230 gnt_check_box_set_checked(GNT_CHECK_BOX(checkbox
),
231 xfer_dialog
->auto_clear
);
232 g_signal_connect(G_OBJECT(checkbox
), "toggled",
233 G_CALLBACK(toggle_clear_finished_cb
), NULL
);
234 gnt_box_add_widget(GNT_BOX(window
), checkbox
);
236 bbox
= gnt_hbox_new(FALSE
);
238 xfer_dialog
->remove_button
= button
= gnt_button_new(_("Remove"));
239 g_signal_connect(G_OBJECT(button
), "activate",
240 G_CALLBACK(remove_button_cb
), NULL
);
241 gnt_box_add_widget(GNT_BOX(bbox
), button
);
243 xfer_dialog
->stop_button
= button
= gnt_button_new(_("Stop"));
244 g_signal_connect(G_OBJECT(button
), "activate",
245 G_CALLBACK(stop_button_cb
), NULL
);
246 gnt_box_add_widget(GNT_BOX(bbox
), button
);
248 xfer_dialog
->close_button
= button
= gnt_button_new(_("Close"));
249 g_signal_connect(G_OBJECT(button
), "activate",
250 G_CALLBACK(finch_xfer_dialog_destroy
), NULL
);
251 gnt_box_add_widget(GNT_BOX(bbox
), button
);
253 gnt_box_add_widget(GNT_BOX(window
), bbox
);
255 for (iter
= purple_xfers_get_all(); iter
; iter
= iter
->next
) {
256 PurpleXfer
*xfer
= (PurpleXfer
*)iter
->data
;
257 PurpleGntXferUiData
*data
= purple_xfer_get_ui_data(xfer
);
259 finch_xfer_dialog_add_xfer(xfer
);
260 finch_xfer_dialog_update_xfer(xfer
);
261 gnt_tree_set_selected(GNT_TREE(tree
), xfer
);
264 gnt_widget_show(xfer_dialog
->window
);
268 finch_xfer_dialog_destroy()
270 gnt_widget_destroy(xfer_dialog
->window
);
276 finch_xfer_dialog_show()
278 if (xfer_dialog
== NULL
)
279 finch_xfer_dialog_new();
281 gnt_window_present(xfer_dialog
->window
);
285 finch_xfer_dialog_add_xfer(PurpleXfer
*xfer
)
287 PurpleGntXferUiData
*data
;
289 char *size_str
, *remaining_str
;
290 char *lfilename
, *utf8
;
292 g_return_if_fail(xfer_dialog
!= NULL
);
293 g_return_if_fail(xfer
!= NULL
);
297 data
= purple_xfer_get_ui_data(xfer
);
298 data
->in_list
= TRUE
;
300 finch_xfer_dialog_show();
302 data
->last_updated_time
= 0;
304 type
= purple_xfer_get_xfer_type(xfer
);
306 size_str
= purple_str_size_to_units(purple_xfer_get_size(xfer
));
307 remaining_str
= purple_str_size_to_units(purple_xfer_get_bytes_remaining(xfer
));
309 lfilename
= g_path_get_basename(purple_xfer_get_local_filename(xfer
));
310 utf8
= g_filename_to_utf8(lfilename
, -1, NULL
, NULL
, NULL
);
313 gnt_tree_add_row_last(GNT_TREE(xfer_dialog
->tree
), xfer
,
314 gnt_tree_create_row(GNT_TREE(xfer_dialog
->tree
),
315 "0.0", (type
== PURPLE_XFER_TYPE_RECEIVE
) ? purple_xfer_get_filename(xfer
) : lfilename
,
316 size_str
, "0.0", "",_("Waiting for transfer to begin")), NULL
);
320 g_free(remaining_str
);
322 xfer_dialog
->num_transfers
++;
324 update_title_progress();
328 finch_xfer_dialog_remove_xfer(PurpleXfer
*xfer
)
330 PurpleGntXferUiData
*data
;
332 g_return_if_fail(xfer_dialog
!= NULL
);
333 g_return_if_fail(xfer
!= NULL
);
335 data
= purple_xfer_get_ui_data(xfer
);
343 data
->in_list
= FALSE
;
345 gnt_tree_remove(GNT_TREE(xfer_dialog
->tree
), xfer
);
347 xfer_dialog
->num_transfers
--;
349 if (xfer_dialog
->num_transfers
== 0 && !xfer_dialog
->keep_open
)
350 finch_xfer_dialog_destroy();
352 update_title_progress();
353 g_object_unref(xfer
);
357 finch_xfer_dialog_cancel_xfer(PurpleXfer
*xfer
)
359 PurpleGntXferUiData
*data
;
362 g_return_if_fail(xfer_dialog
!= NULL
);
363 g_return_if_fail(xfer
!= NULL
);
365 data
= purple_xfer_get_ui_data(xfer
);
373 if ((purple_xfer_get_status(xfer
) == PURPLE_XFER_STATUS_CANCEL_LOCAL
) && (xfer_dialog
->auto_clear
)) {
374 finch_xfer_dialog_remove_xfer(xfer
);
378 update_title_progress();
380 if (purple_xfer_is_cancelled(xfer
))
381 status
= _("Cancelled");
383 status
= _("Failed");
385 gnt_tree_change_text(GNT_TREE(xfer_dialog
->tree
), xfer
, COLUMN_STATUS
, status
);
389 finch_xfer_dialog_update_xfer(PurpleXfer
*xfer
)
391 PurpleGntXferUiData
*data
;
392 char *size_str
, *remaining_str
;
401 if ((now
= purple_xfer_get_end_time(xfer
)) == 0)
404 kb_sent
= purple_xfer_get_bytes_sent(xfer
) / 1024.0;
405 elapsed
= (purple_xfer_get_start_time(xfer
) > 0 ? now
- purple_xfer_get_start_time(xfer
) : 0);
406 kbps
= (elapsed
> 0 ? (kb_sent
/ elapsed
) : 0);
408 g_return_if_fail(xfer_dialog
!= NULL
);
409 g_return_if_fail(xfer
!= NULL
);
411 if ((data
= purple_xfer_get_ui_data(xfer
)) == NULL
)
414 if (data
->in_list
== FALSE
|| data
->notified
)
417 current_time
= g_get_monotonic_time();
418 if (((current_time
- data
->last_updated_time
) < G_USEC_PER_SEC
) &&
419 (!purple_xfer_is_completed(xfer
))) {
420 /* Don't update the window more than once per second */
423 data
->last_updated_time
= current_time
;
425 send
= (purple_xfer_get_xfer_type(xfer
) == PURPLE_XFER_TYPE_SEND
);
426 size_str
= purple_str_size_to_units(purple_xfer_get_size(xfer
));
427 remaining_str
= purple_str_size_to_units(purple_xfer_get_bytes_remaining(xfer
));
428 kbsec
= g_strdup_printf(_("%.2f KiB/s"), kbps
);
430 gnt_tree_change_text(GNT_TREE(xfer_dialog
->tree
), xfer
, COLUMN_PROGRESS
,
431 g_ascii_dtostr(prog_str
, sizeof(prog_str
), purple_xfer_get_progress(xfer
) * 100.));
432 gnt_tree_change_text(GNT_TREE(xfer_dialog
->tree
), xfer
, COLUMN_SIZE
, size_str
);
433 gnt_tree_change_text(GNT_TREE(xfer_dialog
->tree
), xfer
, COLUMN_REMAINING
, remaining_str
);
434 gnt_tree_change_text(GNT_TREE(xfer_dialog
->tree
), xfer
, COLUMN_SPEED
, kbsec
);
436 g_free(remaining_str
);
438 if (purple_xfer_is_completed(xfer
)) {
439 gnt_tree_change_text(GNT_TREE(xfer_dialog
->tree
), xfer
, COLUMN_STATUS
, send
? _("Sent") : _("Received"));
440 gnt_tree_change_text(GNT_TREE(xfer_dialog
->tree
), xfer
, COLUMN_REMAINING
, _("Finished"));
442 char *msg
= g_strdup_printf(_("The file was saved as %s."), purple_xfer_get_local_filename(xfer
));
443 purple_xfer_conversation_write(xfer
, msg
, FALSE
);
446 data
->notified
= TRUE
;
448 gnt_tree_change_text(GNT_TREE(xfer_dialog
->tree
), xfer
, COLUMN_STATUS
,
449 send
? _("Sending") : _("Receiving"));
452 update_title_progress();
454 if (purple_xfer_is_completed(xfer
) && xfer_dialog
->auto_clear
)
455 finch_xfer_dialog_remove_xfer(xfer
);
458 /**************************************************************************
459 * File Transfer UI Ops
460 **************************************************************************/
462 finch_xfer_new_xfer(PurpleXfer
*xfer
)
464 PurpleGntXferUiData
*data
;
466 /* This is where we're setting xfer's "ui_data" for the first time. */
467 data
= g_new0(PurpleGntXferUiData
, 1);
468 purple_xfer_set_ui_data(xfer
, data
);
472 finch_xfer_destroy(PurpleXfer
*xfer
)
474 PurpleGntXferUiData
*data
;
476 data
= purple_xfer_get_ui_data(xfer
);
480 purple_xfer_set_ui_data(xfer
, NULL
);
485 finch_xfer_add_xfer(PurpleXfer
*xfer
)
488 finch_xfer_dialog_new();
490 finch_xfer_dialog_add_xfer(xfer
);
491 gnt_tree_set_selected(GNT_TREE(xfer_dialog
->tree
), xfer
);
495 finch_xfer_update_progress(PurpleXfer
*xfer
, double percent
)
498 finch_xfer_dialog_update_xfer(xfer
);
502 finch_xfer_cancel_local(PurpleXfer
*xfer
)
505 finch_xfer_dialog_cancel_xfer(xfer
);
509 finch_xfer_cancel_remote(PurpleXfer
*xfer
)
512 finch_xfer_dialog_cancel_xfer(xfer
);
515 static PurpleXferUiOps ops
=
520 finch_xfer_update_progress
,
521 finch_xfer_cancel_local
,
522 finch_xfer_cancel_remote
,
525 NULL
, /* data_not_sent */
526 NULL
/* add_thumbnail */
529 /**************************************************************************
530 * GNT File Transfer API
531 **************************************************************************/
533 finch_xfers_init(void)
535 purple_prefs_add_none("/finch/filetransfer");
536 purple_prefs_add_bool("/finch/filetransfer/clear_finished", TRUE
);
537 purple_prefs_add_bool("/finch/filetransfer/keep_open", FALSE
);
541 finch_xfers_uninit(void)
543 if (xfer_dialog
!= NULL
)
544 finch_xfer_dialog_destroy();
548 finch_xfers_get_ui_ops(void)