Merged pidgin/main into default
[pidgin-git.git] / finch / gntxfer.c
blobc38f170b3a4f55858b29e3802327bcd6ed23b13d
1 /* finch
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
5 * source distribution.
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
21 #include <internal.h>
22 #include "finch.h"
24 #include <gnt.h>
25 #include <gntbox.h>
26 #include <gntbutton.h>
27 #include <gntcheckbox.h>
28 #include <gntlabel.h>
29 #include <gnttree.h>
31 #include "debug.h"
32 #include "notify.h"
33 #include "xfer.h"
34 #include "protocol.h"
35 #include "util.h"
37 #include "gntxfer.h"
38 #include "prefs.h"
40 typedef struct
42 gboolean keep_open;
43 gboolean auto_clear;
44 gint num_transfers;
46 GntWidget *window;
47 GntWidget *tree;
49 GntWidget *remove_button;
50 GntWidget *stop_button;
51 GntWidget *close_button;
52 } PurpleGntXferDialog;
54 static PurpleGntXferDialog *xfer_dialog = NULL;
56 typedef struct
58 gint64 last_updated_time;
59 gboolean in_list;
61 char *name;
62 gboolean notified; /* Has the completion of the transfer been notified? */
64 } PurpleGntXferUiData;
66 enum
68 COLUMN_PROGRESS = 0,
69 COLUMN_FILENAME,
70 COLUMN_SIZE,
71 COLUMN_SPEED,
72 COLUMN_REMAINING,
73 COLUMN_STATUS,
74 NUM_COLUMNS
78 /**************************************************************************
79 * Utility Functions
80 **************************************************************************/
82 static void
83 update_title_progress(void)
85 GList *list;
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)
91 return;
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) {
98 num_active_xfers++;
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) {
106 gchar *title;
107 int total_pct = 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",
115 num_active_xfers),
116 total_pct, num_active_xfers);
117 gnt_screen_rename_widget((xfer_dialog->window), title);
118 g_free(title);
119 } else {
120 gnt_screen_rename_widget((xfer_dialog->window), _("File Transfers"));
125 /**************************************************************************
126 * Callbacks
127 **************************************************************************/
128 static void
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);
136 static void
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();
144 while (iter) {
145 PurpleXfer *xfer = iter->data;
146 iter = iter->next;
147 if (purple_xfer_is_completed(xfer) || purple_xfer_is_cancelled(xfer))
148 finch_xfer_dialog_remove_xfer(xfer);
153 static void
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);
163 static void
164 stop_button_cb(GntButton *button)
166 PurpleXfer *selected_xfer = gnt_tree_get_selection_data(GNT_TREE(xfer_dialog->tree));
167 PurpleXferStatus status;
169 if (!selected_xfer)
170 return;
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 **************************************************************************/
184 void
185 finch_xfer_dialog_new(void)
187 GList *iter;
188 GntWidget *window;
189 GntWidget *bbox;
190 GntWidget *button;
191 GntWidget *checkbox;
192 GntWidget *tree;
193 int widths[] = {8, 12, 8, 8, 8, 8, -1};
195 if (!xfer_dialog)
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);
258 if (data->in_list) {
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);
267 void
268 finch_xfer_dialog_destroy()
270 gnt_widget_destroy(xfer_dialog->window);
271 g_free(xfer_dialog);
272 xfer_dialog = NULL;
275 void
276 finch_xfer_dialog_show()
278 if (xfer_dialog == NULL)
279 finch_xfer_dialog_new();
280 else
281 gnt_window_present(xfer_dialog->window);
284 void
285 finch_xfer_dialog_add_xfer(PurpleXfer *xfer)
287 PurpleGntXferUiData *data;
288 PurpleXferType type;
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);
295 g_object_ref(xfer);
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);
311 g_free(lfilename);
312 lfilename = utf8;
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);
317 g_free(lfilename);
319 g_free(size_str);
320 g_free(remaining_str);
322 xfer_dialog->num_transfers++;
324 update_title_progress();
327 void
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);
337 if (data == NULL)
338 return;
340 if (!data->in_list)
341 return;
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();
351 else
352 update_title_progress();
353 g_object_unref(xfer);
356 void
357 finch_xfer_dialog_cancel_xfer(PurpleXfer *xfer)
359 PurpleGntXferUiData *data;
360 const gchar *status;
362 g_return_if_fail(xfer_dialog != NULL);
363 g_return_if_fail(xfer != NULL);
365 data = purple_xfer_get_ui_data(xfer);
367 if (data == NULL)
368 return;
370 if (!data->in_list)
371 return;
373 if ((purple_xfer_get_status(xfer) == PURPLE_XFER_STATUS_CANCEL_LOCAL) && (xfer_dialog->auto_clear)) {
374 finch_xfer_dialog_remove_xfer(xfer);
375 return;
378 update_title_progress();
380 if (purple_xfer_is_cancelled(xfer))
381 status = _("Cancelled");
382 else
383 status = _("Failed");
385 gnt_tree_change_text(GNT_TREE(xfer_dialog->tree), xfer, COLUMN_STATUS, status);
388 void
389 finch_xfer_dialog_update_xfer(PurpleXfer *xfer)
391 PurpleGntXferUiData *data;
392 char *size_str, *remaining_str;
393 gint64 current_time;
394 char prog_str[5];
395 double kb_sent;
396 double kbps = 0.0;
397 time_t elapsed, now;
398 char *kbsec;
399 gboolean send;
401 if ((now = purple_xfer_get_end_time(xfer)) == 0)
402 now = time(NULL);
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)
412 return;
414 if (data->in_list == FALSE || data->notified)
415 return;
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 */
421 return;
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);
435 g_free(size_str);
436 g_free(remaining_str);
437 g_free(kbsec);
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"));
441 if (!send) {
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);
444 g_free(msg);
446 data->notified = TRUE;
447 } else {
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 **************************************************************************/
461 static void
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);
471 static void
472 finch_xfer_destroy(PurpleXfer *xfer)
474 PurpleGntXferUiData *data;
476 data = purple_xfer_get_ui_data(xfer);
477 if (data) {
478 g_free(data->name);
479 g_free(data);
480 purple_xfer_set_ui_data(xfer, NULL);
484 static void
485 finch_xfer_add_xfer(PurpleXfer *xfer)
487 if (!xfer_dialog)
488 finch_xfer_dialog_new();
490 finch_xfer_dialog_add_xfer(xfer);
491 gnt_tree_set_selected(GNT_TREE(xfer_dialog->tree), xfer);
494 static void
495 finch_xfer_update_progress(PurpleXfer *xfer, double percent)
497 if (xfer_dialog)
498 finch_xfer_dialog_update_xfer(xfer);
501 static void
502 finch_xfer_cancel_local(PurpleXfer *xfer)
504 if (xfer_dialog)
505 finch_xfer_dialog_cancel_xfer(xfer);
508 static void
509 finch_xfer_cancel_remote(PurpleXfer *xfer)
511 if (xfer_dialog)
512 finch_xfer_dialog_cancel_xfer(xfer);
515 static PurpleXferUiOps ops =
517 finch_xfer_new_xfer,
518 finch_xfer_destroy,
519 finch_xfer_add_xfer,
520 finch_xfer_update_progress,
521 finch_xfer_cancel_local,
522 finch_xfer_cancel_remote,
523 NULL, /* ui_write */
524 NULL, /* ui_read */
525 NULL, /* data_not_sent */
526 NULL /* add_thumbnail */
529 /**************************************************************************
530 * GNT File Transfer API
531 **************************************************************************/
532 void
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);
540 void
541 finch_xfers_uninit(void)
543 if (xfer_dialog != NULL)
544 finch_xfer_dialog_destroy();
547 PurpleXferUiOps *
548 finch_xfers_get_ui_ops(void)
550 return &ops;