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