]> git.0d.be Git - empathy.git/blob - src/empathy-ft-manager.c
Remove hashing progress debug message
[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   row_ref = ft_manager_get_row_from_handler (manager, handler);
565   g_return_if_fail (row_ref != NULL);
566
567   first_line =  g_strdup_printf (_("Hashing \"%s\""),
568       empathy_ft_handler_get_filename (handler));
569   second_line = ft_manager_format_progress_bytes_and_percentage
570     (current_bytes, total_bytes, NULL);
571
572   message = g_strdup_printf ("%s\n%s", first_line, second_line);
573
574   ft_manager_update_handler_message (manager, row_ref, message);
575
576   g_free (message);
577 }
578
579 static void
580 ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
581                                EmpathyFTManager *manager)
582 {
583   char *message;
584   GtkTreeRowReference *row_ref;
585
586   DEBUG ("Hashing started");
587
588   g_signal_connect (handler, "hashing-progress",
589      G_CALLBACK (ft_handler_hashing_progress_cb), manager);
590   g_signal_connect (handler, "hashing-done",
591      G_CALLBACK (ft_handler_hashing_done_cb), manager);
592
593   row_ref = ft_manager_get_row_from_handler (manager, handler);
594   g_return_if_fail (row_ref != NULL);
595
596   message =  g_strdup_printf (_("Hashing \"%s\""),
597       empathy_ft_handler_get_filename (handler));
598
599   ft_manager_update_handler_message (manager, row_ref, message);
600
601   g_free (message);
602 }
603
604 static void
605 ft_manager_start_transfer (EmpathyFTManager *manager,
606                            EmpathyFTHandler *handler)
607 {
608   EmpathyFTManagerPriv *priv;
609   gboolean is_incoming;
610
611   priv = GET_PRIV (manager);
612
613   is_incoming = empathy_ft_handler_is_incoming (handler);
614
615   DEBUG ("Start transfer, is incoming %d", is_incoming);
616
617   /* now connect the signals */
618   g_signal_connect (handler, "transfer-error",
619       G_CALLBACK (ft_handler_transfer_error_cb), manager);
620
621   if (is_incoming) {
622     g_signal_connect (handler, "transfer-started",
623         G_CALLBACK (ft_handler_transfer_started_cb), manager);
624   } else {
625     g_signal_connect (handler, "hashing-started",
626         G_CALLBACK (ft_handler_hashing_started_cb), manager);
627   }
628
629   empathy_ft_handler_start_transfer (handler);
630 }
631
632 static void
633 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
634                                 EmpathyFTHandler *handler,
635                                 const GError *error)
636 {
637   GtkTreeRowReference *row_ref;
638   GtkTreeIter iter;
639   GtkTreeSelection *selection;
640   GtkTreePath *path;
641   GIcon *icon;
642   const char *content_type, *second_line;
643   char *first_line, *message;
644   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
645
646   icon = NULL;
647
648   /* get the icon name from the mime-type of the file. */
649   content_type = empathy_ft_handler_get_content_type (handler);
650
651   if (content_type != NULL)
652     icon = g_content_type_get_icon (content_type);
653
654   /* append the handler in the store */
655   gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
656       &iter, G_MAXINT, COL_FT_OBJECT, handler,
657       COL_ICON, icon, -1);
658
659   if (icon != NULL)
660     g_object_unref (icon);
661
662   /* insert the new row_ref in the hash table  */
663   path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
664   row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path);
665   gtk_tree_path_free (path);
666   g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler),
667       row_ref);
668
669   /* select the new row */
670   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
671   gtk_tree_selection_select_iter (selection, &iter);
672
673   if (error != NULL)
674     {
675       message = ft_manager_format_error_message (handler, error);
676       ft_manager_update_handler_message (manager, row_ref, message);
677
678       g_free (message);
679       return;
680     }
681
682   /* update the row with the initial values */
683   if (empathy_ft_handler_is_incoming (handler)) {
684     first_line = ft_manager_format_contact_info (handler);
685     second_line = _("Waiting for the other participant's response");
686     message = g_strdup_printf ("%s\n%s", first_line, second_line);
687
688     ft_manager_update_handler_message (manager, row_ref, message);
689
690     g_free (first_line);
691     g_free (message);
692   }
693
694
695   /* hook up the signals and start the transfer */
696   ft_manager_start_transfer (manager, handler);
697 }
698
699 static void
700 ft_manager_clear (EmpathyFTManager *manager)
701 {
702   EmpathyFTManagerPriv *priv;
703
704   DEBUG ("Clearing file transfer list");
705
706   priv = GET_PRIV (manager);
707
708   /* Remove completed and cancelled transfers */
709   g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
710       remove_finished_transfer_foreach, manager);
711 }
712
713 static void
714 ft_manager_open (EmpathyFTManager *manager)
715 {
716   GtkTreeSelection *selection;
717   GtkTreeIter iter;
718   GtkTreeModel *model;
719   EmpathyFTHandler *handler;
720   char *uri;
721   GFile *file;
722   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
723
724   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
725
726   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
727     return;
728
729   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
730
731   file = empathy_ft_handler_get_gfile (handler);
732   uri = g_file_get_uri (file);
733
734   DEBUG ("Opening URI: %s", uri);
735   empathy_url_show (GTK_WIDGET (priv->window), uri);
736
737   g_object_unref (handler);
738   g_free (uri);
739 }
740
741 static void
742 ft_manager_stop (EmpathyFTManager *manager)
743 {
744   GtkTreeSelection *selection;
745   GtkTreeIter iter;
746   GtkTreeModel *model;
747   EmpathyFTHandler *handler;
748   EmpathyFTManagerPriv *priv;
749
750   priv = GET_PRIV (manager);
751
752   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
753
754   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
755     return;
756
757   gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1);
758   g_return_if_fail (handler != NULL);
759
760   DEBUG ("Stopping file transfer: contact=%s, filename=%s",
761       empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
762       empathy_ft_handler_get_filename (handler));
763
764   empathy_ft_handler_cancel_transfer (handler);
765
766   g_object_unref (handler);
767 }
768
769 static gboolean
770 ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager)
771 {
772   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
773   gint x, y, w, h;
774
775   gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h);
776   gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y);
777
778   empathy_geometry_save ("ft-manager", x, y, w, h);
779
780   priv->save_geometry_id = 0;
781
782   return FALSE;
783 }
784
785 static gboolean
786 ft_manager_configure_event_cb (GtkWidget *widget,
787                                GdkEventConfigure *event,
788                                EmpathyFTManager *manager)
789 {
790   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
791
792   if (priv->save_geometry_id != 0)
793     g_source_remove (priv->save_geometry_id);
794
795   priv->save_geometry_id = g_timeout_add (500,
796       (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager);
797
798   return FALSE;
799 }
800
801 static void
802 ft_manager_response_cb (GtkWidget *widget,
803                         gint response,
804                         EmpathyFTManager *manager)
805 {
806   switch (response)
807     {
808       case RESPONSE_CLEAR:
809         ft_manager_clear (manager);
810         break;
811       case RESPONSE_OPEN:
812         ft_manager_open (manager);
813         break;
814       case RESPONSE_STOP:
815         ft_manager_stop (manager);
816         break;
817     }
818 }
819
820 static gboolean
821 ft_manager_delete_event_cb (GtkWidget *widget,
822                             GdkEvent *event,
823                             EmpathyFTManager *manager)
824 {
825   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
826
827   /* remove all the completed/cancelled/errored transfers */
828   ft_manager_clear (manager);
829
830   if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0)
831     {
832       /* There is still FTs on flight, just hide the window */
833       DEBUG ("Hiding window");
834       gtk_widget_hide (widget);
835       return TRUE;
836     }
837
838   return FALSE;
839 }
840
841 static void
842 ft_manager_destroy_cb (GtkWidget *widget,
843                        EmpathyFTManager *manager)
844 {
845   g_object_unref (manager);
846 }
847
848 static void
849 ft_manager_build_ui (EmpathyFTManager *manager)
850 {
851   GtkBuilder *gui;
852   gint x, y, w, h;
853   GtkTreeView *view;
854   GtkListStore *liststore;
855   GtkTreeViewColumn *column;
856   GtkCellRenderer *renderer;
857   GtkTreeSelection *selection;
858   gchar *filename;
859   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
860
861   filename = empathy_file_lookup ("empathy-ft-manager.ui", "src");
862   gui = empathy_builder_get_file (filename,
863       "ft_manager_dialog", &priv->window,
864       "ft_list", &priv->treeview,
865       "open_button", &priv->open_button,
866       "abort_button", &priv->abort_button,
867       NULL);
868   g_free (filename);
869
870   empathy_builder_connect (gui, manager,
871       "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
872       "ft_manager_dialog", "response", ft_manager_response_cb,
873       "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
874       "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb,
875       NULL);
876
877   g_object_unref (gui);
878
879   /* Window geometry. */
880   empathy_geometry_load ("ft-manager", &x, &y, &w, &h);
881
882   if (x >= 0 && y >= 0)
883     {
884       /* Let the window manager position it if we don't have
885        * good x, y coordinates. */
886       gtk_window_move (GTK_WINDOW (priv->window), x, y);
887     }
888
889   if (w > 0 && h > 0)
890     {
891       /* Use the defaults from the ui file if we don't have
892        * good w, h geometry. */
893       gtk_window_resize (GTK_WINDOW (priv->window), w, h);
894     }
895
896   /* Setup the tree view */
897   view = GTK_TREE_VIEW (priv->treeview);
898   selection = gtk_tree_view_get_selection (view);
899   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
900   g_signal_connect (selection, "changed",
901       G_CALLBACK (ft_manager_selection_changed), manager);
902   gtk_tree_view_set_headers_visible (view, TRUE);
903   gtk_tree_view_set_enable_search (view, FALSE);
904
905   /* Setup the model */
906   liststore = gtk_list_store_new (5,
907       G_TYPE_INT,     /* percent */
908       G_TYPE_ICON,    /* icon */
909       G_TYPE_STRING,  /* message */
910       G_TYPE_STRING,  /* remaining */
911       G_TYPE_OBJECT); /* ft_handler */
912   gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore));
913   priv->model = GTK_TREE_MODEL (liststore);
914   g_object_unref (liststore);
915
916   /* Progress column */
917   column = gtk_tree_view_column_new ();
918   gtk_tree_view_column_set_title (column, _("%"));
919   gtk_tree_view_column_set_sort_column_id (column, COL_PERCENT);
920   gtk_tree_view_insert_column (view, column, -1);
921
922   renderer = gtk_cell_renderer_progress_new ();
923   g_object_set (renderer, "xalign", 0.5, NULL);
924   gtk_tree_view_column_pack_start (column, renderer, FALSE);
925   gtk_tree_view_column_set_cell_data_func (column, renderer,
926       ft_manager_progress_cell_data_func, NULL, NULL);
927
928   /* Icon and filename column*/
929   column = gtk_tree_view_column_new ();
930   gtk_tree_view_column_set_title (column, _("File"));
931   gtk_tree_view_column_set_expand (column, TRUE);
932   gtk_tree_view_column_set_resizable (column, TRUE);
933   gtk_tree_view_column_set_sort_column_id (column, COL_MESSAGE);
934   gtk_tree_view_column_set_spacing (column, 3);
935   gtk_tree_view_insert_column (view, column, -1);
936
937   renderer = gtk_cell_renderer_pixbuf_new ();
938   g_object_set (renderer, "xpad", 3,
939       "stock-size", GTK_ICON_SIZE_DND, NULL);
940   gtk_tree_view_column_pack_start (column, renderer, FALSE);
941   gtk_tree_view_column_set_attributes (column, renderer,
942       "gicon", COL_ICON, NULL);
943
944   renderer = gtk_cell_renderer_text_new ();
945   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
946   gtk_tree_view_column_pack_start (column, renderer, TRUE);
947   gtk_tree_view_column_set_attributes (column, renderer,
948       "text", COL_MESSAGE, NULL);
949
950   /* Remaining time column */
951   column = gtk_tree_view_column_new ();
952   gtk_tree_view_column_set_title (column, _("Remaining"));
953   gtk_tree_view_column_set_sort_column_id (column, COL_REMAINING);
954   gtk_tree_view_insert_column (view, column, -1);
955
956   renderer = gtk_cell_renderer_text_new ();
957   g_object_set (renderer, "xalign", 0.5, NULL);
958   gtk_tree_view_column_pack_start (column, renderer, FALSE);
959   gtk_tree_view_column_set_attributes (column, renderer,
960       "text", COL_REMAINING, NULL);
961 }
962
963 /* GObject method overrides */
964
965 static void
966 empathy_ft_manager_finalize (GObject *object)
967 {
968   EmpathyFTManagerPriv *priv = GET_PRIV (object);
969
970   DEBUG ("%p", object);
971
972   g_hash_table_destroy (priv->ft_handler_to_row_ref);
973
974   if (priv->save_geometry_id != 0)
975     g_source_remove (priv->save_geometry_id);
976
977   G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
978 }
979
980 static void
981 empathy_ft_manager_init (EmpathyFTManager *manager)
982 {
983   EmpathyFTManagerPriv *priv;
984
985   priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER,
986       EmpathyFTManagerPriv);
987
988   manager->priv = priv;
989
990   priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
991       g_direct_equal, (GDestroyNotify) g_object_unref,
992       (GDestroyNotify) gtk_tree_row_reference_free);
993
994   ft_manager_build_ui (manager);
995 }
996
997 static GObject *
998 empathy_ft_manager_constructor (GType type,
999                                 guint n_props,
1000                                 GObjectConstructParam *props)
1001 {
1002   GObject *retval;
1003
1004   if (manager_singleton)
1005     {
1006       retval = g_object_ref (manager_singleton);
1007     }
1008   else
1009     {
1010       retval = G_OBJECT_CLASS (empathy_ft_manager_parent_class)->constructor
1011           (type, n_props, props);
1012
1013       manager_singleton = EMPATHY_FT_MANAGER (retval);
1014       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
1015     }
1016
1017   return retval;
1018 }
1019
1020 static void
1021 empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
1022 {
1023   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1024
1025   object_class->finalize = empathy_ft_manager_finalize;
1026   object_class->constructor = empathy_ft_manager_constructor;
1027
1028   g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv));
1029 }
1030
1031 /* public methods */
1032
1033 /**
1034  * empathy_ft_manager_dup_singleton:
1035  *
1036  * Returns a reference to the #EmpathyFTManager singleton object.
1037  *
1038  * Returns: a #EmpathyFTManager
1039  */
1040 EmpathyFTManager *
1041 empathy_ft_manager_dup_singleton (void)
1042 {
1043   return g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
1044 }
1045
1046 void
1047 empathy_ft_manager_add_handler (EmpathyFTManager *manager,
1048                                 EmpathyFTHandler *handler)
1049 {
1050   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1051
1052   DEBUG ("Adding handler");
1053
1054   g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1055   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1056
1057   ft_manager_add_handler_to_list (manager, handler, NULL);
1058   gtk_window_present (GTK_WINDOW (priv->window));
1059 }
1060
1061 void
1062 empathy_ft_manager_display_error (EmpathyFTManager *manager,
1063                                   EmpathyFTHandler *handler,
1064                                   const GError *error)
1065 {
1066   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
1067
1068   g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
1069   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
1070   g_return_if_fail (error != NULL);
1071
1072   ft_manager_add_handler_to_list (manager, handler, error);
1073   gtk_window_present (GTK_WINDOW (priv->window));
1074 }