]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-widget.c
contactinfo-utils: move from Empathy to tp-account-widgets
[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
30 #ifdef HAVE_LIBCHAMPLAIN
31 #include <champlain/champlain.h>
32 #include <champlain-gtk/champlain-gtk.h>
33 #endif
34
35 #include "empathy-avatar-image.h"
36 #include "empathy-groups-widget.h"
37 #include "empathy-gtk-enum-types.h"
38 #include "empathy-location.h"
39 #include "empathy-request-util.h"
40 #include "empathy-ui-utils.h"
41 #include "empathy-utils.h"
42
43 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
44 #include "empathy-debug.h"
45
46 /**
47  * SECTION:empathy-individual-widget
48  * @title:EmpathyIndividualWidget
49  * @short_description: A widget used to display and edit details about an
50  * individual
51  * @include: libempathy-empathy-individual-widget.h
52  *
53  * #EmpathyIndividualWidget is a widget which displays appropriate widgets
54  * with details about an individual, also allowing changing these details,
55  * if desired.
56  */
57
58 /**
59  * EmpathyIndividualWidget:
60  * @parent: parent object
61  *
62  * Widget which displays appropriate widgets with details about an individual,
63  * also allowing changing these details, if desired.
64  */
65
66 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualWidget)
67
68 typedef struct {
69   FolksIndividual *individual; /* owned */
70   EmpathyIndividualWidgetFlags flags;
71
72   /* weak pointer to the contact whose contact details we're displaying */
73   TpContact *contact;
74
75   /* unowned Persona (borrowed from priv->individual) -> GtkGrid child */
76   GHashTable *persona_grids;
77   /* Table containing the information for the individual as whole, or NULL */
78   GtkGrid *individual_grid;
79
80   /* Individual */
81   GtkWidget *hbox_presence;
82   GtkWidget *vbox_individual_widget;
83   GtkWidget *scrolled_window_individual;
84   GtkWidget *viewport_individual;
85   GtkWidget *vbox_individual;
86
87   /* Location */
88   GtkWidget *vbox_location;
89   GtkWidget *subvbox_location;
90   GtkWidget *grid_location;
91   GtkWidget *label_location;
92 #ifdef HAVE_LIBCHAMPLAIN
93   GtkWidget *viewport_map;
94   GtkWidget *map_view_embed;
95   ChamplainView *map_view;
96 #endif
97
98   /* Groups */
99   GtkWidget *groups_widget;
100
101   /* Client types */
102   GtkWidget *hbox_client_types;
103
104   /* Details */
105   GtkWidget *vbox_details;
106   GtkWidget *grid_details;
107   GtkWidget *hbox_details_requested;
108   GtkWidget *details_spinner;
109   GCancellable *details_cancellable; /* owned */
110 } EmpathyIndividualWidgetPriv;
111
112 G_DEFINE_TYPE (EmpathyIndividualWidget, empathy_individual_widget,
113     GTK_TYPE_BOX);
114
115 enum {
116   PROP_INDIVIDUAL = 1,
117   PROP_FLAGS
118 };
119
120 static void client_types_update (EmpathyIndividualWidget *self);
121 static void remove_weak_contact (EmpathyIndividualWidget *self);
122
123 static void
124 details_set_up (EmpathyIndividualWidget *self)
125 {
126   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
127
128   gtk_widget_hide (priv->vbox_details);
129
130   priv->details_spinner = gtk_spinner_new ();
131   gtk_box_pack_end (GTK_BOX (priv->hbox_details_requested),
132       priv->details_spinner, TRUE, TRUE, 0);
133   gtk_widget_show (priv->details_spinner);
134 }
135
136 static void
137 client_types_notify_cb (TpContact *contact,
138     GParamSpec *pspec,
139     EmpathyIndividualWidget *self)
140 {
141   client_types_update (self);
142 }
143
144 typedef struct {
145   EmpathyIndividualWidget *widget; /* weak */
146   TpContact *contact; /* owned */
147 } DetailsData;
148
149 static void
150 update_weak_contact (EmpathyIndividualWidget *self)
151 {
152   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
153   TpContact *tp_contact = NULL;
154
155   remove_weak_contact (self);
156
157   if (priv->individual != NULL)
158     {
159       /* FIXME: We take the most available TpContact we find and only
160        * use its details. It would be a lot better if we would get the
161        * details for every TpContact in the Individual and merge them
162        * all, but that requires vCard support in libfolks for it to
163        * not be hideously complex.  (bgo#627399) */
164       GeeSet *personas;
165       GeeIterator *iter;
166       FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
167
168       personas = folks_individual_get_personas (priv->individual);
169       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
170       while (gee_iterator_next (iter))
171         {
172           FolksPersona *persona = gee_iterator_get (iter);
173
174           /* We only want personas which have presence and a TpContact */
175           if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
176             {
177               FolksPresenceDetails *presence;
178               FolksPresenceType presence_type_cur;
179
180               presence = FOLKS_PRESENCE_DETAILS (persona);
181               presence_type_cur = folks_presence_details_get_presence_type (
182                   presence);
183
184               if (tp_contact == NULL || folks_presence_details_typecmp (
185                     presence_type_cur, presence_type) > 0)
186                 {
187                   presence_type = presence_type_cur;
188                   tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
189                 }
190             }
191
192           g_clear_object (&persona);
193         }
194       g_clear_object (&iter);
195     }
196
197   if (tp_contact != NULL)
198     {
199       priv->contact = tp_contact;
200       g_object_add_weak_pointer (G_OBJECT (tp_contact),
201           (gpointer *) &priv->contact);
202
203       g_signal_connect (priv->contact, "notify::client-types",
204           (GCallback) client_types_notify_cb, self);
205     }
206 }
207
208 static void
209 add_row (GtkGrid *grid,
210     guint row,
211     GtkWidget *title,
212     GtkWidget *value)
213 {
214   gtk_grid_attach (grid, title, 0, row, 1, 1);
215   gtk_misc_set_alignment (GTK_MISC (title), 0, 0.5);
216   gtk_widget_show (title);
217
218   gtk_grid_attach (grid, value, 1, row, 1, 1);
219   gtk_misc_set_alignment (GTK_MISC (value), 0, 0.5);
220   gtk_widget_show (value);
221 }
222
223 static gboolean
224 channel_name_activated_cb (
225     GtkLabel *label,
226     gchar *uri,
227     TpAccount *account)
228 {
229   empathy_join_muc (account, uri, empathy_get_current_action_time ());
230   return TRUE;
231 }
232
233 static GtkWidget *
234 create_channel_list_label (TpAccount *account,
235     GList *info,
236     guint row)
237 {
238   GtkWidget *label = NULL;
239   GString *label_markup = g_string_new ("");
240   guint i;
241   GPtrArray *channels;
242   GList *l;
243
244   /* Is there channels? */
245   channels = g_ptr_array_new ();
246
247   for (l = info; l != NULL; l = l->next)
248     {
249       TpContactInfoField *field = l->data;
250
251       if (!tp_strdiff (field->field_name, "x-irc-channel"))
252         g_ptr_array_add (channels, (gpointer) field->field_value[0]);
253     }
254
255   if (channels->len == 0)
256     goto out;
257
258   for (i = 0; i < channels->len; i++)
259     {
260       const gchar *channel_name = g_ptr_array_index (channels, i);
261       /* We abuse the URI of the link to hold the channel name. It seems to
262        * be okay to just use it essentially verbatim, rather than trying to
263        * ensure it's actually a valid URI. */
264       gchar *escaped = g_markup_escape_text (channel_name, -1);
265
266       if (i > 0)
267         g_string_append (label_markup, ", ");
268
269       g_string_append_printf (label_markup, "<a href='%s'>%s</a>",
270           escaped, escaped);
271       g_free (escaped);
272     }
273
274   label = gtk_label_new (NULL);
275   gtk_label_set_markup (GTK_LABEL (label), label_markup->str);
276   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
277
278   g_signal_connect (label, "activate-link",
279       (GCallback) channel_name_activated_cb, account);
280
281 out:
282   g_ptr_array_unref (channels);
283   g_string_free (label_markup, TRUE);
284
285   return label;
286 }
287 static guint
288 details_update_show (EmpathyIndividualWidget *self,
289     TpContact *contact)
290 {
291   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
292   GList *info, *l;
293   guint n_rows = 0;
294   GtkWidget *channels_label;
295   TpConnection *conn;
296   TpAccount *account;
297
298   info = tp_contact_dup_contact_info (contact);
299   info = g_list_sort (info, (GCompareFunc) tpaw_contact_info_field_cmp);
300   for (l = info; l != NULL; l = l->next)
301     {
302       TpContactInfoField *field = l->data;
303       gchar *title;
304       const gchar *value;
305       TpawContactInfoFormatFunc format;
306       GtkWidget *title_widget, *value_widget;
307
308       if (field->field_value == NULL || field->field_value[0] == NULL)
309         continue;
310
311       value = field->field_value[0];
312
313       if (!tpaw_contact_info_lookup_field (field->field_name,
314           NULL, &format))
315         {
316           DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
317           continue;
318         }
319
320       /* Skip empty field */
321       if (tp_str_empty (value))
322         continue;
323
324       /* Add Title */
325       title = tpaw_contact_info_field_label (field->field_name,
326           field->parameters, TRUE);
327       title_widget = gtk_label_new (title);
328
329       /* Add Value */
330       value_widget = gtk_label_new (value);
331
332       if (format != NULL)
333         {
334           gchar *markup;
335
336           markup = format (field->field_value);
337           gtk_label_set_markup (GTK_LABEL (value_widget), markup);
338           g_free (markup);
339         }
340
341       gtk_label_set_selectable (GTK_LABEL (value_widget),
342           (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
343
344       add_row (GTK_GRID (priv->grid_details), n_rows, title_widget,
345           value_widget);
346
347       n_rows++;
348     }
349
350   conn = tp_contact_get_connection (contact);
351   account = tp_connection_get_account (conn);
352
353   channels_label = create_channel_list_label (account, info, n_rows);
354
355   if (channels_label != NULL)
356     {
357       GtkWidget *title_widget;
358
359       title_widget =  gtk_label_new (_("Channels:"));
360
361       add_row (GTK_GRID (priv->grid_details), n_rows, title_widget,
362           channels_label);
363
364       n_rows++;
365     }
366
367   tp_contact_info_list_free (info);
368
369   return n_rows;
370 }
371
372 static void
373 details_notify_cb (TpContact *contact,
374     GParamSpec *pspec,
375     EmpathyIndividualWidget *self)
376 {
377   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
378   guint n_rows;
379
380   gtk_container_foreach (GTK_CONTAINER (priv->grid_details),
381       (GtkCallback) gtk_widget_destroy, NULL);
382
383   n_rows = details_update_show (self, contact);
384
385   if (n_rows > 0)
386     {
387       gtk_widget_show (priv->vbox_details);
388       gtk_widget_show (priv->grid_details);
389     }
390   else
391     {
392       gtk_widget_hide (priv->vbox_details);
393     }
394
395   gtk_widget_hide (priv->hbox_details_requested);
396   gtk_spinner_stop (GTK_SPINNER (priv->details_spinner));
397 }
398
399 static void
400 details_request_cb (GObject *source,
401     GAsyncResult *res,
402     gpointer user_data)
403 {
404   EmpathyIndividualWidget *self = user_data;
405   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
406   TpContact *contact = (TpContact *) source;
407   gboolean hide_widget = FALSE;
408   GError *error = NULL;
409
410   if (tp_contact_request_contact_info_finish (contact, res, &error) == TRUE)
411     {
412       details_notify_cb (contact, NULL, self);
413     }
414   else
415     {
416       /* If the request got cancelled it could mean the contact widget is
417        * destroyed, so we should not dereference information */
418       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
419         {
420             g_error_free (error);
421             return;
422         }
423
424       hide_widget = TRUE;
425       g_error_free (error);
426     }
427
428   if (hide_widget == TRUE)
429     gtk_widget_hide (GET_PRIV (self)->vbox_details);
430
431   tp_clear_object (&priv->details_cancellable);
432
433   tp_g_signal_connect_object (contact, "notify::contact-info",
434       (GCallback) details_notify_cb, self, 0);
435 }
436
437 static void
438 fetch_contact_information (EmpathyIndividualWidget *self)
439 {
440   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
441   TpConnection *connection;
442
443   connection = tp_contact_get_connection (priv->contact);
444
445   if (!tp_proxy_has_interface_by_id (connection,
446           TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_INFO))
447     {
448       gtk_widget_hide (GET_PRIV (self)->vbox_details);
449       return;
450     }
451
452   /* Request the Individual's info */
453   gtk_widget_show (priv->vbox_details);
454   gtk_widget_show (priv->hbox_details_requested);
455   gtk_widget_hide (priv->grid_details);
456   gtk_spinner_start (GTK_SPINNER (priv->details_spinner));
457
458   if (priv->details_cancellable == NULL)
459     {
460       priv->details_cancellable = g_cancellable_new ();
461
462       tp_contact_request_contact_info_async (priv->contact,
463           priv->details_cancellable, details_request_cb, self);
464     }
465 }
466
467 static void
468 details_update (EmpathyIndividualWidget *self)
469 {
470   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
471
472   if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS))
473     return;
474
475   gtk_widget_hide (priv->vbox_details);
476
477   if (priv->contact == NULL)
478     update_weak_contact (self);
479
480   if (priv->contact != NULL)
481     {
482       fetch_contact_information (self);
483     }
484 }
485
486 static void
487 groups_update (EmpathyIndividualWidget *self)
488 {
489   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
490
491   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS &&
492       priv->individual != NULL)
493     {
494       empathy_groups_widget_set_group_details (
495           EMPATHY_GROUPS_WIDGET (priv->groups_widget),
496           FOLKS_GROUP_DETAILS (priv->individual));
497       gtk_widget_show (priv->groups_widget);
498     }
499   else
500     {
501       gtk_widget_hide (priv->groups_widget);
502     }
503 }
504
505 /* Converts the Location's GHashTable's key to a user readable string */
506 static const gchar *
507 location_key_to_label (const gchar *key)
508 {
509   if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
510     return _("Country ISO Code:");
511   else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
512     return _("Country:");
513   else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
514     return _("State:");
515   else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
516     return _("City:");
517   else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
518     return _("Area:");
519   else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
520     return _("Postal Code:");
521   else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
522     return _("Street:");
523   else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
524     return _("Building:");
525   else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
526     return _("Floor:");
527   else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
528     return _("Room:");
529   else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
530     return _("Text:");
531   else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
532     return _("Description:");
533   else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
534     return _("URI:");
535   else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
536     return _("Accuracy Level:");
537   else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
538     return _("Error:");
539   else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
540     return _("Vertical Error (meters):");
541   else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
542     return _("Horizontal Error (meters):");
543   else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
544     return _("Speed:");
545   else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
546     return _("Bearing:");
547   else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
548     return _("Climb Speed:");
549   else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
550     return _("Last Updated on:");
551   else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
552     return _("Longitude:");
553   else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
554     return _("Latitude:");
555   else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
556     return _("Altitude:");
557   else
558   {
559     DEBUG ("Unexpected Location key: %s", key);
560     return key;
561   }
562 }
563
564 static void
565 location_update (EmpathyIndividualWidget *self)
566 {
567   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
568   EmpathyContact *contact = NULL;
569   GHashTable *location = NULL;
570   GValue *value;
571   GtkWidget *label;
572   guint row = 0;
573   static const gchar* ordered_geolocation_keys[] = {
574     EMPATHY_LOCATION_TEXT,
575     EMPATHY_LOCATION_URI,
576     EMPATHY_LOCATION_DESCRIPTION,
577     EMPATHY_LOCATION_BUILDING,
578     EMPATHY_LOCATION_FLOOR,
579     EMPATHY_LOCATION_ROOM,
580     EMPATHY_LOCATION_STREET,
581     EMPATHY_LOCATION_AREA,
582     EMPATHY_LOCATION_LOCALITY,
583     EMPATHY_LOCATION_REGION,
584     EMPATHY_LOCATION_COUNTRY,
585     NULL
586   };
587   int i;
588   const gchar *skey;
589   gboolean display_map = FALSE;
590   GeeSet *personas;
591   GeeIterator *iter;
592
593   if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION) ||
594       priv->individual == NULL)
595     {
596       gtk_widget_hide (priv->vbox_location);
597       return;
598     }
599
600   /* FIXME: For the moment, we just display the first location data we can
601    * find amongst the Individual's Personas. Once libfolks grows a location
602    * interface, we can use that. (bgo#627400) */
603
604   personas = folks_individual_get_personas (priv->individual);
605   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
606   while (location == NULL && gee_iterator_next (iter))
607     {
608       FolksPersona *persona = gee_iterator_get (iter);
609
610       if (empathy_folks_persona_is_interesting (persona))
611         {
612           TpContact *tp_contact;
613
614           /* Get the contact. If it turns out to have location information, we
615            * have to keep it alive for the duration of the function, since we're
616            * accessing its private data. */
617           tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
618           if (tp_contact != NULL)
619             {
620               contact = empathy_contact_dup_from_tp_contact (tp_contact);
621               empathy_contact_set_persona (contact, persona);
622
623               /* Try and get a location */
624               location = empathy_contact_get_location (contact);
625               /* if location isn't fully valid, treat the contact as
626                * insufficient */
627               if (location != NULL && g_hash_table_size (location) <= 0)
628                 {
629                   location = NULL;
630                   g_clear_object (&contact);
631                 }
632             }
633         }
634       g_clear_object (&persona);
635     }
636   g_clear_object (&iter);
637
638   if (contact == NULL || location == NULL)
639     {
640       gtk_widget_hide (priv->vbox_location);
641       tp_clear_object (&contact);
642       return;
643     }
644
645   value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
646   if (value == NULL)
647     {
648       gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
649       gtk_label_set_markup (GTK_LABEL (priv->label_location), loc);
650       g_free (loc);
651     }
652   else
653     {
654       gchar *user_date;
655       gchar *text;
656       gint64 stamp;
657       gchar *tmp;
658
659       stamp = g_value_get_int64 (value);
660
661       user_date = tpaw_time_to_string_relative (stamp);
662
663       tmp = g_strdup_printf ("<b>%s</b>", _("Location"));
664       /* translators: format is "Location, $date" */
665       text = g_strdup_printf (_("%s, %s"), tmp, user_date);
666       g_free (tmp);
667       gtk_label_set_markup (GTK_LABEL (priv->label_location), text);
668       g_free (user_date);
669       g_free (text);
670     }
671
672   /* Prepare the location information table */
673   if (priv->grid_location != NULL)
674     gtk_widget_destroy (priv->grid_location);
675
676   priv->grid_location = gtk_grid_new ();
677   gtk_box_pack_start (GTK_BOX (priv->subvbox_location),
678       priv->grid_location, FALSE, FALSE, 5);
679
680
681   for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
682     {
683       const gchar* user_label;
684       GValue *gvalue;
685       char *svalue = NULL;
686
687       gvalue = g_hash_table_lookup (location, (gpointer) skey);
688       if (gvalue == NULL)
689         continue;
690
691       user_label = location_key_to_label (skey);
692
693       label = gtk_label_new (user_label);
694       gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
695       gtk_grid_attach (GTK_GRID (priv->grid_location),
696           label, 0, row, 1, 1);
697       gtk_widget_show (label);
698
699       if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
700         {
701           gdouble dvalue;
702           dvalue = g_value_get_double (gvalue);
703           svalue = g_strdup_printf ("%f", dvalue);
704         }
705       else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
706         {
707           svalue = g_value_dup_string (gvalue);
708         }
709       else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
710         {
711           gint64 time_;
712
713           time_ = g_value_get_int64 (value);
714           svalue = tpaw_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
715         }
716
717       if (svalue != NULL)
718         {
719           label = gtk_label_new (svalue);
720           gtk_grid_attach (GTK_GRID (priv->grid_location),
721               label, 1, row, 1, 1);
722           gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
723           gtk_widget_show (label);
724
725           gtk_label_set_selectable (GTK_LABEL (label),
726               (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE :
727                   TRUE);
728         }
729
730       g_free (svalue);
731       row++;
732     }
733
734   tp_clear_object (&contact);
735
736 #ifdef HAVE_LIBCHAMPLAIN
737   if ((g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL) &&
738       (g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL))
739     {
740       /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
741        * windows */
742       display_map = TRUE;
743     }
744 #endif
745
746   if (row > 0)
747     {
748       /* We can display some fields */
749       gtk_widget_show (priv->grid_location);
750     }
751   else if (display_map == FALSE)
752     {
753       /* Can't display either fields or map */
754       gtk_widget_hide (priv->vbox_location);
755       return;
756     }
757
758 #ifdef HAVE_LIBCHAMPLAIN
759   if (display_map == TRUE)
760     {
761       ChamplainMarkerLayer *layer;
762
763       priv->map_view_embed = gtk_champlain_embed_new ();
764       priv->map_view = gtk_champlain_embed_get_view (
765           GTK_CHAMPLAIN_EMBED (priv->map_view_embed));
766
767       gtk_container_add (GTK_CONTAINER (priv->viewport_map),
768           priv->map_view_embed);
769       g_object_set (G_OBJECT (priv->map_view),
770           "kinetic-mode", TRUE,
771           "zoom-level", 10,
772           NULL);
773
774       layer = champlain_marker_layer_new ();
775       champlain_view_add_layer (priv->map_view, CHAMPLAIN_LAYER (layer));
776
777       /* FIXME: For now, we have to do this manually. Once libfolks grows a
778        * location interface, we can use that. (bgo#627400) */
779
780       personas = folks_individual_get_personas (priv->individual);
781       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
782       while (gee_iterator_next (iter))
783         {
784           FolksPersona *persona = gee_iterator_get (iter);
785
786           if (empathy_folks_persona_is_interesting (persona))
787             {
788               gdouble lat = 0.0, lon = 0.0;
789               ClutterActor *marker;
790               TpContact *tp_contact;
791
792               /* Get the contact */
793               tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
794               if (tp_contact == NULL)
795                 goto while_finish;
796
797               contact = empathy_contact_dup_from_tp_contact (tp_contact);
798               empathy_contact_set_persona (contact, persona);
799
800               /* Try and get a location */
801               location = empathy_contact_get_location (contact);
802               if (location == NULL || g_hash_table_size (location) == 0)
803                 goto while_finish;
804
805               /* Get this persona's latitude and longitude */
806               value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
807               if (value == NULL)
808                 goto while_finish;
809
810               lat = g_value_get_double (value);
811
812               value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
813               if (value == NULL)
814                 goto while_finish;
815
816               lon = g_value_get_double (value);
817
818               /* Add a marker to the map */
819               marker = champlain_label_new_with_text (
820                   folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona)),
821                   NULL, NULL, NULL);
822               champlain_location_set_location (CHAMPLAIN_LOCATION (marker),
823                   lat, lon);
824               champlain_marker_layer_add_marker (layer,
825                   CHAMPLAIN_MARKER (marker));
826             }
827
828 while_finish:
829           g_clear_object (&persona);
830           g_clear_object (&contact);
831         }
832       g_clear_object (&iter);
833
834       /* Zoom to show all of the markers */
835       champlain_view_ensure_layers_visible (priv->map_view, FALSE);
836
837       gtk_widget_show_all (priv->viewport_map);
838     }
839 #endif
840
841     gtk_widget_show (priv->vbox_location);
842 }
843
844 static void
845 client_types_update (EmpathyIndividualWidget *self)
846 {
847   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
848   const gchar * const *types;
849
850   if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_CLIENT_TYPES) ||
851       priv->individual == NULL)
852     {
853       gtk_widget_hide (priv->hbox_client_types);
854       return;
855     }
856
857   if (priv->contact == NULL)
858     update_weak_contact (self);
859
860   /* let's try that again... */
861   if (priv->contact == NULL)
862     return;
863
864   types = tp_contact_get_client_types (priv->contact);
865
866   if (empathy_client_types_contains_mobile_device ((GStrv) types))
867     {
868       gtk_widget_show (priv->hbox_client_types);
869     }
870   else
871     {
872       gtk_widget_hide (priv->hbox_client_types);
873     }
874
875 }
876
877 static void
878 remove_weak_contact (EmpathyIndividualWidget *self)
879 {
880   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
881
882   if (priv->contact == NULL)
883     return;
884
885   g_signal_handlers_disconnect_by_func (priv->contact, client_types_notify_cb,
886       self);
887
888   g_object_remove_weak_pointer (G_OBJECT (priv->contact),
889       (gpointer *) &priv->contact);
890   priv->contact = NULL;
891 }
892
893 static EmpathyAvatar *
894 persona_dup_avatar (FolksPersona *persona)
895 {
896   TpContact *tp_contact;
897   EmpathyContact *contact;
898   EmpathyAvatar *avatar;
899
900   if (!empathy_folks_persona_is_interesting (persona))
901     return NULL;
902
903   tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
904   if (tp_contact == NULL)
905     return NULL;
906
907   contact = empathy_contact_dup_from_tp_contact (tp_contact);
908   empathy_contact_set_persona (contact, persona);
909
910   avatar = empathy_contact_get_avatar (contact);
911   if (avatar != NULL)
912     empathy_avatar_ref (avatar);
913   g_object_unref (contact);
914
915   return avatar;
916 }
917
918 static EmpathyAvatar *
919 individual_dup_avatar (FolksIndividual *individual)
920 {
921   GeeSet *personas;
922   GeeIterator *iter;
923   EmpathyAvatar *avatar = NULL;
924
925   /* FIXME: We just choose the first Persona which has an avatar, and save that.
926    * The avatar handling in EmpathyContact needs to be moved into libfolks as
927    * much as possible, and this code rewritten to use FolksHasAvatar.
928    * (bgo#627401) */
929
930   personas = folks_individual_get_personas (individual);
931   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
932   while (avatar == NULL && gee_iterator_next (iter))
933     {
934       FolksPersona *persona = gee_iterator_get (iter);
935       avatar = persona_dup_avatar (persona);
936
937       g_clear_object (&persona);
938     }
939   g_clear_object (&iter);
940
941   return avatar;
942 }
943
944 static void
945 save_avatar_menu_activate_cb (GtkWidget *widget,
946     EmpathyIndividualWidget *self)
947 {
948   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
949   GtkWidget *dialog;
950   EmpathyAvatar *avatar;
951   gchar *ext = NULL, *filename;
952
953   dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
954       NULL,
955       GTK_FILE_CHOOSER_ACTION_SAVE,
956       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
957       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
958       NULL);
959
960   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
961       TRUE);
962
963   avatar = individual_dup_avatar (priv->individual);
964   if (avatar == NULL)
965     return;
966
967   /* look for the avatar extension */
968   if (avatar->format != NULL)
969     {
970       gchar **splitted;
971
972       splitted = g_strsplit (avatar->format, "/", 2);
973       if (splitted[0] != NULL && splitted[1] != NULL)
974           ext = g_strdup (splitted[1]);
975
976       g_strfreev (splitted);
977     }
978   else
979     {
980       /* Avatar was loaded from the cache so was converted to PNG */
981       ext = g_strdup ("png");
982     }
983
984   if (ext != NULL)
985     {
986       gchar *id;
987
988       id = tp_escape_as_identifier (folks_individual_get_id (priv->individual));
989
990       filename = g_strdup_printf ("%s.%s", id, ext);
991       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
992
993       g_free (id);
994       g_free (ext);
995       g_free (filename);
996     }
997
998   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
999     {
1000       GError *error = NULL;
1001
1002       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1003
1004       if (empathy_avatar_save_to_file (avatar, filename, &error) == FALSE)
1005         {
1006           /* Save error */
1007           GtkWidget *error_dialog;
1008
1009           error_dialog = gtk_message_dialog_new (NULL, 0,
1010               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1011               _("Unable to save avatar"));
1012
1013           gtk_message_dialog_format_secondary_text (
1014               GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
1015
1016           g_signal_connect (error_dialog, "response",
1017               (GCallback) gtk_widget_destroy, NULL);
1018
1019           gtk_window_present (GTK_WINDOW (error_dialog));
1020
1021           g_clear_error (&error);
1022         }
1023
1024       g_free (filename);
1025     }
1026
1027   gtk_widget_destroy (dialog);
1028   empathy_avatar_unref (avatar);
1029 }
1030
1031 static gboolean
1032 popup_avatar_menu (EmpathyIndividualWidget *self,
1033     GtkWidget *parent,
1034     GdkEventButton *event)
1035 {
1036   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1037   GtkWidget *menu, *item;
1038   EmpathyAvatar *avatar;
1039   gint button, event_time;
1040
1041   if (priv->individual == NULL)
1042     return FALSE;
1043
1044   avatar = individual_dup_avatar (priv->individual);
1045   if (avatar == NULL)
1046     return FALSE;
1047   empathy_avatar_unref (avatar);
1048
1049   menu = empathy_context_menu_new (parent);
1050
1051   /* Add "Save as..." entry */
1052   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
1053   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1054   gtk_widget_show (item);
1055
1056   g_signal_connect (item, "activate",
1057       (GCallback) save_avatar_menu_activate_cb, self);
1058
1059   if (event != NULL)
1060     {
1061       button = event->button;
1062       event_time = event->time;
1063     }
1064   else
1065     {
1066       button = 0;
1067       event_time = gtk_get_current_event_time ();
1068     }
1069
1070   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, button, event_time);
1071
1072   return TRUE;
1073 }
1074
1075 static gboolean
1076 avatar_widget_popup_menu_cb (GtkWidget *widget,
1077     EmpathyIndividualWidget *self)
1078 {
1079   return popup_avatar_menu (self, widget, NULL);
1080 }
1081
1082 static gboolean
1083 avatar_widget_button_press_event_cb (GtkWidget *widget,
1084     GdkEventButton *event,
1085     EmpathyIndividualWidget *self)
1086 {
1087   /* Ignore double-clicks and triple-clicks */
1088   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1089     return popup_avatar_menu (self, widget, event);
1090
1091   return FALSE;
1092 }
1093
1094 /* Returns the TpAccount for the user as a convenience. Note that it has a ref
1095  * added. */
1096 static TpAccount *
1097 individual_is_user (FolksIndividual *individual)
1098 {
1099   GeeSet *personas;
1100   GeeIterator *iter;
1101   TpAccount *retval = NULL;
1102
1103   /* FIXME: This should move into libfolks when libfolks grows a way of
1104    * determining "self". (bgo#627402) */
1105   personas = folks_individual_get_personas (individual);
1106   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1107   while (gee_iterator_next (iter))
1108     {
1109       FolksPersona *persona = gee_iterator_get (iter);
1110
1111       if (TPF_IS_PERSONA (persona))
1112         {
1113           TpContact *tp_contact;
1114           EmpathyContact *contact = NULL;
1115
1116           /* Get the contact */
1117           tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1118           if (tp_contact != NULL)
1119             {
1120               contact = empathy_contact_dup_from_tp_contact (tp_contact);
1121               empathy_contact_set_persona (contact, persona);
1122
1123               /* Determine if the contact is the user */
1124               if (empathy_contact_is_user (contact))
1125                 retval = g_object_ref (empathy_contact_get_account (contact));
1126             }
1127
1128           g_object_unref (contact);
1129         }
1130       g_clear_object (&persona);
1131     }
1132   g_clear_object (&iter);
1133
1134   return retval;
1135 }
1136
1137 static void
1138 set_nickname_cb (TpAccount *account,
1139     GAsyncResult *result,
1140     gpointer user_data)
1141 {
1142   GError *error = NULL;
1143
1144   if (tp_account_set_nickname_finish (account, result, &error) == FALSE)
1145     {
1146       DEBUG ("Failed to set Account.Nickname: %s", error->message);
1147       g_error_free (error);
1148     }
1149 }
1150
1151 static gboolean
1152 entry_alias_focus_event_cb (GtkEditable *editable,
1153     GdkEventFocus *event,
1154     EmpathyIndividualWidget *self)
1155 {
1156   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1157
1158   if (priv->individual != NULL)
1159     {
1160       const gchar *alias;
1161       TpAccount *account;
1162
1163       alias = gtk_entry_get_text (GTK_ENTRY (editable));
1164       account = individual_is_user (priv->individual);
1165
1166       if (account != NULL)
1167         {
1168           DEBUG ("Set Account.Nickname to %s", alias);
1169           tp_account_set_nickname_async (account, alias,
1170               (GAsyncReadyCallback) set_nickname_cb, NULL);
1171           g_object_unref (account);
1172         }
1173       else
1174         {
1175           folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (priv->individual),
1176               alias);
1177         }
1178     }
1179
1180   return FALSE;
1181 }
1182
1183 static void
1184 favourite_toggled_cb (GtkToggleButton *button,
1185     EmpathyIndividualWidget *self)
1186 {
1187   gboolean active = gtk_toggle_button_get_active (button);
1188   folks_favourite_details_set_is_favourite (
1189       FOLKS_FAVOURITE_DETAILS (GET_PRIV (self)->individual), active);
1190 }
1191
1192 static void
1193 notify_avatar_cb (gpointer folks_object,
1194     GParamSpec *pspec,
1195     EmpathyIndividualWidget *self)
1196 {
1197   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1198   EmpathyAvatar *avatar = NULL;
1199   GObject *grid;
1200   GtkWidget *avatar_widget;
1201
1202   if (FOLKS_IS_INDIVIDUAL (folks_object))
1203     {
1204       avatar = individual_dup_avatar (FOLKS_INDIVIDUAL (folks_object));
1205       grid = G_OBJECT (priv->individual_grid);
1206     }
1207   else if (FOLKS_IS_PERSONA (folks_object))
1208     {
1209       avatar = persona_dup_avatar (FOLKS_PERSONA (folks_object));
1210       grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1211     }
1212   else
1213     {
1214       g_assert_not_reached ();
1215     }
1216
1217   if (grid == NULL)
1218     return;
1219
1220   avatar_widget = g_object_get_data (grid, "avatar-widget");
1221   empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (avatar_widget), avatar);
1222
1223   if (avatar != NULL)
1224     empathy_avatar_unref (avatar);
1225 }
1226
1227 static void
1228 notify_alias_cb (gpointer folks_object,
1229     GParamSpec *pspec,
1230     EmpathyIndividualWidget *self)
1231 {
1232   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1233   GObject *grid;
1234   GtkWidget *alias_widget;
1235
1236   if (FOLKS_IS_INDIVIDUAL (folks_object))
1237     grid = G_OBJECT (priv->individual_grid);
1238   else if (FOLKS_IS_PERSONA (folks_object))
1239     grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1240   else
1241     g_assert_not_reached ();
1242
1243   if (grid == NULL)
1244     return;
1245
1246   alias_widget = g_object_get_data (grid, "alias-widget");
1247
1248   if (GTK_IS_ENTRY (alias_widget))
1249     {
1250       gtk_entry_set_text (GTK_ENTRY (alias_widget),
1251           folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (folks_object)));
1252     }
1253   else
1254     {
1255       gtk_label_set_label (GTK_LABEL (alias_widget),
1256           folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (folks_object)));
1257     }
1258 }
1259
1260 static void
1261 notify_presence_cb (gpointer folks_object,
1262     GParamSpec *pspec,
1263     EmpathyIndividualWidget *self)
1264 {
1265   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1266   GObject *grid;
1267   GtkWidget *status_label, *state_image;
1268   const gchar *message;
1269   gchar *markup_text = NULL;
1270   FolksPresenceType presence;
1271   gboolean visible = TRUE;
1272
1273   if (FOLKS_IS_INDIVIDUAL (folks_object))
1274     grid = G_OBJECT (priv->individual_grid);
1275   else if (FOLKS_IS_PERSONA (folks_object))
1276     grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1277   else
1278     g_assert_not_reached ();
1279
1280   if (grid == NULL)
1281     return;
1282
1283   status_label = g_object_get_data (grid, "status-label");
1284   state_image = g_object_get_data (grid, "state-image");
1285
1286   presence = folks_presence_details_get_presence_type (
1287       FOLKS_PRESENCE_DETAILS (folks_object));
1288   if (presence == FOLKS_PRESENCE_TYPE_UNKNOWN ||
1289       presence == FOLKS_PRESENCE_TYPE_ERROR)
1290     {
1291       /* Don't display anything if we don't know the presence */
1292       visible = FALSE;
1293       goto out;
1294     }
1295
1296   /* FIXME: Default messages should be moved into libfolks (bgo#627403) */
1297   message = folks_presence_details_get_presence_message (
1298       FOLKS_PRESENCE_DETAILS (folks_object));
1299   if (EMP_STR_EMPTY (message))
1300     {
1301       message = empathy_presence_get_default_message (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 }