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