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