]> git.0d.be Git - empathy.git/blob - src/empathy-ft-manager.c
8d94e397dd887e21b1b4220a6709c7fd4c1d2e57
[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 #if 0
157 static const gchar *
158 ft_manager_state_change_reason_to_string (TpFileTransferStateChangeReason reason)
159 {
160   switch (reason)
161     {
162       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
163         return _("No reason was specified");
164       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
165         return _("The change in state was requested");
166       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
167         return _("You canceled the file transfer");
168       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
169         return _("The other participant canceled the file transfer");
170       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
171         return _("Error while trying to transfer the file");
172       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
173         return _("The other participant is unable to transfer the file");
174     }
175   return _("Unknown reason");
176 }
177 #endif
178
179 static void
180 ft_manager_selection_changed (GtkTreeSelection *selection,
181                               EmpathyFTManager *manager)
182 {
183   ft_manager_update_buttons (manager);
184 }
185
186 static void
187 ft_manager_progress_cell_data_func (GtkTreeViewColumn *col,
188                                     GtkCellRenderer *renderer,
189                                     GtkTreeModel *model,
190                                     GtkTreeIter *iter,
191                                     gpointer user_data)
192 {
193   const gchar *text = NULL;
194   gint percent;
195
196   gtk_tree_model_get (model, iter, COL_PERCENT, &percent, -1);
197
198   if (percent < 0)
199     {
200       percent = 0;
201       text = C_("file transfer percent", "Unknown");
202     }
203
204   g_object_set (renderer, "text", text, "value", percent, NULL);
205 }
206
207 static GtkTreeRowReference *
208 ft_manager_get_row_from_handler (EmpathyFTManager *manager,
209                                  EmpathyFTHandler *handler)
210 {
211   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
212
213   return g_hash_table_lookup (priv->ft_handler_to_row_ref, handler);
214 }
215
216 static void
217 ft_manager_remove_file_from_model (EmpathyFTManager *manager,
218                                    EmpathyFTHandler *handler)
219 {
220   GtkTreeRowReference *row_ref;
221   GtkTreeSelection *selection;
222   GtkTreePath *path = NULL;
223   GtkTreeIter iter;
224   gboolean update_selection;
225   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
226
227   row_ref = ft_manager_get_row_from_handler (manager, handler);
228   g_return_if_fail (row_ref);
229
230   DEBUG ("Removing file transfer from window: contact=%s, filename=%s",
231       empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
232       empathy_ft_handler_get_filename (handler));
233
234   /* Get the iter from the row_ref */
235   path = gtk_tree_row_reference_get_path (row_ref);
236   gtk_tree_model_get_iter (priv->model, &iter, path);
237   gtk_tree_path_free (path);
238
239   /* We have to update the selection only if we are removing the selected row */
240   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
241   update_selection = gtk_tree_selection_iter_is_selected (selection, &iter);
242
243   /* Remove tp_file's row. After that iter points to the next row */
244   if (!gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter))
245     {
246       gint n_row;
247
248       /* There is no next row, set iter to the last row */
249       n_row = gtk_tree_model_iter_n_children (priv->model, NULL);
250       if (n_row > 0)
251         gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, n_row - 1);
252       else
253         update_selection = FALSE;
254     }
255
256   if (update_selection)
257     gtk_tree_selection_select_iter (selection, &iter);
258 }
259
260 static gboolean
261 remove_finished_transfer_foreach (gpointer key,
262                                   gpointer value,
263                                   gpointer user_data)
264 {
265   EmpathyFTHandler *handler = key;
266   EmpathyFTManager *manager = user_data;
267
268   if (empathy_ft_handler_is_completed (handler) ||
269       empathy_ft_handler_is_cancelled (handler))
270     {
271       ft_manager_remove_file_from_model (manager, handler);
272       return TRUE;
273     }
274
275   return FALSE;
276 }
277
278 static char *
279 ft_manager_format_progress_bytes_and_percentage (guint64 current,
280                                                  guint64 total,
281                                                  int *percentage)
282 {
283   char *total_str, *current_str, *retval;
284
285   total_str = g_format_size_for_display (total);
286   current_str = g_format_size_for_display (current);
287
288   /* translators: first %s is the currently processed size, second %s is
289    * the total file size */
290   retval = g_strdup_printf (_("%s of %s"), current_str, total_str);
291
292   g_free (total_str);
293   g_free (current_str);
294
295   if (percentage != NULL)
296     {
297       if (total != 0) 
298         *percentage = current * 100 / total;
299       else
300         *percentage = -1;
301     }
302
303   return retval;
304 }
305
306 static char *
307 ft_manager_format_contact_info (EmpathyFTHandler *handler)
308 {
309   gboolean incoming;
310   const char *filename, *contact_name, *first_line_format;
311   char *retval;
312
313   incoming = empathy_ft_handler_is_incoming (handler);
314   contact_name = empathy_contact_get_name
315     (empathy_ft_handler_get_contact (handler));
316   filename = empathy_ft_handler_get_filename (handler);
317
318   if (incoming)
319     /* translators: first %s is filename, second %s is the contact name */
320     first_line_format = _("Receiving \"%s\" from %s");
321   else
322     /* translators: first %s is filename, second %s is the contact name */
323     first_line_format = _("Sending \"%s\" to %s");
324
325   retval = g_strdup_printf (first_line_format, filename, contact_name);
326
327   return retval;
328 }
329
330 static void
331 ft_manager_update_handler_message (EmpathyFTManager *manager,
332                                    GtkTreeRowReference *row_ref,
333                                    const char *message)
334 {
335   GtkTreePath *path;
336   GtkTreeIter iter;
337   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
338
339   /* Set new value in the store */
340   path = gtk_tree_row_reference_get_path (row_ref);
341   gtk_tree_model_get_iter (priv->model, &iter, path);
342   gtk_list_store_set (GTK_LIST_STORE (priv->model),
343       &iter,
344       COL_MESSAGE, message ? message : "",
345       -1);
346
347   gtk_tree_path_free (path);
348 }
349
350 static void
351 ft_manager_update_handler_progress (EmpathyFTManager *manager,
352                                     GtkTreeRowReference *row_ref,
353                                     int percentage)
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_PERCENT, percentage,
365       -1);
366
367   gtk_tree_path_free (path);
368
369 }
370
371 static void
372 ft_handler_transfer_error_cb (EmpathyFTHandler *handler,
373                               GError *error,
374                               EmpathyFTManager *manager)
375 {
376   const char *contact_name, *filename;
377   char *first_line, *message;
378   gboolean incoming;
379   GtkTreeRowReference *row_ref;
380
381   DEBUG ("Transfer error %s", error->message);
382
383   row_ref = ft_manager_get_row_from_handler (manager, handler);
384   g_return_if_fail (row_ref != NULL);
385
386   incoming = empathy_ft_handler_is_incoming (handler);  
387   contact_name = empathy_contact_get_name
388     (empathy_ft_handler_get_contact (handler));
389   filename = empathy_ft_handler_get_filename (handler);
390
391   if (incoming)
392     /* translators: first %s is filename, second %s
393      * is the contact name */
394     first_line = g_strdup_printf (_("Error receiving \"%s\" from %s"), filename,
395         contact_name);
396   else
397     /* translators: first %s is filename, second %s
398      * is the contact name */
399     first_line = g_strdup_printf (_("Error sensing \"%s\" to %s"), filename,
400         contact_name);
401
402   message = g_strdup_printf ("%s\n%s", first_line, error->message);
403
404   ft_manager_update_handler_message (manager, row_ref, message);
405   ft_manager_update_buttons (manager);
406
407   g_free (first_line);
408   g_free (message);
409 }
410
411 static void
412 ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
413                              EmpathyTpFile *tp_file,
414                              EmpathyFTManager *manager)
415 {
416   const char *contact_name;
417   const char *filename;
418   char *first_line, *second_line, *message;
419   gboolean incoming;
420   GtkTreeRowReference *row_ref;
421
422   DEBUG ("Transfer done");
423
424   row_ref = ft_manager_get_row_from_handler (manager, handler);
425   g_return_if_fail (row_ref != NULL);
426
427   incoming = empathy_ft_handler_is_incoming (handler);
428   contact_name = empathy_contact_get_name
429     (empathy_ft_handler_get_contact (handler));
430   filename = empathy_ft_handler_get_filename (handler);
431
432   if (incoming)
433     /* translators: first %s is filename, second %s
434      * is the contact name */
435     first_line = g_strdup_printf (_("\"%s\" received from %s"), filename,
436         contact_name);
437   else
438     /* translators: first %s is filename, second %s
439      * is the contact name */
440     first_line = g_strdup_printf (_("\"%s\" sent to %s"), filename,
441         contact_name);
442
443   second_line = g_strdup (_("File transfer completed"));
444
445   message = g_strdup_printf ("%s\n%s", first_line, second_line);
446   ft_manager_update_handler_message (manager, row_ref, message);
447
448   /* update buttons */
449   ft_manager_update_buttons (manager);
450
451   g_free (message);
452   g_free (first_line);
453   g_free (second_line);
454 }
455
456 static void
457 ft_handler_transfer_progress_cb (EmpathyFTHandler *handler,
458                                  guint64 current_bytes,
459                                  guint64 total_bytes,
460                                  EmpathyFTManager *manager)
461 {
462   char *first_line, *second_line, *message;
463   int percentage;
464   GtkTreeRowReference *row_ref;
465
466   DEBUG ("Transfer progress");
467
468   row_ref = ft_manager_get_row_from_handler (manager, handler);
469   g_return_if_fail (row_ref != NULL);
470
471   first_line = ft_manager_format_contact_info (handler);
472   second_line = ft_manager_format_progress_bytes_and_percentage
473     (current_bytes, total_bytes, &percentage);
474
475   message = g_strdup_printf ("%s\n%s", first_line, second_line);
476
477   ft_manager_update_handler_message (manager, row_ref, message);
478   ft_manager_update_handler_progress (manager, row_ref, percentage);
479
480   g_free (message);
481 }
482
483 static void
484 ft_handler_transfer_started_cb (EmpathyFTHandler *handler,
485                                 EmpathyTpFile *tp_file,
486                                 EmpathyFTManager *manager)
487 {
488   guint64 transferred_bytes, total_bytes;
489
490   DEBUG ("Transfer started");
491
492   g_signal_connect (handler, "transfer-progress",
493       G_CALLBACK (ft_handler_transfer_progress_cb), manager);
494   g_signal_connect (handler, "transfer-done",
495       G_CALLBACK (ft_handler_transfer_done_cb), manager);
496
497   transferred_bytes = empathy_ft_handler_get_transferred_bytes (handler);
498   total_bytes = empathy_ft_handler_get_total_bytes (handler);
499
500   ft_handler_transfer_progress_cb (handler, transferred_bytes, total_bytes,
501       manager);
502 }
503
504 static void
505 ft_handler_hashing_done_cb (EmpathyFTHandler *handler,
506                             EmpathyFTManager *manager)
507 {
508   GtkTreeRowReference *row_ref;
509   char *first_line, *second_line, *message;
510
511   DEBUG ("Hashing done");
512
513   row_ref = ft_manager_get_row_from_handler (manager, handler);
514   g_return_if_fail (row_ref != NULL);
515
516   /* update the message */
517   first_line = ft_manager_format_contact_info (handler);
518   second_line = g_strdup (_("Waiting for the other participant's response"));
519   message = g_strdup_printf ("%s\n%s", first_line, second_line);
520
521   ft_manager_update_handler_message (manager, row_ref, message);
522
523   g_free (message);
524   g_free (first_line);
525   g_free (second_line);
526
527   g_signal_connect (handler, "transfer-started",
528       G_CALLBACK (ft_handler_transfer_started_cb), manager);
529 }
530
531 static void
532 ft_handler_hashing_progress_cb (EmpathyFTHandler *handler,
533                                 guint64 current_bytes,
534                                 guint64 total_bytes,
535                                 EmpathyFTManager *manager)
536 {
537   char *first_line, *second_line, *message;
538   GtkTreeRowReference *row_ref;
539
540   DEBUG ("Hashing progress");
541
542   row_ref = ft_manager_get_row_from_handler (manager, handler);
543   g_return_if_fail (row_ref != NULL);
544
545   first_line =  g_strdup_printf (_("Hashing \"%s\""),
546       empathy_ft_handler_get_filename (handler));
547   second_line = ft_manager_format_progress_bytes_and_percentage
548     (current_bytes, total_bytes, NULL);
549
550   message = g_strdup_printf ("%s\n%s", first_line, second_line);
551
552   ft_manager_update_handler_message (manager, row_ref, message);
553
554   g_free (message);
555 }
556
557 static void
558 ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
559                                EmpathyFTManager *manager)
560 {
561   char *message;
562   GtkTreeRowReference *row_ref;
563
564   DEBUG ("Hashing started");
565
566   g_signal_connect (handler, "hashing-progress",
567      G_CALLBACK (ft_handler_hashing_progress_cb), manager);
568   g_signal_connect (handler, "hashing-done",
569      G_CALLBACK (ft_handler_hashing_done_cb), manager);
570
571   row_ref = ft_manager_get_row_from_handler (manager, handler);
572   g_return_if_fail (row_ref != NULL);
573
574   message =  g_strdup_printf (_("Hashing \"%s\""),
575       empathy_ft_handler_get_filename (handler));
576
577   ft_manager_update_handler_message (manager, row_ref, message);
578
579   g_free (message);
580 }
581
582 static void
583 ft_manager_start_transfer (EmpathyFTManager *manager,
584                            EmpathyFTHandler *handler)
585 {
586   EmpathyFTManagerPriv *priv;
587   gboolean is_incoming;
588
589   priv = GET_PRIV (manager);
590
591   is_incoming = empathy_ft_handler_is_incoming (handler);
592
593   DEBUG ("Start transfer, is incoming %d", is_incoming);
594
595   /* now connect the signals */
596   g_signal_connect (handler, "transfer-error",
597       G_CALLBACK (ft_handler_transfer_error_cb), manager);
598
599   if (is_incoming) {
600     g_signal_connect (handler, "transfer-started",
601         G_CALLBACK (ft_handler_transfer_started_cb), manager);
602   } else {
603     g_signal_connect (handler, "hashing-started",
604         G_CALLBACK (ft_handler_hashing_started_cb), manager);
605   }
606
607   empathy_ft_handler_start_transfer (handler);
608 }
609
610 static void
611 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
612                                 EmpathyFTHandler *handler)
613 {
614   GtkTreeRowReference *row_ref;
615   GtkTreeIter iter;
616   GtkTreeSelection *selection;
617   GtkTreePath *path;
618   GIcon *icon;
619   const char *content_type;
620   char *first_line, *second_line, *message;
621   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
622
623   /* get the icon name from the mime-type of the file. */
624   content_type = empathy_ft_handler_get_content_type (handler);
625   icon = g_content_type_get_icon (content_type);
626
627   /* append the handler in the store */
628   gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
629       &iter, G_MAXINT, COL_FT_OBJECT, handler, COL_ICON, icon, -1);
630   g_object_unref (icon);
631
632   /* insert the new row_ref in the hash table  */
633   path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
634   row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path);
635   gtk_tree_path_free (path);
636   g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler),
637       row_ref);
638
639   /* select the new row */
640   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
641   gtk_tree_selection_select_iter (selection, &iter);
642
643   /* update the row with the initial values */
644   if (empathy_ft_handler_is_incoming (handler)) {
645     first_line = ft_manager_format_contact_info (handler);
646     second_line = g_strdup (_("Waiting for the other participant's response"));
647     message = g_strdup_printf ("%s\n%s", first_line, second_line);
648
649     ft_manager_update_handler_message (manager, row_ref, message);
650   }
651
652   /* hook up the signals and start the transfer */
653   ft_manager_start_transfer (manager, handler);
654
655   g_free (first_line);
656   g_free (second_line);
657   g_free (message);
658 }
659
660 static void
661 ft_manager_clear (EmpathyFTManager *manager)
662 {
663   EmpathyFTManagerPriv *priv;
664
665   DEBUG ("Clearing file transfer list");
666
667   priv = GET_PRIV (manager);
668
669   /* Remove completed and cancelled transfers */
670   g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
671       remove_finished_transfer_foreach, manager);
672 }
673
674 static void
675 ft_manager_open (EmpathyFTManager *manager)
676 {
677   GtkTreeSelection *selection;
678   GtkTreeIter iter;
679   GtkTreeModel *model;
680   EmpathyFTHandler *handler;
681   char *uri;
682   GFile *file;
683   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
684
685   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
686
687   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
688     return;
689
690   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
691
692   file = empathy_ft_handler_get_gfile (handler);
693   uri = g_file_get_uri (file);
694
695   DEBUG ("Opening URI: %s", uri);
696   empathy_url_show (GTK_WIDGET (priv->window), uri);
697
698   g_object_unref (handler);
699   g_free (uri);
700 }
701
702 static void
703 ft_manager_stop (EmpathyFTManager *manager)
704 {
705   GtkTreeSelection *selection;
706   GtkTreeIter iter;
707   GtkTreeModel *model;
708   EmpathyFTHandler *handler;
709   EmpathyFTManagerPriv *priv;
710
711   priv = GET_PRIV (manager);
712
713   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
714
715   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
716     return;
717
718   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
719   g_return_if_fail (handler != NULL);
720
721   DEBUG ("Stopping file transfer: contact=%s, filename=%s",
722       empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
723       empathy_ft_handler_get_filename (handler));
724
725   empathy_ft_handler_cancel_transfer (handler);
726
727   g_object_unref (handler);
728 }
729
730 static gboolean
731 ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager)
732 {
733   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
734   gint x, y, w, h;
735
736   gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h);
737   gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y);
738
739   empathy_geometry_save ("ft-manager", x, y, w, h);
740
741   priv->save_geometry_id = 0;
742
743   return FALSE;
744 }
745
746 static gboolean
747 ft_manager_configure_event_cb (GtkWidget *widget,
748                                GdkEventConfigure *event,
749                                EmpathyFTManager *manager)
750 {
751   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
752
753   if (priv->save_geometry_id != 0)
754     g_source_remove (priv->save_geometry_id);
755
756   priv->save_geometry_id = g_timeout_add (500,
757       (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager);
758
759   return FALSE;
760 }
761
762 static void
763 ft_manager_response_cb (GtkWidget *widget,
764                         gint response,
765                         EmpathyFTManager *manager)
766 {
767   switch (response)
768     {
769       case RESPONSE_CLEAR:
770         ft_manager_clear (manager);
771         break;
772       case RESPONSE_OPEN:
773         ft_manager_open (manager);
774         break;
775       case RESPONSE_STOP:
776         ft_manager_stop (manager);
777         break;
778     }
779 }
780
781 static gboolean
782 ft_manager_delete_event_cb (GtkWidget *widget,
783                             GdkEvent *event,
784                             EmpathyFTManager *manager)
785 {
786   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
787
788   /* remove all the completed/cancelled/errored transfers */
789   ft_manager_clear (manager);
790
791   if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
792     {
793       /* There is still FTs on flight, just hide the window */
794       DEBUG ("Hiding window");
795       gtk_widget_hide (widget);
796       return TRUE;
797     }
798
799   return FALSE;
800 }
801
802 static void
803 ft_manager_destroy_cb (GtkWidget *widget,
804                        EmpathyFTManager *manager)
805 {
806   g_object_unref (manager);
807 }
808
809 static void
810 ft_manager_build_ui (EmpathyFTManager *manager)
811 {
812   GtkBuilder *gui;
813   gint x, y, w, h;
814   GtkTreeView *view;
815   GtkListStore *liststore;
816   GtkTreeViewColumn *column;
817   GtkCellRenderer *renderer;
818   GtkTreeSelection *selection;
819   gchar *filename;
820   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
821
822   filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
823   gui = empathy_builder_get_file (filename,
824       "ft_manager_dialog", &priv->window,
825       "ft_list", &priv->treeview,
826       "open_button", &priv->open_button,
827       "abort_button", &priv->abort_button,
828       NULL);
829   g_free (filename);
830
831   empathy_builder_connect (gui, manager,
832       "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
833       "ft_manager_dialog", "response", ft_manager_response_cb,
834       "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
835       "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb,
836       NULL);
837
838   g_object_unref (gui);
839
840   /* Window geometry. */
841   empathy_geometry_load ("ft-manager", &x, &y, &w, &h);
842
843   if (x >= 0 && y >= 0)
844     {
845       /* Let the window manager position it if we don't have
846        * good x, y coordinates. */
847       gtk_window_move (GTK_WINDOW (priv->window), x, y);
848     }
849
850   if (w > 0 && h > 0)
851     {
852       /* Use the defaults from the ui file if we don't have
853        * good w, h geometry. */
854       gtk_window_resize (GTK_WINDOW (priv->window), w, h);
855     }
856
857   /* Setup the tree view */
858   view = GTK_TREE_VIEW (priv->treeview);
859   selection = gtk_tree_view_get_selection (view);
860   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
861   g_signal_connect (selection, "changed",
862       G_CALLBACK (ft_manager_selection_changed), manager);
863   gtk_tree_view_set_headers_visible (view, TRUE);
864   gtk_tree_view_set_enable_search (view, FALSE);
865
866   /* Setup the model */
867   liststore = gtk_list_store_new (5,
868       G_TYPE_INT,     /* percent */
869       G_TYPE_ICON,    /* icon */
870       G_TYPE_STRING,  /* message */
871       G_TYPE_STRING,  /* remaining */
872       G_TYPE_OBJECT); /* ft_handler */
873   gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
874   priv->model = GTK_TREE_MODEL (liststore);
875   g_object_unref (liststore);
876
877   /* Progress column */
878   column = gtk_tree_view_column_new ();
879   gtk_tree_view_column_set_title (column, _("%"));
880   gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
881   gtk_tree_view_insert_column (view, column, -1);
882
883   renderer = gtk_cell_renderer_progress_new ();
884   g_object_set (renderer, "xalign", 0.5, NULL);
885   gtk_tree_view_column_pack_start (column, renderer, FALSE);
886   gtk_tree_view_column_set_cell_data_func (column, renderer,
887       ft_manager_progress_cell_data_func, NULL, NULL);
888
889   /* Icon and filename column*/
890   column = gtk_tree_view_column_new ();
891   gtk_tree_view_column_set_title (column, _("File"));
892   gtk_tree_view_column_set_expand (column, TRUE);
893   gtk_tree_view_column_set_resizable (column, TRUE);
894   gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
895   gtk_tree_view_column_set_spacing (column, 3);
896   gtk_tree_view_insert_column (view, column, -1);
897
898   renderer = gtk_cell_renderer_pixbuf_new ();
899   g_object_set (renderer, "xpad", 3,
900       "stock-size", GTK_ICON_SIZE_DND, NULL);
901   gtk_tree_view_column_pack_start (column, renderer, FALSE);
902   gtk_tree_view_column_set_attributes (column, renderer,
903       "gicon", COL_ICON, NULL);
904
905   renderer = gtk_cell_renderer_text_new ();
906   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
907   gtk_tree_view_column_pack_start (column, renderer, TRUE);
908   gtk_tree_view_column_set_attributes (column, renderer,
909       "text", COL_MESSAGE, NULL);
910
911   /* Remaining time column */
912   column = gtk_tree_view_column_new ();
913   gtk_tree_view_column_set_title (column, _("Remaining"));
914   gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
915   gtk_tree_view_insert_column (view, column, -1);
916
917   renderer = gtk_cell_renderer_text_new ();
918   g_object_set (renderer, "xalign", 0.5, NULL);
919   gtk_tree_view_column_pack_start (column, renderer, FALSE);
920   gtk_tree_view_column_set_attributes (column, renderer,
921       "text", COL_REMAINING, NULL);
922 }
923
924 /* GObject method overrides */
925
926 static void
927 empathy_ft_manager_finalize (GObject *object)
928 {
929   EmpathyFTManagerPriv *priv = GET_PRIV (object);
930
931   DEBUG ("%p", object);
932
933   g_hash_table_destroy (priv->ft_handler_to_row_ref);
934
935   if (priv->save_geometry_id != 0)
936     g_source_remove (priv->save_geometry_id);
937
938   G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
939 }
940
941 static void
942 empathy_ft_manager_init (EmpathyFTManager *manager)
943 {
944   EmpathyFTManagerPriv *priv;
945
946   priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
947       EmpathyFTManagerPriv);
948
949   manager->priv = priv;
950
951   priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
952       g_direct_equal, (GDestroyNotify) g_object_unref,
953       (GDestroyNotify) gtk_tree_row_reference_free);
954
955   ft_manager_build_ui (manager);
956 }
957
958 static GObject *
959 empathy_ft_manager_constructor (GType type,
960                                 guint n_props,
961                                 GObjectConstructParam *props)
962 {
963   GObject *retval;
964
965   if (manager_singleton)
966     {
967       retval = g_object_ref (manager_singleton);
968     }
969   else
970     {
971       retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
972           (type, n_props, props);
973
974       manager_singleton = EMPATHY_FT_MANAGER (retval);
975       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
976     }
977
978   return retval;
979 }
980
981 static void
982 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
983 {
984   GObjectClass *object_class = G_OBJECT_CLASS (klass);
985
986   object_class->finalize = empathy_ft_manager_finalize;
987   object_class->constructor = empathy_ft_manager_constructor;
988
989   g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
990 }
991
992 /* public methods */
993
994 /**
995  * empathy_ft_manager_dup_singleton:
996  *
997  * Returns a reference to the #EmpathyFTManager singleton object.
998  *
999  * Returns: a #EmpathyFTManager
1000  */
1001 EmpathyFTManager *
1002 empathy_ft_manager_dup_singleton (void)
1003 {
1004   return g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1005 }
1006
1007 void
1008 empathy_ft_manager_add_handler (EmpathyFTManager *manager,
1009                                 EmpathyFTHandler *handler)
1010 {
1011   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1012
1013   DEBUG ("Adding handler");
1014
1015   g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1016   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1017
1018   ft_manager_add_handler_to_list (manager, handler);
1019   gtk_window_present (GTK_WINDOW (priv->window));
1020 }
1021