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