]> git.0d.be Git - empathy.git/blob - src/empathy-ft-manager.c
Remove the singleton logic from the manager
[empathy.git] / src / empathy-ft-manager.c
1 /*
2  * Copyright (C) 2003, 2004 Xan Lopez
3  * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
4  * Copyright (C) 2008-2009 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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
20  *
21  * Authors: Xan Lopez
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>
26  */
27
28 /* The original file transfer manager code was copied from Epiphany */
29
30 #include "config.h"
31
32 #include <string.h>
33
34 #include <glib/gi18n.h>
35 #include <gtk/gtk.h>
36
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>
41
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>
46
47 #include "empathy-ft-manager.h"
48
49
50 /**
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
55  *
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()).
59  */
60
61 enum
62 {
63   COL_PERCENT,
64   COL_ICON,
65   COL_MESSAGE,
66   COL_REMAINING,
67   COL_FT_OBJECT
68 };
69
70 /**
71  * EmpathyFTManagerPriv:
72  *
73  * Private fields of the #EmpathyFTManager class.
74  */
75 typedef struct {
76   GtkTreeModel *model;
77   GHashTable *ft_handler_to_row_ref;
78
79   /* Widgets */
80   GtkWidget *window;
81   GtkWidget *treeview;
82   GtkWidget *open_button;
83   GtkWidget *abort_button;
84   GtkWidget *clear_button;
85
86   guint save_geometry_id;
87 } EmpathyFTManagerPriv;
88
89 enum
90 {
91   RESPONSE_OPEN  = 1,
92   RESPONSE_STOP  = 2,
93   RESPONSE_CLEAR = 3
94 };
95
96 G_DEFINE_TYPE (EmpathyFTManager, empathy_ft_manager, G_TYPE_OBJECT);
97
98 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTManager)
99
100 static EmpathyFTManager *manager_singleton = NULL;
101
102 static void ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
103     EmpathyFTManager *manager);
104
105 static gchar *
106 ft_manager_format_interval (guint interval)
107 {
108   gint hours, mins, secs;
109
110   hours = interval / 3600;
111   interval -= hours * 3600;
112   mins = interval / 60;
113   interval -= mins * 60;
114   secs = interval;
115
116   if (hours > 0)
117     /* Translators: time left, when it is more than one hour */
118     return g_strdup_printf (_("%u:%02u.%02u"), hours, mins, secs);
119   else
120     /* Translators: time left, when is is less than one hour */
121     return g_strdup_printf (_("%02u.%02u"), mins, secs);
122 }
123
124 static void
125 ft_manager_update_buttons (EmpathyFTManager *manager)
126 {
127   GtkTreeSelection *selection;
128   GtkTreeModel *model;
129   GtkTreeIter iter;
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);
137
138   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
139
140   if (gtk_tree_selection_get_selected (selection, &model, &iter))
141     {
142       gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
143
144       is_completed = empathy_ft_handler_is_completed (handler);
145       is_cancelled = empathy_ft_handler_is_cancelled (handler);
146
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));
149
150       /* I can abort if the transfer is not already finished */
151       abort_enabled = (is_cancelled == FALSE && is_completed == FALSE);
152
153       g_object_unref (handler);
154     }
155
156   g_hash_table_iter_init (&hash_iter, priv->ft_handler_to_row_ref);
157
158   while (g_hash_table_iter_next (&hash_iter, (gpointer *) &handler, NULL))
159     {
160       if (empathy_ft_handler_is_completed (handler) ||
161           empathy_ft_handler_is_cancelled (handler))
162           clear_enabled = TRUE;
163
164       if (clear_enabled)
165         break;
166     }
167
168   gtk_widget_set_sensitive (priv->open_button, open_enabled);
169   gtk_widget_set_sensitive (priv->abort_button, abort_enabled);
170
171   if (clear_enabled)
172     gtk_widget_set_sensitive (priv->clear_button, TRUE);
173 }
174
175 static void
176 ft_manager_selection_changed (GtkTreeSelection *selection,
177                               EmpathyFTManager *manager)
178 {
179   ft_manager_update_buttons (manager);
180 }
181
182 static void
183 ft_manager_progress_cell_data_func (GtkTreeViewColumn *col,
184                                     GtkCellRenderer *renderer,
185                                     GtkTreeModel *model,
186                                     GtkTreeIter *iter,
187                                     gpointer user_data)
188 {
189   const gchar *text = NULL;
190   gint percent;
191
192   gtk_tree_model_get (model, iter, COL_PERCENT, &percent, -1);
193
194   if (percent < 0)
195     {
196       percent = 0;
197       text = C_("file transfer percent", "Unknown");
198     }
199
200   g_object_set (renderer, "text", text, "value", percent, NULL);
201 }
202
203 static GtkTreeRowReference *
204 ft_manager_get_row_from_handler (EmpathyFTManager *manager,
205                                  EmpathyFTHandler *handler)
206 {
207   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
208
209   return g_hash_table_lookup (priv->ft_handler_to_row_ref, handler);
210 }
211
212 static void
213 ft_manager_remove_file_from_model (EmpathyFTManager *manager,
214                                    EmpathyFTHandler *handler)
215 {
216   GtkTreeRowReference *row_ref;
217   GtkTreeSelection *selection;
218   GtkTreePath *path = NULL;
219   GtkTreeIter iter;
220   gboolean update_selection;
221   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
222
223   row_ref = ft_manager_get_row_from_handler (manager, handler);
224   g_return_if_fail (row_ref);
225
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));
229
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);
234
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);
238
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))
241     {
242       gint n_row;
243
244       /* There is no next row, set iter to the last row */
245       n_row = gtk_tree_model_iter_n_children (priv->model, NULL);
246       if (n_row > 0)
247         gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, n_row - 1);
248       else
249         update_selection = FALSE;
250     }
251
252   if (update_selection)
253     gtk_tree_selection_select_iter (selection, &iter);
254 }
255
256 static gboolean
257 remove_finished_transfer_foreach (gpointer key,
258                                   gpointer value,
259                                   gpointer user_data)
260 {
261   EmpathyFTHandler *handler = key;
262   EmpathyFTManager *manager = user_data;
263
264   if (empathy_ft_handler_is_completed (handler) ||
265       empathy_ft_handler_is_cancelled (handler))
266     {
267       ft_manager_remove_file_from_model (manager, handler);
268       return TRUE;
269     }
270
271   return FALSE;
272 }
273
274 static char *
275 ft_manager_format_progress_bytes_and_percentage (guint64 current,
276                                                  guint64 total,
277                                                  gdouble speed,
278                                                  int *percentage)
279 {
280   char *total_str, *current_str, *retval;
281   char *speed_str = NULL;
282
283   total_str = g_format_size_for_display (total);
284   current_str = g_format_size_for_display (current);
285
286   if (speed > 0)
287     speed_str = g_format_size_for_display ((goffset) speed);
288
289   /* translators: first %s is the currently processed size, second %s is
290    * the total file size */
291   retval = speed_str ?
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);
294
295   g_free (total_str);
296   g_free (current_str);
297   g_free (speed_str);
298
299   if (percentage != NULL)
300     {
301       if (total != 0) 
302         *percentage = current * 100 / total;
303       else
304         *percentage = -1;
305     }
306
307   return retval;
308 }
309
310 static char *
311 ft_manager_format_contact_info (EmpathyFTHandler *handler)
312 {
313   gboolean incoming;
314   const char *filename, *contact_name, *first_line_format;
315   char *retval;
316
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);
321
322   if (incoming)
323     /* translators: first %s is filename, second %s is the contact name */
324     first_line_format = _("Receiving \"%s\" from %s");
325   else
326     /* translators: first %s is filename, second %s is the contact name */
327     first_line_format = _("Sending \"%s\" to %s");
328
329   retval = g_strdup_printf (first_line_format, filename, contact_name);
330
331   return retval;
332 }
333
334 static char *
335 ft_manager_format_error_message (EmpathyFTHandler *handler,
336                                  const GError *error)
337 {
338   const char *contact_name, *filename;
339   EmpathyContact *contact;
340   char *first_line, *message;
341   gboolean incoming;
342
343   contact_name = NULL;
344   incoming = empathy_ft_handler_is_incoming (handler);
345
346   contact = empathy_ft_handler_get_contact (handler);
347   if (contact)
348     contact_name = empathy_contact_get_name (contact);
349
350   filename = empathy_ft_handler_get_filename (handler);
351
352   if (incoming)
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,
358           contact_name);
359     else
360       first_line = g_strdup (_("Error receiving a file"));
361   else
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,
366           contact_name);
367     else
368       first_line = g_strdup (_("Error sending a file"));
369
370   message = g_strdup_printf ("%s\n%s", first_line, error->message);
371
372   g_free (first_line);
373
374   return message;
375 }
376
377 static void
378 ft_manager_update_handler_message (EmpathyFTManager *manager,
379                                    GtkTreeRowReference *row_ref,
380                                    const char *message)
381 {
382   GtkTreePath *path;
383   GtkTreeIter iter;
384   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
385
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),
390       &iter,
391       COL_MESSAGE, message ? message : "",
392       -1);
393
394   gtk_tree_path_free (path);
395 }
396
397 static void
398 ft_manager_update_handler_progress (EmpathyFTManager *manager,
399                                     GtkTreeRowReference *row_ref,
400                                     int percentage)
401 {
402   GtkTreePath *path;
403   GtkTreeIter iter;
404   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
405
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),
410       &iter,
411       COL_PERCENT, percentage,
412       -1);
413
414   gtk_tree_path_free (path);
415
416 }
417
418 static void
419 ft_manager_update_handler_time (EmpathyFTManager *manager,
420                                 GtkTreeRowReference *row_ref,
421                                 guint remaining_time)
422 {
423   GtkTreePath *path;
424   GtkTreeIter iter;
425   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
426   char *remaining_str;
427
428   remaining_str = ft_manager_format_interval (remaining_time);
429
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),
434       &iter,
435       COL_REMAINING, remaining_str,
436       -1);
437
438   gtk_tree_path_free (path);
439   g_free (remaining_str);
440 }
441
442 static void
443 ft_handler_transfer_error_cb (EmpathyFTHandler *handler,
444                               GError *error,
445                               EmpathyFTManager *manager)
446 {
447   char *message;
448   GtkTreeRowReference *row_ref;
449
450   DEBUG ("Transfer error %s", error->message);
451
452   row_ref = ft_manager_get_row_from_handler (manager, handler);
453   g_return_if_fail (row_ref != NULL);
454
455   message = ft_manager_format_error_message (handler, error);
456
457   ft_manager_update_handler_message (manager, row_ref, message);
458   ft_manager_update_buttons (manager);
459
460   g_free (message);
461 }
462
463 static void
464 ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
465                              EmpathyTpFile *tp_file,
466                              EmpathyFTManager *manager)
467 {
468   const char *contact_name;
469   const char *filename;
470   char *first_line, *second_line, *message;
471   gboolean incoming;
472   GtkTreeRowReference *row_ref;
473
474   DEBUG ("Transfer done");
475
476   row_ref = ft_manager_get_row_from_handler (manager, handler);
477   g_return_if_fail (row_ref != NULL);
478
479   incoming = empathy_ft_handler_is_incoming (handler);
480   contact_name = empathy_contact_get_name
481     (empathy_ft_handler_get_contact (handler));
482   filename = empathy_ft_handler_get_filename (handler);
483
484   if (incoming)
485     /* translators: first %s is filename, second %s
486      * is the contact name */
487     first_line = g_strdup_printf (_("\"%s\" received from %s"), filename,
488         contact_name);
489   else
490     /* translators: first %s is filename, second %s
491      * is the contact name */
492     first_line = g_strdup_printf (_("\"%s\" sent to %s"), filename,
493         contact_name);
494
495   second_line = g_strdup (_("File transfer completed"));
496
497   message = g_strdup_printf ("%s\n%s", first_line, second_line);
498   ft_manager_update_handler_message (manager, row_ref, message);
499
500   /* update buttons */
501   ft_manager_update_buttons (manager);
502
503   g_free (message);
504   g_free (first_line);
505   g_free (second_line);
506
507   if (empathy_ft_handler_is_incoming (handler) &&
508       empathy_ft_handler_get_use_hash (handler))
509       g_signal_connect (handler, "hashing-started",
510           G_CALLBACK (ft_handler_hashing_started_cb), manager);
511 }
512
513 static void
514 ft_handler_transfer_progress_cb (EmpathyFTHandler *handler,
515                                  guint64 current_bytes,
516                                  guint64 total_bytes,
517                                  guint remaining_time,
518                                  gdouble speed,
519                                  EmpathyFTManager *manager)
520 {
521   char *first_line, *second_line, *message;
522   int percentage;
523   GtkTreeRowReference *row_ref;
524
525   DEBUG ("Transfer progress");
526
527   row_ref = ft_manager_get_row_from_handler (manager, handler);
528   g_return_if_fail (row_ref != NULL);
529
530   first_line = ft_manager_format_contact_info (handler);
531   second_line = ft_manager_format_progress_bytes_and_percentage
532     (current_bytes, total_bytes, speed, &percentage);
533
534   message = g_strdup_printf ("%s\n%s", first_line, second_line);
535
536   ft_manager_update_handler_message (manager, row_ref, message);
537   ft_manager_update_handler_progress (manager, row_ref, percentage);
538
539   if (remaining_time > 0)
540     ft_manager_update_handler_time (manager, row_ref, remaining_time);
541
542   g_free (message);
543 }
544
545 static void
546 ft_handler_transfer_started_cb (EmpathyFTHandler *handler,
547                                 EmpathyTpFile *tp_file,
548                                 EmpathyFTManager *manager)
549 {
550   guint64 transferred_bytes, total_bytes;
551
552   DEBUG ("Transfer started");
553
554   g_signal_connect (handler, "transfer-progress",
555       G_CALLBACK (ft_handler_transfer_progress_cb), manager);
556   g_signal_connect (handler, "transfer-done",
557       G_CALLBACK (ft_handler_transfer_done_cb), manager);
558
559   transferred_bytes = empathy_ft_handler_get_transferred_bytes (handler);
560   total_bytes = empathy_ft_handler_get_total_bytes (handler);
561
562   ft_handler_transfer_progress_cb (handler, transferred_bytes, total_bytes,
563       0, -1, manager);
564 }
565
566 static void
567 ft_handler_hashing_done_cb (EmpathyFTHandler *handler,
568                             EmpathyFTManager *manager)
569 {
570   GtkTreeRowReference *row_ref;
571   char *first_line, *second_line, *message;
572
573   DEBUG ("Hashing done");
574
575   row_ref = ft_manager_get_row_from_handler (manager, handler);
576   g_return_if_fail (row_ref != NULL);
577
578   /* update the message */
579   first_line = ft_manager_format_contact_info (handler);
580   second_line = g_strdup (_("Waiting for the other participant's response"));
581   message = g_strdup_printf ("%s\n%s", first_line, second_line);
582
583   ft_manager_update_handler_message (manager, row_ref, message);
584
585   g_free (message);
586   g_free (first_line);
587   g_free (second_line);
588
589   if (!empathy_ft_handler_is_incoming (handler))
590     g_signal_connect (handler, "transfer-started",
591         G_CALLBACK (ft_handler_transfer_started_cb), manager);
592 }
593
594 static void
595 ft_handler_hashing_progress_cb (EmpathyFTHandler *handler,
596                                 guint64 current_bytes,
597                                 guint64 total_bytes,
598                                 EmpathyFTManager *manager)
599 {
600   char *first_line, *second_line, *message;
601   GtkTreeRowReference *row_ref;
602
603   row_ref = ft_manager_get_row_from_handler (manager, handler);
604   g_return_if_fail (row_ref != NULL);
605
606   first_line =  g_strdup_printf (_("Hashing \"%s\""),
607       empathy_ft_handler_get_filename (handler));
608   second_line = ft_manager_format_progress_bytes_and_percentage
609     (current_bytes, total_bytes, -1, NULL);
610
611   message = g_strdup_printf ("%s\n%s", first_line, second_line);
612
613   ft_manager_update_handler_message (manager, row_ref, message);
614
615   g_free (message);
616 }
617
618 static void
619 ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
620                                EmpathyFTManager *manager)
621 {
622   char *message;
623   GtkTreeRowReference *row_ref;
624
625   DEBUG ("Hashing started");
626
627   g_signal_connect (handler, "hashing-progress",
628      G_CALLBACK (ft_handler_hashing_progress_cb), manager);
629   g_signal_connect (handler, "hashing-done",
630      G_CALLBACK (ft_handler_hashing_done_cb), manager);
631
632   row_ref = ft_manager_get_row_from_handler (manager, handler);
633   g_return_if_fail (row_ref != NULL);
634
635   message =  g_strdup_printf (_("Hashing \"%s\""),
636       empathy_ft_handler_get_filename (handler));
637
638   ft_manager_update_handler_message (manager, row_ref, message);
639
640   g_free (message);
641 }
642
643 static void
644 ft_manager_start_transfer (EmpathyFTManager *manager,
645                            EmpathyFTHandler *handler)
646 {
647   EmpathyFTManagerPriv *priv;
648   gboolean is_outgoing;
649
650   priv = GET_PRIV (manager);
651
652   is_outgoing = !empathy_ft_handler_is_incoming (handler);
653
654   DEBUG ("Start transfer, is outgoing %s",
655       is_outgoing ? "True" : "False");
656
657   /* now connect the signals */
658   g_signal_connect (handler, "transfer-error",
659       G_CALLBACK (ft_handler_transfer_error_cb), manager);
660
661   if (is_outgoing && empathy_ft_handler_get_use_hash (handler)) {
662     g_signal_connect (handler, "hashing-started",
663         G_CALLBACK (ft_handler_hashing_started_cb), manager);
664   } else {
665     /* either incoming or outgoing without hash */
666     g_signal_connect (handler, "transfer-started",
667         G_CALLBACK (ft_handler_transfer_started_cb), manager);
668   }
669
670   empathy_ft_handler_start_transfer (handler);
671 }
672
673 static void
674 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
675                                 EmpathyFTHandler *handler,
676                                 const GError *error)
677 {
678   GtkTreeRowReference *row_ref;
679   GtkTreeIter iter;
680   GtkTreeSelection *selection;
681   GtkTreePath *path;
682   GIcon *icon;
683   const char *content_type, *second_line;
684   char *first_line, *message;
685   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
686
687   icon = NULL;
688
689   /* get the icon name from the mime-type of the file. */
690   content_type = empathy_ft_handler_get_content_type (handler);
691
692   if (content_type != NULL)
693     icon = g_content_type_get_icon (content_type);
694
695   /* append the handler in the store */
696   gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
697       &iter, G_MAXINT, COL_FT_OBJECT, handler,
698       COL_ICON, icon, -1);
699
700   if (icon != NULL)
701     g_object_unref (icon);
702
703   /* insert the new row_ref in the hash table  */
704   path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
705   row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path);
706   gtk_tree_path_free (path);
707   g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler),
708       row_ref);
709
710   /* select the new row */
711   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
712   gtk_tree_selection_select_iter (selection, &iter);
713
714   if (error != NULL)
715     {
716       message = ft_manager_format_error_message (handler, error);
717       ft_manager_update_handler_message (manager, row_ref, message);
718
719       g_free (message);
720       return;
721     }
722
723   /* update the row with the initial values */
724   if (empathy_ft_handler_is_incoming (handler)) {
725     first_line = ft_manager_format_contact_info (handler);
726     second_line = _("Waiting for the other participant's response");
727     message = g_strdup_printf ("%s\n%s", first_line, second_line);
728
729     ft_manager_update_handler_message (manager, row_ref, message);
730
731     g_free (first_line);
732     g_free (message);
733   }
734
735
736   /* hook up the signals and start the transfer */
737   ft_manager_start_transfer (manager, handler);
738 }
739
740 static void
741 ft_manager_clear (EmpathyFTManager *manager)
742 {
743   EmpathyFTManagerPriv *priv;
744
745   DEBUG ("Clearing file transfer list");
746
747   priv = GET_PRIV (manager);
748
749   /* Remove completed and cancelled transfers */
750   g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
751       remove_finished_transfer_foreach, manager);
752
753   /* set the clear button back to insensitive */
754   gtk_widget_set_sensitive (priv->clear_button, FALSE);
755 }
756
757 static void
758 ft_manager_open (EmpathyFTManager *manager)
759 {
760   GtkTreeSelection *selection;
761   GtkTreeIter iter;
762   GtkTreeModel *model;
763   EmpathyFTHandler *handler;
764   char *uri;
765   GFile *file;
766   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
767
768   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
769
770   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
771     return;
772
773   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
774
775   file = empathy_ft_handler_get_gfile (handler);
776   uri = g_file_get_uri (file);
777
778   DEBUG ("Opening URI: %s", uri);
779   empathy_url_show (GTK_WIDGET (priv->window), uri);
780
781   g_object_unref (handler);
782   g_free (uri);
783 }
784
785 static void
786 ft_manager_stop (EmpathyFTManager *manager)
787 {
788   GtkTreeSelection *selection;
789   GtkTreeIter iter;
790   GtkTreeModel *model;
791   EmpathyFTHandler *handler;
792   EmpathyFTManagerPriv *priv;
793
794   priv = GET_PRIV (manager);
795
796   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
797
798   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
799     return;
800
801   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
802   g_return_if_fail (handler != NULL);
803
804   DEBUG ("Stopping file transfer: contact=%s, filename=%s",
805       empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
806       empathy_ft_handler_get_filename (handler));
807
808   empathy_ft_handler_cancel_transfer (handler);
809
810   g_object_unref (handler);
811 }
812
813 static gboolean
814 ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager)
815 {
816   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
817   gint x, y, w, h;
818
819   gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h);
820   gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y);
821
822   empathy_geometry_save ("ft-manager", x, y, w, h);
823
824   priv->save_geometry_id = 0;
825
826   return FALSE;
827 }
828
829 static gboolean
830 ft_manager_configure_event_cb (GtkWidget *widget,
831                                GdkEventConfigure *event,
832                                EmpathyFTManager *manager)
833 {
834   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
835
836   if (priv->save_geometry_id != 0)
837     g_source_remove (priv->save_geometry_id);
838
839   priv->save_geometry_id = g_timeout_add (500,
840       (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager);
841
842   return FALSE;
843 }
844
845 static void
846 ft_manager_response_cb (GtkWidget *widget,
847                         gint response,
848                         EmpathyFTManager *manager)
849 {
850   switch (response)
851     {
852       case RESPONSE_CLEAR:
853         ft_manager_clear (manager);
854         break;
855       case RESPONSE_OPEN:
856         ft_manager_open (manager);
857         break;
858       case RESPONSE_STOP:
859         ft_manager_stop (manager);
860         break;
861     }
862 }
863
864 static gboolean
865 ft_manager_delete_event_cb (GtkWidget *widget,
866                             GdkEvent *event,
867                             EmpathyFTManager *manager)
868 {
869   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
870
871   DEBUG ("%p", manager);
872
873   /* remove all the completed/cancelled/errored transfers */
874   ft_manager_clear (manager);
875
876   if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
877     {
878       /* There is still FTs on flight, just hide the window */
879       DEBUG ("Hiding window");
880       gtk_widget_hide (widget);
881       return TRUE;      
882     }
883
884   return FALSE;
885 }
886
887 static void
888 ft_manager_destroy_cb (GtkWidget *widget,
889                        EmpathyFTManager *manager)
890 {
891   DEBUG ("%p", manager);
892
893   g_object_unref (manager);
894 }
895
896 static void
897 ft_manager_build_ui (EmpathyFTManager *manager)
898 {
899   GtkBuilder *gui;
900   gint x, y, w, h;
901   GtkTreeView *view;
902   GtkListStore *liststore;
903   GtkTreeViewColumn *column;
904   GtkCellRenderer *renderer;
905   GtkTreeSelection *selection;
906   gchar *filename;
907   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
908
909   filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
910   gui = empathy_builder_get_file (filename,
911       "ft_manager_dialog", &priv->window,
912       "ft_list", &priv->treeview,
913       "clear_button", &priv->clear_button,
914       "open_button", &priv->open_button,
915       "abort_button", &priv->abort_button,
916       NULL);
917   g_free (filename);
918
919   empathy_builder_connect (gui, manager,
920       "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
921       "ft_manager_dialog", "response", ft_manager_response_cb,
922       "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
923       "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb,
924       NULL);
925
926   g_object_unref (gui);
927
928   /* Window geometry. */
929   empathy_geometry_load ("ft-manager", &x, &y, &w, &h);
930
931   if (x >= 0 && y >= 0)
932     {
933       /* Let the window manager position it if we don't have
934        * good x, y coordinates. */
935       gtk_window_move (GTK_WINDOW (priv->window), x, y);
936     }
937
938   if (w > 0 && h > 0)
939     {
940       /* Use the defaults from the ui file if we don't have
941        * good w, h geometry. */
942       gtk_window_resize (GTK_WINDOW (priv->window), w, h);
943     }
944
945   /* Setup the tree view */
946   view = GTK_TREE_VIEW (priv->treeview);
947   selection = gtk_tree_view_get_selection (view);
948   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
949   g_signal_connect (selection, "changed",
950       G_CALLBACK (ft_manager_selection_changed), manager);
951   gtk_tree_view_set_headers_visible (view, TRUE);
952   gtk_tree_view_set_enable_search (view, FALSE);
953
954   /* Setup the model */
955   liststore = gtk_list_store_new (5,
956       G_TYPE_INT,     /* percent */
957       G_TYPE_ICON,    /* icon */
958       G_TYPE_STRING,  /* message */
959       G_TYPE_STRING,  /* remaining */
960       G_TYPE_OBJECT); /* ft_handler */
961   gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
962   priv->model = GTK_TREE_MODEL (liststore);
963   g_object_unref (liststore);
964
965   /* Progress column */
966   column = gtk_tree_view_column_new ();
967   gtk_tree_view_column_set_title (column, _("%"));
968   gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
969   gtk_tree_view_insert_column (view, column, -1);
970
971   renderer = gtk_cell_renderer_progress_new ();
972   g_object_set (renderer, "xalign", 0.5, NULL);
973   gtk_tree_view_column_pack_start (column, renderer, FALSE);
974   gtk_tree_view_column_set_cell_data_func (column, renderer,
975       ft_manager_progress_cell_data_func, NULL, NULL);
976
977   /* Icon and filename column*/
978   column = gtk_tree_view_column_new ();
979   gtk_tree_view_column_set_title (column, _("File"));
980   gtk_tree_view_column_set_expand (column, TRUE);
981   gtk_tree_view_column_set_resizable (column, TRUE);
982   gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
983   gtk_tree_view_column_set_spacing (column, 3);
984   gtk_tree_view_insert_column (view, column, -1);
985
986   renderer = gtk_cell_renderer_pixbuf_new ();
987   g_object_set (renderer, "xpad", 3,
988       "stock-size", GTK_ICON_SIZE_DND, NULL);
989   gtk_tree_view_column_pack_start (column, renderer, FALSE);
990   gtk_tree_view_column_set_attributes (column, renderer,
991       "gicon", COL_ICON, NULL);
992
993   renderer = gtk_cell_renderer_text_new ();
994   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
995   gtk_tree_view_column_pack_start (column, renderer, TRUE);
996   gtk_tree_view_column_set_attributes (column, renderer,
997       "text", COL_MESSAGE, NULL);
998
999   /* Remaining time column */
1000   column = gtk_tree_view_column_new ();
1001   gtk_tree_view_column_set_title (column, _("Remaining"));
1002   gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
1003   gtk_tree_view_insert_column (view, column, -1);
1004
1005   renderer = gtk_cell_renderer_text_new ();
1006   g_object_set (renderer, "xalign", 0.5, NULL);
1007   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1008   gtk_tree_view_column_set_attributes (column, renderer,
1009       "text", COL_REMAINING, NULL);
1010
1011   /* clear button should be sensitive only if there are completed/cancelled
1012    * handlers in the store.
1013    */
1014   gtk_widget_set_sensitive (priv->clear_button, FALSE);
1015 }
1016
1017 /* GObject method overrides */
1018
1019 static void
1020 empathy_ft_manager_finalize (GObject *object)
1021 {
1022   EmpathyFTManagerPriv *priv = GET_PRIV (object);
1023
1024   DEBUG ("FT Manager %p", object);
1025
1026   g_hash_table_destroy (priv->ft_handler_to_row_ref);
1027
1028   if (priv->save_geometry_id != 0)
1029     g_source_remove (priv->save_geometry_id);
1030
1031   G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
1032 }
1033
1034 static void
1035 empathy_ft_manager_init (EmpathyFTManager *manager)
1036 {
1037   EmpathyFTManagerPriv *priv;
1038
1039   priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
1040       EmpathyFTManagerPriv);
1041
1042   manager->priv = priv;
1043
1044   priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
1045       g_direct_equal, (GDestroyNotify) g_object_unref,
1046       (GDestroyNotify) gtk_tree_row_reference_free);
1047
1048   ft_manager_build_ui (manager);
1049 }
1050
1051 static GObject *
1052 empathy_ft_manager_constructor (GType type,
1053                                 guint n_props,
1054                                 GObjectConstructParam *props)
1055 {
1056   GObject *retval;
1057
1058   if (manager_singleton)
1059     {
1060       retval = G_OBJECT (manager_singleton);
1061     }
1062   else
1063     {
1064       retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
1065           (type, n_props, props);
1066
1067       manager_singleton = EMPATHY_FT_MANAGER (retval);
1068       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1069     }
1070
1071   return retval;
1072 }
1073
1074 static void
1075 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
1076 {
1077   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1078
1079   object_class->finalize = empathy_ft_manager_finalize;
1080   object_class->constructor = empathy_ft_manager_constructor;
1081
1082   g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
1083 }
1084
1085 /* public methods */
1086
1087 void
1088 empathy_ft_manager_add_handler (EmpathyFTHandler *handler)
1089 {
1090   EmpathyFTManager *manager;
1091   EmpathyFTManagerPriv *priv;
1092
1093   DEBUG ("Adding handler");
1094
1095   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1096
1097   manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1098   priv = GET_PRIV (manager);
1099
1100   ft_manager_add_handler_to_list (manager, handler, NULL);
1101   gtk_window_present (GTK_WINDOW (priv->window));
1102 }
1103
1104 void
1105 empathy_ft_manager_display_error (EmpathyFTHandler *handler,
1106                                   const GError *error)
1107 {
1108   EmpathyFTManager *manager;
1109   EmpathyFTManagerPriv *priv;
1110
1111   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1112   g_return_if_fail (error != NULL);
1113
1114   manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1115   priv = GET_PRIV (manager);
1116
1117   ft_manager_add_handler_to_list (manager, handler, error);
1118   gtk_window_present (GTK_WINDOW (priv->window));
1119 }
1120
1121 void
1122 empathy_ft_manager_show (void)
1123 {
1124   EmpathyFTManager *manager;
1125   EmpathyFTManagerPriv *priv;
1126
1127   manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1128   priv = GET_PRIV (manager);
1129
1130   gtk_window_present (GTK_WINDOW (priv->window));
1131 }