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