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