]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-widget.c
Expand EmpathyIndividualWidget
[empathy.git] / libempathy-gtk / empathy-individual-widget.c
1 /*
2  * Copyright (C) 2007-2010 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Philip Withnall <philip.withnall@collabora.co.uk>
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 #include <telepathy-glib/util.h>
31
32 #include <folks/folks.h>
33 #include <folks/folks-telepathy.h>
34
35 #ifdef HAVE_LIBCHAMPLAIN
36 #include <champlain/champlain.h>
37 #include <champlain-gtk/champlain-gtk.h>
38 #endif
39
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-location.h>
42 #include <libempathy/empathy-time.h>
43
44 #include "empathy-avatar-image.h"
45 #include "empathy-groups-widget.h"
46 #include "empathy-gtk-enum-types.h"
47 #include "empathy-individual-widget.h"
48 #include "empathy-kludge-label.h"
49 #include "empathy-string-parser.h"
50 #include "empathy-ui-utils.h"
51
52 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
53 #include <libempathy/empathy-debug.h>
54
55 /**
56  * SECTION:empathy-individual-widget
57  * @title:EmpathyIndividualWidget
58  * @short_description: A widget used to display and edit details about an
59  * individual
60  * @include: libempathy-empathy-individual-widget.h
61  *
62  * #EmpathyIndividualWidget is a widget which displays appropriate widgets
63  * with details about an individual, also allowing changing these details,
64  * if desired.
65  */
66
67 /**
68  * EmpathyIndividualWidget:
69  * @parent: parent object
70  *
71  * Widget which displays appropriate widgets with details about an individual,
72  * also allowing changing these details, if desired.
73  */
74
75 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualWidget)
76
77 typedef struct {
78   FolksIndividual *individual; /* owned */
79   EmpathyIndividualWidgetFlags flags;
80
81   /* weak pointer to the contact whose contact details we're displaying */
82   TpContact *contact_info_contact;
83
84   /* unowned Persona (borrowed from priv->individual) -> GtkTable child */
85   GHashTable *persona_tables;
86   /* Table containing the information for the individual as whole, or NULL */
87   GtkTable *individual_table;
88
89   /* Individual */
90   GtkWidget *hbox_presence;
91   GtkWidget *vbox_individual;
92
93   /* Location */
94   GtkWidget *vbox_location;
95   GtkWidget *subvbox_location;
96   GtkWidget *table_location;
97   GtkWidget *label_location;
98 #ifdef HAVE_LIBCHAMPLAIN
99   GtkWidget *viewport_map;
100   GtkWidget *map_view_embed;
101   ChamplainView *map_view;
102 #endif
103
104   /* Groups */
105   GtkWidget *groups_widget;
106
107   /* Details */
108   GtkWidget *vbox_details;
109   GtkWidget *table_details;
110   GtkWidget *hbox_details_requested;
111   GtkWidget *details_spinner;
112   GCancellable *details_cancellable; /* owned */
113 } EmpathyIndividualWidgetPriv;
114
115 G_DEFINE_TYPE (EmpathyIndividualWidget, empathy_individual_widget,
116     GTK_TYPE_BOX);
117
118 enum {
119   PROP_INDIVIDUAL = 1,
120   PROP_FLAGS
121 };
122
123 static void
124 details_set_up (EmpathyIndividualWidget *self)
125 {
126   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
127
128   gtk_widget_hide (priv->vbox_details);
129
130   priv->details_spinner = gtk_spinner_new ();
131   gtk_box_pack_end (GTK_BOX (priv->hbox_details_requested),
132       priv->details_spinner, TRUE, TRUE, 0);
133   gtk_widget_show (priv->details_spinner);
134 }
135
136 typedef struct
137 {
138   const gchar *field_name;
139   const gchar *title;
140   gboolean linkify;
141 } InfoFieldData;
142
143 static InfoFieldData info_field_data[] =
144 {
145   { "fn",    N_("Full name:"),      FALSE },
146   { "tel",   N_("Phone number:"),   FALSE },
147   { "email", N_("E-mail address:"), TRUE },
148   { "url",   N_("Website:"),        TRUE },
149   { "bday",  N_("Birthday:"),       FALSE },
150   { NULL, NULL }
151 };
152
153 static InfoFieldData *
154 find_info_field_data (const gchar *field_name)
155 {
156   guint i;
157
158   for (i = 0; info_field_data[i].field_name != NULL; i++)
159     {
160       if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
161         return info_field_data + i;
162     }
163   return NULL;
164 }
165
166 static gint
167 contact_info_field_name_cmp (const gchar *name1,
168     const gchar *name2)
169 {
170   guint i;
171
172   if (tp_strdiff (name1, name2) == FALSE)
173     return 0;
174
175   /* We use the order of info_field_data */
176   for (i = 0; info_field_data[i].field_name != NULL; i++)
177     {
178       if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
179         return -1;
180       if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
181         return +1;
182     }
183
184   return g_strcmp0 (name1, name2);
185 }
186
187 static gint
188 contact_info_field_cmp (TpContactInfoField *field1,
189     TpContactInfoField *field2)
190 {
191   return contact_info_field_name_cmp (field1->field_name, field2->field_name);
192 }
193
194 typedef struct {
195   EmpathyIndividualWidget *widget; /* weak */
196   TpContact *contact; /* owned */
197 } DetailsData;
198
199 static void
200 details_data_free (DetailsData *data)
201 {
202   if (data->widget != NULL)
203     {
204       g_object_remove_weak_pointer (G_OBJECT (data->widget),
205           (gpointer *) &data->widget);
206     }
207   g_object_unref (data->contact);
208   g_slice_free (DetailsData, data);
209 }
210
211 static guint
212 details_update_show (EmpathyIndividualWidget *self,
213     TpContact *contact)
214 {
215   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
216   GList *info, *l;
217   guint n_rows = 0;
218
219   info = tp_contact_get_contact_info (contact);
220   info = g_list_sort (info, (GCompareFunc) contact_info_field_cmp);
221   for (l = info; l != NULL; l = l->next)
222     {
223       TpContactInfoField *field = l->data;
224       InfoFieldData *field_data;
225       const gchar *value;
226       GtkWidget *w;
227
228       if (field->field_value == NULL || field->field_value[0] == NULL)
229         continue;
230
231       value = field->field_value[0];
232
233       field_data = find_info_field_data (field->field_name);
234       if (field_data == NULL)
235         {
236           DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
237           continue;
238         }
239
240       /* Add Title */
241       w = gtk_label_new (_(field_data->title));
242       gtk_table_attach (GTK_TABLE (priv->table_details),
243           w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
244       gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
245       gtk_widget_show (w);
246
247       /* Add Value */
248       w = gtk_label_new (value);
249       if (field_data->linkify == TRUE)
250         {
251           gchar *markup;
252
253           markup = empathy_add_link_markup (value);
254           gtk_label_set_markup (GTK_LABEL (w), markup);
255           g_free (markup);
256         }
257
258       if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
259         gtk_label_set_selectable (GTK_LABEL (w), TRUE);
260
261       gtk_table_attach_defaults (GTK_TABLE (priv->table_details),
262           w, 1, 2, n_rows, n_rows + 1);
263       gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
264       gtk_widget_show (w);
265
266       n_rows++;
267     }
268   g_list_free (info);
269
270   return n_rows;
271 }
272
273 static void
274 details_notify_cb (TpContact *contact,
275     GParamSpec *pspec,
276     EmpathyIndividualWidget *self)
277 {
278   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
279   guint n_rows;
280
281   gtk_container_foreach (GTK_CONTAINER (priv->table_details),
282       (GtkCallback) gtk_widget_destroy, NULL);
283
284   n_rows = details_update_show (self, contact);
285
286   if (n_rows > 0)
287     {
288       gtk_widget_show (priv->vbox_details);
289       gtk_widget_show (priv->table_details);
290     }
291   else
292     {
293       gtk_widget_hide (priv->vbox_details);
294     }
295
296   gtk_widget_hide (priv->hbox_details_requested);
297   gtk_spinner_stop (GTK_SPINNER (priv->details_spinner));
298 }
299
300 static void
301 details_request_cb (TpContact *contact,
302     GAsyncResult *res,
303     DetailsData *data)
304 {
305   EmpathyIndividualWidget *self = data->widget;
306   gboolean hide_widget = FALSE;
307   GError *error = NULL;
308
309   if (tp_contact_request_contact_info_finish (contact, res, &error) == TRUE)
310     details_notify_cb (contact, NULL, self);
311   else
312     hide_widget = TRUE;
313
314   g_clear_error (&error);
315
316   if (self != NULL)
317     {
318       EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
319
320       if (hide_widget == TRUE)
321         gtk_widget_hide (GET_PRIV (self)->vbox_details);
322
323       tp_clear_object (&priv->details_cancellable);
324
325       /* We need a (weak) pointer to the contact so that we can disconnect the
326        * signal handler on deconstruction. */
327       if (priv->contact_info_contact != NULL)
328         {
329           g_object_remove_weak_pointer (G_OBJECT (priv->contact_info_contact),
330             (gpointer *) &priv->contact_info_contact);
331         }
332
333       priv->contact_info_contact = contact;
334       g_object_add_weak_pointer (G_OBJECT (contact),
335           (gpointer *) &priv->contact_info_contact);
336
337       g_signal_connect (contact, "notify::contact-info",
338           (GCallback) details_notify_cb, self);
339     }
340
341   details_data_free (data);
342 }
343
344 static void
345 details_feature_prepared_cb (TpConnection *connection,
346     GAsyncResult *res,
347     DetailsData *data)
348 {
349   EmpathyIndividualWidget *self = data->widget;
350   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
351
352   if (tp_proxy_prepare_finish (connection, res, NULL) == FALSE)
353     {
354       gtk_widget_hide (priv->vbox_details);
355       details_data_free (data);
356       return;
357     }
358
359   /* Request the Individual's info */
360   gtk_widget_show (priv->vbox_details);
361   gtk_widget_show (priv->hbox_details_requested);
362   gtk_widget_hide (priv->table_details);
363   gtk_spinner_start (GTK_SPINNER (priv->details_spinner));
364
365   if (priv->details_cancellable == NULL)
366     {
367       priv->details_cancellable = g_cancellable_new ();
368       tp_contact_request_contact_info_async (data->contact,
369           priv->details_cancellable, (GAsyncReadyCallback) details_request_cb,
370           data);
371     }
372 }
373
374 static void
375 details_update (EmpathyIndividualWidget *self)
376 {
377   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
378   TpContact *tp_contact = NULL;
379
380   if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS))
381     return;
382
383   gtk_widget_hide (priv->vbox_details);
384
385   if (priv->individual != NULL)
386     {
387       /* FIXME: We take the first TpContact we find and only use its details.
388        * It would be a lot better if we would get the details for every
389        * TpContact in the Individual and merge them all, but that requires
390        * vCard support in libfolks for it to not be hideously complex.
391        * (bgo#627399) */
392       GList *personas, *l;
393
394       personas = folks_individual_get_personas (priv->individual);
395       for (l = personas; l != NULL; l = l->next)
396         {
397           if (TPF_IS_PERSONA (l->data))
398             {
399               tp_contact = tpf_persona_get_contact (TPF_PERSONA (l->data));
400               if (tp_contact != NULL)
401                 break;
402             }
403         }
404     }
405
406   if (tp_contact != NULL)
407     {
408       GQuark features[] = { TP_CONNECTION_FEATURE_CONTACT_INFO, 0 };
409       TpConnection *connection;
410       DetailsData *data;
411
412       data = g_slice_new (DetailsData);
413       data->widget = self;
414       g_object_add_weak_pointer (G_OBJECT (self), (gpointer *) &data->widget);
415       data->contact = g_object_ref (tp_contact);
416
417       /* First, make sure the CONTACT_INFO feature is ready on the connection */
418       connection = tp_contact_get_connection (tp_contact);
419       tp_proxy_prepare_async (connection, features,
420           (GAsyncReadyCallback) details_feature_prepared_cb, data);
421     }
422 }
423
424 static void
425 groups_update (EmpathyIndividualWidget *self)
426 {
427   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
428
429   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS &&
430       priv->individual != NULL)
431     {
432       empathy_groups_widget_set_groupable (
433           EMPATHY_GROUPS_WIDGET (priv->groups_widget),
434           FOLKS_GROUPS (priv->individual));
435       gtk_widget_show (priv->groups_widget);
436     }
437   else
438     {
439       gtk_widget_hide (priv->groups_widget);
440     }
441 }
442
443 /* Converts the Location's GHashTable's key to a user readable string */
444 static const gchar *
445 location_key_to_label (const gchar *key)
446 {
447   if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
448     return _("Country ISO Code:");
449   else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
450     return _("Country:");
451   else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
452     return _("State:");
453   else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
454     return _("City:");
455   else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
456     return _("Area:");
457   else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
458     return _("Postal Code:");
459   else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
460     return _("Street:");
461   else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
462     return _("Building:");
463   else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
464     return _("Floor:");
465   else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
466     return _("Room:");
467   else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
468     return _("Text:");
469   else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
470     return _("Description:");
471   else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
472     return _("URI:");
473   else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
474     return _("Accuracy Level:");
475   else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
476     return _("Error:");
477   else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
478     return _("Vertical Error (meters):");
479   else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
480     return _("Horizontal Error (meters):");
481   else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
482     return _("Speed:");
483   else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
484     return _("Bearing:");
485   else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
486     return _("Climb Speed:");
487   else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
488     return _("Last Updated on:");
489   else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
490     return _("Longitude:");
491   else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
492     return _("Latitude:");
493   else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
494     return _("Altitude:");
495   else
496   {
497     DEBUG ("Unexpected Location key: %s", key);
498     return key;
499   }
500 }
501
502 static void
503 location_update (EmpathyIndividualWidget *self)
504 {
505   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
506   EmpathyContact *contact = NULL;
507   GHashTable *location = NULL;
508   GValue *value;
509   GtkWidget *label;
510   guint row = 0;
511   static const gchar* ordered_geolocation_keys[] = {
512     EMPATHY_LOCATION_TEXT,
513     EMPATHY_LOCATION_URI,
514     EMPATHY_LOCATION_DESCRIPTION,
515     EMPATHY_LOCATION_BUILDING,
516     EMPATHY_LOCATION_FLOOR,
517     EMPATHY_LOCATION_ROOM,
518     EMPATHY_LOCATION_STREET,
519     EMPATHY_LOCATION_AREA,
520     EMPATHY_LOCATION_LOCALITY,
521     EMPATHY_LOCATION_REGION,
522     EMPATHY_LOCATION_COUNTRY,
523     NULL
524   };
525   int i;
526   const gchar *skey;
527   gboolean display_map = FALSE;
528   GList *personas, *l;
529
530   if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION))
531     {
532       gtk_widget_hide (priv->vbox_location);
533       return;
534     }
535
536   /* FIXME: For the moment, we just display the first location data we can
537    * find amongst the Individual's Personas. Once libfolks grows a location
538    * interface, we can use that. (bgo#627400) */
539   personas = folks_individual_get_personas (priv->individual);
540   for (l = personas; l != NULL; l = l->next)
541     {
542       FolksPersona *persona = FOLKS_PERSONA (l->data);
543
544       if (TPF_IS_PERSONA (persona))
545         {
546           TpContact *tp_contact;
547
548           /* Get the contact. If it turns out to have location information, we
549            * have to keep it alive for the duration of the function, since we're
550            * accessing its private data. */
551           tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
552           contact = empathy_contact_dup_from_tp_contact (tp_contact);
553           empathy_contact_set_persona (contact, persona);
554
555           /* Try and get a location */
556           location = empathy_contact_get_location (contact);
557           if (location != NULL && g_hash_table_size (location) > 0)
558             break;
559
560           location = NULL;
561           tp_clear_object (&contact);
562         }
563     }
564
565   if (contact == NULL || location == NULL)
566     {
567       gtk_widget_hide (priv->vbox_location);
568       tp_clear_object (&contact);
569       return;
570     }
571
572   value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
573   if (value == NULL)
574     {
575       gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
576       gtk_label_set_markup (GTK_LABEL (priv->label_location), loc);
577       g_free (loc);
578     }
579   else
580     {
581       gchar *user_date;
582       gchar *text;
583       gint64 stamp;
584       time_t time_;
585
586       stamp = g_value_get_int64 (value);
587       time_ = stamp;
588
589       user_date = empathy_time_to_string_relative (time_);
590
591       text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
592       gtk_label_set_markup (GTK_LABEL (priv->label_location), text);
593       g_free (user_date);
594       g_free (text);
595     }
596
597   /* Prepare the location information table */
598   if (priv->table_location != NULL)
599     gtk_widget_destroy (priv->table_location);
600
601   priv->table_location = gtk_table_new (1, 2, FALSE);
602   gtk_box_pack_start (GTK_BOX (priv->subvbox_location),
603       priv->table_location, FALSE, FALSE, 5);
604
605
606   for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
607     {
608       const gchar* user_label;
609       GValue *gvalue;
610       char *svalue = NULL;
611
612       gvalue = g_hash_table_lookup (location, (gpointer) skey);
613       if (gvalue == NULL)
614         continue;
615
616       user_label = location_key_to_label (skey);
617
618       label = gtk_label_new (user_label);
619       gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
620       gtk_table_attach (GTK_TABLE (priv->table_location),
621           label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
622       gtk_widget_show (label);
623
624       if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
625         {
626           gdouble dvalue;
627           dvalue = g_value_get_double (gvalue);
628           svalue = g_strdup_printf ("%f", dvalue);
629         }
630       else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
631         {
632           svalue = g_value_dup_string (gvalue);
633         }
634       else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
635         {
636           time_t time_;
637
638           time_ = g_value_get_int64 (value);
639           svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
640         }
641
642       if (svalue != NULL)
643         {
644           label = gtk_label_new (svalue);
645           gtk_table_attach_defaults (GTK_TABLE (priv->table_location),
646               label, 1, 2, row, row + 1);
647           gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
648           gtk_widget_show (label);
649
650           if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
651             gtk_label_set_selectable (GTK_LABEL (label), TRUE);
652         }
653
654       g_free (svalue);
655       row++;
656     }
657
658   tp_clear_object (&contact);
659
660 #ifdef HAVE_LIBCHAMPLAIN
661   if ((g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL) &&
662       (g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL) &&
663       !(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
664     {
665       /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
666        * windows */
667       display_map = TRUE;
668     }
669 #endif
670
671   if (row > 0)
672     {
673       /* We can display some fields */
674       gtk_widget_show (priv->table_location);
675     }
676   else if (display_map == FALSE)
677     {
678       /* Can't display either fields or map */
679       gtk_widget_hide (priv->vbox_location);
680       return;
681     }
682
683 #ifdef HAVE_LIBCHAMPLAIN
684   if (display_map == TRUE)
685     {
686       GPtrArray *markers;
687       ChamplainLayer *layer;
688
689       priv->map_view_embed = gtk_champlain_embed_new ();
690       priv->map_view = gtk_champlain_embed_get_view (
691           GTK_CHAMPLAIN_EMBED (priv->map_view_embed));
692
693       gtk_container_add (GTK_CONTAINER (priv->viewport_map),
694           priv->map_view_embed);
695       g_object_set (G_OBJECT (priv->map_view),
696           "show-license", TRUE,
697           "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
698           "zoom-level", 10,
699           NULL);
700
701       layer = champlain_layer_new ();
702       champlain_view_add_layer (priv->map_view, layer);
703       markers = g_ptr_array_new ();
704
705       /* FIXME: For now, we have to do this manually. Once libfolks grows a
706        * location interface, we can use that. (bgo#627400) */
707       personas = folks_individual_get_personas (priv->individual);
708       for (l = personas; l != NULL; l = l->next)
709         {
710           FolksPersona *persona = FOLKS_PERSONA (l->data);
711
712           if (TPF_IS_PERSONA (persona))
713             {
714               gdouble lat = 0.0, lon = 0.0;
715               ClutterActor *marker;
716               TpContact *tp_contact;
717
718               /* Get the contact */
719               tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
720               contact = empathy_contact_dup_from_tp_contact (tp_contact);
721               empathy_contact_set_persona (contact, persona);
722
723               /* Try and get a location */
724               location = empathy_contact_get_location (contact);
725               if (location == NULL || g_hash_table_size (location) == 0)
726                 {
727                   g_object_unref (contact);
728                   continue;
729                 }
730
731               /* Get this persona's latitude and longitude */
732               value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
733               if (value == NULL)
734                 {
735                   g_object_unref (contact);
736                   continue;
737                 }
738
739               lat = g_value_get_double (value);
740
741               value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
742               if (value == NULL)
743                 {
744                   g_object_unref (contact);
745                   continue;
746                 }
747
748               lon = g_value_get_double (value);
749
750               /* Add a marker to the map */
751               marker = champlain_marker_new_with_text (
752                   folks_alias_get_alias (FOLKS_ALIAS (persona)), NULL, NULL,
753                   NULL);
754               champlain_base_marker_set_position (
755                   CHAMPLAIN_BASE_MARKER (marker), lat, lon);
756               clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
757
758               g_ptr_array_add (markers, marker);
759
760               g_object_unref (contact);
761             }
762         }
763
764       /* Zoom to show all of the markers */
765       g_ptr_array_add (markers, NULL); /* NULL-terminate the array */
766       champlain_view_ensure_markers_visible (priv->map_view,
767           (ChamplainBaseMarker **) markers->pdata, FALSE);
768       g_ptr_array_free (markers, TRUE);
769
770       gtk_widget_show_all (priv->viewport_map);
771     }
772 #endif
773
774     gtk_widget_show (priv->vbox_location);
775 }
776
777 static EmpathyAvatar *
778 persona_dup_avatar (FolksPersona *persona)
779 {
780   TpContact *tp_contact;
781   EmpathyContact *contact;
782   EmpathyAvatar *avatar;
783
784   if (!TPF_IS_PERSONA (persona))
785     return NULL;
786
787   tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
788   contact = empathy_contact_dup_from_tp_contact (tp_contact);
789   empathy_contact_set_persona (contact, persona);
790
791   avatar = empathy_contact_get_avatar (contact);
792   if (avatar != NULL)
793     empathy_avatar_ref (avatar);
794   g_object_unref (contact);
795
796   return avatar;
797 }
798
799 static EmpathyAvatar *
800 individual_dup_avatar (FolksIndividual *individual)
801 {
802   GList *personas, *l;
803   EmpathyAvatar *avatar = NULL;
804
805   /* FIXME: We just choose the first Persona which has an avatar, and save that.
806    * The avatar handling in EmpathyContact needs to be moved into libfolks as
807    * much as possible, and this code rewritten to use FolksAvatar.
808    * (bgo#627401) */
809   personas = folks_individual_get_personas (individual);
810   for (l = personas; l != NULL; l = l->next)
811     {
812       avatar = persona_dup_avatar (FOLKS_PERSONA (l->data));
813       if (avatar != NULL)
814         break;
815     }
816
817   return avatar;
818 }
819
820 static void
821 save_avatar_menu_activate_cb (GtkWidget *widget,
822     EmpathyIndividualWidget *self)
823 {
824   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
825   GtkWidget *dialog;
826   EmpathyAvatar *avatar;
827   gchar *ext = NULL, *filename;
828
829   dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
830       NULL,
831       GTK_FILE_CHOOSER_ACTION_SAVE,
832       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
833       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
834       NULL);
835
836   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
837       TRUE);
838
839   avatar = individual_dup_avatar (priv->individual);
840   if (avatar == NULL)
841     return;
842
843   /* look for the avatar extension */
844   if (avatar->format != NULL)
845     {
846       gchar **splitted;
847
848       splitted = g_strsplit (avatar->format, "/", 2);
849       if (splitted[0] != NULL && splitted[1] != NULL)
850           ext = g_strdup (splitted[1]);
851
852       g_strfreev (splitted);
853     }
854   else
855     {
856       /* Avatar was loaded from the cache so was converted to PNG */
857       ext = g_strdup ("png");
858     }
859
860   if (ext != NULL)
861     {
862       gchar *id;
863
864       id = tp_escape_as_identifier (folks_individual_get_id (priv->individual));
865
866       filename = g_strdup_printf ("%s.%s", id, ext);
867       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
868
869       g_free (id);
870       g_free (ext);
871       g_free (filename);
872     }
873
874   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
875     {
876       GError *error = NULL;
877
878       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
879
880       if (empathy_avatar_save_to_file (avatar, filename, &error) == FALSE)
881         {
882           /* Save error */
883           GtkWidget *error_dialog;
884
885           error_dialog = gtk_message_dialog_new (NULL, 0,
886               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
887               _("Unable to save avatar"));
888
889           gtk_message_dialog_format_secondary_text (
890               GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
891
892           g_signal_connect (error_dialog, "response",
893               (GCallback) gtk_widget_destroy, NULL);
894
895           gtk_window_present (GTK_WINDOW (error_dialog));
896
897           g_clear_error (&error);
898         }
899
900       g_free (filename);
901     }
902
903   gtk_widget_destroy (dialog);
904   empathy_avatar_unref (avatar);
905 }
906
907 static gboolean
908 popup_avatar_menu (EmpathyIndividualWidget *self,
909     GtkWidget *parent,
910     GdkEventButton *event)
911 {
912   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
913   GtkWidget *menu, *item;
914   EmpathyAvatar *avatar;
915   gint button, event_time;
916
917   if (priv->individual == NULL)
918     return FALSE;
919
920   avatar = individual_dup_avatar (priv->individual);
921   if (avatar == NULL)
922     return FALSE;
923   empathy_avatar_unref (avatar);
924
925   menu = gtk_menu_new ();
926
927   /* Add "Save as..." entry */
928   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
929   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
930   gtk_widget_show (item);
931
932   g_signal_connect (item, "activate",
933       (GCallback) save_avatar_menu_activate_cb, self);
934
935   if (event != NULL)
936     {
937       button = event->button;
938       event_time = event->time;
939     }
940   else
941     {
942       button = 0;
943       event_time = gtk_get_current_event_time ();
944     }
945
946   gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
947   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, button, event_time);
948   g_object_ref_sink (menu);
949   g_object_unref (menu);
950
951   return TRUE;
952 }
953
954 static gboolean
955 avatar_widget_popup_menu_cb (GtkWidget *widget,
956     EmpathyIndividualWidget *self)
957 {
958   return popup_avatar_menu (self, widget, NULL);
959 }
960
961 static gboolean
962 avatar_widget_button_press_event_cb (GtkWidget *widget,
963     GdkEventButton *event,
964     EmpathyIndividualWidget *self)
965 {
966   /* Ignore double-clicks and triple-clicks */
967   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
968     return popup_avatar_menu (self, widget, event);
969
970   return FALSE;
971 }
972
973 /* Returns the TpAccount for the user as a convenience. Note that it has a ref
974  * added. */
975 static TpAccount *
976 individual_is_user (FolksIndividual *individual)
977 {
978   GList *personas, *l;
979
980   /* FIXME: This should move into libfolks when libfolks grows a way of
981    * determining "self". (bgo#627402) */
982   personas = folks_individual_get_personas (individual);
983   for (l = personas; l != NULL; l = l->next)
984     {
985       FolksPersona *persona = FOLKS_PERSONA (l->data);
986
987       if (TPF_IS_PERSONA (persona))
988         {
989           TpContact *tp_contact;
990           EmpathyContact *contact;
991
992           /* Get the contact */
993           tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
994           contact = empathy_contact_dup_from_tp_contact (tp_contact);
995           empathy_contact_set_persona (contact, persona);
996
997           /* Determine if the contact is the user */
998           if (empathy_contact_is_user (contact))
999             {
1000               g_object_unref (contact);
1001               return g_object_ref (empathy_contact_get_account (contact));
1002             }
1003
1004           g_object_unref (contact);
1005         }
1006     }
1007
1008   return NULL;
1009 }
1010
1011 static void
1012 set_nickname_cb (TpAccount *account,
1013     GAsyncResult *result,
1014     gpointer user_data)
1015 {
1016   GError *error = NULL;
1017
1018   if (tp_account_set_nickname_finish (account, result, &error) == FALSE)
1019     {
1020       DEBUG ("Failed to set Account.Nickname: %s", error->message);
1021       g_error_free (error);
1022     }
1023 }
1024
1025 static gboolean
1026 entry_alias_focus_event_cb (GtkEditable *editable,
1027     GdkEventFocus *event,
1028     EmpathyIndividualWidget *self)
1029 {
1030   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1031
1032   if (priv->individual != NULL)
1033     {
1034       const gchar *alias;
1035       TpAccount *account;
1036
1037       alias = gtk_entry_get_text (GTK_ENTRY (editable));
1038       account = individual_is_user (priv->individual);
1039
1040       if (account != NULL)
1041         {
1042           DEBUG ("Set Account.Nickname to %s", alias);
1043           tp_account_set_nickname_async (account, alias,
1044               (GAsyncReadyCallback) set_nickname_cb, NULL);
1045           g_object_unref (account);
1046         }
1047       else
1048         {
1049           folks_alias_set_alias (FOLKS_ALIAS (priv->individual), alias);
1050         }
1051     }
1052
1053   return FALSE;
1054 }
1055
1056 static void
1057 favourite_toggled_cb (GtkToggleButton *button,
1058     EmpathyIndividualWidget *self)
1059 {
1060   gboolean active = gtk_toggle_button_get_active (button);
1061   folks_favourite_set_is_favourite (
1062       FOLKS_FAVOURITE (GET_PRIV (self)->individual), active);
1063 }
1064
1065 static void
1066 notify_avatar_cb (gpointer folks_object,
1067     GParamSpec *pspec,
1068     EmpathyIndividualWidget *self)
1069 {
1070   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1071   EmpathyAvatar *avatar = NULL;
1072   GObject *table;
1073   GtkWidget *avatar_widget;
1074
1075   if (FOLKS_IS_INDIVIDUAL (folks_object))
1076     {
1077       avatar = individual_dup_avatar (FOLKS_INDIVIDUAL (folks_object));
1078       table = G_OBJECT (priv->individual_table);
1079     }
1080   else if (FOLKS_IS_PERSONA (folks_object))
1081     {
1082       avatar = persona_dup_avatar (FOLKS_PERSONA (folks_object));
1083       table = g_hash_table_lookup (priv->persona_tables, folks_object);
1084     }
1085   else
1086     {
1087       g_assert_not_reached ();
1088     }
1089
1090   if (table == NULL)
1091     return;
1092
1093   avatar_widget = g_object_get_data (table, "avatar-widget");
1094   empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (avatar_widget), avatar);
1095
1096   if (avatar != NULL)
1097     empathy_avatar_unref (avatar);
1098 }
1099
1100 static void
1101 notify_alias_cb (gpointer folks_object,
1102     GParamSpec *pspec,
1103     EmpathyIndividualWidget *self)
1104 {
1105   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1106   GObject *table;
1107   GtkWidget *alias_widget;
1108
1109   if (FOLKS_IS_INDIVIDUAL (folks_object))
1110     table = G_OBJECT (priv->individual_table);
1111   else if (FOLKS_IS_PERSONA (folks_object))
1112     table = g_hash_table_lookup (priv->persona_tables, folks_object);
1113   else
1114     g_assert_not_reached ();
1115
1116   if (table == NULL)
1117     return;
1118
1119   alias_widget = g_object_get_data (table, "alias-widget");
1120
1121   if (GTK_IS_ENTRY (alias_widget))
1122     {
1123       gtk_entry_set_text (GTK_ENTRY (alias_widget),
1124           folks_alias_get_alias (FOLKS_ALIAS (folks_object)));
1125     }
1126   else
1127     {
1128       gtk_label_set_label (GTK_LABEL (alias_widget),
1129           folks_alias_get_alias (FOLKS_ALIAS (folks_object)));
1130     }
1131 }
1132
1133 static void
1134 notify_presence_cb (gpointer folks_object,
1135     GParamSpec *pspec,
1136     EmpathyIndividualWidget *self)
1137 {
1138   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1139   GObject *table;
1140   GtkWidget *status_label, *state_image;
1141   const gchar *message;
1142   gchar *markup_text = NULL;
1143
1144   if (FOLKS_IS_INDIVIDUAL (folks_object))
1145     table = G_OBJECT (priv->individual_table);
1146   else if (FOLKS_IS_PERSONA (folks_object))
1147     table = g_hash_table_lookup (priv->persona_tables, folks_object);
1148   else
1149     g_assert_not_reached ();
1150
1151   if (table == NULL)
1152     return;
1153
1154   status_label = g_object_get_data (table, "status-label");
1155   state_image = g_object_get_data (table, "state-image");
1156
1157   /* FIXME: Default messages should be moved into libfolks (bgo#627403) */
1158   message = folks_presence_get_presence_message (FOLKS_PRESENCE (folks_object));
1159   if (EMP_STR_EMPTY (message))
1160     {
1161       message = empathy_presence_get_default_message (
1162           folks_presence_get_presence_type (FOLKS_PRESENCE (folks_object)));
1163     }
1164
1165   if (message != NULL)
1166     markup_text = empathy_add_link_markup (message);
1167   gtk_label_set_markup (GTK_LABEL (status_label), markup_text);
1168   g_free (markup_text);
1169
1170   gtk_image_set_from_icon_name (GTK_IMAGE (state_image),
1171       empathy_icon_name_for_presence (
1172           folks_presence_get_presence_type (FOLKS_PRESENCE (folks_object))),
1173       GTK_ICON_SIZE_BUTTON);
1174   gtk_widget_show (state_image);
1175 }
1176
1177 static void
1178 notify_is_favourite_cb (gpointer folks_object,
1179     GParamSpec *pspec,
1180     EmpathyIndividualWidget *self)
1181 {
1182   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1183   GObject *table;
1184   GtkWidget *favourite_widget;
1185
1186   if (FOLKS_IS_INDIVIDUAL (folks_object))
1187     table = G_OBJECT (priv->individual_table);
1188   else if (FOLKS_IS_PERSONA (folks_object))
1189     table = g_hash_table_lookup (priv->persona_tables, folks_object);
1190   else
1191     g_assert_not_reached ();
1192
1193   if (table == NULL)
1194     return;
1195
1196   favourite_widget = g_object_get_data (table, "favourite-widget");
1197
1198   if (GTK_IS_TOGGLE_BUTTON (favourite_widget))
1199     {
1200       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (favourite_widget),
1201           folks_favourite_get_is_favourite (FOLKS_FAVOURITE (folks_object)));
1202     }
1203 }
1204
1205 static void
1206 alias_presence_avatar_favourite_set_up (EmpathyIndividualWidget *self,
1207     GtkTable *table,
1208     guint starting_row)
1209 {
1210   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1211   GtkWidget *label, *alias, *image, *avatar, *alignment;
1212   guint current_row = starting_row;
1213
1214   /* Alias */
1215   label = gtk_label_new (_("Alias:"));
1216   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1217   gtk_table_attach (table, label, 0, 1, current_row, current_row + 1, GTK_FILL,
1218       GTK_FILL, 0, 0);
1219   gtk_widget_show (label);
1220
1221   /* Set up alias label/entry */
1222   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS)
1223     {
1224       alias = gtk_entry_new ();
1225
1226       g_signal_connect (alias, "focus-out-event",
1227           (GCallback) entry_alias_focus_event_cb, self);
1228
1229       /* Make return activate the window default (the Close button) */
1230       gtk_entry_set_activates_default (GTK_ENTRY (alias), TRUE);
1231     }
1232   else
1233     {
1234       alias = gtk_label_new (NULL);
1235       if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1236         gtk_label_set_selectable (GTK_LABEL (alias), TRUE);
1237       gtk_misc_set_alignment (GTK_MISC (alias), 0.0, 0.5);
1238     }
1239
1240   g_object_set_data (G_OBJECT (table), "alias-widget", alias);
1241   gtk_table_attach (table, alias, 1, 2, current_row, current_row + 1,
1242       GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1243   gtk_widget_show (alias);
1244
1245   current_row++;
1246
1247   /* Presence */
1248   priv->hbox_presence = gtk_hbox_new (FALSE, 6);
1249
1250   /* Presence image */
1251   image = gtk_image_new_from_stock (GTK_STOCK_MISSING_IMAGE,
1252       GTK_ICON_SIZE_BUTTON);
1253   g_object_set_data (G_OBJECT (table), "state-image", image);
1254   gtk_box_pack_start (GTK_BOX (priv->hbox_presence), image, FALSE,
1255       FALSE, 0);
1256   gtk_widget_show (image);
1257
1258   /* Set up status_label as a KludgeLabel */
1259   label = empathy_kludge_label_new ("");
1260   gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
1261   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1262
1263   gtk_label_set_selectable (GTK_LABEL (label),
1264       (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1265
1266   g_object_set_data (G_OBJECT (table), "status-label", label);
1267   gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, TRUE,
1268       TRUE, 0);
1269   gtk_widget_show (label);
1270
1271   gtk_table_attach (table, priv->hbox_presence, 0, 2, current_row,
1272       current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1273   gtk_widget_show (priv->hbox_presence);
1274
1275   current_row++;
1276
1277   /* Set up favourite toggle button */
1278   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1279     {
1280       GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1281
1282       g_signal_connect (favourite, "toggled",
1283           (GCallback) favourite_toggled_cb, self);
1284
1285       g_object_set_data (G_OBJECT (table), "favourite-widget", favourite);
1286       gtk_table_attach (table, favourite, 0, 2, current_row, current_row + 1,
1287           GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1288       gtk_widget_show (favourite);
1289
1290       current_row++;
1291     }
1292
1293   /* Set up avatar display */
1294   avatar = empathy_avatar_image_new ();
1295
1296   if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1297     {
1298       g_signal_connect (avatar, "popup-menu",
1299           (GCallback) avatar_widget_popup_menu_cb, self);
1300       g_signal_connect (avatar, "button-press-event",
1301           (GCallback) avatar_widget_button_press_event_cb, self);
1302     }
1303
1304   g_object_set_data (G_OBJECT (table), "avatar-widget", avatar);
1305
1306   alignment = gtk_alignment_new (1.0, 0.0, 0.0, 0.0);
1307   gtk_container_add (GTK_CONTAINER (alignment), avatar);
1308   gtk_widget_show (avatar);
1309
1310   gtk_table_attach (table, alignment, 2, 3, 0, current_row + 1,
1311       GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 6, 6);
1312   gtk_widget_show (alignment);
1313 }
1314
1315 static void
1316 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1317 {
1318   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1319   TpContact *tp_contact;
1320   EmpathyContact *contact;
1321   TpAccount *account;
1322   GtkTable *table;
1323   GtkLabel *label;
1324   GtkImage *image;
1325   const gchar *id;
1326
1327   table = g_hash_table_lookup (priv->persona_tables, persona);
1328
1329   g_assert (table != NULL);
1330
1331   tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1332   contact = empathy_contact_dup_from_tp_contact (tp_contact);
1333   empathy_contact_set_persona (contact, persona);
1334
1335   account = empathy_contact_get_account (contact);
1336
1337   /* Update account widget */
1338   if (account != NULL)
1339     {
1340       const gchar *name;
1341
1342       label = g_object_get_data (G_OBJECT (table), "account-label");
1343       image = g_object_get_data (G_OBJECT (table), "account-image");
1344
1345       name = tp_account_get_display_name (account);
1346       gtk_label_set_label (label, name);
1347
1348       name = tp_account_get_icon_name (account);
1349       gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
1350     }
1351
1352   /* Update id widget */
1353   label = g_object_get_data (G_OBJECT (table), "id-widget");
1354   id = folks_persona_get_display_id (persona);
1355   gtk_label_set_label (label, (id != NULL) ? id : "");
1356
1357   /* Update other widgets */
1358   notify_alias_cb (persona, NULL, self);
1359   notify_presence_cb (persona, NULL, self);
1360   notify_avatar_cb (persona, NULL, self);
1361
1362   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1363     notify_is_favourite_cb (persona, NULL, self);
1364 }
1365
1366 static void
1367 add_persona (EmpathyIndividualWidget *self,
1368     FolksPersona *persona)
1369 {
1370   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1371   GtkBox *hbox;
1372   GtkTable *table;
1373   GtkWidget *label, *account_label, *account_image, *separator;
1374   guint current_row = 0;
1375
1376   if (!TPF_IS_PERSONA (persona))
1377     return;
1378
1379   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1380     table = GTK_TABLE (gtk_table_new (5, 3, FALSE));
1381   else
1382     table = GTK_TABLE (gtk_table_new (4, 3, FALSE));
1383   gtk_table_set_row_spacings (table, 6);
1384   gtk_table_set_col_spacings (table, 6);
1385
1386   /* Account and Identifier */
1387   label = gtk_label_new (_("Account:"));
1388   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1389   gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1390       GTK_FILL, GTK_FILL, 0, 0);
1391   gtk_widget_show (label);
1392
1393   /* Pack the protocol icon with the account name in an hbox */
1394   hbox = GTK_BOX (gtk_hbox_new (FALSE, 6));
1395
1396   account_label = gtk_label_new (NULL);
1397   gtk_label_set_selectable (GTK_LABEL (account_label), TRUE);
1398   gtk_misc_set_alignment (GTK_MISC (account_label), 0.0, 0.5);
1399   gtk_widget_show (account_label);
1400
1401   account_image = gtk_image_new ();
1402   gtk_widget_show (account_image);
1403
1404   gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1405   gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
1406
1407   g_object_set_data (G_OBJECT (table), "account-image", account_image);
1408   g_object_set_data (G_OBJECT (table), "account-label", account_label);
1409   gtk_table_attach (table, GTK_WIDGET (hbox), 1, 2, current_row,
1410       current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1411   gtk_widget_show (GTK_WIDGET (hbox));
1412
1413   current_row++;
1414
1415   /* Translators: Identifier to connect to Instant Messaging network */
1416   label = gtk_label_new (_("Identifier:"));
1417   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1418   gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1419       GTK_FILL, GTK_FILL, 0, 0);
1420   gtk_widget_show (label);
1421
1422   /* Set up ID label */
1423   label = gtk_label_new (NULL);
1424   gtk_label_set_selectable (GTK_LABEL (label), TRUE);
1425   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1426
1427   g_object_set_data (G_OBJECT (table), "id-widget", label);
1428   gtk_table_attach (table, label, 1, 2, current_row, current_row + 1,
1429       GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1430   gtk_widget_show (label);
1431
1432   current_row++;
1433
1434   alias_presence_avatar_favourite_set_up (self, table, current_row);
1435
1436   /* Connect to signals and display the table */
1437   g_signal_connect (persona, "notify::alias",
1438       (GCallback) notify_alias_cb, self);
1439   g_signal_connect (persona, "notify::avatar",
1440       (GCallback) notify_avatar_cb, self);
1441   g_signal_connect (persona, "notify::presence-type",
1442       (GCallback) notify_presence_cb, self);
1443   g_signal_connect (persona, "notify::presence-message",
1444       (GCallback) notify_presence_cb, self);
1445
1446   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1447     {
1448       g_signal_connect (persona, "notify::is-favourite",
1449           (GCallback) notify_is_favourite_cb, self);
1450     }
1451
1452   gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1453       GTK_WIDGET (table), FALSE, TRUE, 0);
1454   gtk_widget_show (GTK_WIDGET (table));
1455
1456   /* Pack a separator after the table */
1457   separator = gtk_hseparator_new ();
1458   g_object_set_data (G_OBJECT (table), "separator", separator);
1459   gtk_box_pack_start (GTK_BOX (priv->vbox_individual), separator, FALSE, FALSE,
1460       0);
1461   gtk_widget_show (separator);
1462
1463   g_hash_table_replace (priv->persona_tables, persona, table);
1464
1465   /* Update the new widgets */
1466   update_persona (self, persona);
1467 }
1468
1469 static void
1470 remove_persona (EmpathyIndividualWidget *self,
1471     FolksPersona *persona)
1472 {
1473   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1474   GtkWidget *separator;
1475   GtkTable *table;
1476
1477   if (!TPF_IS_PERSONA (persona))
1478     return;
1479
1480   table = g_hash_table_lookup (priv->persona_tables, persona);
1481   if (table == NULL)
1482     return;
1483
1484   g_signal_handlers_disconnect_by_func (persona, notify_alias_cb, self);
1485   g_signal_handlers_disconnect_by_func (persona, notify_avatar_cb, self);
1486   g_signal_handlers_disconnect_by_func (persona, notify_presence_cb, self);
1487
1488   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1489     {
1490       g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
1491           self);
1492     }
1493
1494   /* Remove the separator */
1495   separator = g_object_get_data (G_OBJECT (table), "separator");
1496   if (separator != NULL)
1497     gtk_container_remove (GTK_CONTAINER (priv->vbox_individual), separator);
1498
1499   /* Remove the widget */
1500   gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1501       GTK_WIDGET (table));
1502
1503   g_hash_table_remove (priv->persona_tables, persona);
1504 }
1505
1506 static void
1507 update_individual_table (EmpathyIndividualWidget *self)
1508 {
1509   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1510
1511   notify_alias_cb (priv->individual, NULL, self);
1512   notify_presence_cb (priv->individual, NULL, self);
1513   notify_avatar_cb (priv->individual, NULL, self);
1514
1515   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1516     notify_is_favourite_cb (priv->individual, NULL, self);
1517 }
1518
1519 static void
1520 individual_table_set_up (EmpathyIndividualWidget *self)
1521 {
1522   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1523   GtkTable *table;
1524   GtkWidget *label;
1525   gchar *message;
1526   GList *personas, *l;
1527   guint num_personas = 0, current_row = 0;
1528
1529   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1530     table = GTK_TABLE (gtk_table_new (4, 3, FALSE));
1531   else
1532     table = GTK_TABLE (gtk_table_new (3, 3, FALSE));
1533   gtk_table_set_row_spacings (table, 6);
1534   gtk_table_set_col_spacings (table, 6);
1535
1536   /* Meta-contacts message displaying how many Telepathy personas we have */
1537   personas = folks_individual_get_personas (priv->individual);
1538   for (l = personas; l != NULL; l = l->next)
1539     {
1540       if (TPF_IS_PERSONA (l->data))
1541         num_personas++;
1542     }
1543
1544   message = g_strdup_printf (ngettext ("Meta-contact containing %u contact",
1545       "Meta-contact containing %u contacts", num_personas), num_personas);
1546   label = gtk_label_new (message);
1547   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1548   g_free (message);
1549
1550   gtk_table_attach (table, label, 0, 2, current_row, current_row + 1,
1551       GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1552   gtk_widget_show (label);
1553
1554   current_row++;
1555
1556   alias_presence_avatar_favourite_set_up (self, table, current_row);
1557
1558   /* Display the table */
1559   gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (table),
1560       TRUE, TRUE, 0);
1561   gtk_widget_show (GTK_WIDGET (table));
1562
1563   priv->individual_table = table;
1564
1565   /* Update the table */
1566   update_individual_table (self);
1567 }
1568
1569 static void
1570 individual_table_destroy (EmpathyIndividualWidget *self)
1571 {
1572   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1573
1574   if (priv->individual_table == NULL)
1575     return;
1576
1577   gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1578       GTK_WIDGET (priv->individual_table));
1579   priv->individual_table = NULL;
1580 }
1581
1582 static void
1583 notify_personas_cb (FolksIndividual *individual,
1584     GParamSpec *pspec,
1585     EmpathyIndividualWidget *self)
1586 {
1587   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1588   GList *personas, *l, *children, *remove_personas = NULL;
1589   GHashTableIter iter;
1590   FolksPersona *persona;
1591   GtkTable *table;
1592   gboolean is_last;
1593   guint old_num_personas, new_num_personas;
1594
1595   personas = folks_individual_get_personas (individual);
1596   old_num_personas = g_hash_table_size (priv->persona_tables);
1597   new_num_personas = g_list_length (personas);
1598
1599   /* Remove Personas */
1600   g_hash_table_iter_init (&iter, priv->persona_tables);
1601   while (g_hash_table_iter_next (&iter, (gpointer *) &persona,
1602       (gpointer *) &table))
1603     {
1604       /* FIXME: This is slow. bgo#626725 */
1605       /* Old persona or we were displaying all personas and now just want to
1606        * display the individual table.
1607        * We can't remove the persona inside this loop, as that would invalidate
1608        * the hash table iter. */
1609       if (g_list_find (personas, persona) == NULL ||
1610           (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) &&
1611            new_num_personas > 1))
1612         {
1613           remove_personas = g_list_prepend (remove_personas, persona);
1614         }
1615     }
1616
1617   for (l = remove_personas; l != NULL; l = l->next)
1618     remove_persona (self, FOLKS_PERSONA (l->data));
1619   g_list_free (remove_personas);
1620
1621   individual_table_destroy (self);
1622
1623   /* If we're !SHOW_PERSONAS and have more than one Persona, we only display
1624    * the Individual's alias, avatar and presence, and a label saying
1625    * "Meta-contact containing x contacts". (i.e. One table.)
1626    * If we're SHOW_PERSONAS or have only one Persona, we display the
1627    * alias, avatar, presence, account and identifier for each of the
1628    * Individual's Personas. (i.e. One table per Persona.) */
1629   if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) &&
1630       new_num_personas > 1)
1631     {
1632       individual_table_set_up (self);
1633     }
1634   else
1635     {
1636       /* Add Personas */
1637       for (l = personas; l != NULL; l = l->next)
1638         {
1639           persona = FOLKS_PERSONA (l->data);
1640
1641           if (!TPF_IS_PERSONA (persona))
1642             continue;
1643
1644           /* New persona or we were displaying the individual table and we
1645            * now want to display all personas */
1646           if ((!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) &&
1647                new_num_personas <= 1) ||
1648               g_hash_table_lookup (priv->persona_tables, persona) == NULL)
1649             {
1650               add_persona (self, persona);
1651             }
1652         }
1653     }
1654
1655   /* Hide the last separator and show the others */
1656   children = gtk_container_get_children (GTK_CONTAINER (priv->vbox_individual));
1657   children = g_list_reverse (children);
1658   is_last = TRUE;
1659
1660   for (l = children; l != NULL; l = l->next)
1661     {
1662       if (GTK_IS_SEPARATOR (l->data))
1663         {
1664           gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1665           is_last = FALSE;
1666         }
1667     }
1668
1669   g_list_free (children);
1670 }
1671
1672 static void
1673 remove_individual (EmpathyIndividualWidget *self)
1674 {
1675   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1676   if (priv->individual != NULL)
1677     {
1678       GList *personas, *l;
1679
1680       g_signal_handlers_disconnect_by_func (priv->individual,
1681           notify_alias_cb, self);
1682       g_signal_handlers_disconnect_by_func (priv->individual,
1683           notify_presence_cb, self);
1684       g_signal_handlers_disconnect_by_func (priv->individual,
1685           notify_avatar_cb, self);
1686       g_signal_handlers_disconnect_by_func (priv->individual,
1687           notify_personas_cb, self);
1688
1689       if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1690         {
1691           g_signal_handlers_disconnect_by_func (priv->individual,
1692               notify_is_favourite_cb, self);
1693         }
1694
1695       personas = folks_individual_get_personas (priv->individual);
1696       for (l = personas; l != NULL; l = l->next)
1697         remove_persona (self, FOLKS_PERSONA (l->data));
1698
1699       if (priv->contact_info_contact != NULL)
1700         {
1701           g_signal_handlers_disconnect_by_func (priv->contact_info_contact,
1702               details_notify_cb, self);
1703           g_object_remove_weak_pointer (G_OBJECT (priv->contact_info_contact),
1704               (gpointer *) &priv->contact_info_contact);
1705           priv->contact_info_contact = NULL;
1706         }
1707
1708       tp_clear_object (&priv->individual);
1709     }
1710
1711   if (priv->details_cancellable != NULL)
1712     g_cancellable_cancel (priv->details_cancellable);
1713 }
1714
1715 static void
1716 individual_update (EmpathyIndividualWidget *self)
1717 {
1718   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1719
1720   /* Connect and get info from new Individual */
1721   if (priv->individual != NULL)
1722     {
1723       g_signal_connect (priv->individual, "notify::alias",
1724           (GCallback) notify_alias_cb, self);
1725       g_signal_connect (priv->individual, "notify::presence-type",
1726           (GCallback) notify_presence_cb, self);
1727       g_signal_connect (priv->individual, "notify::presence-message",
1728           (GCallback) notify_presence_cb, self);
1729       g_signal_connect (priv->individual, "notify::avatar",
1730           (GCallback) notify_avatar_cb, self);
1731       g_signal_connect (priv->individual, "notify::personas",
1732           (GCallback) notify_personas_cb, self);
1733
1734       if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1735         {
1736           g_signal_connect (priv->individual, "notify::is-favourite",
1737               (GCallback) notify_is_favourite_cb, self);
1738         }
1739
1740       /* Update individual table */
1741       notify_personas_cb (priv->individual, NULL, self);
1742     }
1743
1744   if (priv->individual == NULL)
1745     {
1746       gtk_widget_hide (priv->vbox_individual);
1747     }
1748   else if (priv->individual_table != NULL)
1749     {
1750       /* We only need to update the details for the Individual as a whole */
1751       update_individual_table (self);
1752       gtk_widget_show (priv->vbox_individual);
1753     }
1754   else
1755     {
1756       /* We need to update the details for every Persona in the Individual */
1757       GList *personas, *l;
1758
1759       personas = folks_individual_get_personas (priv->individual);
1760       for (l = personas; l != NULL; l = l->next)
1761         {
1762           if (!TPF_IS_PERSONA (l->data))
1763             continue;
1764
1765           update_persona (self, FOLKS_PERSONA (l->data));
1766         }
1767
1768       gtk_widget_show (priv->vbox_individual);
1769     }
1770 }
1771
1772 static void
1773 empathy_individual_widget_init (EmpathyIndividualWidget *self)
1774 {
1775   EmpathyIndividualWidgetPriv *priv;
1776   GtkBuilder *gui;
1777   GtkWidget *vbox_individual_widget;
1778   gchar *filename;
1779
1780   priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1781       EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
1782   self->priv = priv;
1783
1784   gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
1785       GTK_ORIENTATION_VERTICAL);
1786
1787   filename = empathy_file_lookup ("empathy-individual-widget.ui",
1788       "libempathy-gtk");
1789   gui = empathy_builder_get_file (filename,
1790       "vbox_individual_widget", &vbox_individual_widget,
1791       "vbox_individual", &priv->vbox_individual,
1792       "vbox_location", &priv->vbox_location,
1793       "subvbox_location", &priv->subvbox_location,
1794       "label_location", &priv->label_location,
1795 #ifdef HAVE_LIBCHAMPLAIN
1796       "viewport_map", &priv->viewport_map,
1797 #endif
1798       "groups_widget", &priv->groups_widget,
1799       "vbox_details", &priv->vbox_details,
1800       "table_details", &priv->table_details,
1801       "hbox_details_requested", &priv->hbox_details_requested,
1802       NULL);
1803   g_free (filename);
1804
1805   priv->table_location = NULL;
1806
1807   gtk_box_pack_start (GTK_BOX (self), vbox_individual_widget, TRUE, TRUE, 0);
1808   gtk_widget_show (vbox_individual_widget);
1809
1810   priv->persona_tables = g_hash_table_new (NULL, NULL);
1811   priv->individual_table = NULL;
1812
1813   /* Create widgets */
1814   details_set_up (self);
1815
1816   g_object_unref (gui);
1817 }
1818
1819 static void
1820 get_property (GObject *object,
1821     guint param_id,
1822     GValue *value,
1823     GParamSpec *pspec)
1824 {
1825   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1826
1827   switch (param_id)
1828     {
1829       case PROP_INDIVIDUAL:
1830         g_value_set_object (value, priv->individual);
1831         break;
1832       case PROP_FLAGS:
1833         g_value_set_flags (value, priv->flags);
1834         break;
1835       default:
1836         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1837         break;
1838     }
1839 }
1840
1841 static void
1842 set_property (GObject *object,
1843     guint param_id,
1844     const GValue *value,
1845     GParamSpec *pspec)
1846 {
1847   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1848
1849   switch (param_id)
1850     {
1851       case PROP_INDIVIDUAL:
1852         empathy_individual_widget_set_individual (
1853             EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
1854         break;
1855       case PROP_FLAGS:
1856         priv->flags = g_value_get_flags (value);
1857         break;
1858       default:
1859         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1860         break;
1861     }
1862 }
1863
1864 static void
1865 dispose (GObject *object)
1866 {
1867   remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
1868
1869   G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
1870 }
1871
1872 static void
1873 finalize (GObject *object)
1874 {
1875   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1876
1877   g_hash_table_destroy (priv->persona_tables);
1878
1879   G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
1880 }
1881
1882 static void
1883 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
1884 {
1885   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1886
1887   object_class->get_property = get_property;
1888   object_class->set_property = set_property;
1889   object_class->dispose = dispose;
1890   object_class->finalize = finalize;
1891
1892   /**
1893    * EmpathyIndividualWidget:individual:
1894    *
1895    * The #FolksIndividual to display in the widget.
1896    */
1897   g_object_class_install_property (object_class, PROP_INDIVIDUAL,
1898       g_param_spec_object ("individual",
1899           "Individual",
1900           "The #FolksIndividual to display in the widget.",
1901           FOLKS_TYPE_INDIVIDUAL,
1902           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1903
1904   /**
1905    * EmpathyIndividualWidget:flags:
1906    *
1907    * A set of flags which affect the widget's behaviour.
1908    */
1909   g_object_class_install_property (object_class, PROP_FLAGS,
1910       g_param_spec_flags ("flags",
1911           "Flags",
1912           "A set of flags which affect the widget's behaviour.",
1913           EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
1914           EMPATHY_INDIVIDUAL_WIDGET_NONE,
1915           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
1916
1917   g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
1918 }
1919
1920 /**
1921  * empathy_individual_widget_new:
1922  * @individual: the #FolksIndividual to display
1923  * @flags: flags affecting how the widget behaves and what it displays
1924  *
1925  * Creates a new #EmpathyIndividualWidget.
1926  *
1927  * Return value: a new #EmpathyIndividualWidget
1928  */
1929 GtkWidget *
1930 empathy_individual_widget_new (FolksIndividual *individual,
1931     EmpathyIndividualWidgetFlags flags)
1932 {
1933   g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
1934       NULL);
1935
1936   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
1937       "individual", individual,
1938       "flags", flags,
1939       NULL);
1940 }
1941
1942 /**
1943  * empathy_individual_widget_get_individual:
1944  * @self: an #EmpathyIndividualWidget
1945  *
1946  * Returns the #FolksIndividual being displayed by the widget.
1947  *
1948  * Return value: the #FolksIndividual being displayed, or %NULL
1949  */
1950 FolksIndividual *
1951 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
1952 {
1953   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
1954
1955   return GET_PRIV (self)->individual;
1956 }
1957
1958 /**
1959  * empathy_individual_widget_set_individual:
1960  * @self: an #EmpathyIndividualWidget
1961  * @individual: the #FolksIndividual to display, or %NULL
1962  *
1963  * Set the #FolksIndividual to be displayed by the widget:
1964  * #EmpathyIndividualWidget:individual.
1965  *
1966  * The @individual may be %NULL in order to display nothing in the widget.
1967  */
1968 void
1969 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
1970     FolksIndividual *individual)
1971 {
1972   EmpathyIndividualWidgetPriv *priv;
1973
1974   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
1975   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
1976
1977   priv = GET_PRIV (self);
1978
1979   if (individual == priv->individual)
1980     return;
1981
1982   /* Out with the old… */
1983   remove_individual (self);
1984
1985   /* …and in with the new. */
1986   if (individual != NULL)
1987     g_object_ref (individual);
1988   priv->individual = individual;
1989
1990   /* Update information for widgets */
1991   individual_update (self);
1992   groups_update (self);
1993   details_update (self);
1994   location_update (self);
1995 }