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