]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
5b03c597e1d26d394c678d8792c0fe9337f2ea9f
[empathy.git] / libempathy-gtk / empathy-contact-widget.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2009 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
29
30 #if HAVE_LIBCHAMPLAIN
31 #include <champlain/champlain.h>
32 #include <champlain-gtk/champlain-gtk.h>
33 #endif
34
35 #include <telepathy-glib/account.h>
36 #include <telepathy-glib/util.h>
37 #include <telepathy-glib/interfaces.h>
38
39 #include <libempathy/empathy-tp-contact-factory.h>
40 #include <libempathy/empathy-contact-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-location.h>
43 #include <libempathy/empathy-time.h>
44 #include <libempathy/empathy-utils.h>
45
46 #include "empathy-contact-widget.h"
47 #include "empathy-account-chooser.h"
48 #include "empathy-avatar-chooser.h"
49 #include "empathy-avatar-image.h"
50 #include "empathy-ui-utils.h"
51 #include "empathy-string-parser.h"
52 #include "empathy-kludge-label.h"
53
54 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
55 #include <libempathy/empathy-debug.h>
56
57 /**
58  * SECTION:empathy-contact-widget
59  * @title:EmpathyContactWidget
60  * @short_description: A widget used to display and edit details about a contact
61  * @include: libempathy-empathy-contact-widget.h
62  *
63  * #EmpathyContactWidget is a widget which displays appropriate widgets
64  * with details about a contact, also allowing changing these details,
65  * if desired.
66  */
67
68 /**
69  * EmpathyContactWidget:
70  * @parent: parent object
71  *
72  * Widget which displays appropriate widgets with details about a contact,
73  * also allowing changing these details, if desired.
74  */
75
76 /* Delay before updating the widget when the id entry changed (seconds) */
77 #define ID_CHANGED_TIMEOUT 1
78
79 typedef struct
80 {
81   EmpathyContactManager *manager;
82   EmpathyContact *contact;
83   EmpathyContactWidgetFlags flags;
84   guint widget_id_timeout;
85   gulong fav_sig_id;
86
87   GtkWidget *vbox_contact_widget;
88
89   /* Contact */
90   GtkWidget *hbox_contact;
91   GtkWidget *widget_avatar;
92   GtkWidget *widget_account;
93   GtkWidget *image_account;
94   GtkWidget *label_account;
95   GtkWidget *widget_id;
96   GtkWidget *widget_alias;
97   GtkWidget *label_alias;
98   GtkWidget *hbox_presence;
99   GtkWidget *image_state;
100   GtkWidget *label_status;
101   GtkWidget *table_contact;
102   GtkWidget *vbox_avatar;
103   GtkWidget *favourite_checkbox;
104
105   /* Location */
106   GtkWidget *vbox_location;
107   GtkWidget *subvbox_location;
108   GtkWidget *table_location;
109   GtkWidget *label_location;
110 #if HAVE_LIBCHAMPLAIN
111   GtkWidget *viewport_map;
112   GtkWidget *map_view_embed;
113   ChamplainView *map_view;
114 #endif
115
116   /* Groups */
117   GtkWidget *vbox_groups;
118   GtkWidget *entry_group;
119   GtkWidget *button_group;
120   GtkWidget *treeview_groups;
121
122   /* Details */
123   GtkWidget *vbox_details;
124   GtkWidget *table_details;
125   GtkWidget *hbox_details_requested;
126   GList *details_to_set;
127   GCancellable *details_cancellable;
128
129   /* Client */
130   GtkWidget *vbox_client;
131   GtkWidget *table_client;
132   GtkWidget *hbox_client_requested;
133 } EmpathyContactWidget;
134
135 typedef struct
136 {
137   EmpathyContactWidget *information;
138   const gchar *name;
139   gboolean found;
140   GtkTreeIter found_iter;
141 } FindName;
142
143 enum
144 {
145   COL_NAME,
146   COL_ENABLED,
147   COL_EDITABLE,
148   COL_COUNT
149 };
150
151 static void
152 contact_widget_save (EmpathyContactWidget *information)
153 {
154   TpConnection *connection;
155   GList *l, *next;
156
157   connection = empathy_contact_get_connection (information->contact);
158
159   /* Remove empty fields */
160   for (l = information->details_to_set; l != NULL; l = next)
161     {
162       TpContactInfoField *field = l->data;
163
164       next = l->next;
165       if (field->field_value == NULL || EMP_STR_EMPTY (field->field_value[0]))
166         {
167           DEBUG ("Drop empty field: %s", field->field_name);
168           tp_contact_info_field_free (field);
169           information->details_to_set =
170               g_list_delete_link (information->details_to_set, l);
171         }
172     }
173
174   if (information->details_to_set != NULL)
175     {
176       tp_connection_set_contact_info_async (connection,
177           information->details_to_set, NULL, NULL);
178       tp_contact_info_list_free (information->details_to_set);
179       information->details_to_set = NULL;
180     }
181 }
182
183 static void
184 contact_widget_details_setup (EmpathyContactWidget *information)
185 {
186   gtk_widget_hide (information->vbox_details);
187 }
188
189 static void
190 contact_widget_details_changed_cb (GtkEntry *entry,
191     TpContactInfoField *field)
192 {
193   const gchar *strv[] = { NULL, NULL };
194
195   strv[0] = gtk_entry_get_text (entry);
196
197   if (field->field_value != NULL)
198     g_strfreev (field->field_value);
199   field->field_value = g_strdupv ((GStrv) strv);
200 }
201
202 static void contact_widget_details_notify_cb (EmpathyContactWidget *information);
203
204 typedef struct
205 {
206   const gchar *field_name;
207   const gchar *title;
208   gboolean linkify;
209 } InfoFieldData;
210
211 static InfoFieldData info_field_datas[] =
212 {
213   { "fn",    N_("Full name:"),      FALSE },
214   { "tel",   N_("Phone number:"),   FALSE },
215   { "email", N_("E-mail address:"), TRUE },
216   { "url",   N_("Website:"),        TRUE },
217   { "bday",  N_("Birthday:"),       FALSE },
218   { NULL, NULL }
219 };
220
221 static InfoFieldData *
222 find_info_field_data (const gchar *field_name)
223 {
224   guint i;
225
226   for (i = 0; info_field_datas[i].field_name != NULL; i++)
227     {
228       if (!tp_strdiff (info_field_datas[i].field_name, field_name))
229         return info_field_datas + i;
230     }
231   return NULL;
232 }
233
234 static gint
235 contact_info_field_name_cmp (const gchar *name1,
236     const gchar *name2)
237 {
238   guint i;
239
240   if (!tp_strdiff (name1, name2))
241     return 0;
242
243   /* We use the order of info_field_datas */
244   for (i = 0; info_field_datas[i].field_name != NULL; i++)
245     {
246       if (!tp_strdiff (info_field_datas[i].field_name, name1))
247         return -1;
248       if (!tp_strdiff (info_field_datas[i].field_name, name2))
249         return +1;
250     }
251
252   return g_strcmp0 (name1, name2);
253 }
254
255 static gint
256 contact_info_field_cmp (TpContactInfoField *field1,
257     TpContactInfoField *field2)
258 {
259   return contact_info_field_name_cmp (field1->field_name, field2->field_name);
260 }
261
262 static gint
263 contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
264     TpContactInfoFieldSpec *spec2)
265 {
266   return contact_info_field_name_cmp (spec1->name, spec2->name);
267 }
268
269 static guint
270 contact_widget_details_update_edit (EmpathyContactWidget *information)
271 {
272   TpContact *contact;
273   TpConnection *connection;
274   GList *specs, *l;
275   guint n_rows = 0;
276
277   g_assert (information->details_to_set == NULL);
278
279   contact = empathy_contact_get_tp_contact (information->contact);
280   connection = tp_contact_get_connection (contact);
281
282   specs = tp_connection_get_contact_info_supported_fields (connection);
283   specs = g_list_sort (specs, (GCompareFunc) contact_info_field_spec_cmp);
284   for (l = specs; l != NULL; l = l->next)
285     {
286       TpContactInfoFieldSpec *spec = l->data;
287       TpContactInfoField *field;
288       InfoFieldData *field_data;
289       GList *info, *ll;
290       GStrv value = NULL;
291       GtkWidget *w;
292
293       field_data = find_info_field_data (spec->name);
294       if (field_data == NULL)
295         {
296           DEBUG ("Unhandled ContactInfo field spec: %s", spec->name);
297           continue;
298         }
299
300       /* Search initial value */
301       info = tp_contact_get_contact_info (contact);
302       for (ll = info; ll != NULL; ll = ll->next)
303         {
304           field = ll->data;
305           if (!tp_strdiff (field->field_name, spec->name))
306             {
307               value = field->field_value;
308               break;
309             }
310         }
311
312       field = tp_contact_info_field_new (spec->name, spec->parameters, value);
313       information->details_to_set = g_list_prepend (information->details_to_set,
314           field);
315
316       /* Add Title */
317       w = gtk_label_new (_(field_data->title));
318       gtk_table_attach (GTK_TABLE (information->table_details),
319           w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
320       gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
321       gtk_widget_show (w);
322
323       /* Add Value */
324       w = gtk_entry_new ();
325       gtk_entry_set_text (GTK_ENTRY (w),
326           field->field_value[0] ? field->field_value[0] : "");
327       gtk_table_attach_defaults (GTK_TABLE (information->table_details),
328           w, 1, 2, n_rows, n_rows + 1);
329       gtk_widget_show (w);
330
331       g_signal_connect (w, "changed",
332         G_CALLBACK (contact_widget_details_changed_cb), field);
333
334       n_rows++;
335     }
336   g_list_free (specs);
337
338   return n_rows;
339 }
340
341 static guint
342 contact_widget_details_update_show (EmpathyContactWidget *information)
343 {
344   TpContact *contact;
345   GList *info, *l;
346   guint n_rows = 0;
347
348   contact = empathy_contact_get_tp_contact (information->contact);
349   info = tp_contact_get_contact_info (contact);
350   info = g_list_sort (info, (GCompareFunc) contact_info_field_cmp);
351   for (l = info; l != NULL; l = l->next)
352     {
353       TpContactInfoField *field = l->data;
354       InfoFieldData *field_data;
355       const gchar *value;
356       GtkWidget *w;
357
358       if (field->field_value == NULL || field->field_value[0] == NULL)
359         continue;
360
361       value = field->field_value[0];
362
363       field_data = find_info_field_data (field->field_name);
364       if (field_data == NULL)
365         {
366           DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
367           continue;
368         }
369
370       /* Add Title */
371       w = gtk_label_new (_(field_data->title));
372       gtk_table_attach (GTK_TABLE (information->table_details),
373           w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
374       gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
375       gtk_widget_show (w);
376
377       /* Add Value */
378       w = gtk_label_new (value);
379       if (field_data->linkify)
380         {
381           gchar *markup;
382
383           markup = empathy_add_link_markup (value);
384           gtk_label_set_markup (GTK_LABEL (w), markup);
385           g_free (markup);
386         }
387
388       if ((information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) == 0)
389         gtk_label_set_selectable (GTK_LABEL (w), TRUE);
390
391       gtk_table_attach_defaults (GTK_TABLE (information->table_details),
392           w, 1, 2, n_rows, n_rows + 1);
393       gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
394       gtk_widget_show (w);
395
396       n_rows++;
397     }
398   g_list_free (info);
399
400   return n_rows;
401 }
402
403 static void
404 contact_widget_details_notify_cb (EmpathyContactWidget *information)
405 {
406   guint n_rows;
407
408   gtk_container_foreach (GTK_CONTAINER (information->table_details),
409       (GtkCallback) gtk_widget_destroy, NULL);
410
411   if ((information->flags & EMPATHY_CONTACT_WIDGET_EDIT_DETAILS) != 0)
412     n_rows = contact_widget_details_update_edit (information);
413   else
414     n_rows = contact_widget_details_update_show (information);
415
416   if (n_rows > 0)
417     {
418       gtk_widget_show (information->vbox_details);
419       gtk_widget_show (information->table_details);
420     }
421   else
422     {
423       gtk_widget_hide (information->vbox_details);
424     }
425
426   gtk_widget_hide (information->hbox_details_requested);
427 }
428
429 static void
430 contact_widget_details_request_cb (GObject *object,
431     GAsyncResult *res,
432     gpointer user_data)
433 {
434   TpContact *contact = TP_CONTACT (object);
435   EmpathyContactWidget *information = user_data;
436   GError *error = NULL;
437
438   if (!tp_contact_request_contact_info_finish (contact, res, &error))
439     {
440       /* If the request got cancelled it could mean the contact widget is
441        * destroyed, so we should not dereference information */
442       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
443         {
444           g_clear_error (&error);
445           return;
446         }
447
448       gtk_widget_hide (information->vbox_details);
449       g_clear_error (&error);
450     }
451   else
452     {
453       contact_widget_details_notify_cb (information);
454     }
455
456   /* If we are going to edit ContactInfo, we don't want live updates */
457   if ((information->flags & EMPATHY_CONTACT_WIDGET_EDIT_DETAILS) == 0)
458     {
459       g_signal_connect_swapped (contact, "notify::contact-info",
460           G_CALLBACK (contact_widget_details_notify_cb), information);
461     }
462
463   g_object_unref (information->details_cancellable);
464   information->details_cancellable = NULL;
465 }
466
467 static void
468 contact_widget_details_feature_prepared_cb (GObject *object,
469     GAsyncResult *res,
470     gpointer user_data)
471 {
472   TpConnection *connection = TP_CONNECTION (object);
473   EmpathyContactWidget *information = user_data;
474   TpContact *contact;
475   TpContactInfoFlags flags;
476
477   if (!tp_proxy_prepare_finish (connection, res, NULL))
478     {
479       gtk_widget_hide (information->vbox_details);
480       return;
481     }
482
483   /* If we want to edit info, but connection does not support that, stop */
484   flags = tp_connection_get_contact_info_flags (connection);
485   if ((flags & TP_CONTACT_INFO_FLAG_CAN_SET) == 0 &&
486       (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_DETAILS) != 0)
487     {
488       gtk_widget_hide (information->vbox_details);
489       return;
490     }
491
492   /* Request the contact's info */
493   gtk_widget_show (information->vbox_details);
494   gtk_widget_show (information->hbox_details_requested);
495   gtk_widget_hide (information->table_details);
496
497   contact = empathy_contact_get_tp_contact (information->contact);
498   g_assert (information->details_cancellable == NULL);
499   information->details_cancellable = g_cancellable_new ();
500   tp_contact_request_contact_info_async (contact,
501       information->details_cancellable, contact_widget_details_request_cb,
502       information);
503 }
504
505 static void
506 contact_widget_details_update (EmpathyContactWidget *information)
507 {
508   TpContact *tp_contact = NULL;
509
510   if ((information->flags & EMPATHY_CONTACT_WIDGET_SHOW_DETAILS) == 0 &&
511       (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_DETAILS) == 0)
512     return;
513
514   gtk_widget_hide (information->vbox_details);
515
516   if (information->contact != NULL)
517     tp_contact = empathy_contact_get_tp_contact (information->contact);
518
519   if (tp_contact != NULL)
520     {
521       GQuark features[] = { TP_CONNECTION_FEATURE_CONTACT_INFO, 0 };
522       TpConnection *connection;
523
524       /* First, make sure the CONTACT_INFO feature is ready on the connection */
525       connection = tp_contact_get_connection (tp_contact);
526       tp_proxy_prepare_async (connection, features,
527           contact_widget_details_feature_prepared_cb, information);
528     }
529 }
530
531 static void
532 contact_widget_client_update (EmpathyContactWidget *information)
533 {
534   /* FIXME: Needs new telepathy spec */
535 }
536
537 static void
538 contact_widget_client_setup (EmpathyContactWidget *information)
539 {
540   /* FIXME: Needs new telepathy spec */
541   gtk_widget_hide (information->vbox_client);
542 }
543
544 static void
545 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
546                              gchar *path_string,
547                              EmpathyContactWidget *information)
548 {
549   GtkTreeView *view;
550   GtkTreeModel *model;
551   GtkListStore *store;
552   GtkTreePath *path;
553   GtkTreeIter iter;
554   gboolean enabled;
555   gchar *group;
556
557   view = GTK_TREE_VIEW (information->treeview_groups);
558   model = gtk_tree_view_get_model (view);
559   store = GTK_LIST_STORE (model);
560
561   path = gtk_tree_path_new_from_string (path_string);
562
563   gtk_tree_model_get_iter (model, &iter, path);
564   gtk_tree_model_get (model, &iter,
565       COL_ENABLED, &enabled,
566       COL_NAME, &group,
567       -1);
568
569   gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
570   gtk_tree_path_free (path);
571
572   if (group)
573     {
574       if (enabled)
575         {
576           empathy_contact_list_remove_from_group (
577               EMPATHY_CONTACT_LIST (information->manager), information->contact,
578               group);
579         }
580       else
581         {
582           empathy_contact_list_add_to_group (
583               EMPATHY_CONTACT_LIST (information->manager), information->contact,
584               group);
585         }
586       g_free (group);
587     }
588 }
589
590 static void
591 contact_widget_model_populate_columns (EmpathyContactWidget *information)
592 {
593   GtkTreeView *view;
594   GtkTreeModel *model;
595   GtkTreeViewColumn *column;
596   GtkCellRenderer  *renderer;
597   guint col_offset;
598
599   view = GTK_TREE_VIEW (information->treeview_groups);
600   model = gtk_tree_view_get_model (view);
601
602   renderer = gtk_cell_renderer_toggle_new ();
603   g_signal_connect (renderer, "toggled",
604       G_CALLBACK (contact_widget_cell_toggled), information);
605
606   column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
607       "active", COL_ENABLED, NULL);
608
609   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
610   gtk_tree_view_column_set_fixed_width (column, 50);
611   gtk_tree_view_append_column (view, column);
612
613   renderer = gtk_cell_renderer_text_new ();
614   col_offset = gtk_tree_view_insert_column_with_attributes (view,
615       -1, _("Group"),
616       renderer,
617       "text", COL_NAME,
618       /* "editable", COL_EDITABLE, */
619       NULL);
620
621   g_object_set_data (G_OBJECT (renderer),
622       "column", GINT_TO_POINTER (COL_NAME));
623
624   column = gtk_tree_view_get_column (view, col_offset - 1);
625   gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
626   gtk_tree_view_column_set_resizable (column,FALSE);
627   gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
628 }
629
630 static void
631 contact_widget_model_setup (EmpathyContactWidget *information)
632 {
633   GtkTreeView *view;
634   GtkListStore *store;
635   GtkTreeSelection *selection;
636
637   view = GTK_TREE_VIEW (information->treeview_groups);
638
639   store = gtk_list_store_new (COL_COUNT,
640       G_TYPE_STRING,   /* name */
641       G_TYPE_BOOLEAN,  /* enabled */
642       G_TYPE_BOOLEAN); /* editable */
643
644   gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
645
646   selection = gtk_tree_view_get_selection (view);
647   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
648
649   contact_widget_model_populate_columns (information);
650
651   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
652       COL_NAME, GTK_SORT_ASCENDING);
653
654   g_object_unref (store);
655 }
656
657 static void
658 contact_widget_groups_populate_data (EmpathyContactWidget *information)
659 {
660   GtkTreeView *view;
661   GtkListStore *store;
662   GtkTreeIter iter;
663   GList *my_groups, *l;
664   GList *all_groups;
665
666   view = GTK_TREE_VIEW (information->treeview_groups);
667   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
668   gtk_list_store_clear (store);
669
670   all_groups = empathy_contact_list_get_all_groups (
671       EMPATHY_CONTACT_LIST (information->manager));
672   my_groups = empathy_contact_list_get_groups (
673       EMPATHY_CONTACT_LIST (information->manager),
674       information->contact);
675
676   for (l = all_groups; l; l = l->next)
677     {
678       const gchar *group_str;
679       gboolean enabled;
680
681       group_str = l->data;
682
683       enabled = g_list_find_custom (my_groups,
684           group_str, (GCompareFunc) strcmp) != NULL;
685
686       gtk_list_store_append (store, &iter);
687       gtk_list_store_set (store, &iter,
688           COL_NAME, group_str,
689           COL_EDITABLE, TRUE,
690           COL_ENABLED, enabled,
691           -1);
692     }
693
694   g_list_foreach (all_groups, (GFunc) g_free, NULL);
695   g_list_foreach (my_groups, (GFunc) g_free, NULL);
696   g_list_free (all_groups);
697   g_list_free (my_groups);
698 }
699
700 static gboolean
701 contact_widget_model_find_name_foreach (GtkTreeModel *model,
702                                         GtkTreePath *path,
703                                         GtkTreeIter *iter,
704                                         FindName *data)
705 {
706   gchar *name;
707
708   gtk_tree_model_get (model, iter,
709       COL_NAME, &name,
710       -1);
711
712   if (!name)
713       return FALSE;
714
715   if (data->name && strcmp (data->name, name) == 0)
716     {
717       data->found = TRUE;
718       data->found_iter = *iter;
719
720       g_free (name);
721
722       return TRUE;
723     }
724
725   g_free (name);
726
727   return FALSE;
728 }
729
730 static gboolean
731 contact_widget_model_find_name (EmpathyContactWidget *information,
732                                 const gchar *name,
733                                 GtkTreeIter *iter)
734 {
735   GtkTreeView *view;
736   GtkTreeModel *model;
737   FindName data;
738
739   if (EMP_STR_EMPTY (name))
740       return FALSE;
741
742   data.information = information;
743   data.name = name;
744   data.found = FALSE;
745
746   view = GTK_TREE_VIEW (information->treeview_groups);
747   model = gtk_tree_view_get_model (view);
748
749   gtk_tree_model_foreach (model,
750       (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
751       &data);
752
753   if (data.found == TRUE)
754     {
755       *iter = data.found_iter;
756       return TRUE;
757     }
758
759   return FALSE;
760 }
761
762 static void
763 contact_widget_entry_group_changed_cb (GtkEditable *editable,
764                                        EmpathyContactWidget *information)
765 {
766   GtkTreeIter iter;
767   const gchar *group;
768
769   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
770
771   if (contact_widget_model_find_name (information, group, &iter))
772       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
773   else
774       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
775           !EMP_STR_EMPTY (group));
776 }
777
778 static void
779 contact_widget_entry_group_activate_cb (GtkEntry *entry,
780                                         EmpathyContactWidget  *information)
781 {
782   gtk_widget_activate (GTK_WIDGET (information->button_group));
783 }
784
785 static void
786 contact_widget_button_group_clicked_cb (GtkButton *button,
787                                         EmpathyContactWidget *information)
788 {
789   GtkTreeView *view;
790   GtkListStore *store;
791   GtkTreeIter iter;
792   const gchar *group;
793
794   view = GTK_TREE_VIEW (information->treeview_groups);
795   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
796
797   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
798
799   gtk_list_store_append (store, &iter);
800   gtk_list_store_set (store, &iter,
801       COL_NAME, group,
802       COL_ENABLED, TRUE,
803       -1);
804
805   empathy_contact_list_add_to_group (
806       EMPATHY_CONTACT_LIST (information->manager), information->contact,
807       group);
808 }
809
810 static void
811 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
812 {
813   /* FIXME: not implemented */
814 }
815
816 static void
817 contact_widget_groups_setup (EmpathyContactWidget *information)
818 {
819   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
820     {
821       contact_widget_model_setup (information);
822     }
823 }
824
825 static void
826 contact_widget_groups_update (EmpathyContactWidget *information)
827 {
828   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
829       information->contact)
830     {
831       g_signal_connect_swapped (information->contact, "notify::groups",
832           G_CALLBACK (contact_widget_groups_notify_cb), information);
833       contact_widget_groups_populate_data (information);
834
835       gtk_widget_show (information->vbox_groups);
836     }
837   else
838       gtk_widget_hide (information->vbox_groups);
839 }
840
841 /* Converts the Location's GHashTable's key to a user readable string */
842 static const gchar *
843 location_key_to_label (const gchar *key)
844 {
845   if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
846     return _("Country ISO Code:");
847   else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
848     return _("Country:");
849   else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
850     return _("State:");
851   else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
852     return _("City:");
853   else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
854     return _("Area:");
855   else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
856     return _("Postal Code:");
857   else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
858     return _("Street:");
859   else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
860     return _("Building:");
861   else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
862     return _("Floor:");
863   else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
864     return _("Room:");
865   else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
866     return _("Text:");
867   else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
868     return _("Description:");
869   else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
870     return _("URI:");
871   else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
872     return _("Accuracy Level:");
873   else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
874     return _("Error:");
875   else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
876     return _("Vertical Error (meters):");
877   else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
878     return _("Horizontal Error (meters):");
879   else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
880     return _("Speed:");
881   else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
882     return _("Bearing:");
883   else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
884     return _("Climb Speed:");
885   else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
886     return _("Last Updated on:");
887   else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
888     return _("Longitude:");
889   else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
890     return _("Latitude:");
891   else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
892     return _("Altitude:");
893   else
894   {
895     DEBUG ("Unexpected Location key: %s", key);
896     return key;
897   }
898 }
899
900 static void
901 contact_widget_location_update (EmpathyContactWidget *information)
902 {
903   GHashTable *location;
904   GValue *value;
905   gdouble lat = 0.0, lon = 0.0;
906   gboolean has_position = TRUE;
907   GtkWidget *label;
908   guint row = 0;
909   static const gchar* ordered_geolocation_keys[] = {
910     EMPATHY_LOCATION_TEXT,
911     EMPATHY_LOCATION_URI,
912     EMPATHY_LOCATION_DESCRIPTION,
913     EMPATHY_LOCATION_BUILDING,
914     EMPATHY_LOCATION_FLOOR,
915     EMPATHY_LOCATION_ROOM,
916     EMPATHY_LOCATION_STREET,
917     EMPATHY_LOCATION_AREA,
918     EMPATHY_LOCATION_LOCALITY,
919     EMPATHY_LOCATION_REGION,
920     EMPATHY_LOCATION_COUNTRY,
921     NULL
922   };
923   int i;
924   const gchar *skey;
925   gboolean display_map = FALSE;
926
927   if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
928     {
929       gtk_widget_hide (information->vbox_location);
930       return;
931     }
932
933   location = empathy_contact_get_location (information->contact);
934   if (location == NULL || g_hash_table_size (location) == 0)
935     {
936       gtk_widget_hide (information->vbox_location);
937       return;
938     }
939
940   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
941   if (value == NULL)
942       has_position = FALSE;
943   else
944       lat = g_value_get_double (value);
945
946   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
947   if (value == NULL)
948       has_position = FALSE;
949   else
950       lon = g_value_get_double (value);
951
952   value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
953   if (value == NULL)
954     {
955       gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
956       gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
957       g_free (loc);
958     }
959   else
960     {
961       gchar *user_date;
962       gchar *text;
963       gint64 stamp;
964       time_t time_;
965
966       stamp = g_value_get_int64 (value);
967       time_ = stamp;
968
969       user_date = empathy_time_to_string_relative (time_);
970
971       text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
972       gtk_label_set_markup (GTK_LABEL (information->label_location), text);
973       g_free (text);
974     }
975
976
977   /* Prepare the location information table */
978   if (information->table_location != NULL)
979     {
980       gtk_widget_destroy (information->table_location);
981     }
982
983   information->table_location = gtk_table_new (1, 2, FALSE);
984   gtk_box_pack_start (GTK_BOX (information->subvbox_location),
985       information->table_location, FALSE, FALSE, 5);
986
987
988   for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
989     {
990       const gchar* user_label;
991       GValue *gvalue;
992       char *svalue = NULL;
993
994       gvalue = g_hash_table_lookup (location, (gpointer) skey);
995       if (gvalue == NULL)
996         continue;
997
998       user_label = location_key_to_label (skey);
999
1000       label = gtk_label_new (user_label);
1001       gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1002       gtk_table_attach (GTK_TABLE (information->table_location),
1003           label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
1004       gtk_widget_show (label);
1005
1006       if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
1007         {
1008           gdouble dvalue;
1009           dvalue = g_value_get_double (gvalue);
1010           svalue = g_strdup_printf ("%f", dvalue);
1011         }
1012       else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
1013         {
1014           svalue = g_value_dup_string (gvalue);
1015         }
1016       else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
1017         {
1018           time_t time_;
1019
1020           time_ = g_value_get_int64 (value);
1021           svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
1022         }
1023
1024       if (svalue != NULL)
1025         {
1026           label = gtk_label_new (svalue);
1027           gtk_table_attach_defaults (GTK_TABLE (information->table_location),
1028               label, 1, 2, row, row + 1);
1029           gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1030           gtk_widget_show (label);
1031
1032           if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1033             gtk_label_set_selectable (GTK_LABEL (label), TRUE);
1034         }
1035
1036       g_free (svalue);
1037       row++;
1038     }
1039
1040 #if HAVE_LIBCHAMPLAIN
1041   if (has_position &&
1042       !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1043     {
1044       /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
1045        * windows */
1046       display_map = TRUE;
1047     }
1048 #endif
1049
1050   if (row > 0)
1051     {
1052       /* We can display some fields */
1053       gtk_widget_show (information->table_location);
1054     }
1055   else if (!display_map)
1056     {
1057       /* Can't display either fields or map */
1058       gtk_widget_hide (information->vbox_location);
1059       return;
1060     }
1061
1062 #if HAVE_LIBCHAMPLAIN
1063   if (display_map)
1064     {
1065       ClutterActor *marker;
1066       ChamplainLayer *layer;
1067
1068       information->map_view_embed = gtk_champlain_embed_new ();
1069       information->map_view = gtk_champlain_embed_get_view (
1070           GTK_CHAMPLAIN_EMBED (information->map_view_embed));
1071
1072       gtk_container_add (GTK_CONTAINER (information->viewport_map),
1073           information->map_view_embed);
1074       g_object_set (G_OBJECT (information->map_view),
1075           "show-license", TRUE,
1076           "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
1077           "zoom-level", 10,
1078           NULL);
1079
1080       layer = champlain_layer_new ();
1081       champlain_view_add_layer (information->map_view, layer);
1082
1083       marker = champlain_marker_new_with_text (
1084           empathy_contact_get_name (information->contact), NULL, NULL, NULL);
1085       champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
1086       clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
1087
1088       champlain_view_center_on (information->map_view, lat, lon);
1089       gtk_widget_show_all (information->viewport_map);
1090     }
1091 #endif
1092
1093     gtk_widget_show (information->vbox_location);
1094 }
1095
1096 static void
1097 save_avatar_menu_activate_cb (GtkWidget *widget,
1098                               EmpathyContactWidget *information)
1099 {
1100   GtkWidget *dialog;
1101   EmpathyAvatar *avatar;
1102   gchar *ext = NULL, *filename;
1103
1104   dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
1105       NULL,
1106       GTK_FILE_CHOOSER_ACTION_SAVE,
1107       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1108       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1109       NULL);
1110
1111   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
1112       TRUE);
1113
1114   /* look for the avatar extension */
1115   avatar = empathy_contact_get_avatar (information->contact);
1116   if (avatar->format != NULL)
1117     {
1118       gchar **splitted;
1119
1120       splitted = g_strsplit (avatar->format, "/", 2);
1121       if (splitted[0] != NULL && splitted[1] != NULL)
1122           ext = g_strdup (splitted[1]);
1123
1124       g_strfreev (splitted);
1125     }
1126   else
1127     {
1128       /* Avatar was loaded from the cache so was converted to PNG */
1129       ext = g_strdup ("png");
1130     }
1131
1132   if (ext != NULL)
1133     {
1134       gchar *id;
1135
1136       id = tp_escape_as_identifier (empathy_contact_get_id (
1137             information->contact));
1138
1139       filename = g_strdup_printf ("%s.%s", id, ext);
1140       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
1141
1142       g_free (id);
1143       g_free (ext);
1144       g_free (filename);
1145     }
1146
1147   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
1148     {
1149       GError *error = NULL;
1150
1151       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1152
1153       if (!empathy_avatar_save_to_file (avatar, filename, &error))
1154         {
1155           /* Save error */
1156           GtkWidget *error_dialog;
1157
1158           error_dialog = gtk_message_dialog_new (NULL, 0,
1159               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1160               _("Unable to save avatar"));
1161
1162           gtk_message_dialog_format_secondary_text (
1163               GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
1164
1165           g_signal_connect (error_dialog, "response",
1166               G_CALLBACK (gtk_widget_destroy), NULL);
1167
1168           gtk_window_present (GTK_WINDOW (error_dialog));
1169
1170           g_clear_error (&error);
1171         }
1172
1173       g_free (filename);
1174     }
1175
1176   gtk_widget_destroy (dialog);
1177 }
1178
1179 static void
1180 popup_avatar_menu (EmpathyContactWidget *information,
1181                    GtkWidget *parent,
1182                    GdkEventButton *event)
1183 {
1184   GtkWidget *menu, *item;
1185   gint button, event_time;
1186
1187   if (information->contact == NULL ||
1188       empathy_contact_get_avatar (information->contact) == NULL)
1189       return;
1190
1191   menu = gtk_menu_new ();
1192
1193   /* Add "Save as..." entry */
1194   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
1195   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1196   gtk_widget_show (item);
1197
1198   g_signal_connect (item, "activate",
1199       G_CALLBACK (save_avatar_menu_activate_cb), information);
1200
1201   if (event)
1202     {
1203       button = event->button;
1204       event_time = event->time;
1205     }
1206   else
1207     {
1208       button = 0;
1209       event_time = gtk_get_current_event_time ();
1210     }
1211
1212   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1213       button, event_time);
1214   g_object_ref_sink (menu);
1215   g_object_unref (menu);
1216 }
1217
1218 static gboolean
1219 widget_avatar_popup_menu_cb (GtkWidget *widget,
1220                              EmpathyContactWidget *information)
1221 {
1222   popup_avatar_menu (information, widget, NULL);
1223
1224   return TRUE;
1225 }
1226
1227 static gboolean
1228 widget_avatar_button_press_event_cb (GtkWidget *widget,
1229                                      GdkEventButton *event,
1230                                      EmpathyContactWidget *information)
1231 {
1232   /* Ignore double-clicks and triple-clicks */
1233   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1234     {
1235       popup_avatar_menu (information, widget, event);
1236       return TRUE;
1237     }
1238
1239   return FALSE;
1240 }
1241
1242 static void
1243 set_avatar_cb (GObject *source,
1244     GAsyncResult *res,
1245     gpointer user_data)
1246 {
1247   GError *error = NULL;
1248
1249   if (!tp_account_set_avatar_finish (TP_ACCOUNT (source), res, &error)) {
1250       DEBUG ("Failed to set Account.Avatar: %s", error->message);
1251       g_error_free (error);
1252   }
1253 }
1254
1255 static void
1256 set_avatar_on_account (TpAccount *account,
1257     const gchar *data,
1258     gsize size,
1259     const gchar *mime_type)
1260 {
1261   DEBUG ("%s Account.Avatar on %s", size > 0 ? "Set": "Clear",
1262       tp_proxy_get_object_path (account));
1263
1264   tp_account_set_avatar_async (account, (const guchar *) data, size,
1265       mime_type, set_avatar_cb, NULL);
1266 }
1267
1268 static void
1269 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
1270                                   EmpathyContactWidget *information)
1271 {
1272   const gchar *data;
1273   gsize size;
1274   const gchar *mime_type;
1275   TpAccount *account;
1276
1277   empathy_avatar_chooser_get_image_data (
1278       EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
1279       &data, &size, &mime_type);
1280
1281   account = empathy_contact_get_account (information->contact);
1282   set_avatar_on_account (account, data, size, mime_type);
1283 }
1284
1285 static void
1286 set_nickname_cb (GObject *source,
1287     GAsyncResult *res,
1288     gpointer user_data)
1289 {
1290   GError *error = NULL;
1291
1292   if (!tp_account_set_nickname_finish (TP_ACCOUNT (source), res, &error))
1293     {
1294       DEBUG ("Failed to set Account.Nickname: %s", error->message);
1295       g_error_free (error);
1296     }
1297 }
1298
1299 static void
1300 set_alias_on_account (TpAccount *account,
1301     const gchar *alias)
1302 {
1303   DEBUG ("Set Account.Nickname to %s", alias);
1304
1305   tp_account_set_nickname_async (account, alias, set_nickname_cb, NULL);
1306 }
1307
1308 static gboolean
1309 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
1310                                            GdkEventFocus *event,
1311                                            EmpathyContactWidget *information)
1312 {
1313   if (information->contact)
1314     {
1315       const gchar *alias;
1316
1317       alias = gtk_entry_get_text (GTK_ENTRY (editable));
1318
1319       if (empathy_contact_is_user (information->contact))
1320         {
1321           TpAccount * account;
1322
1323           account = empathy_contact_get_account (information->contact);
1324           set_alias_on_account (account, alias);
1325         }
1326       else
1327         {
1328           empathy_contact_set_alias (information->contact, alias);
1329         }
1330     }
1331
1332   return FALSE;
1333 }
1334
1335 static void
1336 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
1337                                   EmpathyAvatarChooser *avatar_chooser)
1338 {
1339   TpAccount *account;
1340
1341   account = empathy_account_chooser_get_account (account_chooser);
1342   if (account == NULL)
1343     return;
1344
1345   empathy_avatar_chooser_set_account (avatar_chooser, account);
1346 }
1347
1348 static void
1349 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
1350 {
1351   EmpathyAvatar *avatar = NULL;
1352
1353   if (information->contact)
1354       avatar = empathy_contact_get_avatar (information->contact);
1355
1356   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1357     {
1358       g_signal_handlers_block_by_func (information->widget_avatar,
1359           contact_widget_avatar_changed_cb,
1360           information);
1361       empathy_avatar_chooser_set (
1362           EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
1363       g_signal_handlers_unblock_by_func (information->widget_avatar,
1364           contact_widget_avatar_changed_cb, information);
1365     }
1366   else
1367       empathy_avatar_image_set (
1368           EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
1369 }
1370
1371 static void
1372 contact_widget_name_notify_cb (EmpathyContactWidget *information)
1373 {
1374   if (GTK_IS_ENTRY (information->widget_alias))
1375       gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
1376           empathy_contact_get_name (information->contact));
1377   else
1378       gtk_label_set_label (GTK_LABEL (information->widget_alias),
1379           empathy_contact_get_name (information->contact));
1380 }
1381
1382 static void
1383 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
1384 {
1385   const gchar *status;
1386   gchar *markup_text = NULL;
1387
1388   status = empathy_contact_get_status (information->contact);
1389   if (status != NULL)
1390     markup_text = empathy_add_link_markup (status);
1391   gtk_label_set_markup (GTK_LABEL (information->label_status), markup_text);
1392   g_free (markup_text);
1393
1394   gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
1395       empathy_icon_name_for_contact (information->contact),
1396       GTK_ICON_SIZE_BUTTON);
1397   gtk_widget_show (information->image_state);
1398 }
1399
1400 #if HAVE_FAVOURITE_CONTACTS
1401 static void
1402 contact_widget_favourites_changed_cb (EmpathyContactManager *manager,
1403     EmpathyContact *contact,
1404     gboolean is_favourite,
1405     EmpathyContactWidget *information)
1406 {
1407   if (contact != information->contact)
1408     return;
1409
1410   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
1411             information->favourite_checkbox), is_favourite);
1412 }
1413 #endif
1414
1415 static void
1416 contact_widget_remove_contact (EmpathyContactWidget *information)
1417 {
1418   if (information->contact)
1419     {
1420       TpContact *tp_contact;
1421
1422       contact_widget_save (information);
1423
1424       g_signal_handlers_disconnect_by_func (information->contact,
1425           contact_widget_name_notify_cb, information);
1426       g_signal_handlers_disconnect_by_func (information->contact,
1427           contact_widget_presence_notify_cb, information);
1428       g_signal_handlers_disconnect_by_func (information->contact,
1429           contact_widget_avatar_notify_cb, information);
1430       g_signal_handlers_disconnect_by_func (information->contact,
1431           contact_widget_groups_notify_cb, information);
1432
1433       tp_contact = empathy_contact_get_tp_contact (information->contact);
1434       if (tp_contact != NULL)
1435         {
1436           g_signal_handlers_disconnect_by_func (tp_contact,
1437               contact_widget_details_notify_cb, information);
1438         }
1439
1440       g_object_unref (information->contact);
1441       information->contact = NULL;
1442     }
1443
1444   if (information->details_cancellable != NULL)
1445     {
1446       g_cancellable_cancel (information->details_cancellable);
1447       g_object_unref (information->details_cancellable);
1448       information->details_cancellable = NULL;
1449     }
1450 }
1451
1452 static void contact_widget_change_contact (EmpathyContactWidget *information);
1453
1454 static void
1455 contact_widget_contact_update (EmpathyContactWidget *information)
1456 {
1457   TpAccount *account = NULL;
1458   const gchar *id = NULL;
1459
1460   /* Connect and get info from new contact */
1461   if (information->contact)
1462     {
1463       g_signal_connect_swapped (information->contact, "notify::name",
1464           G_CALLBACK (contact_widget_name_notify_cb), information);
1465       g_signal_connect_swapped (information->contact, "notify::presence",
1466           G_CALLBACK (contact_widget_presence_notify_cb), information);
1467       g_signal_connect_swapped (information->contact,
1468           "notify::presence-message",
1469           G_CALLBACK (contact_widget_presence_notify_cb), information);
1470       g_signal_connect_swapped (information->contact, "notify::avatar",
1471           G_CALLBACK (contact_widget_avatar_notify_cb), information);
1472
1473       account = empathy_contact_get_account (information->contact);
1474       id = empathy_contact_get_id (information->contact);
1475     }
1476
1477   /* Update account widget */
1478   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1479     {
1480       if (account)
1481         {
1482           g_signal_handlers_block_by_func (information->widget_account,
1483                    contact_widget_change_contact,
1484                    information);
1485           empathy_account_chooser_set_account (
1486               EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1487           g_signal_handlers_unblock_by_func (information->widget_account,
1488               contact_widget_change_contact, information);
1489         }
1490     }
1491   else
1492     {
1493       if (account)
1494         {
1495           const gchar *name;
1496
1497           name = tp_account_get_display_name (account);
1498           gtk_label_set_label (GTK_LABEL (information->label_account), name);
1499
1500           name = tp_account_get_icon_name (account);
1501           gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1502               name, GTK_ICON_SIZE_MENU);
1503         }
1504     }
1505
1506   /* Update id widget */
1507   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1508       gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1509   else
1510       gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1511
1512   /* Update other widgets */
1513   if (information->contact)
1514     {
1515       contact_widget_name_notify_cb (information);
1516       contact_widget_presence_notify_cb (information);
1517       contact_widget_avatar_notify_cb (information);
1518
1519 #if HAVE_FAVOURITE_CONTACTS
1520       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1521         {
1522           gboolean is_favourite;
1523
1524           is_favourite = empathy_contact_list_is_favourite (
1525               EMPATHY_CONTACT_LIST (information->manager),
1526               information->contact);
1527
1528           contact_widget_favourites_changed_cb (information->manager,
1529               information->contact, is_favourite, information);
1530         }
1531 #endif
1532
1533       gtk_widget_show (information->label_alias);
1534       gtk_widget_show (information->widget_alias);
1535       gtk_widget_show (information->hbox_presence);
1536       gtk_widget_show (information->widget_avatar);
1537     }
1538   else
1539     {
1540       gtk_widget_hide (information->label_alias);
1541       gtk_widget_hide (information->widget_alias);
1542       gtk_widget_hide (information->hbox_presence);
1543       gtk_widget_hide (information->widget_avatar);
1544     }
1545 }
1546
1547 static void
1548 contact_widget_set_contact (EmpathyContactWidget *information,
1549                             EmpathyContact *contact)
1550 {
1551   if (contact == information->contact)
1552     return;
1553
1554   contact_widget_remove_contact (information);
1555   if (contact)
1556     information->contact = g_object_ref (contact);
1557
1558   /* set the selected account to be the account this contact came from */
1559   if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1560       empathy_account_chooser_set_account (
1561                       EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1562                       empathy_contact_get_account (contact));
1563   }
1564
1565   /* Update information for widgets */
1566   contact_widget_contact_update (information);
1567   contact_widget_groups_update (information);
1568   contact_widget_details_update (information);
1569   contact_widget_client_update (information);
1570   contact_widget_location_update (information);
1571 }
1572
1573 static void
1574 contact_widget_got_contact_cb (TpConnection *connection,
1575                                EmpathyContact *contact,
1576                                const GError *error,
1577                                gpointer user_data,
1578                                GObject *weak_object)
1579 {
1580   EmpathyContactWidget *information = user_data;
1581
1582   if (error != NULL)
1583     {
1584       DEBUG ("Error: %s", error->message);
1585       return;
1586     }
1587
1588   contact_widget_set_contact (information, contact);
1589 }
1590
1591 static void
1592 contact_widget_change_contact (EmpathyContactWidget *information)
1593 {
1594   TpConnection *connection;
1595
1596   connection = empathy_account_chooser_get_connection (
1597       EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1598   if (!connection)
1599       return;
1600
1601   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1602     {
1603       const gchar *id;
1604
1605       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1606       if (!EMP_STR_EMPTY (id))
1607         {
1608           empathy_tp_contact_factory_get_from_id (connection, id,
1609               contact_widget_got_contact_cb, information, NULL,
1610               G_OBJECT (information->vbox_contact_widget));
1611         }
1612     }
1613   else
1614     {
1615       empathy_tp_contact_factory_get_from_handle (connection,
1616           tp_connection_get_self_handle (connection),
1617           contact_widget_got_contact_cb, information, NULL,
1618           G_OBJECT (information->vbox_contact_widget));
1619     }
1620 }
1621
1622 static gboolean
1623 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1624 {
1625   contact_widget_change_contact (self);
1626   return FALSE;
1627 }
1628
1629 static void
1630 contact_widget_id_changed_cb (GtkEntry *entry,
1631                               EmpathyContactWidget *self)
1632 {
1633   if (self->widget_id_timeout != 0)
1634     {
1635       g_source_remove (self->widget_id_timeout);
1636     }
1637
1638   self->widget_id_timeout =
1639     g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1640         (GSourceFunc) contact_widget_id_activate_timeout, self);
1641 }
1642
1643 static gboolean
1644 contact_widget_id_focus_out_cb (GtkWidget *widget,
1645                                 GdkEventFocus *event,
1646                                 EmpathyContactWidget *information)
1647 {
1648   contact_widget_change_contact (information);
1649   return FALSE;
1650 }
1651
1652 #if HAVE_FAVOURITE_CONTACTS
1653 static void
1654 favourite_toggled_cb (GtkToggleButton *button,
1655     EmpathyContactWidget *information)
1656 {
1657   gboolean active;
1658
1659   active = gtk_toggle_button_get_active (button);
1660
1661   if (active)
1662     {
1663       empathy_contact_list_add_to_favourites (
1664           EMPATHY_CONTACT_LIST (information->manager), information->contact);
1665     }
1666   else
1667     {
1668       empathy_contact_list_remove_from_favourites (
1669           EMPATHY_CONTACT_LIST (information->manager), information->contact);
1670     }
1671 }
1672 #endif
1673
1674 static void
1675 contact_widget_contact_setup (EmpathyContactWidget *information)
1676 {
1677   /* Setup label_status as a KludgeLabel */
1678   information->label_status = empathy_kludge_label_new ("");
1679   gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1680                                 PANGO_WRAP_WORD_CHAR);
1681   gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1682                            TRUE);
1683
1684   if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1685     gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1686
1687   gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1688         information->label_status, TRUE, TRUE, 0);
1689   gtk_widget_show (information->label_status);
1690
1691   /* Setup account label/chooser */
1692   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1693     {
1694       information->widget_account = empathy_account_chooser_new ();
1695
1696       g_signal_connect_swapped (information->widget_account, "changed",
1697             G_CALLBACK (contact_widget_change_contact),
1698             information);
1699     }
1700   else
1701     {
1702       /* Pack the protocol icon with the account name in an hbox */
1703       information->widget_account = gtk_hbox_new (FALSE, 6);
1704
1705       information->label_account = gtk_label_new (NULL);
1706       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1707         gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1708       }
1709       gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1710       gtk_widget_show (information->label_account);
1711
1712       information->image_account = gtk_image_new ();
1713       gtk_widget_show (information->image_account);
1714
1715       gtk_box_pack_start (GTK_BOX (information->widget_account),
1716           information->image_account, FALSE, FALSE, 0);
1717       gtk_box_pack_start (GTK_BOX (information->widget_account),
1718           information->label_account, FALSE, TRUE, 0);
1719     }
1720   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1721            information->widget_account,
1722            1, 2, 0, 1);
1723   gtk_widget_show (information->widget_account);
1724
1725   /* Set up avatar chooser/display */
1726   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1727     {
1728       information->widget_avatar = empathy_avatar_chooser_new ();
1729       g_signal_connect (information->widget_avatar, "changed",
1730             G_CALLBACK (contact_widget_avatar_changed_cb),
1731             information);
1732       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1733         {
1734           g_signal_connect (information->widget_account, "changed",
1735               G_CALLBACK (update_avatar_chooser_account_cb),
1736               information->widget_avatar);
1737           update_avatar_chooser_account_cb (
1738               EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1739               EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1740         }
1741     }
1742   else
1743     {
1744       information->widget_avatar = empathy_avatar_image_new ();
1745
1746       g_signal_connect (information->widget_avatar, "popup-menu",
1747           G_CALLBACK (widget_avatar_popup_menu_cb), information);
1748       g_signal_connect (information->widget_avatar, "button-press-event",
1749           G_CALLBACK (widget_avatar_button_press_event_cb), information);
1750     }
1751
1752   gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1753           information->widget_avatar,
1754           FALSE, FALSE,
1755           6);
1756   gtk_widget_show (information->widget_avatar);
1757
1758   /* Setup id label/entry */
1759   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1760     {
1761       information->widget_id = gtk_entry_new ();
1762       g_signal_connect (information->widget_id, "focus-out-event",
1763             G_CALLBACK (contact_widget_id_focus_out_cb),
1764             information);
1765       g_signal_connect (information->widget_id, "changed",
1766             G_CALLBACK (contact_widget_id_changed_cb),
1767             information);
1768     }
1769   else
1770     {
1771       information->widget_id = gtk_label_new (NULL);
1772       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1773         gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1774       }
1775       gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1776     }
1777   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1778            information->widget_id,
1779            1, 2, 1, 2);
1780   gtk_widget_show (information->widget_id);
1781
1782   /* Setup alias label/entry */
1783   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1784     {
1785       information->widget_alias = gtk_entry_new ();
1786
1787       if (!(information->flags & EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS))
1788         g_signal_connect (information->widget_alias, "focus-out-event",
1789               G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1790               information);
1791
1792       /* Make return activate the window default (the Close button) */
1793       gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1794           TRUE);
1795     }
1796   else
1797     {
1798       information->widget_alias = gtk_label_new (NULL);
1799       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1800         gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1801       }
1802       gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1803     }
1804   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1805            information->widget_alias,
1806            1, 2, 2, 3);
1807   if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1808     gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1809   }
1810   gtk_widget_show (information->widget_alias);
1811
1812 #if HAVE_FAVOURITE_CONTACTS
1813   /* Favorite */
1814   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1815     {
1816       information->favourite_checkbox = gtk_check_button_new_with_label (
1817           _("Favorite"));
1818
1819       g_signal_connect (information->favourite_checkbox, "toggled",
1820           G_CALLBACK (favourite_toggled_cb), information);
1821
1822       gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1823            information->favourite_checkbox, 0, 2, 3, 4);
1824
1825       information->fav_sig_id = g_signal_connect (information->manager,
1826           "favourites-changed",
1827           G_CALLBACK (contact_widget_favourites_changed_cb), information);
1828
1829       gtk_widget_show (information->favourite_checkbox);
1830     }
1831 #endif
1832 }
1833
1834 static void
1835 contact_widget_destroy_cb (GtkWidget *widget,
1836                            EmpathyContactWidget *information)
1837 {
1838   contact_widget_remove_contact (information);
1839
1840   if (information->widget_id_timeout != 0)
1841     {
1842       g_source_remove (information->widget_id_timeout);
1843     }
1844
1845   if (information->fav_sig_id != 0)
1846     g_signal_handler_disconnect (information->manager, information->fav_sig_id);
1847
1848   g_object_unref (information->manager);
1849
1850   g_slice_free (EmpathyContactWidget, information);
1851 }
1852
1853 /**
1854  * empathy_contact_widget_new:
1855  * @contact: an #EmpathyContact
1856  * @flags: #EmpathyContactWidgetFlags for the new contact widget
1857  *
1858  * Creates a new #EmpathyContactWidget.
1859  *
1860  * Return value: a new #EmpathyContactWidget
1861  */
1862 GtkWidget *
1863 empathy_contact_widget_new (EmpathyContact *contact,
1864                             EmpathyContactWidgetFlags flags)
1865 {
1866   EmpathyContactWidget *information;
1867   GtkBuilder *gui;
1868   gchar *filename;
1869
1870   g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1871
1872   information = g_slice_new0 (EmpathyContactWidget);
1873   information->flags = flags;
1874
1875   filename = empathy_file_lookup ("empathy-contact-widget.ui",
1876       "libempathy-gtk");
1877   gui = empathy_builder_get_file (filename,
1878        "vbox_contact_widget", &information->vbox_contact_widget,
1879        "hbox_contact", &information->hbox_contact,
1880        "hbox_presence", &information->hbox_presence,
1881        "label_alias", &information->label_alias,
1882        "image_state", &information->image_state,
1883        "table_contact", &information->table_contact,
1884        "vbox_avatar", &information->vbox_avatar,
1885        "vbox_location", &information->vbox_location,
1886        "subvbox_location", &information->subvbox_location,
1887        "label_location", &information->label_location,
1888 #if HAVE_LIBCHAMPLAIN
1889        "viewport_map", &information->viewport_map,
1890 #endif
1891        "vbox_groups", &information->vbox_groups,
1892        "entry_group", &information->entry_group,
1893        "button_group", &information->button_group,
1894        "treeview_groups", &information->treeview_groups,
1895        "vbox_details", &information->vbox_details,
1896        "table_details", &information->table_details,
1897        "hbox_details_requested", &information->hbox_details_requested,
1898        "vbox_client", &information->vbox_client,
1899        "table_client", &information->table_client,
1900        "hbox_client_requested", &information->hbox_client_requested,
1901        NULL);
1902   g_free (filename);
1903
1904   empathy_builder_connect (gui, information,
1905       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1906       "entry_group", "changed", contact_widget_entry_group_changed_cb,
1907       "entry_group", "activate", contact_widget_entry_group_activate_cb,
1908       "button_group", "clicked", contact_widget_button_group_clicked_cb,
1909       NULL);
1910   information->table_location = NULL;
1911
1912   g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1913       "EmpathyContactWidget",
1914       information);
1915
1916   information->manager = empathy_contact_manager_dup_singleton ();
1917
1918   /* Create widgets */
1919   contact_widget_contact_setup (information);
1920   contact_widget_groups_setup (information);
1921   contact_widget_details_setup (information);
1922   contact_widget_client_setup (information);
1923
1924   if (contact != NULL)
1925     contact_widget_set_contact (information, contact);
1926   else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1927       information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1928     contact_widget_change_contact (information);
1929
1930   return empathy_builder_unref_and_keep_widget (gui,
1931     information->vbox_contact_widget);
1932 }
1933
1934 /**
1935  * empathy_contact_widget_get_contact:
1936  * @widget: an #EmpathyContactWidget
1937  *
1938  * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1939  *
1940  * Returns: the #EmpathyContact associated with @widget
1941  */
1942 EmpathyContact *
1943 empathy_contact_widget_get_contact (GtkWidget *widget)
1944 {
1945   EmpathyContactWidget *information;
1946
1947   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1948
1949   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1950   if (!information)
1951       return NULL;
1952
1953   return information->contact;
1954 }
1955
1956 const gchar *
1957 empathy_contact_widget_get_alias (GtkWidget *widget)
1958 {
1959   EmpathyContactWidget *information;
1960
1961   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1962
1963   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1964   if (!information)
1965       return NULL;
1966
1967   return gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
1968 }
1969
1970 /**
1971  * empathy_contact_widget_set_contact:
1972  * @widget: an #EmpathyContactWidget
1973  * @contact: a different #EmpathyContact
1974  *
1975  * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1976  */
1977 void
1978 empathy_contact_widget_set_contact (GtkWidget *widget,
1979                                     EmpathyContact *contact)
1980 {
1981   EmpathyContactWidget *information;
1982
1983   g_return_if_fail (GTK_IS_WIDGET (widget));
1984   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1985
1986   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1987   if (!information)
1988     return;
1989
1990   contact_widget_set_contact (information, contact);
1991 }
1992
1993 /**
1994  * empathy_contact_widget_set_account_filter:
1995  * @widget: an #EmpathyContactWidget
1996  * @filter: a #EmpathyAccountChooserFilterFunc
1997  * @user_data: user data to pass to @filter, or %NULL
1998  *
1999  * Set a filter on the #EmpathyAccountChooser included in the
2000  * #EmpathyContactWidget.
2001  */
2002 void
2003 empathy_contact_widget_set_account_filter (
2004     GtkWidget *widget,
2005     EmpathyAccountChooserFilterFunc filter,
2006     gpointer user_data)
2007 {
2008   EmpathyContactWidget *information;
2009   EmpathyAccountChooser *chooser;
2010
2011   g_return_if_fail (GTK_IS_WIDGET (widget));
2012
2013   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
2014   if (!information)
2015     return;
2016
2017   chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
2018   if (chooser)
2019       empathy_account_chooser_set_filter (chooser, filter, user_data);
2020 }
2021