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 row_ref = ft_manager_get_row_from_handler (manager, handler);
565 g_return_if_fail (row_ref != NULL);
567 first_line = g_strdup_printf (_("Hashing \"%s\""),
568 empathy_ft_handler_get_filename (handler));
569 second_line = ft_manager_format_progress_bytes_and_percentage
570 (current_bytes, total_bytes, NULL);
572 message = g_strdup_printf ("%s\n%s", first_line, second_line);
574 ft_manager_update_handler_message (manager, row_ref, message);
580 ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
581 EmpathyFTManager *manager)
584 GtkTreeRowReference *row_ref;
586 DEBUG ("Hashing started");
588 g_signal_connect (handler, "hashing-progress",
589 G_CALLBACK (ft_handler_hashing_progress_cb), manager);
590 g_signal_connect (handler, "hashing-done",
591 G_CALLBACK (ft_handler_hashing_done_cb), manager);
593 row_ref = ft_manager_get_row_from_handler (manager, handler);
594 g_return_if_fail (row_ref != NULL);
596 message = g_strdup_printf (_("Hashing \"%s\""),
597 empathy_ft_handler_get_filename (handler));
599 ft_manager_update_handler_message (manager, row_ref, message);
605 ft_manager_start_transfer (EmpathyFTManager *manager,
606 EmpathyFTHandler *handler)
608 EmpathyFTManagerPriv *priv;
609 gboolean is_incoming;
611 priv = GET_PRIV (manager);
613 is_incoming = empathy_ft_handler_is_incoming (handler);
615 DEBUG ("Start transfer, is incoming %d", is_incoming);
617 /* now connect the signals */
618 g_signal_connect (handler, "transfer-error",
619 G_CALLBACK (ft_handler_transfer_error_cb), manager);
622 g_signal_connect (handler, "transfer-started",
623 G_CALLBACK (ft_handler_transfer_started_cb), manager);
625 g_signal_connect (handler, "hashing-started",
626 G_CALLBACK (ft_handler_hashing_started_cb), manager);
629 empathy_ft_handler_start_transfer (handler);
633 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
634 EmpathyFTHandler *handler,
637 GtkTreeRowReference *row_ref;
639 GtkTreeSelection *selection;
642 const char *content_type, *second_line;
643 char *first_line, *message;
644 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
648 /* get the icon name from the mime-type of the file. */
649 content_type = empathy_ft_handler_get_content_type (handler);
651 if (content_type != NULL)
652 icon = g_content_type_get_icon (content_type);
654 /* append the handler in the store */
655 gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
656 &iter, G_MAXINT, COL_FT_OBJECT, handler,
660 g_object_unref (icon);
662 /* insert the new row_ref in the hash table */
663 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
664 row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path);
665 gtk_tree_path_free (path);
666 g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler),
669 /* select the new row */
670 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
671 gtk_tree_selection_select_iter (selection, &iter);
675 message = ft_manager_format_error_message (handler, error);
676 ft_manager_update_handler_message (manager, row_ref, message);
682 /* update the row with the initial values */
683 if (empathy_ft_handler_is_incoming (handler)) {
684 first_line = ft_manager_format_contact_info (handler);
685 second_line = _("Waiting for the other participant's response");
686 message = g_strdup_printf ("%s\n%s", first_line, second_line);
688 ft_manager_update_handler_message (manager, row_ref, message);
695 /* hook up the signals and start the transfer */
696 ft_manager_start_transfer (manager, handler);
700 ft_manager_clear (EmpathyFTManager *manager)
702 EmpathyFTManagerPriv *priv;
704 DEBUG ("Clearing file transfer list");
706 priv = GET_PRIV (manager);
708 /* Remove completed and cancelled transfers */
709 g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
710 remove_finished_transfer_foreach, manager);
714 ft_manager_open (EmpathyFTManager *manager)
716 GtkTreeSelection *selection;
719 EmpathyFTHandler *handler;
722 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
724 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
726 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
729 gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
731 file = empathy_ft_handler_get_gfile (handler);
732 uri = g_file_get_uri (file);
734 DEBUG ("Opening URI: %s", uri);
735 empathy_url_show (GTK_WIDGET (priv->window), uri);
737 g_object_unref (handler);
742 ft_manager_stop (EmpathyFTManager *manager)
744 GtkTreeSelection *selection;
747 EmpathyFTHandler *handler;
748 EmpathyFTManagerPriv *priv;
750 priv = GET_PRIV (manager);
752 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
754 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
757 gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
758 g_return_if_fail (handler != NULL);
760 DEBUG ("Stopping file transfer: contact=%s, filename=%s",
761 empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
762 empathy_ft_handler_get_filename (handler));
764 empathy_ft_handler_cancel_transfer (handler);
766 g_object_unref (handler);
770 ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager)
772 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
775 gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h);
776 gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y);
778 empathy_geometry_save ("ft-manager", x, y, w, h);
780 priv->save_geometry_id = 0;
786 ft_manager_configure_event_cb (GtkWidget *widget,
787 GdkEventConfigure *event,
788 EmpathyFTManager *manager)
790 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
792 if (priv->save_geometry_id != 0)
793 g_source_remove (priv->save_geometry_id);
795 priv->save_geometry_id = g_timeout_add (500,
796 (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager);
802 ft_manager_response_cb (GtkWidget *widget,
804 EmpathyFTManager *manager)
809 ft_manager_clear (manager);
812 ft_manager_open (manager);
815 ft_manager_stop (manager);
821 ft_manager_delete_event_cb (GtkWidget *widget,
823 EmpathyFTManager *manager)
825 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
827 /* remove all the completed/cancelled/errored transfers */
828 ft_manager_clear (manager);
830 if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
832 /* There is still FTs on flight, just hide the window */
833 DEBUG ("Hiding window");
834 gtk_widget_hide (widget);
842 ft_manager_destroy_cb (GtkWidget *widget,
843 EmpathyFTManager *manager)
845 g_object_unref (manager);
849 ft_manager_build_ui (EmpathyFTManager *manager)
854 GtkListStore *liststore;
855 GtkTreeViewColumn *column;
856 GtkCellRenderer *renderer;
857 GtkTreeSelection *selection;
859 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
861 filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
862 gui = empathy_builder_get_file (filename,
863 "ft_manager_dialog", &priv->window,
864 "ft_list", &priv->treeview,
865 "open_button", &priv->open_button,
866 "abort_button", &priv->abort_button,
870 empathy_builder_connect (gui, manager,
871 "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
872 "ft_manager_dialog", "response", ft_manager_response_cb,
873 "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
874 "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb,
877 g_object_unref (gui);
879 /* Window geometry. */
880 empathy_geometry_load ("ft-manager", &x, &y, &w, &h);
882 if (x >= 0 && y >= 0)
884 /* Let the window manager position it if we don't have
885 * good x, y coordinates. */
886 gtk_window_move (GTK_WINDOW (priv->window), x, y);
891 /* Use the defaults from the ui file if we don't have
892 * good w, h geometry. */
893 gtk_window_resize (GTK_WINDOW (priv->window), w, h);
896 /* Setup the tree view */
897 view = GTK_TREE_VIEW (priv->treeview);
898 selection = gtk_tree_view_get_selection (view);
899 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
900 g_signal_connect (selection, "changed",
901 G_CALLBACK (ft_manager_selection_changed), manager);
902 gtk_tree_view_set_headers_visible (view, TRUE);
903 gtk_tree_view_set_enable_search (view, FALSE);
905 /* Setup the model */
906 liststore = gtk_list_store_new (5,
907 G_TYPE_INT, /* percent */
908 G_TYPE_ICON, /* icon */
909 G_TYPE_STRING, /* message */
910 G_TYPE_STRING, /* remaining */
911 G_TYPE_OBJECT); /* ft_handler */
912 gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
913 priv->model = GTK_TREE_MODEL (liststore);
914 g_object_unref (liststore);
916 /* Progress column */
917 column = gtk_tree_view_column_new ();
918 gtk_tree_view_column_set_title (column, _("%"));
919 gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
920 gtk_tree_view_insert_column (view, column, -1);
922 renderer = gtk_cell_renderer_progress_new ();
923 g_object_set (renderer, "xalign", 0.5, NULL);
924 gtk_tree_view_column_pack_start (column, renderer, FALSE);
925 gtk_tree_view_column_set_cell_data_func (column, renderer,
926 ft_manager_progress_cell_data_func, NULL, NULL);
928 /* Icon and filename column*/
929 column = gtk_tree_view_column_new ();
930 gtk_tree_view_column_set_title (column, _("File"));
931 gtk_tree_view_column_set_expand (column, TRUE);
932 gtk_tree_view_column_set_resizable (column, TRUE);
933 gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
934 gtk_tree_view_column_set_spacing (column, 3);
935 gtk_tree_view_insert_column (view, column, -1);
937 renderer = gtk_cell_renderer_pixbuf_new ();
938 g_object_set (renderer, "xpad", 3,
939 "stock-size", GTK_ICON_SIZE_DND, NULL);
940 gtk_tree_view_column_pack_start (column, renderer, FALSE);
941 gtk_tree_view_column_set_attributes (column, renderer,
942 "gicon", COL_ICON, NULL);
944 renderer = gtk_cell_renderer_text_new ();
945 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
946 gtk_tree_view_column_pack_start (column, renderer, TRUE);
947 gtk_tree_view_column_set_attributes (column, renderer,
948 "text", COL_MESSAGE, NULL);
950 /* Remaining time column */
951 column = gtk_tree_view_column_new ();
952 gtk_tree_view_column_set_title (column, _("Remaining"));
953 gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
954 gtk_tree_view_insert_column (view, column, -1);
956 renderer = gtk_cell_renderer_text_new ();
957 g_object_set (renderer, "xalign", 0.5, NULL);
958 gtk_tree_view_column_pack_start (column, renderer, FALSE);
959 gtk_tree_view_column_set_attributes (column, renderer,
960 "text", COL_REMAINING, NULL);
963 /* GObject method overrides */
966 empathy_ft_manager_finalize (GObject *object)
968 EmpathyFTManagerPriv *priv = GET_PRIV (object);
970 DEBUG ("%p", object);
972 g_hash_table_destroy (priv->ft_handler_to_row_ref);
974 if (priv->save_geometry_id != 0)
975 g_source_remove (priv->save_geometry_id);
977 G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
981 empathy_ft_manager_init (EmpathyFTManager *manager)
983 EmpathyFTManagerPriv *priv;
985 priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
986 EmpathyFTManagerPriv);
988 manager->priv = priv;
990 priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
991 g_direct_equal, (GDestroyNotify) g_object_unref,
992 (GDestroyNotify) gtk_tree_row_reference_free);
994 ft_manager_build_ui (manager);
998 empathy_ft_manager_constructor (GType type,
1000 GObjectConstructParam *props)
1004 if (manager_singleton)
1006 retval = g_object_ref (manager_singleton);
1010 retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
1011 (type, n_props, props);
1013 manager_singleton = EMPATHY_FT_MANAGER (retval);
1014 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1021 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
1023 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1025 object_class->finalize = empathy_ft_manager_finalize;
1026 object_class->constructor = empathy_ft_manager_constructor;
1028 g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
1031 /* public methods */
1034 * empathy_ft_manager_dup_singleton:
1036 * Returns a reference to the #EmpathyFTManager singleton object.
1038 * Returns: a #EmpathyFTManager
1041 empathy_ft_manager_dup_singleton (void)
1043 return g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1047 empathy_ft_manager_add_handler (EmpathyFTManager *manager,
1048 EmpathyFTHandler *handler)
1050 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1052 DEBUG ("Adding handler");
1054 g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1055 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1057 ft_manager_add_handler_to_list (manager, handler, NULL);
1058 gtk_window_present (GTK_WINDOW (priv->window));
1062 empathy_ft_manager_display_error (EmpathyFTManager *manager,
1063 EmpathyFTHandler *handler,
1064 const GError *error)
1066 EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1068 g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1069 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1070 g_return_if_fail (error != NULL);
1072 ft_manager_add_handler_to_list (manager, handler, error);
1073 gtk_window_present (GTK_WINDOW (priv->window));