]> git.0d.be Git - empathy.git/blob - src/empathy-ft-manager.c
React to Tp remote errors
[empathy.git] / src / empathy-ft-manager.c
1 /*
2  * Copyright (C) 2003, 2004 Xan Lopez
3  * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
4  * Copyright (C) 2008-2009 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Xan Lopez
22  *          Marco Barisione <marco@barisione.org>
23  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
24  *          Xavier Claessens <xclaesse@gmail.com>
25  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
26  */
27
28 /* The original file transfer manager code was copied from Epiphany */
29
30 #include "config.h"
31
32 #include <string.h>
33
34 #include <glib/gi18n.h>
35 #include <gtk/gtk.h>
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_FT
38 #include <libempathy/empathy-debug.h>
39 #include <libempathy/empathy-tp-file.h>
40 #include <libempathy/empathy-utils.h>
41
42 #include <libempathy-gtk/empathy-conf.h>
43 #include <libempathy-gtk/empathy-ui-utils.h>
44 #include <libempathy-gtk/empathy-geometry.h>
45 #include <libempathy-gtk/empathy-images.h>
46
47 #include "empathy-ft-manager.h"
48
49
50 /**
51  * SECTION:empathy-ft-manager
52  * @short_description: File transfer dialog
53  * @see_also: #EmpathyTpFile, empathy_dispatcher_send_file()
54  * @include: libempthy-gtk/empathy-ft-manager.h
55  *
56  * The #EmpathyFTManager object represents the file transfer dialog,
57  * it can show multiple file transfers at the same time (added
58  * with empathy_ft_manager_add_tp_file()).
59  */
60
61 enum
62 {
63   COL_PERCENT,
64   COL_ICON,
65   COL_MESSAGE,
66   COL_REMAINING,
67   COL_FT_OBJECT
68 };
69
70 /**
71  * EmpathyFTManagerPriv:
72  *
73  * Private fields of the #EmpathyFTManager class.
74  */
75 typedef struct {
76   GtkTreeModel *model;
77   GHashTable *ft_handler_to_row_ref;
78
79   /* Widgets */
80   GtkWidget *window;
81   GtkWidget *treeview;
82   GtkWidget *open_button;
83   GtkWidget *abort_button;
84
85   guint save_geometry_id;
86 } EmpathyFTManagerPriv;
87
88 enum
89 {
90   RESPONSE_OPEN  = 1,
91   RESPONSE_STOP  = 2,
92   RESPONSE_CLEAR = 3
93 };
94
95 G_DEFINE_TYPE (EmpathyFTManager, empathy_ft_manager, G_TYPE_OBJECT);
96
97 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTManager)
98
99 static EmpathyFTManager *manager_singleton = NULL;
100
101 #if 0
102 static gchar *
103 ft_manager_format_interval (gint interval)
104 {
105   gint hours, mins, secs;
106
107   hours = interval / 3600;
108   interval -= hours * 3600;
109   mins = interval / 60;
110   interval -= mins * 60;
111   secs = interval;
112
113   if (hours > 0)
114     /* Translators: time left, when it is more than one hour */
115     return g_strdup_printf (_("%u:%02u.%02u"), hours, mins, secs);
116   else
117     /* Translators: time left, when is is less than one hour */
118     return g_strdup_printf (_("%02u.%02u"), mins, secs);
119 }
120 #endif
121
122 static void
123 ft_manager_update_buttons (EmpathyFTManager *manager)
124 {
125   GtkTreeSelection *selection;
126   GtkTreeModel *model;
127   GtkTreeIter iter;
128   EmpathyFTHandler *handler;
129   gboolean open_enabled = FALSE;
130   gboolean abort_enabled = FALSE;
131   gboolean is_completed, is_cancelled;
132   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
133
134   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
135
136   if (gtk_tree_selection_get_selected (selection, &model, &iter))
137     {
138       gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
139
140       is_completed = empathy_ft_handler_is_completed (handler);
141       is_cancelled = empathy_ft_handler_is_cancelled (handler);
142
143       /* I can open the file if the transfer is completed and was incoming */
144       open_enabled = (is_completed && empathy_ft_handler_is_incoming (handler));
145
146       /* I can abort if the transfer is not already finished */
147       abort_enabled = (is_cancelled == FALSE && is_completed == FALSE);
148
149       g_object_unref (handler);
150     }
151
152   gtk_widget_set_sensitive (priv->open_button, open_enabled);
153   gtk_widget_set_sensitive (priv->abort_button, abort_enabled);
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_name (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 char *
256 ft_manager_format_progress_bytes_and_percentage (guint64 current,
257                                                  guint64 total,
258                                                  int *percentage)
259 {
260   char *total_str, *current_str, *retval;
261
262   total_str = g_format_size_for_display (total);
263   current_str = g_format_size_for_display (current);
264
265   /* translators: first %s is the currently processed size, second %s is
266    * the total file size */
267   retval = g_strdup_printf (_("%s of %s"), current_str, total_str);
268
269   g_free (total_str);
270   g_free (current_str);
271
272   if (percentage != NULL)
273     {
274       if (total != 0) 
275         *percentage = current * 100 / total;
276       else
277         *percentage = -1;
278     }
279
280   return retval;
281 }
282
283 static char *
284 ft_manager_format_contact_info (EmpathyFTHandler *handler)
285 {
286   gboolean incoming;
287   const char *filename, *contact_name, *first_line_format;
288   char *retval;
289
290   incoming = empathy_ft_handler_is_incoming (handler);
291   contact_name = empathy_contact_get_name
292     (empathy_ft_handler_get_contact (handler));
293   filename = empathy_ft_handler_get_filename (handler);
294
295   if (incoming)
296     /* translators: first %s is filename, second %s is the contact name */
297     first_line_format = _("Receiving \"%s\" from %s");
298   else
299     /* translators: first %s is filename, second %s is the contact name */
300     first_line_format = _("Sending \"%s\" to %s");
301
302   retval = g_strdup_printf (first_line_format, filename, contact_name);
303
304   return retval;
305 }
306
307 static char *
308 ft_manager_format_error_message (EmpathyFTHandler *handler,
309                                  const GError *error)
310 {
311   const char *contact_name, *filename;
312   EmpathyContact *contact;
313   char *first_line, *message;
314   gboolean incoming;
315
316   contact_name = NULL;
317   incoming = empathy_ft_handler_is_incoming (handler);
318
319   contact = empathy_ft_handler_get_contact (handler);
320   if (contact)
321     contact_name = empathy_contact_get_name (contact);
322
323   filename = empathy_ft_handler_get_filename (handler);
324
325   if (incoming)
326     /* filename/contact_name here are either both NULL or both valid */
327     if (filename && contact_name)
328       /* translators: first %s is filename, second %s
329        * is the contact name */
330       first_line = g_strdup_printf (_("Error receiving \"%s\" from %s"), filename,
331           contact_name);
332     else
333       first_line = g_strdup (_("Error receiving a file"));
334   else
335     /* translators: first %s is filename, second %s
336      * is the contact name */
337     if (filename && contact_name)
338       first_line = g_strdup_printf (_("Error sending \"%s\" to %s"), filename,
339           contact_name);
340     else
341       first_line = g_strdup (_("Error sending a file"));
342
343   message = g_strdup_printf ("%s\n%s", first_line, error->message);
344
345   g_free (first_line);
346
347   return message;
348 }
349
350 static void
351 ft_manager_update_handler_message (EmpathyFTManager *manager,
352                                    GtkTreeRowReference *row_ref,
353                                    const char *message)
354 {
355   GtkTreePath *path;
356   GtkTreeIter iter;
357   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
358
359   /* Set new value in the store */
360   path = gtk_tree_row_reference_get_path (row_ref);
361   gtk_tree_model_get_iter (priv->model, &iter, path);
362   gtk_list_store_set (GTK_LIST_STORE (priv->model),
363       &iter,
364       COL_MESSAGE, message ? message : "",
365       -1);
366
367   gtk_tree_path_free (path);
368 }
369
370 static void
371 ft_manager_update_handler_progress (EmpathyFTManager *manager,
372                                     GtkTreeRowReference *row_ref,
373                                     int percentage)
374 {
375   GtkTreePath *path;
376   GtkTreeIter iter;
377   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
378
379   /* Set new value in the store */
380   path = gtk_tree_row_reference_get_path (row_ref);
381   gtk_tree_model_get_iter (priv->model, &iter, path);
382   gtk_list_store_set (GTK_LIST_STORE (priv->model),
383       &iter,
384       COL_PERCENT, percentage,
385       -1);
386
387   gtk_tree_path_free (path);
388
389 }
390
391 static void
392 ft_handler_transfer_error_cb (EmpathyFTHandler *handler,
393                               GError *error,
394                               EmpathyFTManager *manager)
395 {
396   char *message;
397   GtkTreeRowReference *row_ref;
398
399   DEBUG ("Transfer error %s", error->message);
400
401   row_ref = ft_manager_get_row_from_handler (manager, handler);
402   g_return_if_fail (row_ref != NULL);
403
404   message = ft_manager_format_error_message (handler, error);
405
406   ft_manager_update_handler_message (manager, row_ref, message);
407   ft_manager_update_buttons (manager);
408
409   g_free (message);
410 }
411
412 static void
413 ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
414                              EmpathyTpFile *tp_file,
415                              EmpathyFTManager *manager)
416 {
417   const char *contact_name;
418   const char *filename;
419   char *first_line, *second_line, *message;
420   gboolean incoming;
421   GtkTreeRowReference *row_ref;
422
423   DEBUG ("Transfer done");
424
425   row_ref = ft_manager_get_row_from_handler (manager, handler);
426   g_return_if_fail (row_ref != NULL);
427
428   incoming = empathy_ft_handler_is_incoming (handler);
429   contact_name = empathy_contact_get_name
430     (empathy_ft_handler_get_contact (handler));
431   filename = empathy_ft_handler_get_filename (handler);
432
433   if (incoming)
434     /* translators: first %s is filename, second %s
435      * is the contact name */
436     first_line = g_strdup_printf (_("\"%s\" received from %s"), filename,
437         contact_name);
438   else
439     /* translators: first %s is filename, second %s
440      * is the contact name */
441     first_line = g_strdup_printf (_("\"%s\" sent to %s"), filename,
442         contact_name);
443
444   second_line = g_strdup (_("File transfer completed"));
445
446   message = g_strdup_printf ("%s\n%s", first_line, second_line);
447   ft_manager_update_handler_message (manager, row_ref, message);
448
449   /* update buttons */
450   ft_manager_update_buttons (manager);
451
452   g_free (message);
453   g_free (first_line);
454   g_free (second_line);
455 }
456
457 static void
458 ft_handler_transfer_progress_cb (EmpathyFTHandler *handler,
459                                  guint64 current_bytes,
460                                  guint64 total_bytes,
461                                  EmpathyFTManager *manager)
462 {
463   char *first_line, *second_line, *message;
464   int percentage;
465   GtkTreeRowReference *row_ref;
466
467   DEBUG ("Transfer progress");
468
469   row_ref = ft_manager_get_row_from_handler (manager, handler);
470   g_return_if_fail (row_ref != NULL);
471
472   first_line = ft_manager_format_contact_info (handler);
473   second_line = ft_manager_format_progress_bytes_and_percentage
474     (current_bytes, total_bytes, &percentage);
475
476   message = g_strdup_printf ("%s\n%s", first_line, second_line);
477
478   ft_manager_update_handler_message (manager, row_ref, message);
479   ft_manager_update_handler_progress (manager, row_ref, percentage);
480
481   g_free (message);
482 }
483
484 static void
485 ft_handler_transfer_started_cb (EmpathyFTHandler *handler,
486                                 EmpathyTpFile *tp_file,
487                                 EmpathyFTManager *manager)
488 {
489   guint64 transferred_bytes, total_bytes;
490
491   DEBUG ("Transfer started");
492
493   g_signal_connect (handler, "transfer-progress",
494       G_CALLBACK (ft_handler_transfer_progress_cb), manager);
495   g_signal_connect (handler, "transfer-done",
496       G_CALLBACK (ft_handler_transfer_done_cb), manager);
497
498   transferred_bytes = empathy_ft_handler_get_transferred_bytes (handler);
499   total_bytes = empathy_ft_handler_get_total_bytes (handler);
500
501   ft_handler_transfer_progress_cb (handler, transferred_bytes, total_bytes,
502       manager);
503 }
504
505 static void
506 ft_handler_hashing_done_cb (EmpathyFTHandler *handler,
507                             EmpathyFTManager *manager)
508 {
509   GtkTreeRowReference *row_ref;
510   char *first_line, *second_line, *message;
511
512   DEBUG ("Hashing done");
513
514   row_ref = ft_manager_get_row_from_handler (manager, handler);
515   g_return_if_fail (row_ref != NULL);
516
517   /* update the message */
518   first_line = ft_manager_format_contact_info (handler);
519   second_line = g_strdup (_("Waiting for the other participant's response"));
520   message = g_strdup_printf ("%s\n%s", first_line, second_line);
521
522   ft_manager_update_handler_message (manager, row_ref, message);
523
524   g_free (message);
525   g_free (first_line);
526   g_free (second_line);
527
528   g_signal_connect (handler, "transfer-started",
529       G_CALLBACK (ft_handler_transfer_started_cb), manager);
530 }
531
532 static void
533 ft_handler_hashing_progress_cb (EmpathyFTHandler *handler,
534                                 guint64 current_bytes,
535                                 guint64 total_bytes,
536                                 EmpathyFTManager *manager)
537 {
538   char *first_line, *second_line, *message;
539   GtkTreeRowReference *row_ref;
540
541   row_ref = ft_manager_get_row_from_handler (manager, handler);
542   g_return_if_fail (row_ref != NULL);
543
544   first_line =  g_strdup_printf (_("Hashing \"%s\""),
545       empathy_ft_handler_get_filename (handler));
546   second_line = ft_manager_format_progress_bytes_and_percentage
547     (current_bytes, total_bytes, NULL);
548
549   message = g_strdup_printf ("%s\n%s", first_line, second_line);
550
551   ft_manager_update_handler_message (manager, row_ref, message);
552
553   g_free (message);
554 }
555
556 static void
557 ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
558                                EmpathyFTManager *manager)
559 {
560   char *message;
561   GtkTreeRowReference *row_ref;
562
563   DEBUG ("Hashing started");
564
565   g_signal_connect (handler, "hashing-progress",
566      G_CALLBACK (ft_handler_hashing_progress_cb), manager);
567   g_signal_connect (handler, "hashing-done",
568      G_CALLBACK (ft_handler_hashing_done_cb), manager);
569
570   row_ref = ft_manager_get_row_from_handler (manager, handler);
571   g_return_if_fail (row_ref != NULL);
572
573   message =  g_strdup_printf (_("Hashing \"%s\""),
574       empathy_ft_handler_get_filename (handler));
575
576   ft_manager_update_handler_message (manager, row_ref, message);
577
578   g_free (message);
579 }
580
581 static void
582 ft_manager_start_transfer (EmpathyFTManager *manager,
583                            EmpathyFTHandler *handler)
584 {
585   EmpathyFTManagerPriv *priv;
586   gboolean is_incoming;
587
588   priv = GET_PRIV (manager);
589
590   is_incoming = empathy_ft_handler_is_incoming (handler);
591
592   DEBUG ("Start transfer, is incoming %d", is_incoming);
593
594   /* now connect the signals */
595   g_signal_connect (handler, "transfer-error",
596       G_CALLBACK (ft_handler_transfer_error_cb), manager);
597
598   if (is_incoming) {
599     g_signal_connect (handler, "transfer-started",
600         G_CALLBACK (ft_handler_transfer_started_cb), manager);
601   } else {
602     g_signal_connect (handler, "hashing-started",
603         G_CALLBACK (ft_handler_hashing_started_cb), manager);
604   }
605
606   empathy_ft_handler_start_transfer (handler);
607 }
608
609 static void
610 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
611                                 EmpathyFTHandler *handler,
612                                 const GError *error)
613 {
614   GtkTreeRowReference *row_ref;
615   GtkTreeIter iter;
616   GtkTreeSelection *selection;
617   GtkTreePath *path;
618   GIcon *icon;
619   const char *content_type, *second_line;
620   char *first_line, *message;
621   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
622
623   icon = NULL;
624
625   /* get the icon name from the mime-type of the file. */
626   content_type = empathy_ft_handler_get_content_type (handler);
627
628   if (content_type != NULL)
629     icon = g_content_type_get_icon (content_type);
630
631   /* append the handler in the store */
632   gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
633       &iter, G_MAXINT, COL_FT_OBJECT, handler,
634       COL_ICON, icon, -1);
635
636   if (icon != NULL)
637     g_object_unref (icon);
638
639   /* insert the new row_ref in the hash table  */
640   path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
641   row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path);
642   gtk_tree_path_free (path);
643   g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler),
644       row_ref);
645
646   /* select the new row */
647   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
648   gtk_tree_selection_select_iter (selection, &iter);
649
650   if (error != NULL)
651     {
652       message = ft_manager_format_error_message (handler, error);
653       ft_manager_update_handler_message (manager, row_ref, message);
654
655       g_free (message);
656       return;
657     }
658
659   /* update the row with the initial values */
660   if (empathy_ft_handler_is_incoming (handler)) {
661     first_line = ft_manager_format_contact_info (handler);
662     second_line = _("Waiting for the other participant's response");
663     message = g_strdup_printf ("%s\n%s", first_line, second_line);
664
665     ft_manager_update_handler_message (manager, row_ref, message);
666
667     g_free (first_line);
668     g_free (message);
669   }
670
671
672   /* hook up the signals and start the transfer */
673   ft_manager_start_transfer (manager, handler);
674 }
675
676 static void
677 ft_manager_clear (EmpathyFTManager *manager)
678 {
679   EmpathyFTManagerPriv *priv;
680
681   DEBUG ("Clearing file transfer list");
682
683   priv = GET_PRIV (manager);
684
685   /* Remove completed and cancelled transfers */
686   g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
687       remove_finished_transfer_foreach, manager);
688 }
689
690 static void
691 ft_manager_open (EmpathyFTManager *manager)
692 {
693   GtkTreeSelection *selection;
694   GtkTreeIter iter;
695   GtkTreeModel *model;
696   EmpathyFTHandler *handler;
697   char *uri;
698   GFile *file;
699   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
700
701   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
702
703   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
704     return;
705
706   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
707
708   file = empathy_ft_handler_get_gfile (handler);
709   uri = g_file_get_uri (file);
710
711   DEBUG ("Opening URI: %s", uri);
712   empathy_url_show (GTK_WIDGET (priv->window), uri);
713
714   g_object_unref (handler);
715   g_free (uri);
716 }
717
718 static void
719 ft_manager_stop (EmpathyFTManager *manager)
720 {
721   GtkTreeSelection *selection;
722   GtkTreeIter iter;
723   GtkTreeModel *model;
724   EmpathyFTHandler *handler;
725   EmpathyFTManagerPriv *priv;
726
727   priv = GET_PRIV (manager);
728
729   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
730
731   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
732     return;
733
734   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
735   g_return_if_fail (handler != NULL);
736
737   DEBUG ("Stopping file transfer: contact=%s, filename=%s",
738       empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
739       empathy_ft_handler_get_filename (handler));
740
741   empathy_ft_handler_cancel_transfer (handler);
742
743   g_object_unref (handler);
744 }
745
746 static gboolean
747 ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager)
748 {
749   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
750   gint x, y, w, h;
751
752   gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h);
753   gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y);
754
755   empathy_geometry_save ("ft-manager", x, y, w, h);
756
757   priv->save_geometry_id = 0;
758
759   return FALSE;
760 }
761
762 static gboolean
763 ft_manager_configure_event_cb (GtkWidget *widget,
764                                GdkEventConfigure *event,
765                                EmpathyFTManager *manager)
766 {
767   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
768
769   if (priv->save_geometry_id != 0)
770     g_source_remove (priv->save_geometry_id);
771
772   priv->save_geometry_id = g_timeout_add (500,
773       (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager);
774
775   return FALSE;
776 }
777
778 static void
779 ft_manager_response_cb (GtkWidget *widget,
780                         gint response,
781                         EmpathyFTManager *manager)
782 {
783   switch (response)
784     {
785       case RESPONSE_CLEAR:
786         ft_manager_clear (manager);
787         break;
788       case RESPONSE_OPEN:
789         ft_manager_open (manager);
790         break;
791       case RESPONSE_STOP:
792         ft_manager_stop (manager);
793         break;
794     }
795 }
796
797 static gboolean
798 ft_manager_delete_event_cb (GtkWidget *widget,
799                             GdkEvent *event,
800                             EmpathyFTManager *manager)
801 {
802   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
803
804   /* remove all the completed/cancelled/errored transfers */
805   ft_manager_clear (manager);
806
807   if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
808     {
809       /* There is still FTs on flight, just hide the window */
810       DEBUG ("Hiding window");
811       gtk_widget_hide (widget);
812       return TRUE;
813     }
814
815   return FALSE;
816 }
817
818 static void
819 ft_manager_destroy_cb (GtkWidget *widget,
820                        EmpathyFTManager *manager)
821 {
822   g_object_unref (manager);
823 }
824
825 static void
826 ft_manager_build_ui (EmpathyFTManager *manager)
827 {
828   GtkBuilder *gui;
829   gint x, y, w, h;
830   GtkTreeView *view;
831   GtkListStore *liststore;
832   GtkTreeViewColumn *column;
833   GtkCellRenderer *renderer;
834   GtkTreeSelection *selection;
835   gchar *filename;
836   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
837
838   filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
839   gui = empathy_builder_get_file (filename,
840       "ft_manager_dialog", &priv->window,
841       "ft_list", &priv->treeview,
842       "open_button", &priv->open_button,
843       "abort_button", &priv->abort_button,
844       NULL);
845   g_free (filename);
846
847   empathy_builder_connect (gui, manager,
848       "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
849       "ft_manager_dialog", "response", ft_manager_response_cb,
850       "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
851       "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb,
852       NULL);
853
854   g_object_unref (gui);
855
856   /* Window geometry. */
857   empathy_geometry_load ("ft-manager", &x, &y, &w, &h);
858
859   if (x >= 0 && y >= 0)
860     {
861       /* Let the window manager position it if we don't have
862        * good x, y coordinates. */
863       gtk_window_move (GTK_WINDOW (priv->window), x, y);
864     }
865
866   if (w > 0 && h > 0)
867     {
868       /* Use the defaults from the ui file if we don't have
869        * good w, h geometry. */
870       gtk_window_resize (GTK_WINDOW (priv->window), w, h);
871     }
872
873   /* Setup the tree view */
874   view = GTK_TREE_VIEW (priv->treeview);
875   selection = gtk_tree_view_get_selection (view);
876   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
877   g_signal_connect (selection, "changed",
878       G_CALLBACK (ft_manager_selection_changed), manager);
879   gtk_tree_view_set_headers_visible (view, TRUE);
880   gtk_tree_view_set_enable_search (view, FALSE);
881
882   /* Setup the model */
883   liststore = gtk_list_store_new (5,
884       G_TYPE_INT,     /* percent */
885       G_TYPE_ICON,    /* icon */
886       G_TYPE_STRING,  /* message */
887       G_TYPE_STRING,  /* remaining */
888       G_TYPE_OBJECT); /* ft_handler */
889   gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
890   priv->model = GTK_TREE_MODEL (liststore);
891   g_object_unref (liststore);
892
893   /* Progress column */
894   column = gtk_tree_view_column_new ();
895   gtk_tree_view_column_set_title (column, _("%"));
896   gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
897   gtk_tree_view_insert_column (view, column, -1);
898
899   renderer = gtk_cell_renderer_progress_new ();
900   g_object_set (renderer, "xalign", 0.5, NULL);
901   gtk_tree_view_column_pack_start (column, renderer, FALSE);
902   gtk_tree_view_column_set_cell_data_func (column, renderer,
903       ft_manager_progress_cell_data_func, NULL, NULL);
904
905   /* Icon and filename column*/
906   column = gtk_tree_view_column_new ();
907   gtk_tree_view_column_set_title (column, _("File"));
908   gtk_tree_view_column_set_expand (column, TRUE);
909   gtk_tree_view_column_set_resizable (column, TRUE);
910   gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
911   gtk_tree_view_column_set_spacing (column, 3);
912   gtk_tree_view_insert_column (view, column, -1);
913
914   renderer = gtk_cell_renderer_pixbuf_new ();
915   g_object_set (renderer, "xpad", 3,
916       "stock-size", GTK_ICON_SIZE_DND, NULL);
917   gtk_tree_view_column_pack_start (column, renderer, FALSE);
918   gtk_tree_view_column_set_attributes (column, renderer,
919       "gicon", COL_ICON, NULL);
920
921   renderer = gtk_cell_renderer_text_new ();
922   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
923   gtk_tree_view_column_pack_start (column, renderer, TRUE);
924   gtk_tree_view_column_set_attributes (column, renderer,
925       "text", COL_MESSAGE, NULL);
926
927   /* Remaining time column */
928   column = gtk_tree_view_column_new ();
929   gtk_tree_view_column_set_title (column, _("Remaining"));
930   gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
931   gtk_tree_view_insert_column (view, column, -1);
932
933   renderer = gtk_cell_renderer_text_new ();
934   g_object_set (renderer, "xalign", 0.5, NULL);
935   gtk_tree_view_column_pack_start (column, renderer, FALSE);
936   gtk_tree_view_column_set_attributes (column, renderer,
937       "text", COL_REMAINING, NULL);
938 }
939
940 /* GObject method overrides */
941
942 static void
943 empathy_ft_manager_finalize (GObject *object)
944 {
945   EmpathyFTManagerPriv *priv = GET_PRIV (object);
946
947   DEBUG ("%p", object);
948
949   g_hash_table_destroy (priv->ft_handler_to_row_ref);
950
951   if (priv->save_geometry_id != 0)
952     g_source_remove (priv->save_geometry_id);
953
954   G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
955 }
956
957 static void
958 empathy_ft_manager_init (EmpathyFTManager *manager)
959 {
960   EmpathyFTManagerPriv *priv;
961
962   priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
963       EmpathyFTManagerPriv);
964
965   manager->priv = priv;
966
967   priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
968       g_direct_equal, (GDestroyNotify) g_object_unref,
969       (GDestroyNotify) gtk_tree_row_reference_free);
970
971   ft_manager_build_ui (manager);
972 }
973
974 static GObject *
975 empathy_ft_manager_constructor (GType type,
976                                 guint n_props,
977                                 GObjectConstructParam *props)
978 {
979   GObject *retval;
980
981   if (manager_singleton)
982     {
983       retval = g_object_ref (manager_singleton);
984     }
985   else
986     {
987       retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
988           (type, n_props, props);
989
990       manager_singleton = EMPATHY_FT_MANAGER (retval);
991       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
992     }
993
994   return retval;
995 }
996
997 static void
998 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
999 {
1000   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1001
1002   object_class->finalize = empathy_ft_manager_finalize;
1003   object_class->constructor = empathy_ft_manager_constructor;
1004
1005   g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
1006 }
1007
1008 /* public methods */
1009
1010 /**
1011  * empathy_ft_manager_dup_singleton:
1012  *
1013  * Returns a reference to the #EmpathyFTManager singleton object.
1014  *
1015  * Returns: a #EmpathyFTManager
1016  */
1017 EmpathyFTManager *
1018 empathy_ft_manager_dup_singleton (void)
1019 {
1020   return g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1021 }
1022
1023 void
1024 empathy_ft_manager_add_handler (EmpathyFTManager *manager,
1025                                 EmpathyFTHandler *handler)
1026 {
1027   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1028
1029   DEBUG ("Adding handler");
1030
1031   g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1032   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1033
1034   ft_manager_add_handler_to_list (manager, handler, NULL);
1035   gtk_window_present (GTK_WINDOW (priv->window));
1036 }
1037
1038 void
1039 empathy_ft_manager_display_error (EmpathyFTManager *manager,
1040                                   EmpathyFTHandler *handler,
1041                                   const GError *error)
1042 {
1043   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1044
1045   g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1046   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1047   g_return_if_fail (error != NULL);
1048
1049   ft_manager_add_handler_to_list (manager, handler, error);
1050   gtk_window_present (GTK_WINDOW (priv->window));
1051 }