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