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