]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-widget.c
individual-widget: set xalign on the status message (#632434)
[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   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1273
1274   gtk_label_set_selectable (GTK_LABEL (label),
1275       (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1276
1277   g_object_set_data (G_OBJECT (table), "status-label", label);
1278   gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, TRUE,
1279       TRUE, 0);
1280   gtk_widget_show (label);
1281
1282   gtk_table_attach (table, priv->hbox_presence, 0, 2, current_row,
1283       current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1284   gtk_widget_show (priv->hbox_presence);
1285
1286   current_row++;
1287
1288   /* Set up favourite toggle button */
1289   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1290     {
1291       GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1292
1293       g_signal_connect (favourite, "toggled",
1294           (GCallback) favourite_toggled_cb, self);
1295
1296       g_object_set_data (G_OBJECT (table), "favourite-widget", favourite);
1297       gtk_table_attach (table, favourite, 0, 2, current_row, current_row + 1,
1298           GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1299       gtk_widget_show (favourite);
1300
1301       current_row++;
1302     }
1303
1304   /* Set up avatar display */
1305   avatar = empathy_avatar_image_new ();
1306
1307   if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1308     {
1309       g_signal_connect (avatar, "popup-menu",
1310           (GCallback) avatar_widget_popup_menu_cb, self);
1311       g_signal_connect (avatar, "button-press-event",
1312           (GCallback) avatar_widget_button_press_event_cb, self);
1313     }
1314
1315   g_object_set_data (G_OBJECT (table), "avatar-widget", avatar);
1316
1317   alignment = gtk_alignment_new (1.0, 0.0, 0.0, 0.0);
1318   gtk_container_add (GTK_CONTAINER (alignment), avatar);
1319   gtk_widget_show (avatar);
1320
1321   gtk_table_attach (table, alignment, 2, 3, 0, current_row,
1322       GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 6, 6);
1323   gtk_widget_show (alignment);
1324 }
1325
1326 static void
1327 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1328 {
1329   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1330   TpContact *tp_contact;
1331   EmpathyContact *contact;
1332   TpAccount *account;
1333   GtkTable *table;
1334   GtkLabel *label;
1335   GtkImage *image;
1336   const gchar *id;
1337
1338   table = g_hash_table_lookup (priv->persona_tables, persona);
1339
1340   g_assert (table != NULL);
1341
1342   tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1343   contact = empathy_contact_dup_from_tp_contact (tp_contact);
1344   empathy_contact_set_persona (contact, persona);
1345
1346   account = empathy_contact_get_account (contact);
1347
1348   /* Update account widget */
1349   if (account != NULL)
1350     {
1351       const gchar *name;
1352
1353       label = g_object_get_data (G_OBJECT (table), "account-label");
1354       image = g_object_get_data (G_OBJECT (table), "account-image");
1355
1356       name = tp_account_get_display_name (account);
1357       gtk_label_set_label (label, name);
1358
1359       name = tp_account_get_icon_name (account);
1360       gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
1361     }
1362
1363   /* Update id widget */
1364   label = g_object_get_data (G_OBJECT (table), "id-widget");
1365   id = folks_persona_get_display_id (persona);
1366   gtk_label_set_label (label, (id != NULL) ? id : "");
1367
1368   /* Update other widgets */
1369   notify_alias_cb (persona, NULL, self);
1370   notify_presence_cb (persona, NULL, self);
1371   notify_avatar_cb (persona, NULL, self);
1372
1373   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1374     notify_is_favourite_cb (persona, NULL, self);
1375
1376   g_object_unref (contact);
1377 }
1378
1379 static void
1380 add_persona (EmpathyIndividualWidget *self,
1381     FolksPersona *persona)
1382 {
1383   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1384   GtkBox *hbox;
1385   GtkTable *table;
1386   GtkWidget *label, *account_label, *account_image, *separator;
1387   guint current_row = 0;
1388
1389   if (!TPF_IS_PERSONA (persona))
1390     return;
1391
1392   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1393     table = GTK_TABLE (gtk_table_new (5, 3, FALSE));
1394   else
1395     table = GTK_TABLE (gtk_table_new (4, 3, FALSE));
1396   gtk_table_set_row_spacings (table, 6);
1397   gtk_table_set_col_spacings (table, 6);
1398
1399   /* Account and Identifier */
1400   label = gtk_label_new (_("Account:"));
1401   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1402   gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1403       GTK_FILL, GTK_FILL, 0, 0);
1404   gtk_widget_show (label);
1405
1406   /* Pack the protocol icon with the account name in an hbox */
1407   hbox = GTK_BOX (gtk_hbox_new (FALSE, 6));
1408
1409   account_label = gtk_label_new (NULL);
1410   gtk_label_set_selectable (GTK_LABEL (account_label),
1411       (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1412   gtk_misc_set_alignment (GTK_MISC (account_label), 0.0, 0.5);
1413   gtk_widget_show (account_label);
1414
1415   account_image = gtk_image_new ();
1416   gtk_widget_show (account_image);
1417
1418   gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1419   gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
1420
1421   g_object_set_data (G_OBJECT (table), "account-image", account_image);
1422   g_object_set_data (G_OBJECT (table), "account-label", account_label);
1423   gtk_table_attach (table, GTK_WIDGET (hbox), 1, 2, current_row,
1424       current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1425   gtk_widget_show (GTK_WIDGET (hbox));
1426
1427   current_row++;
1428
1429   /* Translators: Identifier to connect to Instant Messaging network */
1430   label = gtk_label_new (_("Identifier:"));
1431   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1432   gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1433       GTK_FILL, GTK_FILL, 0, 0);
1434   gtk_widget_show (label);
1435
1436   /* Set up ID label */
1437   label = gtk_label_new (NULL);
1438   gtk_label_set_selectable (GTK_LABEL (label),
1439       (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1440   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1441
1442   g_object_set_data (G_OBJECT (table), "id-widget", label);
1443   gtk_table_attach (table, label, 1, 2, current_row, current_row + 1,
1444       GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1445   gtk_widget_show (label);
1446
1447   current_row++;
1448
1449   alias_presence_avatar_favourite_set_up (self, table, current_row);
1450
1451   /* Connect to signals and display the table */
1452   g_signal_connect (persona, "notify::alias",
1453       (GCallback) notify_alias_cb, self);
1454   g_signal_connect (persona, "notify::avatar",
1455       (GCallback) notify_avatar_cb, self);
1456   g_signal_connect (persona, "notify::presence-type",
1457       (GCallback) notify_presence_cb, self);
1458   g_signal_connect (persona, "notify::presence-message",
1459       (GCallback) notify_presence_cb, self);
1460
1461   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1462     {
1463       g_signal_connect (persona, "notify::is-favourite",
1464           (GCallback) notify_is_favourite_cb, self);
1465     }
1466
1467   gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1468       GTK_WIDGET (table), FALSE, TRUE, 0);
1469   gtk_widget_show (GTK_WIDGET (table));
1470
1471   /* Pack a separator after the table */
1472   separator = gtk_hseparator_new ();
1473   g_object_set_data (G_OBJECT (table), "separator", separator);
1474   gtk_box_pack_start (GTK_BOX (priv->vbox_individual), separator, FALSE, FALSE,
1475       0);
1476   gtk_widget_show (separator);
1477
1478   g_hash_table_replace (priv->persona_tables, persona, table);
1479
1480   /* Update the new widgets */
1481   update_persona (self, persona);
1482 }
1483
1484 static void
1485 remove_persona (EmpathyIndividualWidget *self,
1486     FolksPersona *persona)
1487 {
1488   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1489   GtkWidget *separator;
1490   GtkTable *table;
1491
1492   if (!TPF_IS_PERSONA (persona))
1493     return;
1494
1495   table = g_hash_table_lookup (priv->persona_tables, persona);
1496   if (table == NULL)
1497     return;
1498
1499   g_signal_handlers_disconnect_by_func (persona, notify_alias_cb, self);
1500   g_signal_handlers_disconnect_by_func (persona, notify_avatar_cb, self);
1501   g_signal_handlers_disconnect_by_func (persona, notify_presence_cb, self);
1502
1503   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1504     {
1505       g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
1506           self);
1507     }
1508
1509   /* Remove the separator */
1510   separator = g_object_get_data (G_OBJECT (table), "separator");
1511   if (separator != NULL)
1512     gtk_container_remove (GTK_CONTAINER (priv->vbox_individual), separator);
1513
1514   /* Remove the widget */
1515   gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1516       GTK_WIDGET (table));
1517
1518   g_hash_table_remove (priv->persona_tables, persona);
1519 }
1520
1521 static void
1522 update_individual_table (EmpathyIndividualWidget *self)
1523 {
1524   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1525
1526   notify_alias_cb (priv->individual, NULL, self);
1527   notify_presence_cb (priv->individual, NULL, self);
1528   notify_avatar_cb (priv->individual, NULL, self);
1529
1530   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1531     notify_is_favourite_cb (priv->individual, NULL, self);
1532 }
1533
1534 static void
1535 individual_table_set_up (EmpathyIndividualWidget *self)
1536 {
1537   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1538   GtkTable *table;
1539   guint current_row = 0;
1540   guint nb_rows = 2;
1541
1542   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1543     nb_rows++;
1544
1545   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1546     nb_rows++;
1547
1548   table = GTK_TABLE (gtk_table_new (nb_rows, 3, FALSE));
1549   gtk_table_set_row_spacings (table, 6);
1550   gtk_table_set_col_spacings (table, 6);
1551
1552   /* We only display the number of personas in tooltips */
1553   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1554     {
1555       gchar *message;
1556       GtkWidget *label;
1557       GList *personas, *l;
1558       guint num_personas = 0;
1559
1560       /* Meta-contacts message displaying how many Telepathy personas we have */
1561       personas = folks_individual_get_personas (priv->individual);
1562       for (l = personas; l != NULL; l = l->next)
1563         {
1564           if (TPF_IS_PERSONA (l->data))
1565             num_personas++;
1566         }
1567
1568       message = g_strdup_printf (ngettext ("Meta-contact containing %u contact",
1569           "Meta-contact containing %u contacts", num_personas), num_personas);
1570       label = gtk_label_new (message);
1571       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1572       g_free (message);
1573
1574       gtk_table_attach (table, label, 0, 2, current_row, current_row + 1,
1575           GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1576       gtk_widget_show (label);
1577
1578       current_row++;
1579     }
1580
1581   alias_presence_avatar_favourite_set_up (self, table, current_row);
1582
1583   /* Display the table */
1584   gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (table),
1585       FALSE, TRUE, 0);
1586   gtk_widget_show (GTK_WIDGET (table));
1587
1588   priv->individual_table = table;
1589
1590   /* Update the table */
1591   update_individual_table (self);
1592 }
1593
1594 static void
1595 individual_table_destroy (EmpathyIndividualWidget *self)
1596 {
1597   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1598
1599   if (priv->individual_table == NULL)
1600     return;
1601
1602   gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1603       GTK_WIDGET (priv->individual_table));
1604   priv->individual_table = NULL;
1605 }
1606
1607 static void
1608 personas_changed_cb (FolksIndividual *individual,
1609     GList *added,
1610     GList *removed,
1611     EmpathyIndividualWidget *self)
1612 {
1613   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1614   GList *personas, *l, *children;
1615   gboolean show_personas, was_showing_personas, will_show_personas, is_last;
1616   guint old_num_personas, new_num_personas = 0;
1617
1618   personas = folks_individual_get_personas (individual);
1619
1620   /* Note that old_num_personas is the number of persona tables we were
1621    * displaying, not the number of Personas which were in the Individual
1622    * before. */
1623   old_num_personas = g_hash_table_size (priv->persona_tables);
1624
1625   for (l = personas; l != NULL; l = l->next)
1626     {
1627       if (TPF_IS_PERSONA (l->data))
1628         new_num_personas++;
1629     }
1630
1631   /*
1632    * What we display for various conditions:
1633    *  - "Personas": display the alias, avatar, presence account and identifier
1634    *                for each of the Individual's Personas. (i.e. One table per
1635    *                Persona.)
1636    *  - "Individual": display the alias, avatar and presence for the Individual,
1637    *                  and a label saying "Meta-contact containing x contacts".
1638    *                  (i.e. One table in total.)
1639    *
1640    *              | SHOW_PERSONAS | !SHOW_PERSONAS
1641    * -------------+---------------+---------------
1642    * > 1 Persona  | Personas      | Individual
1643    * -------------+---------------+---------------
1644    * == 1 Persona | Personas      | Personas
1645    */
1646   show_personas = (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) != 0;
1647   was_showing_personas = show_personas || old_num_personas == 1;
1648   will_show_personas = show_personas || new_num_personas == 1;
1649
1650   /* If both @added and @removed are NULL, we're being called manually, and we
1651    * need to set up the tables for the first time. We do this simply by
1652    * ensuring was_showing_personas and will_show_personas are different so that
1653    * the code resets the UI.
1654    */
1655   if (added == NULL && removed == NULL)
1656     was_showing_personas = !will_show_personas;
1657
1658   if (was_showing_personas && will_show_personas)
1659     {
1660       /* Remove outdated Personas */
1661       for (l = removed; l != NULL; l = l->next)
1662         remove_persona (self, FOLKS_PERSONA (l->data));
1663
1664       /* Add new Personas */
1665       for (l = added; l != NULL; l = l->next)
1666         add_persona (self, FOLKS_PERSONA (l->data));
1667     }
1668   else if (!was_showing_personas && will_show_personas)
1669     {
1670       /* Remove the old Individual table */
1671       individual_table_destroy (self);
1672
1673       /* Set up all the Persona tables instead */
1674       for (l = personas; l != NULL; l = l->next)
1675         add_persona (self, FOLKS_PERSONA (l->data));
1676     }
1677   else if (was_showing_personas && !will_show_personas)
1678     {
1679       /* Remove all Personas */
1680       for (l = personas; l != NULL; l = l->next)
1681         remove_persona (self, FOLKS_PERSONA (l->data));
1682       for (l = removed; l != NULL; l = l->next)
1683         remove_persona (self, FOLKS_PERSONA (l->data));
1684
1685       /* Set up the Individual table instead */
1686       individual_table_set_up (self);
1687     }
1688
1689   /* Hide the last separator and show the others */
1690   children = gtk_container_get_children (GTK_CONTAINER (priv->vbox_individual));
1691   children = g_list_reverse (children);
1692   is_last = TRUE;
1693
1694   for (l = children; l != NULL; l = l->next)
1695     {
1696       if (GTK_IS_SEPARATOR (l->data))
1697         {
1698           gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1699           is_last = FALSE;
1700         }
1701     }
1702
1703   g_list_free (children);
1704 }
1705
1706 static void
1707 individual_removed_cb (FolksIndividual *individual,
1708     FolksIndividual *replacement_individual,
1709     EmpathyIndividualWidget *self)
1710 {
1711   empathy_individual_widget_set_individual (self, replacement_individual);
1712 }
1713
1714 static void
1715 remove_individual (EmpathyIndividualWidget *self)
1716 {
1717   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1718   if (priv->individual != NULL)
1719     {
1720       GList *personas, *l;
1721
1722       g_signal_handlers_disconnect_by_func (priv->individual,
1723           notify_alias_cb, self);
1724       g_signal_handlers_disconnect_by_func (priv->individual,
1725           notify_presence_cb, self);
1726       g_signal_handlers_disconnect_by_func (priv->individual,
1727           notify_avatar_cb, self);
1728       g_signal_handlers_disconnect_by_func (priv->individual,
1729           personas_changed_cb, self);
1730       g_signal_handlers_disconnect_by_func (priv->individual,
1731           individual_removed_cb, self);
1732
1733       if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1734         {
1735           g_signal_handlers_disconnect_by_func (priv->individual,
1736               notify_is_favourite_cb, self);
1737         }
1738
1739       personas = folks_individual_get_personas (priv->individual);
1740       for (l = personas; l != NULL; l = l->next)
1741         remove_persona (self, FOLKS_PERSONA (l->data));
1742       individual_table_destroy (self);
1743
1744       if (priv->contact_info_contact != NULL)
1745         {
1746           g_signal_handlers_disconnect_by_func (priv->contact_info_contact,
1747               details_notify_cb, self);
1748           g_object_remove_weak_pointer (G_OBJECT (priv->contact_info_contact),
1749               (gpointer *) &priv->contact_info_contact);
1750           priv->contact_info_contact = NULL;
1751         }
1752
1753       tp_clear_object (&priv->individual);
1754     }
1755
1756   if (priv->details_cancellable != NULL)
1757     g_cancellable_cancel (priv->details_cancellable);
1758 }
1759
1760 static void
1761 individual_update (EmpathyIndividualWidget *self)
1762 {
1763   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1764
1765   /* Connect and get info from new Individual */
1766   if (priv->individual != NULL)
1767     {
1768       g_signal_connect (priv->individual, "notify::alias",
1769           (GCallback) notify_alias_cb, self);
1770       g_signal_connect (priv->individual, "notify::presence-type",
1771           (GCallback) notify_presence_cb, self);
1772       g_signal_connect (priv->individual, "notify::presence-message",
1773           (GCallback) notify_presence_cb, self);
1774       g_signal_connect (priv->individual, "notify::avatar",
1775           (GCallback) notify_avatar_cb, self);
1776       g_signal_connect (priv->individual, "personas-changed",
1777           (GCallback) personas_changed_cb, self);
1778       g_signal_connect (priv->individual, "removed",
1779           (GCallback) individual_removed_cb, self);
1780
1781       if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1782         {
1783           g_signal_connect (priv->individual, "notify::is-favourite",
1784               (GCallback) notify_is_favourite_cb, self);
1785         }
1786
1787       /* Update individual table */
1788       personas_changed_cb (priv->individual, NULL, NULL, self);
1789     }
1790
1791   if (priv->individual == NULL)
1792     {
1793       gtk_widget_hide (priv->vbox_individual);
1794     }
1795   else if (priv->individual_table != NULL)
1796     {
1797       /* We only need to update the details for the Individual as a whole */
1798       update_individual_table (self);
1799       gtk_widget_show (priv->vbox_individual);
1800     }
1801   else
1802     {
1803       /* We need to update the details for every Persona in the Individual */
1804       GList *personas, *l;
1805
1806       personas = folks_individual_get_personas (priv->individual);
1807       for (l = personas; l != NULL; l = l->next)
1808         {
1809           if (!TPF_IS_PERSONA (l->data))
1810             continue;
1811
1812           update_persona (self, FOLKS_PERSONA (l->data));
1813         }
1814
1815       gtk_widget_show (priv->vbox_individual);
1816     }
1817 }
1818
1819 static void
1820 empathy_individual_widget_init (EmpathyIndividualWidget *self)
1821 {
1822   EmpathyIndividualWidgetPriv *priv;
1823   GtkBuilder *gui;
1824   gchar *filename;
1825
1826   priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1827       EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
1828   self->priv = priv;
1829
1830   gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
1831       GTK_ORIENTATION_VERTICAL);
1832
1833   filename = empathy_file_lookup ("empathy-individual-widget.ui",
1834       "libempathy-gtk");
1835   gui = empathy_builder_get_file (filename,
1836       "scrolled_window_individual", &priv->scrolled_window_individual,
1837       "viewport_individual", &priv->viewport_individual,
1838       "vbox_individual_widget", &priv->vbox_individual_widget,
1839       "vbox_individual", &priv->vbox_individual,
1840       "vbox_location", &priv->vbox_location,
1841       "subvbox_location", &priv->subvbox_location,
1842       "label_location", &priv->label_location,
1843 #ifdef HAVE_LIBCHAMPLAIN
1844       "viewport_map", &priv->viewport_map,
1845 #endif
1846       "groups_widget", &priv->groups_widget,
1847       "vbox_details", &priv->vbox_details,
1848       "table_details", &priv->table_details,
1849       "hbox_details_requested", &priv->hbox_details_requested,
1850       NULL);
1851   g_free (filename);
1852
1853   priv->table_location = NULL;
1854
1855   gtk_box_pack_start (GTK_BOX (self), priv->vbox_individual_widget, TRUE, TRUE,
1856       0);
1857   gtk_widget_show (priv->vbox_individual_widget);
1858
1859   priv->persona_tables = g_hash_table_new (NULL, NULL);
1860   priv->individual_table = NULL;
1861
1862   /* Create widgets */
1863   details_set_up (self);
1864
1865   g_object_unref (gui);
1866 }
1867
1868 static void
1869 constructed (GObject *object)
1870 {
1871   GObjectClass *klass = G_OBJECT_CLASS (empathy_individual_widget_parent_class);
1872   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1873   GtkScrolledWindow *scrolled_window =
1874       GTK_SCROLLED_WINDOW (priv->scrolled_window_individual);
1875
1876   /* Allow scrolling of the list of Personas if we're showing Personas. */
1877   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS)
1878     {
1879       gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
1880       gtk_scrolled_window_set_policy (scrolled_window,
1881           GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1882       gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
1883           priv->scrolled_window_individual, TRUE, TRUE, 0, GTK_PACK_START);
1884
1885       gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1886           6);
1887       gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
1888     }
1889   else
1890     {
1891       gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_NONE);
1892       gtk_scrolled_window_set_policy (scrolled_window,
1893           GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1894       gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
1895           priv->scrolled_window_individual, FALSE, TRUE, 0, GTK_PACK_START);
1896
1897       gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1898           0);
1899     }
1900
1901   /* Chain up */
1902   if (klass->constructed != NULL)
1903     klass->constructed (object);
1904 }
1905
1906 static void
1907 get_property (GObject *object,
1908     guint param_id,
1909     GValue *value,
1910     GParamSpec *pspec)
1911 {
1912   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1913
1914   switch (param_id)
1915     {
1916       case PROP_INDIVIDUAL:
1917         g_value_set_object (value, priv->individual);
1918         break;
1919       case PROP_FLAGS:
1920         g_value_set_flags (value, priv->flags);
1921         break;
1922       default:
1923         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1924         break;
1925     }
1926 }
1927
1928 static void
1929 set_property (GObject *object,
1930     guint param_id,
1931     const GValue *value,
1932     GParamSpec *pspec)
1933 {
1934   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1935
1936   switch (param_id)
1937     {
1938       case PROP_INDIVIDUAL:
1939         empathy_individual_widget_set_individual (
1940             EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
1941         break;
1942       case PROP_FLAGS:
1943         priv->flags = g_value_get_flags (value);
1944         break;
1945       default:
1946         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1947         break;
1948     }
1949 }
1950
1951 static void
1952 dispose (GObject *object)
1953 {
1954   remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
1955
1956   G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
1957 }
1958
1959 static void
1960 finalize (GObject *object)
1961 {
1962   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1963
1964   g_hash_table_destroy (priv->persona_tables);
1965
1966   G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
1967 }
1968
1969 static void
1970 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
1971 {
1972   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1973
1974   object_class->constructed = constructed;
1975   object_class->get_property = get_property;
1976   object_class->set_property = set_property;
1977   object_class->dispose = dispose;
1978   object_class->finalize = finalize;
1979
1980   /**
1981    * EmpathyIndividualWidget:individual:
1982    *
1983    * The #FolksIndividual to display in the widget.
1984    */
1985   g_object_class_install_property (object_class, PROP_INDIVIDUAL,
1986       g_param_spec_object ("individual",
1987           "Individual",
1988           "The #FolksIndividual to display in the widget.",
1989           FOLKS_TYPE_INDIVIDUAL,
1990           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1991
1992   /**
1993    * EmpathyIndividualWidget:flags:
1994    *
1995    * A set of flags which affect the widget's behaviour.
1996    */
1997   g_object_class_install_property (object_class, PROP_FLAGS,
1998       g_param_spec_flags ("flags",
1999           "Flags",
2000           "A set of flags which affect the widget's behaviour.",
2001           EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
2002           EMPATHY_INDIVIDUAL_WIDGET_NONE,
2003           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
2004
2005   g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
2006 }
2007
2008 /**
2009  * empathy_individual_widget_new:
2010  * @individual: the #FolksIndividual to display
2011  * @flags: flags affecting how the widget behaves and what it displays
2012  *
2013  * Creates a new #EmpathyIndividualWidget.
2014  *
2015  * Return value: a new #EmpathyIndividualWidget
2016  */
2017 GtkWidget *
2018 empathy_individual_widget_new (FolksIndividual *individual,
2019     EmpathyIndividualWidgetFlags flags)
2020 {
2021   g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
2022       NULL);
2023
2024   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
2025       "individual", individual,
2026       "flags", flags,
2027       NULL);
2028 }
2029
2030 /**
2031  * empathy_individual_widget_get_individual:
2032  * @self: an #EmpathyIndividualWidget
2033  *
2034  * Returns the #FolksIndividual being displayed by the widget.
2035  *
2036  * Return value: the #FolksIndividual being displayed, or %NULL
2037  */
2038 FolksIndividual *
2039 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
2040 {
2041   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
2042
2043   return GET_PRIV (self)->individual;
2044 }
2045
2046 /**
2047  * empathy_individual_widget_set_individual:
2048  * @self: an #EmpathyIndividualWidget
2049  * @individual: the #FolksIndividual to display, or %NULL
2050  *
2051  * Set the #FolksIndividual to be displayed by the widget:
2052  * #EmpathyIndividualWidget:individual.
2053  *
2054  * The @individual may be %NULL in order to display nothing in the widget.
2055  */
2056 void
2057 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
2058     FolksIndividual *individual)
2059 {
2060   EmpathyIndividualWidgetPriv *priv;
2061
2062   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
2063   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
2064
2065   priv = GET_PRIV (self);
2066
2067   if (individual == priv->individual)
2068     return;
2069
2070   /* Out with the old… */
2071   remove_individual (self);
2072
2073   /* …and in with the new. */
2074   if (individual != NULL)
2075     g_object_ref (individual);
2076   priv->individual = individual;
2077
2078   /* Update information for widgets */
2079   individual_update (self);
2080   groups_update (self);
2081   details_update (self);
2082   location_update (self);
2083 }