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