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