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