]> git.0d.be Git - empathy.git/blob - src/empathy-ft-manager.c
Handle errors coming from the EmpathyFTFactory
[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 char *
331 ft_manager_format_error_message (EmpathyFTHandler *handler,
332                                  const GError *error)
333 {
334   const char *contact_name, *filename;
335   EmpathyContact *contact;
336   char *first_line, *message;
337   gboolean incoming;
338
339   contact_name = NULL;
340   incoming = empathy_ft_handler_is_incoming (handler);
341
342   contact = empathy_ft_handler_get_contact (handler);
343   if (contact)
344     contact_name = empathy_contact_get_name (contact);
345
346   filename = empathy_ft_handler_get_filename (handler);
347
348   if (incoming)
349     /* filename/contact_name here are either both NULL or both valid */
350     if (filename && contact_name)
351       /* translators: first %s is filename, second %s
352        * is the contact name */
353       first_line = g_strdup_printf (_("Error receiving \"%s\" from %s"), filename,
354           contact_name);
355     else
356       first_line = g_strdup (_("Error receiving a file"));
357   else
358     /* translators: first %s is filename, second %s
359      * is the contact name */
360     if (filename && contact_name)
361       first_line = g_strdup_printf (_("Error sending \"%s\" to %s"), filename,
362           contact_name);
363     else
364       first_line = g_strdup (_("Error sending a file"));
365
366   message = g_strdup_printf ("%s\n%s", first_line, error->message);
367
368   g_free (first_line);
369
370   return message;
371 }
372
373 static void
374 ft_manager_update_handler_message (EmpathyFTManager *manager,
375                                    GtkTreeRowReference *row_ref,
376                                    const char *message)
377 {
378   GtkTreePath *path;
379   GtkTreeIter iter;
380   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
381
382   /* Set new value in the store */
383   path = gtk_tree_row_reference_get_path (row_ref);
384   gtk_tree_model_get_iter (priv->model, &iter, path);
385   gtk_list_store_set (GTK_LIST_STORE (priv->model),
386       &iter,
387       COL_MESSAGE, message ? message : "",
388       -1);
389
390   gtk_tree_path_free (path);
391 }
392
393 static void
394 ft_manager_update_handler_progress (EmpathyFTManager *manager,
395                                     GtkTreeRowReference *row_ref,
396                                     int percentage)
397 {
398   GtkTreePath *path;
399   GtkTreeIter iter;
400   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
401
402   /* Set new value in the store */
403   path = gtk_tree_row_reference_get_path (row_ref);
404   gtk_tree_model_get_iter (priv->model, &iter, path);
405   gtk_list_store_set (GTK_LIST_STORE (priv->model),
406       &iter,
407       COL_PERCENT, percentage,
408       -1);
409
410   gtk_tree_path_free (path);
411
412 }
413
414 static void
415 ft_handler_transfer_error_cb (EmpathyFTHandler *handler,
416                               GError *error,
417                               EmpathyFTManager *manager)
418 {
419   char *message;
420   GtkTreeRowReference *row_ref;
421
422   DEBUG ("Transfer error %s", error->message);
423
424   row_ref = ft_manager_get_row_from_handler (manager, handler);
425   g_return_if_fail (row_ref != NULL);
426
427   message = ft_manager_format_error_message (handler, error);
428
429   ft_manager_update_handler_message (manager, row_ref, message);
430   ft_manager_update_buttons (manager);
431
432   g_free (message);
433 }
434
435 static void
436 ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
437                              EmpathyTpFile *tp_file,
438                              EmpathyFTManager *manager)
439 {
440   const char *contact_name;
441   const char *filename;
442   char *first_line, *second_line, *message;
443   gboolean incoming;
444   GtkTreeRowReference *row_ref;
445
446   DEBUG ("Transfer done");
447
448   row_ref = ft_manager_get_row_from_handler (manager, handler);
449   g_return_if_fail (row_ref != NULL);
450
451   incoming = empathy_ft_handler_is_incoming (handler);
452   contact_name = empathy_contact_get_name
453     (empathy_ft_handler_get_contact (handler));
454   filename = empathy_ft_handler_get_filename (handler);
455
456   if (incoming)
457     /* translators: first %s is filename, second %s
458      * is the contact name */
459     first_line = g_strdup_printf (_("\"%s\" received from %s"), filename,
460         contact_name);
461   else
462     /* translators: first %s is filename, second %s
463      * is the contact name */
464     first_line = g_strdup_printf (_("\"%s\" sent to %s"), filename,
465         contact_name);
466
467   second_line = g_strdup (_("File transfer completed"));
468
469   message = g_strdup_printf ("%s\n%s", first_line, second_line);
470   ft_manager_update_handler_message (manager, row_ref, message);
471
472   /* update buttons */
473   ft_manager_update_buttons (manager);
474
475   g_free (message);
476   g_free (first_line);
477   g_free (second_line);
478 }
479
480 static void
481 ft_handler_transfer_progress_cb (EmpathyFTHandler *handler,
482                                  guint64 current_bytes,
483                                  guint64 total_bytes,
484                                  EmpathyFTManager *manager)
485 {
486   char *first_line, *second_line, *message;
487   int percentage;
488   GtkTreeRowReference *row_ref;
489
490   DEBUG ("Transfer progress");
491
492   row_ref = ft_manager_get_row_from_handler (manager, handler);
493   g_return_if_fail (row_ref != NULL);
494
495   first_line = ft_manager_format_contact_info (handler);
496   second_line = ft_manager_format_progress_bytes_and_percentage
497     (current_bytes, total_bytes, &percentage);
498
499   message = g_strdup_printf ("%s\n%s", first_line, second_line);
500
501   ft_manager_update_handler_message (manager, row_ref, message);
502   ft_manager_update_handler_progress (manager, row_ref, percentage);
503
504   g_free (message);
505 }
506
507 static void
508 ft_handler_transfer_started_cb (EmpathyFTHandler *handler,
509                                 EmpathyTpFile *tp_file,
510                                 EmpathyFTManager *manager)
511 {
512   guint64 transferred_bytes, total_bytes;
513
514   DEBUG ("Transfer started");
515
516   g_signal_connect (handler, "transfer-progress",
517       G_CALLBACK (ft_handler_transfer_progress_cb), manager);
518   g_signal_connect (handler, "transfer-done",
519       G_CALLBACK (ft_handler_transfer_done_cb), manager);
520
521   transferred_bytes = empathy_ft_handler_get_transferred_bytes (handler);
522   total_bytes = empathy_ft_handler_get_total_bytes (handler);
523
524   ft_handler_transfer_progress_cb (handler, transferred_bytes, total_bytes,
525       manager);
526 }
527
528 static void
529 ft_handler_hashing_done_cb (EmpathyFTHandler *handler,
530                             EmpathyFTManager *manager)
531 {
532   GtkTreeRowReference *row_ref;
533   char *first_line, *second_line, *message;
534
535   DEBUG ("Hashing done");
536
537   row_ref = ft_manager_get_row_from_handler (manager, handler);
538   g_return_if_fail (row_ref != NULL);
539
540   /* update the message */
541   first_line = ft_manager_format_contact_info (handler);
542   second_line = g_strdup (_("Waiting for the other participant's response"));
543   message = g_strdup_printf ("%s\n%s", first_line, second_line);
544
545   ft_manager_update_handler_message (manager, row_ref, message);
546
547   g_free (message);
548   g_free (first_line);
549   g_free (second_line);
550
551   g_signal_connect (handler, "transfer-started",
552       G_CALLBACK (ft_handler_transfer_started_cb), manager);
553 }
554
555 static void
556 ft_handler_hashing_progress_cb (EmpathyFTHandler *handler,
557                                 guint64 current_bytes,
558                                 guint64 total_bytes,
559                                 EmpathyFTManager *manager)
560 {
561   char *first_line, *second_line, *message;
562   GtkTreeRowReference *row_ref;
563
564   DEBUG ("Hashing progress");
565
566   row_ref = ft_manager_get_row_from_handler (manager, handler);
567   g_return_if_fail (row_ref != NULL);
568
569   first_line =  g_strdup_printf (_("Hashing \"%s\""),
570       empathy_ft_handler_get_filename (handler));
571   second_line = ft_manager_format_progress_bytes_and_percentage
572     (current_bytes, total_bytes, NULL);
573
574   message = g_strdup_printf ("%s\n%s", first_line, second_line);
575
576   ft_manager_update_handler_message (manager, row_ref, message);
577
578   g_free (message);
579 }
580
581 static void
582 ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
583                                EmpathyFTManager *manager)
584 {
585   char *message;
586   GtkTreeRowReference *row_ref;
587
588   DEBUG ("Hashing started");
589
590   g_signal_connect (handler, "hashing-progress",
591      G_CALLBACK (ft_handler_hashing_progress_cb), manager);
592   g_signal_connect (handler, "hashing-done",
593      G_CALLBACK (ft_handler_hashing_done_cb), manager);
594
595   row_ref = ft_manager_get_row_from_handler (manager, handler);
596   g_return_if_fail (row_ref != NULL);
597
598   message =  g_strdup_printf (_("Hashing \"%s\""),
599       empathy_ft_handler_get_filename (handler));
600
601   ft_manager_update_handler_message (manager, row_ref, message);
602
603   g_free (message);
604 }
605
606 static void
607 ft_manager_start_transfer (EmpathyFTManager *manager,
608                            EmpathyFTHandler *handler)
609 {
610   EmpathyFTManagerPriv *priv;
611   gboolean is_incoming;
612
613   priv = GET_PRIV (manager);
614
615   is_incoming = empathy_ft_handler_is_incoming (handler);
616
617   DEBUG ("Start transfer, is incoming %d", is_incoming);
618
619   /* now connect the signals */
620   g_signal_connect (handler, "transfer-error",
621       G_CALLBACK (ft_handler_transfer_error_cb), manager);
622
623   if (is_incoming) {
624     g_signal_connect (handler, "transfer-started",
625         G_CALLBACK (ft_handler_transfer_started_cb), manager);
626   } else {
627     g_signal_connect (handler, "hashing-started",
628         G_CALLBACK (ft_handler_hashing_started_cb), manager);
629   }
630
631   empathy_ft_handler_start_transfer (handler);
632 }
633
634 static void
635 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
636                                 EmpathyFTHandler *handler,
637                                 const GError *error)
638 {
639   GtkTreeRowReference *row_ref;
640   GtkTreeIter iter;
641   GtkTreeSelection *selection;
642   GtkTreePath *path;
643   GIcon *icon;
644   const char *content_type, *second_line;
645   char *first_line, *message;
646   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
647
648   icon = NULL;
649
650   /* get the icon name from the mime-type of the file. */
651   content_type = empathy_ft_handler_get_content_type (handler);
652
653   if (content_type != NULL)
654     icon = g_content_type_get_icon (content_type);
655
656   /* append the handler in the store */
657   gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
658       &iter, G_MAXINT, COL_FT_OBJECT, handler,
659       COL_ICON, icon, -1);
660
661   if (icon != NULL)
662     g_object_unref (icon);
663
664   /* insert the new row_ref in the hash table  */
665   path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
666   row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path);
667   gtk_tree_path_free (path);
668   g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler),
669       row_ref);
670
671   /* select the new row */
672   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
673   gtk_tree_selection_select_iter (selection, &iter);
674
675   if (error != NULL)
676     {
677       message = ft_manager_format_error_message (handler, error);
678       ft_manager_update_handler_message (manager, row_ref, message);
679
680       g_free (message);
681       return;
682     }
683
684   /* update the row with the initial values */
685   if (empathy_ft_handler_is_incoming (handler)) {
686     first_line = ft_manager_format_contact_info (handler);
687     second_line = _("Waiting for the other participant's response");
688     message = g_strdup_printf ("%s\n%s", first_line, second_line);
689
690     ft_manager_update_handler_message (manager, row_ref, message);
691
692     g_free (first_line);
693     g_free (message);
694   }
695
696
697   /* hook up the signals and start the transfer */
698   ft_manager_start_transfer (manager, handler);
699 }
700
701 static void
702 ft_manager_clear (EmpathyFTManager *manager)
703 {
704   EmpathyFTManagerPriv *priv;
705
706   DEBUG ("Clearing file transfer list");
707
708   priv = GET_PRIV (manager);
709
710   /* Remove completed and cancelled transfers */
711   g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
712       remove_finished_transfer_foreach, manager);
713 }
714
715 static void
716 ft_manager_open (EmpathyFTManager *manager)
717 {
718   GtkTreeSelection *selection;
719   GtkTreeIter iter;
720   GtkTreeModel *model;
721   EmpathyFTHandler *handler;
722   char *uri;
723   GFile *file;
724   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
725
726   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
727
728   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
729     return;
730
731   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
732
733   file = empathy_ft_handler_get_gfile (handler);
734   uri = g_file_get_uri (file);
735
736   DEBUG ("Opening URI: %s", uri);
737   empathy_url_show (GTK_WIDGET (priv->window), uri);
738
739   g_object_unref (handler);
740   g_free (uri);
741 }
742
743 static void
744 ft_manager_stop (EmpathyFTManager *manager)
745 {
746   GtkTreeSelection *selection;
747   GtkTreeIter iter;
748   GtkTreeModel *model;
749   EmpathyFTHandler *handler;
750   EmpathyFTManagerPriv *priv;
751
752   priv = GET_PRIV (manager);
753
754   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
755
756   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
757     return;
758
759   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
760   g_return_if_fail (handler != NULL);
761
762   DEBUG ("Stopping file transfer: contact=%s, filename=%s",
763       empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
764       empathy_ft_handler_get_filename (handler));
765
766   empathy_ft_handler_cancel_transfer (handler);
767
768   g_object_unref (handler);
769 }
770
771 static gboolean
772 ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager)
773 {
774   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
775   gint x, y, w, h;
776
777   gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h);
778   gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y);
779
780   empathy_geometry_save ("ft-manager", x, y, w, h);
781
782   priv->save_geometry_id = 0;
783
784   return FALSE;
785 }
786
787 static gboolean
788 ft_manager_configure_event_cb (GtkWidget *widget,
789                                GdkEventConfigure *event,
790                                EmpathyFTManager *manager)
791 {
792   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
793
794   if (priv->save_geometry_id != 0)
795     g_source_remove (priv->save_geometry_id);
796
797   priv->save_geometry_id = g_timeout_add (500,
798       (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager);
799
800   return FALSE;
801 }
802
803 static void
804 ft_manager_response_cb (GtkWidget *widget,
805                         gint response,
806                         EmpathyFTManager *manager)
807 {
808   switch (response)
809     {
810       case RESPONSE_CLEAR:
811         ft_manager_clear (manager);
812         break;
813       case RESPONSE_OPEN:
814         ft_manager_open (manager);
815         break;
816       case RESPONSE_STOP:
817         ft_manager_stop (manager);
818         break;
819     }
820 }
821
822 static gboolean
823 ft_manager_delete_event_cb (GtkWidget *widget,
824                             GdkEvent *event,
825                             EmpathyFTManager *manager)
826 {
827   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
828
829   /* remove all the completed/cancelled/errored transfers */
830   ft_manager_clear (manager);
831
832   if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
833     {
834       /* There is still FTs on flight, just hide the window */
835       DEBUG ("Hiding window");
836       gtk_widget_hide (widget);
837       return TRUE;
838     }
839
840   return FALSE;
841 }
842
843 static void
844 ft_manager_destroy_cb (GtkWidget *widget,
845                        EmpathyFTManager *manager)
846 {
847   g_object_unref (manager);
848 }
849
850 static void
851 ft_manager_build_ui (EmpathyFTManager *manager)
852 {
853   GtkBuilder *gui;
854   gint x, y, w, h;
855   GtkTreeView *view;
856   GtkListStore *liststore;
857   GtkTreeViewColumn *column;
858   GtkCellRenderer *renderer;
859   GtkTreeSelection *selection;
860   gchar *filename;
861   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
862
863   filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
864   gui = empathy_builder_get_file (filename,
865       "ft_manager_dialog", &priv->window,
866       "ft_list", &priv->treeview,
867       "open_button", &priv->open_button,
868       "abort_button", &priv->abort_button,
869       NULL);
870   g_free (filename);
871
872   empathy_builder_connect (gui, manager,
873       "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
874       "ft_manager_dialog", "response", ft_manager_response_cb,
875       "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
876       "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb,
877       NULL);
878
879   g_object_unref (gui);
880
881   /* Window geometry. */
882   empathy_geometry_load ("ft-manager", &x, &y, &w, &h);
883
884   if (x >= 0 && y >= 0)
885     {
886       /* Let the window manager position it if we don't have
887        * good x, y coordinates. */
888       gtk_window_move (GTK_WINDOW (priv->window), x, y);
889     }
890
891   if (w > 0 && h > 0)
892     {
893       /* Use the defaults from the ui file if we don't have
894        * good w, h geometry. */
895       gtk_window_resize (GTK_WINDOW (priv->window), w, h);
896     }
897
898   /* Setup the tree view */
899   view = GTK_TREE_VIEW (priv->treeview);
900   selection = gtk_tree_view_get_selection (view);
901   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
902   g_signal_connect (selection, "changed",
903       G_CALLBACK (ft_manager_selection_changed), manager);
904   gtk_tree_view_set_headers_visible (view, TRUE);
905   gtk_tree_view_set_enable_search (view, FALSE);
906
907   /* Setup the model */
908   liststore = gtk_list_store_new (5,
909       G_TYPE_INT,     /* percent */
910       G_TYPE_ICON,    /* icon */
911       G_TYPE_STRING,  /* message */
912       G_TYPE_STRING,  /* remaining */
913       G_TYPE_OBJECT); /* ft_handler */
914   gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
915   priv->model = GTK_TREE_MODEL (liststore);
916   g_object_unref (liststore);
917
918   /* Progress column */
919   column = gtk_tree_view_column_new ();
920   gtk_tree_view_column_set_title (column, _("%"));
921   gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
922   gtk_tree_view_insert_column (view, column, -1);
923
924   renderer = gtk_cell_renderer_progress_new ();
925   g_object_set (renderer, "xalign", 0.5, NULL);
926   gtk_tree_view_column_pack_start (column, renderer, FALSE);
927   gtk_tree_view_column_set_cell_data_func (column, renderer,
928       ft_manager_progress_cell_data_func, NULL, NULL);
929
930   /* Icon and filename column*/
931   column = gtk_tree_view_column_new ();
932   gtk_tree_view_column_set_title (column, _("File"));
933   gtk_tree_view_column_set_expand (column, TRUE);
934   gtk_tree_view_column_set_resizable (column, TRUE);
935   gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
936   gtk_tree_view_column_set_spacing (column, 3);
937   gtk_tree_view_insert_column (view, column, -1);
938
939   renderer = gtk_cell_renderer_pixbuf_new ();
940   g_object_set (renderer, "xpad", 3,
941       "stock-size", GTK_ICON_SIZE_DND, NULL);
942   gtk_tree_view_column_pack_start (column, renderer, FALSE);
943   gtk_tree_view_column_set_attributes (column, renderer,
944       "gicon", COL_ICON, NULL);
945
946   renderer = gtk_cell_renderer_text_new ();
947   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
948   gtk_tree_view_column_pack_start (column, renderer, TRUE);
949   gtk_tree_view_column_set_attributes (column, renderer,
950       "text", COL_MESSAGE, NULL);
951
952   /* Remaining time column */
953   column = gtk_tree_view_column_new ();
954   gtk_tree_view_column_set_title (column, _("Remaining"));
955   gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
956   gtk_tree_view_insert_column (view, column, -1);
957
958   renderer = gtk_cell_renderer_text_new ();
959   g_object_set (renderer, "xalign", 0.5, NULL);
960   gtk_tree_view_column_pack_start (column, renderer, FALSE);
961   gtk_tree_view_column_set_attributes (column, renderer,
962       "text", COL_REMAINING, NULL);
963 }
964
965 /* GObject method overrides */
966
967 static void
968 empathy_ft_manager_finalize (GObject *object)
969 {
970   EmpathyFTManagerPriv *priv = GET_PRIV (object);
971
972   DEBUG ("%p", object);
973
974   g_hash_table_destroy (priv->ft_handler_to_row_ref);
975
976   if (priv->save_geometry_id != 0)
977     g_source_remove (priv->save_geometry_id);
978
979   G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
980 }
981
982 static void
983 empathy_ft_manager_init (EmpathyFTManager *manager)
984 {
985   EmpathyFTManagerPriv *priv;
986
987   priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
988       EmpathyFTManagerPriv);
989
990   manager->priv = priv;
991
992   priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
993       g_direct_equal, (GDestroyNotify) g_object_unref,
994       (GDestroyNotify) gtk_tree_row_reference_free);
995
996   ft_manager_build_ui (manager);
997 }
998
999 static GObject *
1000 empathy_ft_manager_constructor (GType type,
1001                                 guint n_props,
1002                                 GObjectConstructParam *props)
1003 {
1004   GObject *retval;
1005
1006   if (manager_singleton)
1007     {
1008       retval = g_object_ref (manager_singleton);
1009     }
1010   else
1011     {
1012       retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
1013           (type, n_props, props);
1014
1015       manager_singleton = EMPATHY_FT_MANAGER (retval);
1016       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1017     }
1018
1019   return retval;
1020 }
1021
1022 static void
1023 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
1024 {
1025   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1026
1027   object_class->finalize = empathy_ft_manager_finalize;
1028   object_class->constructor = empathy_ft_manager_constructor;
1029
1030   g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
1031 }
1032
1033 /* public methods */
1034
1035 /**
1036  * empathy_ft_manager_dup_singleton:
1037  *
1038  * Returns a reference to the #EmpathyFTManager singleton object.
1039  *
1040  * Returns: a #EmpathyFTManager
1041  */
1042 EmpathyFTManager *
1043 empathy_ft_manager_dup_singleton (void)
1044 {
1045   return g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1046 }
1047
1048 void
1049 empathy_ft_manager_add_handler (EmpathyFTManager *manager,
1050                                 EmpathyFTHandler *handler)
1051 {
1052   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1053
1054   DEBUG ("Adding handler");
1055
1056   g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1057   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1058
1059   ft_manager_add_handler_to_list (manager, handler, NULL);
1060   gtk_window_present (GTK_WINDOW (priv->window));
1061 }
1062
1063 void
1064 empathy_ft_manager_display_error (EmpathyFTManager *manager,
1065                                   EmpathyFTHandler *handler,
1066                                   const GError *error)
1067 {
1068   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1069
1070   g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1071   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1072   g_return_if_fail (error != NULL);
1073
1074   ft_manager_add_handler_to_list (manager, handler, error);
1075   gtk_window_present (GTK_WINDOW (priv->window));
1076 }