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