2 * Copyright (C) 2003, 2004 Xan Lopez
3 * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
4 * Copyright (C) 2008-2009 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
22 * Marco Barisione <marco@barisione.org>
23 * Jonny Lamb <jonny.lamb@collabora.co.uk>
24 * Xavier Claessens <xclaesse@gmail.com>
25 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
28 /* The original file transfer manager code was copied from Epiphany */
34 #include <glib/gi18n.h>
37 #define DEBUG_FLAG EMPATHY_DEBUG_FT
38 #include <libempathy/empathy-debug.h>
39 #include <libempathy/empathy-tp-file.h>
40 #include <libempathy/empathy-utils.h>
42 #include <libempathy-gtk/empathy-conf.h>
43 #include <libempathy-gtk/empathy-ui-utils.h>
44 #include <libempathy-gtk/empathy-geometry.h>
45 #include <libempathy-gtk/empathy-images.h>
47 #include "empathy-ft-manager.h"
51 * SECTION:empathy-ft-manager
52 * @short_description: File transfer dialog
53 * @see_also: #EmpathyTpFile, empathy_dispatcher_send_file()
54 * @include: libempthy-gtk/empathy-ft-manager.h
56 * The #EmpathyFTManager object represents the file transfer dialog,
57 * it can show multiple file transfers at the same time (added
58 * with empathy_ft_manager_add_tp_file()).
71 * EmpathyFTManagerPriv:
73 * Private fields of the #EmpathyFTManager class.
77 GHashTable *ft_handler_to_row_ref;
82 GtkWidget *open_button;
83 GtkWidget *abort_button;
84 GtkWidget *clear_button;
86 guint save_geometry_id;
87 } EmpathyFTManagerPriv;
96 G_DEFINE_TYPE (EmpathyFTManager, empathy_ft_manager, G_TYPE_OBJECT);
98 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTManager)
100 static EmpathyFTManager *manager_singleton = NULL;
102 static void ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
103 EmpathyFTManager *manager);
106 ft_manager_format_interval (guint interval)
108 gint hours, mins, secs;
110 hours = interval / 3600;
111 interval -= hours * 3600;
112 mins = interval / 60;
113 interval -= mins * 60;
117 /* Translators: time left, when it is more than one hour */
118 return g_strdup_printf (_("%u:%02u.%02u"), hours, mins, secs);
120 /* Translators: time left, when is is less than one hour */
121 return g_strdup_printf (_("%02u.%02u"), mins, secs);
125 ft_manager_update_buttons (EmpathyFTManager *manager)
127 GtkTreeSelection *selection;
130 EmpathyFTHandler *handler;
131 gboolean open_enabled = FALSE;
132 gboolean abort_enabled = FALSE;
133 gboolean clear_enabled = FALSE;
134 gboolean is_completed, is_cancelled;
135 GHashTableIter hash_iter;
136 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
138 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
140 if (gtk_tree_selection_get_selected (selection, &model, &iter))
142 gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
144 is_completed = empathy_ft_handler_is_completed (handler);
145 is_cancelled = empathy_ft_handler_is_cancelled (handler);
147 /* I can open the file if the transfer is completed and was incoming */
148 open_enabled = (is_completed && empathy_ft_handler_is_incoming (handler));
150 /* I can abort if the transfer is not already finished */
151 abort_enabled = (is_cancelled == FALSE && is_completed == FALSE);
153 g_object_unref (handler);
156 g_hash_table_iter_init (&hash_iter, priv->ft_handler_to_row_ref);
158 while (g_hash_table_iter_next (&hash_iter, (gpointer *) &handler, NULL))
160 if (empathy_ft_handler_is_completed (handler) ||
161 empathy_ft_handler_is_cancelled (handler))
162 clear_enabled = TRUE;
168 gtk_widget_set_sensitive (priv->open_button, open_enabled);
169 gtk_widget_set_sensitive (priv->abort_button, abort_enabled);
172 gtk_widget_set_sensitive (priv->clear_button, TRUE);
176 ft_manager_selection_changed (GtkTreeSelection *selection,
177 EmpathyFTManager *manager)
179 ft_manager_update_buttons (manager);
183 ft_manager_progress_cell_data_func (GtkTreeViewColumn *col,
184 GtkCellRenderer *renderer,
189 const gchar *text = NULL;
192 gtk_tree_model_get (model, iter, COL_PERCENT, &percent, -1);
197 text = C_("file transfer percent", "Unknown");
200 g_object_set (renderer, "text", text, "value", percent, NULL);
203 static GtkTreeRowReference *
204 ft_manager_get_row_from_handler (EmpathyFTManager *manager,
205 EmpathyFTHandler *handler)
207 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
209 return g_hash_table_lookup (priv->ft_handler_to_row_ref, handler);
213 ft_manager_remove_file_from_model (EmpathyFTManager *manager,
214 EmpathyFTHandler *handler)
216 GtkTreeRowReference *row_ref;
217 GtkTreeSelection *selection;
218 GtkTreePath *path = NULL;
220 gboolean update_selection;
221 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
223 row_ref = ft_manager_get_row_from_handler (manager, handler);
224 g_return_if_fail (row_ref);
226 DEBUG ("Removing file transfer from window: contact=%s, filename=%s",
227 empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
228 empathy_ft_handler_get_filename (handler));
230 /* Get the iter from the row_ref */
231 path = gtk_tree_row_reference_get_path (row_ref);
232 gtk_tree_model_get_iter (priv->model, &iter, path);
233 gtk_tree_path_free (path);
235 /* We have to update the selection only if we are removing the selected row */
236 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
237 update_selection = gtk_tree_selection_iter_is_selected (selection, &iter);
239 /* Remove tp_file's row. After that iter points to the next row */
240 if (!gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter))
244 /* There is no next row, set iter to the last row */
245 n_row = gtk_tree_model_iter_n_children (priv->model, NULL);
247 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, n_row - 1);
249 update_selection = FALSE;
252 if (update_selection)
253 gtk_tree_selection_select_iter (selection, &iter);
257 remove_finished_transfer_foreach (gpointer key,
261 EmpathyFTHandler *handler = key;
262 EmpathyFTManager *manager = user_data;
264 if (empathy_ft_handler_is_completed (handler) ||
265 empathy_ft_handler_is_cancelled (handler))
267 ft_manager_remove_file_from_model (manager, handler);
275 ft_manager_format_progress_bytes_and_percentage (guint64 current,
280 char *total_str, *current_str, *retval;
281 char *speed_str = NULL;
283 total_str = g_format_size_for_display (total);
284 current_str = g_format_size_for_display (current);
287 speed_str = g_format_size_for_display ((goffset) speed);
289 /* translators: first %s is the currently processed size, second %s is
290 * the total file size */
292 g_strdup_printf (_("%s of %s at %s/s"), current_str, total_str, speed_str) :
293 g_strdup_printf (_("%s of %s"), current_str, total_str);
296 g_free (current_str);
299 if (percentage != NULL)
302 *percentage = current * 100 / total;
311 ft_manager_format_contact_info (EmpathyFTHandler *handler)
314 const char *filename, *contact_name, *first_line_format;
317 incoming = empathy_ft_handler_is_incoming (handler);
318 contact_name = empathy_contact_get_name
319 (empathy_ft_handler_get_contact (handler));
320 filename = empathy_ft_handler_get_filename (handler);
323 /* translators: first %s is filename, second %s is the contact name */
324 first_line_format = _("Receiving \"%s\" from %s");
326 /* translators: first %s is filename, second %s is the contact name */
327 first_line_format = _("Sending \"%s\" to %s");
329 retval = g_strdup_printf (first_line_format, filename, contact_name);
335 ft_manager_format_error_message (EmpathyFTHandler *handler,
338 const char *contact_name, *filename;
339 EmpathyContact *contact;
340 char *first_line, *message;
344 incoming = empathy_ft_handler_is_incoming (handler);
346 contact = empathy_ft_handler_get_contact (handler);
348 contact_name = empathy_contact_get_name (contact);
350 filename = empathy_ft_handler_get_filename (handler);
353 /* filename/contact_name here are either both NULL or both valid */
354 if (filename && contact_name)
355 /* translators: first %s is filename, second %s
356 * is the contact name */
357 first_line = g_strdup_printf (_("Error receiving \"%s\" from %s"), filename,
360 first_line = g_strdup (_("Error receiving a file"));
362 /* translators: first %s is filename, second %s
363 * is the contact name */
364 if (filename && contact_name)
365 first_line = g_strdup_printf (_("Error sending \"%s\" to %s"), filename,
368 first_line = g_strdup (_("Error sending a file"));
370 message = g_strdup_printf ("%s\n%s", first_line, error->message);
378 ft_manager_update_handler_message (EmpathyFTManager *manager,
379 GtkTreeRowReference *row_ref,
384 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
386 /* Set new value in the store */
387 path = gtk_tree_row_reference_get_path (row_ref);
388 gtk_tree_model_get_iter (priv->model, &iter, path);
389 gtk_list_store_set (GTK_LIST_STORE (priv->model),
391 COL_MESSAGE, message ? message : "",
394 gtk_tree_path_free (path);
398 ft_manager_update_handler_progress (EmpathyFTManager *manager,
399 GtkTreeRowReference *row_ref,
404 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
406 /* Set new value in the store */
407 path = gtk_tree_row_reference_get_path (row_ref);
408 gtk_tree_model_get_iter (priv->model, &iter, path);
409 gtk_list_store_set (GTK_LIST_STORE (priv->model),
411 COL_PERCENT, percentage,
414 gtk_tree_path_free (path);
419 ft_manager_update_handler_time (EmpathyFTManager *manager,
420 GtkTreeRowReference *row_ref,
421 guint remaining_time)
425 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
428 remaining_str = ft_manager_format_interval (remaining_time);
430 /* Set new value in the store */
431 path = gtk_tree_row_reference_get_path (row_ref);
432 gtk_tree_model_get_iter (priv->model, &iter, path);
433 gtk_list_store_set (GTK_LIST_STORE (priv->model),
435 COL_REMAINING, remaining_str,
438 gtk_tree_path_free (path);
439 g_free (remaining_str);
443 ft_handler_transfer_error_cb (EmpathyFTHandler *handler,
445 EmpathyFTManager *manager)
448 GtkTreeRowReference *row_ref;
450 DEBUG ("Transfer error %s", error->message);
452 row_ref = ft_manager_get_row_from_handler (manager, handler);
453 g_return_if_fail (row_ref != NULL);
455 message = ft_manager_format_error_message (handler, error);
457 ft_manager_update_handler_message (manager, row_ref, message);
458 ft_manager_update_buttons (manager);
464 do_real_transfer_done (EmpathyFTManager *manager,
465 EmpathyFTHandler *handler)
467 const char *contact_name;
468 const char *filename;
469 char *first_line, *second_line, *message;
471 GtkTreeRowReference *row_ref;
473 row_ref = ft_manager_get_row_from_handler (manager, handler);
474 g_return_if_fail (row_ref != NULL);
476 incoming = empathy_ft_handler_is_incoming (handler);
477 contact_name = empathy_contact_get_name
478 (empathy_ft_handler_get_contact (handler));
479 filename = empathy_ft_handler_get_filename (handler);
482 /* translators: first %s is filename, second %s
483 * is the contact name */
484 first_line = g_strdup_printf (_("\"%s\" received from %s"), filename,
487 /* translators: first %s is filename, second %s
488 * is the contact name */
489 first_line = g_strdup_printf (_("\"%s\" sent to %s"), filename,
492 second_line = g_strdup (_("File transfer completed"));
494 message = g_strdup_printf ("%s\n%s", first_line, second_line);
495 ft_manager_update_handler_message (manager, row_ref, message);
498 ft_manager_update_buttons (manager);
502 g_free (second_line);
507 ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
508 EmpathyTpFile *tp_file,
509 EmpathyFTManager *manager)
511 DEBUG ("Transfer done");
513 if (empathy_ft_handler_is_incoming (handler) &&
514 empathy_ft_handler_get_use_hash (handler))
516 /* connect to the signal and return early */
517 g_signal_connect (handler, "hashing-started",
518 G_CALLBACK (ft_handler_hashing_started_cb), manager);
523 do_real_transfer_done (manager, handler);
527 ft_handler_transfer_progress_cb (EmpathyFTHandler *handler,
528 guint64 current_bytes,
530 guint remaining_time,
532 EmpathyFTManager *manager)
534 char *first_line, *second_line, *message;
536 GtkTreeRowReference *row_ref;
538 DEBUG ("Transfer progress");
540 row_ref = ft_manager_get_row_from_handler (manager, handler);
541 g_return_if_fail (row_ref != NULL);
543 first_line = ft_manager_format_contact_info (handler);
544 second_line = ft_manager_format_progress_bytes_and_percentage
545 (current_bytes, total_bytes, speed, &percentage);
547 message = g_strdup_printf ("%s\n%s", first_line, second_line);
549 ft_manager_update_handler_message (manager, row_ref, message);
550 ft_manager_update_handler_progress (manager, row_ref, percentage);
552 if (remaining_time > 0)
553 ft_manager_update_handler_time (manager, row_ref, remaining_time);
557 g_free (second_line);
561 ft_handler_transfer_started_cb (EmpathyFTHandler *handler,
562 EmpathyTpFile *tp_file,
563 EmpathyFTManager *manager)
565 guint64 transferred_bytes, total_bytes;
567 DEBUG ("Transfer started");
569 g_signal_connect (handler, "transfer-progress",
570 G_CALLBACK (ft_handler_transfer_progress_cb), manager);
571 g_signal_connect (handler, "transfer-done",
572 G_CALLBACK (ft_handler_transfer_done_cb), manager);
574 transferred_bytes = empathy_ft_handler_get_transferred_bytes (handler);
575 total_bytes = empathy_ft_handler_get_total_bytes (handler);
577 ft_handler_transfer_progress_cb (handler, transferred_bytes, total_bytes,
582 ft_handler_hashing_done_cb (EmpathyFTHandler *handler,
583 EmpathyFTManager *manager)
585 GtkTreeRowReference *row_ref;
586 char *first_line, *second_line, *message;
588 DEBUG ("Hashing done");
590 /* update the message */
591 if (empathy_ft_handler_is_incoming (handler))
593 do_real_transfer_done (manager, handler);
597 row_ref = ft_manager_get_row_from_handler (manager, handler);
598 g_return_if_fail (row_ref != NULL);
600 first_line = ft_manager_format_contact_info (handler);
601 second_line = g_strdup (_("Waiting for the other participant's response"));
602 message = g_strdup_printf ("%s\n%s", first_line, second_line);
604 ft_manager_update_handler_message (manager, row_ref, message);
608 g_free (second_line);
610 g_signal_connect (handler, "transfer-started",
611 G_CALLBACK (ft_handler_transfer_started_cb), manager);
615 ft_handler_hashing_progress_cb (EmpathyFTHandler *handler,
616 guint64 current_bytes,
618 EmpathyFTManager *manager)
620 char *first_line, *second_line, *message;
621 GtkTreeRowReference *row_ref;
623 row_ref = ft_manager_get_row_from_handler (manager, handler);
624 g_return_if_fail (row_ref != NULL);
626 if (empathy_ft_handler_is_incoming (handler))
627 first_line = g_strdup_printf (_("Checking integrity of \"%s\""),
628 empathy_ft_handler_get_filename (handler));
630 first_line = g_strdup_printf (_("Hashing \"%s\""),
631 empathy_ft_handler_get_filename (handler));
633 second_line = ft_manager_format_progress_bytes_and_percentage
634 (current_bytes, total_bytes, -1, NULL);
636 message = g_strdup_printf ("%s\n%s", first_line, second_line);
638 ft_manager_update_handler_message (manager, row_ref, message);
644 ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
645 EmpathyFTManager *manager)
648 GtkTreeRowReference *row_ref;
650 DEBUG ("Hashing started");
652 g_signal_connect (handler, "hashing-progress",
653 G_CALLBACK (ft_handler_hashing_progress_cb), manager);
654 g_signal_connect (handler, "hashing-done",
655 G_CALLBACK (ft_handler_hashing_done_cb), manager);
657 row_ref = ft_manager_get_row_from_handler (manager, handler);
658 g_return_if_fail (row_ref != NULL);
660 if (empathy_ft_handler_is_incoming (handler))
661 message = g_strdup_printf (_("Checking integrity of \"%s\""),
662 empathy_ft_handler_get_filename (handler));
664 message = g_strdup_printf (_("Hashing \"%s\""),
665 empathy_ft_handler_get_filename (handler));
667 ft_manager_update_handler_message (manager, row_ref, message);
673 ft_manager_start_transfer (EmpathyFTManager *manager,
674 EmpathyFTHandler *handler)
676 EmpathyFTManagerPriv *priv;
677 gboolean is_outgoing;
679 priv = GET_PRIV (manager);
681 is_outgoing = !empathy_ft_handler_is_incoming (handler);
683 DEBUG ("Start transfer, is outgoing %s",
684 is_outgoing ? "True" : "False");
686 /* now connect the signals */
687 g_signal_connect (handler, "transfer-error",
688 G_CALLBACK (ft_handler_transfer_error_cb), manager);
690 if (is_outgoing && empathy_ft_handler_get_use_hash (handler)) {
691 g_signal_connect (handler, "hashing-started",
692 G_CALLBACK (ft_handler_hashing_started_cb), manager);
694 /* either incoming or outgoing without hash */
695 g_signal_connect (handler, "transfer-started",
696 G_CALLBACK (ft_handler_transfer_started_cb), manager);
699 empathy_ft_handler_start_transfer (handler);
703 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
704 EmpathyFTHandler *handler,
707 GtkTreeRowReference *row_ref;
709 GtkTreeSelection *selection;
712 const char *content_type, *second_line;
713 char *first_line, *message;
714 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
718 /* get the icon name from the mime-type of the file. */
719 content_type = empathy_ft_handler_get_content_type (handler);
721 if (content_type != NULL)
722 icon = g_content_type_get_icon (content_type);
724 /* append the handler in the store */
725 gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
726 &iter, G_MAXINT, COL_FT_OBJECT, handler,
730 g_object_unref (icon);
732 /* insert the new row_ref in the hash table */
733 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
734 row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path);
735 gtk_tree_path_free (path);
736 g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler),
739 /* select the new row */
740 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
741 gtk_tree_selection_select_iter (selection, &iter);
745 message = ft_manager_format_error_message (handler, error);
746 ft_manager_update_handler_message (manager, row_ref, message);
752 /* update the row with the initial values.
753 * the only case where we postpone this is in case we're managing
754 * an outgoing+hashing transfer, as the hashing started signal will
755 * take care of updating the information.
757 if (empathy_ft_handler_is_incoming (handler) ||
758 !empathy_ft_handler_get_use_hash (handler)) {
759 first_line = ft_manager_format_contact_info (handler);
760 second_line = _("Waiting for the other participant's response");
761 message = g_strdup_printf ("%s\n%s", first_line, second_line);
763 ft_manager_update_handler_message (manager, row_ref, message);
769 /* hook up the signals and start the transfer */
770 ft_manager_start_transfer (manager, handler);
774 ft_manager_clear (EmpathyFTManager *manager)
776 EmpathyFTManagerPriv *priv;
778 DEBUG ("Clearing file transfer list");
780 priv = GET_PRIV (manager);
782 /* Remove completed and cancelled transfers */
783 g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
784 remove_finished_transfer_foreach, manager);
786 /* set the clear button back to insensitive */
787 gtk_widget_set_sensitive (priv->clear_button, FALSE);
791 ft_manager_open (EmpathyFTManager *manager)
793 GtkTreeSelection *selection;
796 EmpathyFTHandler *handler;
799 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
801 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
803 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
806 gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
808 file = empathy_ft_handler_get_gfile (handler);
809 uri = g_file_get_uri (file);
811 DEBUG ("Opening URI: %s", uri);
812 empathy_url_show (GTK_WIDGET (priv->window), uri);
814 g_object_unref (handler);
819 ft_manager_stop (EmpathyFTManager *manager)
821 GtkTreeSelection *selection;
824 EmpathyFTHandler *handler;
825 EmpathyFTManagerPriv *priv;
827 priv = GET_PRIV (manager);
829 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
831 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
834 gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
835 g_return_if_fail (handler != NULL);
837 DEBUG ("Stopping file transfer: contact=%s, filename=%s",
838 empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
839 empathy_ft_handler_get_filename (handler));
841 empathy_ft_handler_cancel_transfer (handler);
843 g_object_unref (handler);
847 ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager)
849 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
852 gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h);
853 gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y);
855 empathy_geometry_save ("ft-manager", x, y, w, h);
857 priv->save_geometry_id = 0;
863 ft_manager_configure_event_cb (GtkWidget *widget,
864 GdkEventConfigure *event,
865 EmpathyFTManager *manager)
867 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
869 if (priv->save_geometry_id != 0)
870 g_source_remove (priv->save_geometry_id);
872 priv->save_geometry_id = g_timeout_add (500,
873 (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager);
879 ft_manager_response_cb (GtkWidget *widget,
881 EmpathyFTManager *manager)
886 ft_manager_clear (manager);
889 ft_manager_open (manager);
892 ft_manager_stop (manager);
898 ft_manager_delete_event_cb (GtkWidget *widget,
900 EmpathyFTManager *manager)
902 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
904 DEBUG ("%p", manager);
906 /* remove all the completed/cancelled/errored transfers */
907 ft_manager_clear (manager);
909 if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
911 /* There is still FTs on flight, just hide the window */
912 DEBUG ("Hiding window");
913 gtk_widget_hide (widget);
921 ft_manager_destroy_cb (GtkWidget *widget,
922 EmpathyFTManager *manager)
924 DEBUG ("%p", manager);
926 g_object_unref (manager);
930 ft_manager_build_ui (EmpathyFTManager *manager)
935 GtkListStore *liststore;
936 GtkTreeViewColumn *column;
937 GtkCellRenderer *renderer;
938 GtkTreeSelection *selection;
940 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
942 filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
943 gui = empathy_builder_get_file (filename,
944 "ft_manager_dialog", &priv->window,
945 "ft_list", &priv->treeview,
946 "clear_button", &priv->clear_button,
947 "open_button", &priv->open_button,
948 "abort_button", &priv->abort_button,
952 empathy_builder_connect (gui, manager,
953 "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
954 "ft_manager_dialog", "response", ft_manager_response_cb,
955 "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
956 "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb,
959 g_object_unref (gui);
961 /* Window geometry. */
962 empathy_geometry_load ("ft-manager", &x, &y, &w, &h);
964 if (x >= 0 && y >= 0)
966 /* Let the window manager position it if we don't have
967 * good x, y coordinates. */
968 gtk_window_move (GTK_WINDOW (priv->window), x, y);
973 /* Use the defaults from the ui file if we don't have
974 * good w, h geometry. */
975 gtk_window_resize (GTK_WINDOW (priv->window), w, h);
978 /* Setup the tree view */
979 view = GTK_TREE_VIEW (priv->treeview);
980 selection = gtk_tree_view_get_selection (view);
981 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
982 g_signal_connect (selection, "changed",
983 G_CALLBACK (ft_manager_selection_changed), manager);
984 gtk_tree_view_set_headers_visible (view, TRUE);
985 gtk_tree_view_set_enable_search (view, FALSE);
987 /* Setup the model */
988 liststore = gtk_list_store_new (5,
989 G_TYPE_INT, /* percent */
990 G_TYPE_ICON, /* icon */
991 G_TYPE_STRING, /* message */
992 G_TYPE_STRING, /* remaining */
993 G_TYPE_OBJECT); /* ft_handler */
994 gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
995 priv->model = GTK_TREE_MODEL (liststore);
996 g_object_unref (liststore);
998 /* Progress column */
999 column = gtk_tree_view_column_new ();
1000 gtk_tree_view_column_set_title (column, _("%"));
1001 gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
1002 gtk_tree_view_insert_column (view, column, -1);
1004 renderer = gtk_cell_renderer_progress_new ();
1005 g_object_set (renderer, "xalign", 0.5, NULL);
1006 gtk_tree_view_column_pack_start (column, renderer, FALSE);
1007 gtk_tree_view_column_set_cell_data_func (column, renderer,
1008 ft_manager_progress_cell_data_func, NULL, NULL);
1010 /* Icon and filename column*/
1011 column = gtk_tree_view_column_new ();
1012 gtk_tree_view_column_set_title (column, _("File"));
1013 gtk_tree_view_column_set_expand (column, TRUE);
1014 gtk_tree_view_column_set_resizable (column, TRUE);
1015 gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
1016 gtk_tree_view_column_set_spacing (column, 3);
1017 gtk_tree_view_insert_column (view, column, -1);
1019 renderer = gtk_cell_renderer_pixbuf_new ();
1020 g_object_set (renderer, "xpad", 3,
1021 "stock-size", GTK_ICON_SIZE_DND, NULL);
1022 gtk_tree_view_column_pack_start (column, renderer, FALSE);
1023 gtk_tree_view_column_set_attributes (column, renderer,
1024 "gicon", COL_ICON, NULL);
1026 renderer = gtk_cell_renderer_text_new ();
1027 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1028 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1029 gtk_tree_view_column_set_attributes (column, renderer,
1030 "text", COL_MESSAGE, NULL);
1032 /* Remaining time column */
1033 column = gtk_tree_view_column_new ();
1034 gtk_tree_view_column_set_title (column, _("Remaining"));
1035 gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
1036 gtk_tree_view_insert_column (view, column, -1);
1038 renderer = gtk_cell_renderer_text_new ();
1039 g_object_set (renderer, "xalign", 0.5, NULL);
1040 gtk_tree_view_column_pack_start (column, renderer, FALSE);
1041 gtk_tree_view_column_set_attributes (column, renderer,
1042 "text", COL_REMAINING, NULL);
1044 /* clear button should be sensitive only if there are completed/cancelled
1045 * handlers in the store.
1047 gtk_widget_set_sensitive (priv->clear_button, FALSE);
1050 /* GObject method overrides */
1053 empathy_ft_manager_finalize (GObject *object)
1055 EmpathyFTManagerPriv *priv = GET_PRIV (object);
1057 DEBUG ("FT Manager %p", object);
1059 g_hash_table_destroy (priv->ft_handler_to_row_ref);
1061 if (priv->save_geometry_id != 0)
1062 g_source_remove (priv->save_geometry_id);
1064 G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
1068 empathy_ft_manager_init (EmpathyFTManager *manager)
1070 EmpathyFTManagerPriv *priv;
1072 priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
1073 EmpathyFTManagerPriv);
1075 manager->priv = priv;
1077 priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
1078 g_direct_equal, (GDestroyNotify) g_object_unref,
1079 (GDestroyNotify) gtk_tree_row_reference_free);
1081 ft_manager_build_ui (manager);
1085 empathy_ft_manager_constructor (GType type,
1087 GObjectConstructParam *props)
1091 if (manager_singleton)
1093 retval = G_OBJECT (manager_singleton);
1097 retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
1098 (type, n_props, props);
1100 manager_singleton = EMPATHY_FT_MANAGER (retval);
1101 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1108 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
1110 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1112 object_class->finalize = empathy_ft_manager_finalize;
1113 object_class->constructor = empathy_ft_manager_constructor;
1115 g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
1118 /* public methods */
1121 empathy_ft_manager_add_handler (EmpathyFTHandler *handler)
1123 EmpathyFTManager *manager;
1124 EmpathyFTManagerPriv *priv;
1126 DEBUG ("Adding handler");
1128 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1130 manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1131 priv = GET_PRIV (manager);
1133 ft_manager_add_handler_to_list (manager, handler, NULL);
1134 gtk_window_present (GTK_WINDOW (priv->window));
1138 empathy_ft_manager_display_error (EmpathyFTHandler *handler,
1139 const GError *error)
1141 EmpathyFTManager *manager;
1142 EmpathyFTManagerPriv *priv;
1144 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1145 g_return_if_fail (error != NULL);
1147 manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1148 priv = GET_PRIV (manager);
1150 ft_manager_add_handler_to_list (manager, handler, error);
1151 gtk_window_present (GTK_WINDOW (priv->window));
1155 empathy_ft_manager_show (void)
1157 EmpathyFTManager *manager;
1158 EmpathyFTManagerPriv *priv;
1160 manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1161 priv = GET_PRIV (manager);
1163 gtk_window_present (GTK_WINDOW (priv->window));