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