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