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