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