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