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