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