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