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