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