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