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