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