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