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