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