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