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