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;
85 guint save_geometry_id;
86 } EmpathyFTManagerPriv;
95 G_DEFINE_TYPE (EmpathyFTManager, empathy_ft_manager, G_TYPE_OBJECT);
97 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTManager)
99 static EmpathyFTManager *manager_singleton = NULL;
103 ft_manager_format_interval (gint interval)
105 gint hours, mins, secs;
107 hours = interval / 3600;
108 interval -= hours * 3600;
109 mins = interval / 60;
110 interval -= mins * 60;
114 /* Translators: time left, when it is more than one hour */
115 return g_strdup_printf (_("%u:%02u.%02u"), hours, mins, secs);
117 /* Translators: time left, when is is less than one hour */
118 return g_strdup_printf (_("%02u.%02u"), mins, secs);
123 ft_manager_update_buttons (EmpathyFTManager *manager)
125 GtkTreeSelection *selection;
128 EmpathyFTHandler *handler;
129 gboolean open_enabled = FALSE;
130 gboolean abort_enabled = FALSE;
131 gboolean is_completed, is_cancelled;
132 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
134 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
136 if (gtk_tree_selection_get_selected (selection, &model, &iter))
138 gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
140 is_completed = empathy_ft_handler_is_completed (handler);
141 is_cancelled = empathy_ft_handler_is_cancelled (handler);
143 /* I can open the file if the transfer is completed and was incoming */
144 open_enabled = (is_completed && empathy_ft_handler_is_incoming (handler));
146 /* I can abort if the transfer is not already finished */
147 abort_enabled = (is_cancelled == FALSE && is_completed == FALSE);
149 g_object_unref (handler);
152 gtk_widget_set_sensitive (priv->open_button, open_enabled);
153 gtk_widget_set_sensitive (priv->abort_button, abort_enabled);
158 ft_manager_state_change_reason_to_string (TpFileTransferStateChangeReason reason)
162 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
163 return _("No reason was specified");
164 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
165 return _("The change in state was requested");
166 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
167 return _("You canceled the file transfer");
168 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
169 return _("The other participant canceled the file transfer");
170 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
171 return _("Error while trying to transfer the file");
172 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
173 return _("The other participant is unable to transfer the file");
175 return _("Unknown reason");
180 ft_manager_selection_changed (GtkTreeSelection *selection,
181 EmpathyFTManager *manager)
183 ft_manager_update_buttons (manager);
187 ft_manager_progress_cell_data_func (GtkTreeViewColumn *col,
188 GtkCellRenderer *renderer,
193 const gchar *text = NULL;
196 gtk_tree_model_get (model, iter, COL_PERCENT, &percent, -1);
201 text = C_("file transfer percent", "Unknown");
204 g_object_set (renderer, "text", text, "value", percent, NULL);
207 static GtkTreeRowReference *
208 ft_manager_get_row_from_handler (EmpathyFTManager *manager,
209 EmpathyFTHandler *handler)
211 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
213 return g_hash_table_lookup (priv->ft_handler_to_row_ref, handler);
217 ft_manager_remove_file_from_model (EmpathyFTManager *manager,
218 EmpathyFTHandler *handler)
220 GtkTreeRowReference *row_ref;
221 GtkTreeSelection *selection;
222 GtkTreePath *path = NULL;
224 gboolean update_selection;
225 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
227 row_ref = ft_manager_get_row_from_handler (manager, handler);
228 g_return_if_fail (row_ref);
230 DEBUG ("Removing file transfer from window: contact=%s, filename=%s",
231 empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
232 empathy_ft_handler_get_filename (handler));
234 /* Get the iter from the row_ref */
235 path = gtk_tree_row_reference_get_path (row_ref);
236 gtk_tree_model_get_iter (priv->model, &iter, path);
237 gtk_tree_path_free (path);
239 /* We have to update the selection only if we are removing the selected row */
240 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
241 update_selection = gtk_tree_selection_iter_is_selected (selection, &iter);
243 /* Remove tp_file's row. After that iter points to the next row */
244 if (!gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter))
248 /* There is no next row, set iter to the last row */
249 n_row = gtk_tree_model_iter_n_children (priv->model, NULL);
251 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, n_row - 1);
253 update_selection = FALSE;
256 if (update_selection)
257 gtk_tree_selection_select_iter (selection, &iter);
261 remove_finished_transfer_foreach (gpointer key,
265 EmpathyFTHandler *handler = key;
266 EmpathyFTManager *manager = user_data;
268 if (empathy_ft_handler_is_completed (handler) ||
269 empathy_ft_handler_is_cancelled (handler))
271 ft_manager_remove_file_from_model (manager, handler);
279 ft_manager_format_progress_bytes_and_percentage (guint64 current,
283 char *total_str, *current_str, *retval;
285 total_str = g_format_size_for_display (total);
286 current_str = g_format_size_for_display (current);
288 /* translators: first %s is the currently processed size, second %s is
289 * the total file size */
290 retval = g_strdup_printf (_("%s of %s"), current_str, total_str);
293 g_free (current_str);
295 if (percentage != NULL)
298 *percentage = current * 100 / total;
307 ft_manager_format_contact_info (EmpathyFTHandler *handler)
310 const char *filename, *contact_name, *first_line_format;
313 incoming = empathy_ft_handler_is_incoming (handler);
314 contact_name = empathy_contact_get_name
315 (empathy_ft_handler_get_contact (handler));
316 filename = empathy_ft_handler_get_filename (handler);
319 /* translators: first %s is filename, second %s is the contact name */
320 first_line_format = _("Receiving \"%s\" from %s");
322 /* translators: first %s is filename, second %s is the contact name */
323 first_line_format = _("Sending \"%s\" to %s");
325 retval = g_strdup_printf (first_line_format, filename, contact_name);
331 ft_manager_format_error_message (EmpathyFTHandler *handler,
334 const char *contact_name, *filename;
335 EmpathyContact *contact;
336 char *first_line, *message;
340 incoming = empathy_ft_handler_is_incoming (handler);
342 contact = empathy_ft_handler_get_contact (handler);
344 contact_name = empathy_contact_get_name (contact);
346 filename = empathy_ft_handler_get_filename (handler);
349 /* filename/contact_name here are either both NULL or both valid */
350 if (filename && contact_name)
351 /* translators: first %s is filename, second %s
352 * is the contact name */
353 first_line = g_strdup_printf (_("Error receiving \"%s\" from %s"), filename,
356 first_line = g_strdup (_("Error receiving a file"));
358 /* translators: first %s is filename, second %s
359 * is the contact name */
360 if (filename && contact_name)
361 first_line = g_strdup_printf (_("Error sending \"%s\" to %s"), filename,
364 first_line = g_strdup (_("Error sending a file"));
366 message = g_strdup_printf ("%s\n%s", first_line, error->message);
374 ft_manager_update_handler_message (EmpathyFTManager *manager,
375 GtkTreeRowReference *row_ref,
380 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
382 /* Set new value in the store */
383 path = gtk_tree_row_reference_get_path (row_ref);
384 gtk_tree_model_get_iter (priv->model, &iter, path);
385 gtk_list_store_set (GTK_LIST_STORE (priv->model),
387 COL_MESSAGE, message ? message : "",
390 gtk_tree_path_free (path);
394 ft_manager_update_handler_progress (EmpathyFTManager *manager,
395 GtkTreeRowReference *row_ref,
400 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
402 /* Set new value in the store */
403 path = gtk_tree_row_reference_get_path (row_ref);
404 gtk_tree_model_get_iter (priv->model, &iter, path);
405 gtk_list_store_set (GTK_LIST_STORE (priv->model),
407 COL_PERCENT, percentage,
410 gtk_tree_path_free (path);
415 ft_handler_transfer_error_cb (EmpathyFTHandler *handler,
417 EmpathyFTManager *manager)
420 GtkTreeRowReference *row_ref;
422 DEBUG ("Transfer error %s", error->message);
424 row_ref = ft_manager_get_row_from_handler (manager, handler);
425 g_return_if_fail (row_ref != NULL);
427 message = ft_manager_format_error_message (handler, error);
429 ft_manager_update_handler_message (manager, row_ref, message);
430 ft_manager_update_buttons (manager);
436 ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
437 EmpathyTpFile *tp_file,
438 EmpathyFTManager *manager)
440 const char *contact_name;
441 const char *filename;
442 char *first_line, *second_line, *message;
444 GtkTreeRowReference *row_ref;
446 DEBUG ("Transfer done");
448 row_ref = ft_manager_get_row_from_handler (manager, handler);
449 g_return_if_fail (row_ref != NULL);
451 incoming = empathy_ft_handler_is_incoming (handler);
452 contact_name = empathy_contact_get_name
453 (empathy_ft_handler_get_contact (handler));
454 filename = empathy_ft_handler_get_filename (handler);
457 /* translators: first %s is filename, second %s
458 * is the contact name */
459 first_line = g_strdup_printf (_("\"%s\" received from %s"), filename,
462 /* translators: first %s is filename, second %s
463 * is the contact name */
464 first_line = g_strdup_printf (_("\"%s\" sent to %s"), filename,
467 second_line = g_strdup (_("File transfer completed"));
469 message = g_strdup_printf ("%s\n%s", first_line, second_line);
470 ft_manager_update_handler_message (manager, row_ref, message);
473 ft_manager_update_buttons (manager);
477 g_free (second_line);
481 ft_handler_transfer_progress_cb (EmpathyFTHandler *handler,
482 guint64 current_bytes,
484 EmpathyFTManager *manager)
486 char *first_line, *second_line, *message;
488 GtkTreeRowReference *row_ref;
490 DEBUG ("Transfer progress");
492 row_ref = ft_manager_get_row_from_handler (manager, handler);
493 g_return_if_fail (row_ref != NULL);
495 first_line = ft_manager_format_contact_info (handler);
496 second_line = ft_manager_format_progress_bytes_and_percentage
497 (current_bytes, total_bytes, &percentage);
499 message = g_strdup_printf ("%s\n%s", first_line, second_line);
501 ft_manager_update_handler_message (manager, row_ref, message);
502 ft_manager_update_handler_progress (manager, row_ref, percentage);
508 ft_handler_transfer_started_cb (EmpathyFTHandler *handler,
509 EmpathyTpFile *tp_file,
510 EmpathyFTManager *manager)
512 guint64 transferred_bytes, total_bytes;
514 DEBUG ("Transfer started");
516 g_signal_connect (handler, "transfer-progress",
517 G_CALLBACK (ft_handler_transfer_progress_cb), manager);
518 g_signal_connect (handler, "transfer-done",
519 G_CALLBACK (ft_handler_transfer_done_cb), manager);
521 transferred_bytes = empathy_ft_handler_get_transferred_bytes (handler);
522 total_bytes = empathy_ft_handler_get_total_bytes (handler);
524 ft_handler_transfer_progress_cb (handler, transferred_bytes, total_bytes,
529 ft_handler_hashing_done_cb (EmpathyFTHandler *handler,
530 EmpathyFTManager *manager)
532 GtkTreeRowReference *row_ref;
533 char *first_line, *second_line, *message;
535 DEBUG ("Hashing done");
537 row_ref = ft_manager_get_row_from_handler (manager, handler);
538 g_return_if_fail (row_ref != NULL);
540 /* update the message */
541 first_line = ft_manager_format_contact_info (handler);
542 second_line = g_strdup (_("Waiting for the other participant's response"));
543 message = g_strdup_printf ("%s\n%s", first_line, second_line);
545 ft_manager_update_handler_message (manager, row_ref, message);
549 g_free (second_line);
551 g_signal_connect (handler, "transfer-started",
552 G_CALLBACK (ft_handler_transfer_started_cb), manager);
556 ft_handler_hashing_progress_cb (EmpathyFTHandler *handler,
557 guint64 current_bytes,
559 EmpathyFTManager *manager)
561 char *first_line, *second_line, *message;
562 GtkTreeRowReference *row_ref;
564 DEBUG ("Hashing progress");
566 row_ref = ft_manager_get_row_from_handler (manager, handler);
567 g_return_if_fail (row_ref != NULL);
569 first_line = g_strdup_printf (_("Hashing \"%s\""),
570 empathy_ft_handler_get_filename (handler));
571 second_line = ft_manager_format_progress_bytes_and_percentage
572 (current_bytes, total_bytes, NULL);
574 message = g_strdup_printf ("%s\n%s", first_line, second_line);
576 ft_manager_update_handler_message (manager, row_ref, message);
582 ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
583 EmpathyFTManager *manager)
586 GtkTreeRowReference *row_ref;
588 DEBUG ("Hashing started");
590 g_signal_connect (handler, "hashing-progress",
591 G_CALLBACK (ft_handler_hashing_progress_cb), manager);
592 g_signal_connect (handler, "hashing-done",
593 G_CALLBACK (ft_handler_hashing_done_cb), manager);
595 row_ref = ft_manager_get_row_from_handler (manager, handler);
596 g_return_if_fail (row_ref != NULL);
598 message = g_strdup_printf (_("Hashing \"%s\""),
599 empathy_ft_handler_get_filename (handler));
601 ft_manager_update_handler_message (manager, row_ref, message);
607 ft_manager_start_transfer (EmpathyFTManager *manager,
608 EmpathyFTHandler *handler)
610 EmpathyFTManagerPriv *priv;
611 gboolean is_incoming;
613 priv = GET_PRIV (manager);
615 is_incoming = empathy_ft_handler_is_incoming (handler);
617 DEBUG ("Start transfer, is incoming %d", is_incoming);
619 /* now connect the signals */
620 g_signal_connect (handler, "transfer-error",
621 G_CALLBACK (ft_handler_transfer_error_cb), manager);
624 g_signal_connect (handler, "transfer-started",
625 G_CALLBACK (ft_handler_transfer_started_cb), manager);
627 g_signal_connect (handler, "hashing-started",
628 G_CALLBACK (ft_handler_hashing_started_cb), manager);
631 empathy_ft_handler_start_transfer (handler);
635 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
636 EmpathyFTHandler *handler,
639 GtkTreeRowReference *row_ref;
641 GtkTreeSelection *selection;
644 const char *content_type, *second_line;
645 char *first_line, *message;
646 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
650 /* get the icon name from the mime-type of the file. */
651 content_type = empathy_ft_handler_get_content_type (handler);
653 if (content_type != NULL)
654 icon = g_content_type_get_icon (content_type);
656 /* append the handler in the store */
657 gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
658 &iter, G_MAXINT, COL_FT_OBJECT, handler,
662 g_object_unref (icon);
664 /* insert the new row_ref in the hash table */
665 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
666 row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path);
667 gtk_tree_path_free (path);
668 g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler),
671 /* select the new row */
672 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
673 gtk_tree_selection_select_iter (selection, &iter);
677 message = ft_manager_format_error_message (handler, error);
678 ft_manager_update_handler_message (manager, row_ref, message);
684 /* update the row with the initial values */
685 if (empathy_ft_handler_is_incoming (handler)) {
686 first_line = ft_manager_format_contact_info (handler);
687 second_line = _("Waiting for the other participant's response");
688 message = g_strdup_printf ("%s\n%s", first_line, second_line);
690 ft_manager_update_handler_message (manager, row_ref, message);
697 /* hook up the signals and start the transfer */
698 ft_manager_start_transfer (manager, handler);
702 ft_manager_clear (EmpathyFTManager *manager)
704 EmpathyFTManagerPriv *priv;
706 DEBUG ("Clearing file transfer list");
708 priv = GET_PRIV (manager);
710 /* Remove completed and cancelled transfers */
711 g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
712 remove_finished_transfer_foreach, manager);
716 ft_manager_open (EmpathyFTManager *manager)
718 GtkTreeSelection *selection;
721 EmpathyFTHandler *handler;
724 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
726 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
728 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
731 gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
733 file = empathy_ft_handler_get_gfile (handler);
734 uri = g_file_get_uri (file);
736 DEBUG ("Opening URI: %s", uri);
737 empathy_url_show (GTK_WIDGET (priv->window), uri);
739 g_object_unref (handler);
744 ft_manager_stop (EmpathyFTManager *manager)
746 GtkTreeSelection *selection;
749 EmpathyFTHandler *handler;
750 EmpathyFTManagerPriv *priv;
752 priv = GET_PRIV (manager);
754 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
756 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
759 gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
760 g_return_if_fail (handler != NULL);
762 DEBUG ("Stopping file transfer: contact=%s, filename=%s",
763 empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
764 empathy_ft_handler_get_filename (handler));
766 empathy_ft_handler_cancel_transfer (handler);
768 g_object_unref (handler);
772 ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager)
774 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
777 gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h);
778 gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y);
780 empathy_geometry_save ("ft-manager", x, y, w, h);
782 priv->save_geometry_id = 0;
788 ft_manager_configure_event_cb (GtkWidget *widget,
789 GdkEventConfigure *event,
790 EmpathyFTManager *manager)
792 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
794 if (priv->save_geometry_id != 0)
795 g_source_remove (priv->save_geometry_id);
797 priv->save_geometry_id = g_timeout_add (500,
798 (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager);
804 ft_manager_response_cb (GtkWidget *widget,
806 EmpathyFTManager *manager)
811 ft_manager_clear (manager);
814 ft_manager_open (manager);
817 ft_manager_stop (manager);
823 ft_manager_delete_event_cb (GtkWidget *widget,
825 EmpathyFTManager *manager)
827 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
829 /* remove all the completed/cancelled/errored transfers */
830 ft_manager_clear (manager);
832 if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
834 /* There is still FTs on flight, just hide the window */
835 DEBUG ("Hiding window");
836 gtk_widget_hide (widget);
844 ft_manager_destroy_cb (GtkWidget *widget,
845 EmpathyFTManager *manager)
847 g_object_unref (manager);
851 ft_manager_build_ui (EmpathyFTManager *manager)
856 GtkListStore *liststore;
857 GtkTreeViewColumn *column;
858 GtkCellRenderer *renderer;
859 GtkTreeSelection *selection;
861 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
863 filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
864 gui = empathy_builder_get_file (filename,
865 "ft_manager_dialog", &priv->window,
866 "ft_list", &priv->treeview,
867 "open_button", &priv->open_button,
868 "abort_button", &priv->abort_button,
872 empathy_builder_connect (gui, manager,
873 "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
874 "ft_manager_dialog", "response", ft_manager_response_cb,
875 "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
876 "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb,
879 g_object_unref (gui);
881 /* Window geometry. */
882 empathy_geometry_load ("ft-manager", &x, &y, &w, &h);
884 if (x >= 0 && y >= 0)
886 /* Let the window manager position it if we don't have
887 * good x, y coordinates. */
888 gtk_window_move (GTK_WINDOW (priv->window), x, y);
893 /* Use the defaults from the ui file if we don't have
894 * good w, h geometry. */
895 gtk_window_resize (GTK_WINDOW (priv->window), w, h);
898 /* Setup the tree view */
899 view = GTK_TREE_VIEW (priv->treeview);
900 selection = gtk_tree_view_get_selection (view);
901 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
902 g_signal_connect (selection, "changed",
903 G_CALLBACK (ft_manager_selection_changed), manager);
904 gtk_tree_view_set_headers_visible (view, TRUE);
905 gtk_tree_view_set_enable_search (view, FALSE);
907 /* Setup the model */
908 liststore = gtk_list_store_new (5,
909 G_TYPE_INT, /* percent */
910 G_TYPE_ICON, /* icon */
911 G_TYPE_STRING, /* message */
912 G_TYPE_STRING, /* remaining */
913 G_TYPE_OBJECT); /* ft_handler */
914 gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
915 priv->model = GTK_TREE_MODEL (liststore);
916 g_object_unref (liststore);
918 /* Progress column */
919 column = gtk_tree_view_column_new ();
920 gtk_tree_view_column_set_title (column, _("%"));
921 gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
922 gtk_tree_view_insert_column (view, column, -1);
924 renderer = gtk_cell_renderer_progress_new ();
925 g_object_set (renderer, "xalign", 0.5, NULL);
926 gtk_tree_view_column_pack_start (column, renderer, FALSE);
927 gtk_tree_view_column_set_cell_data_func (column, renderer,
928 ft_manager_progress_cell_data_func, NULL, NULL);
930 /* Icon and filename column*/
931 column = gtk_tree_view_column_new ();
932 gtk_tree_view_column_set_title (column, _("File"));
933 gtk_tree_view_column_set_expand (column, TRUE);
934 gtk_tree_view_column_set_resizable (column, TRUE);
935 gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
936 gtk_tree_view_column_set_spacing (column, 3);
937 gtk_tree_view_insert_column (view, column, -1);
939 renderer = gtk_cell_renderer_pixbuf_new ();
940 g_object_set (renderer, "xpad", 3,
941 "stock-size", GTK_ICON_SIZE_DND, NULL);
942 gtk_tree_view_column_pack_start (column, renderer, FALSE);
943 gtk_tree_view_column_set_attributes (column, renderer,
944 "gicon", COL_ICON, NULL);
946 renderer = gtk_cell_renderer_text_new ();
947 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
948 gtk_tree_view_column_pack_start (column, renderer, TRUE);
949 gtk_tree_view_column_set_attributes (column, renderer,
950 "text", COL_MESSAGE, NULL);
952 /* Remaining time column */
953 column = gtk_tree_view_column_new ();
954 gtk_tree_view_column_set_title (column, _("Remaining"));
955 gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
956 gtk_tree_view_insert_column (view, column, -1);
958 renderer = gtk_cell_renderer_text_new ();
959 g_object_set (renderer, "xalign", 0.5, NULL);
960 gtk_tree_view_column_pack_start (column, renderer, FALSE);
961 gtk_tree_view_column_set_attributes (column, renderer,
962 "text", COL_REMAINING, NULL);
965 /* GObject method overrides */
968 empathy_ft_manager_finalize (GObject *object)
970 EmpathyFTManagerPriv *priv = GET_PRIV (object);
972 DEBUG ("%p", object);
974 g_hash_table_destroy (priv->ft_handler_to_row_ref);
976 if (priv->save_geometry_id != 0)
977 g_source_remove (priv->save_geometry_id);
979 G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
983 empathy_ft_manager_init (EmpathyFTManager *manager)
985 EmpathyFTManagerPriv *priv;
987 priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
988 EmpathyFTManagerPriv);
990 manager->priv = priv;
992 priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
993 g_direct_equal, (GDestroyNotify) g_object_unref,
994 (GDestroyNotify) gtk_tree_row_reference_free);
996 ft_manager_build_ui (manager);
1000 empathy_ft_manager_constructor (GType type,
1002 GObjectConstructParam *props)
1006 if (manager_singleton)
1008 retval = g_object_ref (manager_singleton);
1012 retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
1013 (type, n_props, props);
1015 manager_singleton = EMPATHY_FT_MANAGER (retval);
1016 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1023 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
1025 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1027 object_class->finalize = empathy_ft_manager_finalize;
1028 object_class->constructor = empathy_ft_manager_constructor;
1030 g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
1033 /* public methods */
1036 * empathy_ft_manager_dup_singleton:
1038 * Returns a reference to the #EmpathyFTManager singleton object.
1040 * Returns: a #EmpathyFTManager
1043 empathy_ft_manager_dup_singleton (void)
1045 return g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1049 empathy_ft_manager_add_handler (EmpathyFTManager *manager,
1050 EmpathyFTHandler *handler)
1052 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1054 DEBUG ("Adding handler");
1056 g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1057 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1059 ft_manager_add_handler_to_list (manager, handler, NULL);
1060 gtk_window_present (GTK_WINDOW (priv->window));
1064 empathy_ft_manager_display_error (EmpathyFTManager *manager,
1065 EmpathyFTHandler *handler,
1066 const GError *error)
1068 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1070 g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1071 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1072 g_return_if_fail (error != NULL);
1074 ft_manager_add_handler_to_list (manager, handler, error);
1075 gtk_window_present (GTK_WINDOW (priv->window));