]> git.0d.be Git - empathy.git/blob - src/empathy-ft-manager.c
Update Simplified Chinese translation.
[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   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
820
821   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
822
823   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
824     return;
825
826   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
827
828   if (empathy_ft_handler_is_completed (handler)){
829     char *uri;
830     GFile *file;
831
832     file = empathy_ft_handler_get_gfile (handler);
833     uri = g_file_get_uri (file);
834
835     DEBUG ("Opening URI: %s", uri);
836     empathy_url_show (GTK_WIDGET (priv->window), uri);
837     g_free (uri);
838   }
839
840   g_object_unref (handler);
841 }
842
843 static void
844 ft_manager_stop (EmpathyFTManager *manager)
845 {
846   GtkTreeSelection *selection;
847   GtkTreeIter iter;
848   GtkTreeModel *model;
849   EmpathyFTHandler *handler;
850   EmpathyFTManagerPriv *priv;
851
852   priv = GET_PRIV (manager);
853
854   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
855
856   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
857     return;
858
859   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
860   g_return_if_fail (handler != NULL);
861
862   DEBUG ("Stopping file transfer: contact=%s, filename=%s",
863       empathy_contact_get_alias (empathy_ft_handler_get_contact (handler)),
864       empathy_ft_handler_get_filename (handler));
865
866   empathy_ft_handler_cancel_transfer (handler);
867
868   g_object_unref (handler);
869 }
870
871 static void
872 ft_manager_response_cb (GtkWidget *widget,
873                         gint response,
874                         EmpathyFTManager *manager)
875 {
876   switch (response)
877     {
878       case RESPONSE_CLEAR:
879         ft_manager_clear (manager);
880         break;
881       case RESPONSE_OPEN:
882         ft_manager_open (manager);
883         break;
884       case RESPONSE_STOP:
885         ft_manager_stop (manager);
886         break;
887       case GTK_RESPONSE_NONE:
888       case GTK_RESPONSE_DELETE_EVENT:
889         /* Do nothing */
890         break;
891       default:
892         g_assert_not_reached ();
893     }
894 }
895
896 static gboolean
897 ft_manager_delete_event_cb (GtkWidget *widget,
898                             GdkEvent *event,
899                             EmpathyFTManager *manager)
900 {
901   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
902
903   DEBUG ("%p", manager);
904
905   /* remove all the completed/cancelled/errored transfers */
906   ft_manager_clear (manager);
907
908   if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
909     {
910       /* There is still FTs on flight, just hide the window */
911       DEBUG ("Hiding window");
912       gtk_widget_hide (widget);
913       return TRUE;
914     }
915
916   return FALSE;
917 }
918
919 static void
920 ft_manager_destroy_cb (GtkWidget *widget,
921                        EmpathyFTManager *manager)
922 {
923   DEBUG ("%p", manager);
924
925   g_object_unref (manager);
926 }
927
928 static gboolean
929 ft_view_button_press_event_cb (GtkWidget *widget,
930                                GdkEventKey *event,
931                                EmpathyFTManager *manager)
932 {
933
934   if (event->type != GDK_2BUTTON_PRESS)
935       return FALSE;
936
937   ft_manager_open (manager);
938
939   return FALSE;
940 }
941
942 static gboolean
943 ft_manager_key_press_event_cb (GtkWidget *widget,
944                                GdkEventKey *event,
945                                gpointer user_data)
946 {
947   if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w)
948       || event->keyval == GDK_KEY_Escape)
949     {
950       gtk_widget_destroy (widget);
951       return TRUE;
952     }
953
954   return FALSE;
955 }
956
957 static void
958 ft_manager_build_ui (EmpathyFTManager *manager)
959 {
960   GtkBuilder *gui;
961   GtkTreeView *view;
962   GtkListStore *liststore;
963   GtkTreeViewColumn *column;
964   GtkCellRenderer *renderer;
965   GtkTreeSelection *selection;
966   gchar *filename;
967   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
968
969   filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
970   gui = empathy_builder_get_file (filename,
971       "ft_manager_dialog", &priv->window,
972       "ft_list", &priv->treeview,
973       "clear_button", &priv->clear_button,
974       "open_button", &priv->open_button,
975       "abort_button", &priv->abort_button,
976       NULL);
977   g_free (filename);
978
979   empathy_builder_connect (gui, manager,
980       "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
981       "ft_manager_dialog", "response", ft_manager_response_cb,
982       "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
983       "ft_manager_dialog", "key-press-event", ft_manager_key_press_event_cb,
984       NULL);
985
986   empathy_builder_unref_and_keep_widget (gui, priv->window);
987
988   /* Window geometry. */
989   empathy_geometry_bind (GTK_WINDOW (priv->window), "ft-manager");
990
991   /* Setup the tree view */
992   view = GTK_TREE_VIEW (priv->treeview);
993   selection = gtk_tree_view_get_selection (view);
994   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
995   g_signal_connect (selection, "changed",
996       G_CALLBACK (ft_manager_selection_changed), manager);
997   g_signal_connect (view, "button-press-event",
998                     G_CALLBACK (ft_view_button_press_event_cb),
999                     manager);
1000   gtk_tree_view_set_headers_visible (view, TRUE);
1001   gtk_tree_view_set_enable_search (view, FALSE);
1002
1003   /* Setup the model */
1004   liststore = gtk_list_store_new (5,
1005       G_TYPE_INT,     /* percent */
1006       G_TYPE_ICON,    /* icon */
1007       G_TYPE_STRING,  /* message */
1008       G_TYPE_STRING,  /* remaining */
1009       G_TYPE_OBJECT); /* ft_handler */
1010   gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
1011   priv->model = GTK_TREE_MODEL (liststore);
1012   g_object_unref (liststore);
1013
1014   /* Progress column */
1015   column = gtk_tree_view_column_new ();
1016   gtk_tree_view_column_set_title (column, _("%"));
1017   gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
1018   gtk_tree_view_insert_column (view, column, -1);
1019
1020   renderer = gtk_cell_renderer_progress_new ();
1021   g_object_set (renderer, "xalign", 0.5, NULL);
1022   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1023   gtk_tree_view_column_set_cell_data_func (column, renderer,
1024       ft_manager_progress_cell_data_func, NULL, NULL);
1025
1026   /* Icon and filename column*/
1027   column = gtk_tree_view_column_new ();
1028   gtk_tree_view_column_set_title (column, _("File"));
1029   gtk_tree_view_column_set_expand (column, TRUE);
1030   gtk_tree_view_column_set_resizable (column, TRUE);
1031   gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
1032   gtk_tree_view_column_set_spacing (column, 3);
1033   gtk_tree_view_insert_column (view, column, -1);
1034
1035   renderer = gtk_cell_renderer_pixbuf_new ();
1036   g_object_set (renderer, "xpad", 3,
1037       "stock-size", GTK_ICON_SIZE_DND, NULL);
1038   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1039   gtk_tree_view_column_set_attributes (column, renderer,
1040       "gicon", COL_ICON, NULL);
1041
1042   renderer = gtk_cell_renderer_text_new ();
1043   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1044   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1045   gtk_tree_view_column_set_attributes (column, renderer,
1046       "text", COL_MESSAGE, NULL);
1047
1048   /* Remaining time column */
1049   column = gtk_tree_view_column_new ();
1050   gtk_tree_view_column_set_title (column, _("Remaining"));
1051   gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
1052   gtk_tree_view_insert_column (view, column, -1);
1053
1054   renderer = gtk_cell_renderer_text_new ();
1055   g_object_set (renderer, "xalign", 0.5, NULL);
1056   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1057   gtk_tree_view_column_set_attributes (column, renderer,
1058       "text", COL_REMAINING, NULL);
1059
1060   /* clear button should be sensitive only if there are completed/cancelled
1061    * handlers in the store.
1062    */
1063   gtk_widget_set_sensitive (priv->clear_button, FALSE);
1064 }
1065
1066 /* GObject method overrides */
1067
1068 static void
1069 empathy_ft_manager_finalize (GObject *object)
1070 {
1071   EmpathyFTManagerPriv *priv = GET_PRIV (object);
1072
1073   DEBUG ("FT Manager %p", object);
1074
1075   g_hash_table_destroy (priv->ft_handler_to_row_ref);
1076
1077   G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
1078 }
1079
1080 static void
1081 empathy_ft_manager_init (EmpathyFTManager *manager)
1082 {
1083   EmpathyFTManagerPriv *priv;
1084
1085   priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
1086       EmpathyFTManagerPriv);
1087
1088   manager->priv = priv;
1089
1090   priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
1091       g_direct_equal, (GDestroyNotify) g_object_unref,
1092       (GDestroyNotify) gtk_tree_row_reference_free);
1093
1094   ft_manager_build_ui (manager);
1095 }
1096
1097 static GObject *
1098 empathy_ft_manager_constructor (GType type,
1099                                 guint n_props,
1100                                 GObjectConstructParam *props)
1101 {
1102   GObject *retval;
1103
1104   if (manager_singleton)
1105     {
1106       retval = G_OBJECT (manager_singleton);
1107     }
1108   else
1109     {
1110       retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
1111           (type, n_props, props);
1112
1113       manager_singleton = EMPATHY_FT_MANAGER (retval);
1114       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1115     }
1116
1117   return retval;
1118 }
1119
1120 static void
1121 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
1122 {
1123   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1124
1125   object_class->finalize = empathy_ft_manager_finalize;
1126   object_class->constructor = empathy_ft_manager_constructor;
1127
1128   g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
1129 }
1130
1131 /* public methods */
1132
1133 void
1134 empathy_ft_manager_add_handler (EmpathyFTHandler *handler)
1135 {
1136   EmpathyFTManager *manager;
1137   EmpathyFTManagerPriv *priv;
1138
1139   DEBUG ("Adding handler");
1140
1141   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1142
1143   manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1144   priv = GET_PRIV (manager);
1145
1146   ft_manager_add_handler_to_list (manager, handler, NULL);
1147   gtk_window_present (GTK_WINDOW (priv->window));
1148 }
1149
1150 void
1151 empathy_ft_manager_display_error (EmpathyFTHandler *handler,
1152                                   const GError *error)
1153 {
1154   EmpathyFTManager *manager;
1155   EmpathyFTManagerPriv *priv;
1156
1157   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1158   g_return_if_fail (error != NULL);
1159
1160   manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1161   priv = GET_PRIV (manager);
1162
1163   ft_manager_add_handler_to_list (manager, handler, error);
1164   gtk_window_present (GTK_WINDOW (priv->window));
1165 }
1166
1167 void
1168 empathy_ft_manager_show (void)
1169 {
1170   EmpathyFTManager *manager;
1171   EmpathyFTManagerPriv *priv;
1172
1173   manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1174   priv = GET_PRIV (manager);
1175
1176   gtk_window_present (GTK_WINDOW (priv->window));
1177 }