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