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