]> git.0d.be Git - empathy.git/blob - src/empathy-ft-manager.c
Merge remote-tracking branch 'jonny/ft'
[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-utils.h>
41
42 #include <libempathy-gtk/empathy-ui-utils.h>
43 #include <libempathy-gtk/empathy-geometry.h>
44 #include <libempathy-gtk/empathy-images.h>
45
46 #include "empathy-ft-manager.h"
47
48 enum
49 {
50   COL_PERCENT,
51   COL_ICON,
52   COL_MESSAGE,
53   COL_REMAINING,
54   COL_FT_OBJECT
55 };
56
57 typedef struct {
58   GtkTreeModel *model;
59   GHashTable *ft_handler_to_row_ref;
60
61   /* Widgets */
62   GtkWidget *window;
63   GtkWidget *treeview;
64   GtkWidget *open_button;
65   GtkWidget *abort_button;
66   GtkWidget *clear_button;
67 } EmpathyFTManagerPriv;
68
69 enum
70 {
71   RESPONSE_OPEN  = 1,
72   RESPONSE_STOP  = 2,
73   RESPONSE_CLEAR = 3,
74   RESPONSE_CLOSE = 4
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 (total);
265   current_str = g_format_size (current);
266
267   if (speed > 0)
268     speed_str = g_format_size ((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                              TpFileTransferChannel *channel,
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                                 TpFileTransferChannel *channel,
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   gboolean is_outgoing;
699
700   is_outgoing = !empathy_ft_handler_is_incoming (handler);
701
702   DEBUG ("Start transfer, is outgoing %s",
703       is_outgoing ? "True" : "False");
704
705   /* now connect the signals */
706   g_signal_connect (handler, "transfer-error",
707       G_CALLBACK (ft_handler_transfer_error_cb), manager);
708
709   if (is_outgoing && empathy_ft_handler_get_use_hash (handler)) {
710     g_signal_connect (handler, "hashing-started",
711         G_CALLBACK (ft_handler_hashing_started_cb), manager);
712   } else {
713     /* either incoming or outgoing without hash */
714     g_signal_connect (handler, "transfer-started",
715         G_CALLBACK (ft_handler_transfer_started_cb), manager);
716   }
717
718   empathy_ft_handler_start_transfer (handler);
719 }
720
721 static void
722 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
723                                 EmpathyFTHandler *handler,
724                                 const GError *error)
725 {
726   GtkTreeRowReference *row_ref;
727   GtkTreeIter iter;
728   GtkTreeSelection *selection;
729   GtkTreePath *path;
730   GIcon *icon;
731   const char *content_type, *second_line;
732   char *first_line, *message;
733   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
734
735   icon = NULL;
736
737   /* get the icon name from the mime-type of the file. */
738   content_type = empathy_ft_handler_get_content_type (handler);
739
740   if (content_type != NULL)
741     icon = g_content_type_get_icon (content_type);
742
743   /* append the handler in the store */
744   gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
745       &iter, G_MAXINT, COL_FT_OBJECT, handler,
746       COL_ICON, icon, -1);
747
748   if (icon != NULL)
749     g_object_unref (icon);
750
751   /* insert the new row_ref in the hash table  */
752   path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
753   row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path);
754   gtk_tree_path_free (path);
755   g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler),
756       row_ref);
757
758   /* select the new row */
759   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
760   gtk_tree_selection_select_iter (selection, &iter);
761
762   if (error != NULL)
763     {
764       message = ft_manager_format_error_message (handler, error);
765       ft_manager_update_handler_message (manager, row_ref, message);
766
767       g_free (message);
768       return;
769     }
770
771   /* update the row with the initial values.
772    * the only case where we postpone this is in case we're managing
773    * an outgoing+hashing transfer, as the hashing started signal will
774    * take care of updating the information.
775    */
776   if (empathy_ft_handler_is_incoming (handler) ||
777       !empathy_ft_handler_get_use_hash (handler)) {
778     first_line = ft_manager_format_contact_info (handler);
779     second_line = _("Waiting for the other participant's response");
780     message = g_strdup_printf ("%s\n%s", first_line, second_line);
781
782     ft_manager_update_handler_message (manager, row_ref, message);
783
784     g_free (first_line);
785     g_free (message);
786   }
787
788   /* hook up the signals and start the transfer */
789   ft_manager_start_transfer (manager, handler);
790 }
791
792 static void
793 ft_manager_clear (EmpathyFTManager *manager)
794 {
795   EmpathyFTManagerPriv *priv;
796
797   DEBUG ("Clearing file transfer list");
798
799   priv = GET_PRIV (manager);
800
801   /* Remove completed and cancelled transfers */
802   g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
803       remove_finished_transfer_foreach, manager);
804
805   /* set the clear button back to insensitive */
806   gtk_widget_set_sensitive (priv->clear_button, FALSE);
807 }
808
809 static void
810 ft_manager_open (EmpathyFTManager *manager)
811 {
812   GtkTreeSelection *selection;
813   GtkTreeIter iter;
814   GtkTreeModel *model;
815   EmpathyFTHandler *handler;
816   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
817
818   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
819
820   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
821     return;
822
823   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
824
825   if (empathy_ft_handler_is_completed (handler)){
826     char *uri;
827     GFile *file;
828
829     file = empathy_ft_handler_get_gfile (handler);
830     uri = g_file_get_uri (file);
831
832     DEBUG ("Opening URI: %s", uri);
833     empathy_url_show (GTK_WIDGET (priv->window), uri);
834     g_free (uri);
835   }
836
837   g_object_unref (handler);
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 gboolean
869 close_window (EmpathyFTManager *manager)
870 {
871   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
872
873   DEBUG ("%p", manager);
874
875   /* remove all the completed/cancelled/errored transfers */
876   ft_manager_clear (manager);
877
878   if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
879     {
880       /* There is still FTs on flight, just hide the window */
881       DEBUG ("Hiding window");
882       gtk_widget_hide (priv->window);
883       return TRUE;
884     }
885
886   return FALSE;
887 }
888
889 static void
890 ft_manager_response_cb (GtkWidget *widget,
891                         gint response,
892                         EmpathyFTManager *manager)
893 {
894   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
895
896   switch (response)
897     {
898       case RESPONSE_CLEAR:
899         ft_manager_clear (manager);
900         break;
901       case RESPONSE_OPEN:
902         ft_manager_open (manager);
903         break;
904       case RESPONSE_STOP:
905         ft_manager_stop (manager);
906         break;
907       case RESPONSE_CLOSE:
908         if (!close_window (manager))
909           gtk_widget_destroy (priv->window);
910         break;
911       case GTK_RESPONSE_NONE:
912       case GTK_RESPONSE_DELETE_EVENT:
913         /* Do nothing */
914         break;
915       default:
916         g_assert_not_reached ();
917     }
918 }
919
920 static gboolean
921 ft_manager_delete_event_cb (GtkWidget *widget,
922                             GdkEvent *event,
923                             EmpathyFTManager *manager)
924 {
925   return close_window (manager);
926 }
927
928 static void
929 ft_manager_destroy_cb (GtkWidget *widget,
930                        EmpathyFTManager *manager)
931 {
932   DEBUG ("%p", manager);
933
934   g_object_unref (manager);
935 }
936
937 static gboolean
938 ft_view_button_press_event_cb (GtkWidget *widget,
939                                GdkEventKey *event,
940                                EmpathyFTManager *manager)
941 {
942
943   if (event->type != GDK_2BUTTON_PRESS)
944       return FALSE;
945
946   ft_manager_open (manager);
947
948   return FALSE;
949 }
950
951 static gboolean
952 ft_manager_key_press_event_cb (GtkWidget *widget,
953                                GdkEventKey *event,
954                                gpointer user_data)
955 {
956   if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w)
957       || event->keyval == GDK_KEY_Escape)
958     {
959       gtk_widget_destroy (widget);
960       return TRUE;
961     }
962
963   return FALSE;
964 }
965
966 static void
967 ft_manager_build_ui (EmpathyFTManager *manager)
968 {
969   GtkBuilder *gui;
970   GtkTreeView *view;
971   GtkListStore *liststore;
972   GtkTreeViewColumn *column;
973   GtkCellRenderer *renderer;
974   GtkTreeSelection *selection;
975   gchar *filename;
976   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
977
978   filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
979   gui = empathy_builder_get_file (filename,
980       "ft_manager_dialog", &priv->window,
981       "ft_list", &priv->treeview,
982       "clear_button", &priv->clear_button,
983       "open_button", &priv->open_button,
984       "abort_button", &priv->abort_button,
985       NULL);
986   g_free (filename);
987
988   empathy_builder_connect (gui, manager,
989       "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
990       "ft_manager_dialog", "response", ft_manager_response_cb,
991       "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
992       "ft_manager_dialog", "key-press-event", ft_manager_key_press_event_cb,
993       NULL);
994
995   empathy_builder_unref_and_keep_widget (gui, priv->window);
996
997   /* Window geometry. */
998   empathy_geometry_bind (GTK_WINDOW (priv->window), "ft-manager");
999
1000   /* Setup the tree view */
1001   view = GTK_TREE_VIEW (priv->treeview);
1002   selection = gtk_tree_view_get_selection (view);
1003   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
1004   g_signal_connect (selection, "changed",
1005       G_CALLBACK (ft_manager_selection_changed), manager);
1006   g_signal_connect (view, "button-press-event",
1007                     G_CALLBACK (ft_view_button_press_event_cb),
1008                     manager);
1009   gtk_tree_view_set_headers_visible (view, TRUE);
1010   gtk_tree_view_set_enable_search (view, FALSE);
1011
1012   /* Setup the model */
1013   liststore = gtk_list_store_new (5,
1014       G_TYPE_INT,     /* percent */
1015       G_TYPE_ICON,    /* icon */
1016       G_TYPE_STRING,  /* message */
1017       G_TYPE_STRING,  /* remaining */
1018       G_TYPE_OBJECT); /* ft_handler */
1019   gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
1020   priv->model = GTK_TREE_MODEL (liststore);
1021   g_object_unref (liststore);
1022
1023   /* Progress column */
1024   column = gtk_tree_view_column_new ();
1025   gtk_tree_view_column_set_title (column, _("%"));
1026   gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
1027   gtk_tree_view_insert_column (view, column, -1);
1028
1029   renderer = gtk_cell_renderer_progress_new ();
1030   g_object_set (renderer, "xalign", 0.5, NULL);
1031   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1032   gtk_tree_view_column_set_cell_data_func (column, renderer,
1033       ft_manager_progress_cell_data_func, NULL, NULL);
1034
1035   /* Icon and filename column*/
1036   column = gtk_tree_view_column_new ();
1037   gtk_tree_view_column_set_title (column, _("File"));
1038   gtk_tree_view_column_set_expand (column, TRUE);
1039   gtk_tree_view_column_set_resizable (column, TRUE);
1040   gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
1041   gtk_tree_view_column_set_spacing (column, 3);
1042   gtk_tree_view_insert_column (view, column, -1);
1043
1044   renderer = gtk_cell_renderer_pixbuf_new ();
1045   g_object_set (renderer, "xpad", 3,
1046       "stock-size", GTK_ICON_SIZE_DND, NULL);
1047   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1048   gtk_tree_view_column_set_attributes (column, renderer,
1049       "gicon", COL_ICON, NULL);
1050
1051   renderer = gtk_cell_renderer_text_new ();
1052   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1053   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1054   gtk_tree_view_column_set_attributes (column, renderer,
1055       "text", COL_MESSAGE, NULL);
1056
1057   /* Remaining time column */
1058   column = gtk_tree_view_column_new ();
1059   gtk_tree_view_column_set_title (column, _("Remaining"));
1060   gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
1061   gtk_tree_view_insert_column (view, column, -1);
1062
1063   renderer = gtk_cell_renderer_text_new ();
1064   g_object_set (renderer, "xalign", 0.5, NULL);
1065   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1066   gtk_tree_view_column_set_attributes (column, renderer,
1067       "text", COL_REMAINING, NULL);
1068
1069   /* clear button should be sensitive only if there are completed/cancelled
1070    * handlers in the store.
1071    */
1072   gtk_widget_set_sensitive (priv->clear_button, FALSE);
1073 }
1074
1075 /* GObject method overrides */
1076
1077 static void
1078 empathy_ft_manager_finalize (GObject *object)
1079 {
1080   EmpathyFTManagerPriv *priv = GET_PRIV (object);
1081
1082   DEBUG ("FT Manager %p", object);
1083
1084   g_hash_table_unref (priv->ft_handler_to_row_ref);
1085
1086   G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
1087 }
1088
1089 static void
1090 empathy_ft_manager_init (EmpathyFTManager *manager)
1091 {
1092   EmpathyFTManagerPriv *priv;
1093
1094   priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
1095       EmpathyFTManagerPriv);
1096
1097   manager->priv = priv;
1098
1099   priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
1100       g_direct_equal, (GDestroyNotify) g_object_unref,
1101       (GDestroyNotify) gtk_tree_row_reference_free);
1102
1103   ft_manager_build_ui (manager);
1104 }
1105
1106 static GObject *
1107 empathy_ft_manager_constructor (GType type,
1108                                 guint n_props,
1109                                 GObjectConstructParam *props)
1110 {
1111   GObject *retval;
1112
1113   if (manager_singleton)
1114     {
1115       retval = G_OBJECT (manager_singleton);
1116     }
1117   else
1118     {
1119       retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
1120           (type, n_props, props);
1121
1122       manager_singleton = EMPATHY_FT_MANAGER (retval);
1123       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1124     }
1125
1126   return retval;
1127 }
1128
1129 static void
1130 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
1131 {
1132   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1133
1134   object_class->finalize = empathy_ft_manager_finalize;
1135   object_class->constructor = empathy_ft_manager_constructor;
1136
1137   g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
1138 }
1139
1140 /* public methods */
1141
1142 void
1143 empathy_ft_manager_add_handler (EmpathyFTHandler *handler)
1144 {
1145   EmpathyFTManager *manager;
1146   EmpathyFTManagerPriv *priv;
1147
1148   DEBUG ("Adding handler");
1149
1150   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1151
1152   manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1153   priv = GET_PRIV (manager);
1154
1155   ft_manager_add_handler_to_list (manager, handler, NULL);
1156   gtk_window_present (GTK_WINDOW (priv->window));
1157 }
1158
1159 void
1160 empathy_ft_manager_display_error (EmpathyFTHandler *handler,
1161                                   const GError *error)
1162 {
1163   EmpathyFTManager *manager;
1164   EmpathyFTManagerPriv *priv;
1165
1166   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1167   g_return_if_fail (error != NULL);
1168
1169   manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1170   priv = GET_PRIV (manager);
1171
1172   ft_manager_add_handler_to_list (manager, handler, error);
1173   gtk_window_present (GTK_WINDOW (priv->window));
1174 }
1175
1176 void
1177 empathy_ft_manager_show (void)
1178 {
1179   EmpathyFTManager *manager;
1180   EmpathyFTManagerPriv *priv;
1181
1182   manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1183   priv = GET_PRIV (manager);
1184
1185   gtk_window_present (GTK_WINDOW (priv->window));
1186 }