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