]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-widget.c
remove released flag
[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   /* FIXME: Default messages should be moved into libfolks (bgo#627403) */
1298   message = folks_presence_details_get_presence_message (
1299       FOLKS_PRESENCE_DETAILS (folks_object));
1300   if (TPAW_STR_EMPTY (message))
1301     {
1302       message = empathy_presence_get_default_message (presence);
1303     }
1304
1305   if (message != NULL)
1306     markup_text = tpaw_add_link_markup (message);
1307   gtk_label_set_markup (GTK_LABEL (status_label), markup_text);
1308   g_free (markup_text);
1309
1310   gtk_image_set_from_icon_name (GTK_IMAGE (state_image),
1311       empathy_icon_name_for_presence (presence),
1312       GTK_ICON_SIZE_BUTTON);
1313
1314 out:
1315   gtk_widget_set_visible (status_label, visible);
1316   gtk_widget_set_visible (state_image, visible);
1317 }
1318
1319 static void
1320 notify_is_favourite_cb (gpointer folks_object,
1321     GParamSpec *pspec,
1322     EmpathyIndividualWidget *self)
1323 {
1324   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1325   GObject *grid;
1326   GtkWidget *favourite_widget;
1327
1328   if (FOLKS_IS_INDIVIDUAL (folks_object))
1329     grid = G_OBJECT (priv->individual_grid);
1330   else if (FOLKS_IS_PERSONA (folks_object))
1331     grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1332   else
1333     g_assert_not_reached ();
1334
1335   if (grid == NULL)
1336     return;
1337
1338   favourite_widget = g_object_get_data (grid, "favourite-widget");
1339
1340   if (GTK_IS_TOGGLE_BUTTON (favourite_widget))
1341     {
1342       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (favourite_widget),
1343           folks_favourite_details_get_is_favourite (
1344               FOLKS_FAVOURITE_DETAILS (folks_object)));
1345     }
1346 }
1347
1348 static void
1349 alias_presence_avatar_favourite_set_up (EmpathyIndividualWidget *self,
1350     GtkGrid *grid,
1351     guint starting_row)
1352 {
1353   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1354   GtkWidget *label, *alias, *image, *avatar;
1355   guint current_row = starting_row;
1356
1357   /* Alias */
1358   label = gtk_label_new (_("Alias:"));
1359   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1360   gtk_grid_attach (grid, label, 0, current_row, 1, 1);
1361   gtk_widget_show (label);
1362
1363   /* Set up alias label/entry */
1364   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS)
1365     {
1366       alias = gtk_entry_new ();
1367
1368       g_signal_connect (alias, "focus-out-event",
1369           (GCallback) entry_alias_focus_event_cb, self);
1370
1371       /* Make return activate the window default (the Close button) */
1372       gtk_entry_set_activates_default (GTK_ENTRY (alias), TRUE);
1373     }
1374   else
1375     {
1376       alias = gtk_label_new (NULL);
1377       gtk_label_set_selectable (GTK_LABEL (alias),
1378           (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1379       gtk_misc_set_alignment (GTK_MISC (alias), 0.0, 0.5);
1380     }
1381
1382   g_object_set_data (G_OBJECT (grid), "alias-widget", alias);
1383   gtk_grid_attach_next_to (grid, alias, label,
1384                            GTK_POS_RIGHT, 1, 1);
1385   gtk_widget_show (alias);
1386
1387   current_row++;
1388
1389   /* Presence */
1390   priv->hbox_presence = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
1391
1392   /* Presence image */
1393   image = gtk_image_new_from_stock (GTK_STOCK_MISSING_IMAGE,
1394       GTK_ICON_SIZE_BUTTON);
1395   g_object_set_data (G_OBJECT (grid), "state-image", image);
1396   gtk_box_pack_start (GTK_BOX (priv->hbox_presence), image, FALSE,
1397       FALSE, 0);
1398   gtk_widget_show (image);
1399
1400   label = gtk_label_new ("");
1401   gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
1402   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1403   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1404
1405   gtk_label_set_selectable (GTK_LABEL (label),
1406       (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1407
1408   g_object_set_data (G_OBJECT (grid), "status-label", label);
1409   gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, FALSE,
1410       FALSE, 0);
1411   gtk_widget_show (label);
1412
1413   gtk_grid_attach (grid, priv->hbox_presence,
1414                    0, current_row, 2, 1);
1415   gtk_widget_show (priv->hbox_presence);
1416
1417   current_row++;
1418
1419   /* Set up favourite toggle button */
1420   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1421     {
1422       GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1423
1424       g_signal_connect (favourite, "toggled",
1425           (GCallback) favourite_toggled_cb, self);
1426
1427       g_object_set_data (G_OBJECT (grid), "favourite-widget", favourite);
1428       gtk_grid_attach (grid, favourite,
1429                        0, current_row, 2, 1);
1430       gtk_widget_show (favourite);
1431
1432       current_row++;
1433     }
1434
1435   /* Set up avatar display */
1436   avatar = empathy_avatar_image_new ();
1437
1438   if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1439     {
1440       g_signal_connect (avatar, "popup-menu",
1441           (GCallback) avatar_widget_popup_menu_cb, self);
1442       g_signal_connect (avatar, "button-press-event",
1443           (GCallback) avatar_widget_button_press_event_cb, self);
1444     }
1445
1446   g_object_set_data (G_OBJECT (grid), "avatar-widget", avatar);
1447   g_object_set (avatar,
1448                 "valign", GTK_ALIGN_START,
1449                 "margin-left", 6,
1450                 "margin-right", 6,
1451                 "margin-top", 6,
1452                 "margin-bottom", 6,
1453                 NULL);
1454
1455   gtk_grid_attach (grid, avatar,
1456                    2, 0, 1, current_row);
1457   gtk_widget_show (avatar);
1458 }
1459
1460 static void
1461 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1462 {
1463   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1464   TpContact *tp_contact;
1465   EmpathyContact *contact;
1466   TpAccount *account;
1467   GtkGrid *grid;
1468   GtkLabel *label;
1469   GtkImage *image;
1470   const gchar *id;
1471
1472   grid = g_hash_table_lookup (priv->persona_grids, persona);
1473
1474   g_assert (grid != NULL);
1475
1476   tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1477   if (tp_contact == NULL)
1478     return;
1479
1480   contact = empathy_contact_dup_from_tp_contact (tp_contact);
1481   empathy_contact_set_persona (contact, persona);
1482
1483   account = empathy_contact_get_account (contact);
1484
1485   /* Update account widget */
1486   if (account != NULL)
1487     {
1488       const gchar *name;
1489
1490       label = g_object_get_data (G_OBJECT (grid), "account-label");
1491       image = g_object_get_data (G_OBJECT (grid), "account-image");
1492
1493       name = tp_account_get_display_name (account);
1494       gtk_label_set_label (label, name);
1495
1496       name = tp_account_get_icon_name (account);
1497       gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
1498     }
1499
1500   /* Update id widget */
1501   label = g_object_get_data (G_OBJECT (grid), "id-widget");
1502   id = folks_persona_get_display_id (persona);
1503   gtk_label_set_label (label, (id != NULL) ? id : "");
1504
1505   /* Update other widgets */
1506   notify_alias_cb (persona, NULL, self);
1507   notify_presence_cb (persona, NULL, self);
1508   notify_avatar_cb (persona, NULL, self);
1509
1510   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1511     notify_is_favourite_cb (persona, NULL, self);
1512
1513   g_object_unref (contact);
1514 }
1515
1516 static void
1517 add_persona (EmpathyIndividualWidget *self,
1518     FolksPersona *persona)
1519 {
1520   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1521   GtkBox *hbox;
1522   GtkGrid *grid;
1523   GtkWidget *label, *account_label, *account_image, *separator;
1524   guint current_row = 0;
1525
1526   if (!empathy_folks_persona_is_interesting (persona))
1527     return;
1528
1529   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1530     grid = GTK_GRID (gtk_grid_new ());
1531   else
1532     grid = GTK_GRID (gtk_grid_new ());
1533
1534   gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
1535   gtk_grid_set_row_spacing (grid, 6);
1536   gtk_grid_set_column_spacing (grid, 6);
1537
1538   /* Account and Identifier */
1539   label = gtk_label_new (_("Account:"));
1540   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1541   gtk_grid_attach (grid, label, 0, current_row, 1, 1);
1542   gtk_widget_show (label);
1543
1544   /* Pack the protocol icon with the account name in an hbox */
1545   hbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6));
1546
1547   account_label = gtk_label_new (NULL);
1548   gtk_label_set_selectable (GTK_LABEL (account_label),
1549       (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1550   gtk_misc_set_alignment (GTK_MISC (account_label), 0.0, 0.5);
1551   gtk_widget_show (account_label);
1552
1553   account_image = gtk_image_new ();
1554   gtk_widget_show (account_image);
1555
1556   gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1557   gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
1558
1559   g_object_set_data (G_OBJECT (grid), "account-image", account_image);
1560   g_object_set_data (G_OBJECT (grid), "account-label", account_label);
1561   gtk_grid_attach_next_to (grid, GTK_WIDGET (hbox), label, GTK_POS_RIGHT, 1, 1);
1562   gtk_widget_show (GTK_WIDGET (hbox));
1563
1564   current_row++;
1565
1566   /* Translators: Identifier to connect to Instant Messaging network */
1567   label = gtk_label_new (_("Identifier:"));
1568   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1569   gtk_grid_attach (grid, label, 0, current_row, 1, 1);
1570   gtk_widget_show (label);
1571
1572   /* Set up ID label */
1573   label = gtk_label_new (NULL);
1574   gtk_label_set_selectable (GTK_LABEL (label),
1575       (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1576   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1577
1578   g_object_set_data (G_OBJECT (grid), "id-widget", label);
1579   gtk_grid_attach (grid, label, 1, current_row, 1, 1);
1580   gtk_widget_show (label);
1581
1582   current_row++;
1583
1584   alias_presence_avatar_favourite_set_up (self, grid, current_row);
1585
1586   /* Connect to signals and display the grid */
1587   g_signal_connect (persona, "notify::alias",
1588       (GCallback) notify_alias_cb, self);
1589   g_signal_connect (persona, "notify::avatar",
1590       (GCallback) notify_avatar_cb, self);
1591   g_signal_connect (persona, "notify::presence-type",
1592       (GCallback) notify_presence_cb, self);
1593   g_signal_connect (persona, "notify::presence-message",
1594       (GCallback) notify_presence_cb, self);
1595
1596   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1597     {
1598       g_signal_connect (persona, "notify::is-favourite",
1599           (GCallback) notify_is_favourite_cb, self);
1600     }
1601
1602   gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1603       GTK_WIDGET (grid), FALSE, TRUE, 0);
1604   gtk_widget_show (GTK_WIDGET (grid));
1605
1606   /* Pack a separator after the grid */
1607   separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
1608   g_object_set_data (G_OBJECT (grid), "separator", separator);
1609   gtk_box_pack_start (GTK_BOX (priv->vbox_individual), separator, FALSE, FALSE,
1610       0);
1611   gtk_widget_show (separator);
1612
1613   g_hash_table_replace (priv->persona_grids, persona, grid);
1614
1615   /* Update the new widgets */
1616   update_persona (self, persona);
1617 }
1618
1619 static void
1620 remove_persona (EmpathyIndividualWidget *self,
1621     FolksPersona *persona)
1622 {
1623   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1624   GtkWidget *separator;
1625   GtkGrid *grid;
1626
1627   if (!empathy_folks_persona_is_interesting (persona))
1628     return;
1629
1630   grid = g_hash_table_lookup (priv->persona_grids, persona);
1631   if (grid == NULL)
1632     return;
1633
1634   g_signal_handlers_disconnect_by_func (persona, notify_alias_cb, self);
1635   g_signal_handlers_disconnect_by_func (persona, notify_avatar_cb, self);
1636   g_signal_handlers_disconnect_by_func (persona, notify_presence_cb, self);
1637
1638   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1639     {
1640       g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
1641           self);
1642     }
1643
1644   /* Remove the separator */
1645   separator = g_object_get_data (G_OBJECT (grid), "separator");
1646   if (separator != NULL)
1647     gtk_container_remove (GTK_CONTAINER (priv->vbox_individual), separator);
1648
1649   /* Remove the widget */
1650   gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1651       GTK_WIDGET (grid));
1652
1653   g_hash_table_remove (priv->persona_grids, persona);
1654 }
1655
1656 static void
1657 update_individual_grid (EmpathyIndividualWidget *self)
1658 {
1659   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1660
1661   notify_alias_cb (priv->individual, NULL, self);
1662   notify_presence_cb (priv->individual, NULL, self);
1663   notify_avatar_cb (priv->individual, NULL, self);
1664
1665   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1666     notify_is_favourite_cb (priv->individual, NULL, self);
1667 }
1668
1669 static void
1670 individual_grid_set_up (EmpathyIndividualWidget *self)
1671 {
1672   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1673   guint current_row = 0;
1674   GtkGrid *grid;
1675
1676   grid = GTK_GRID (gtk_grid_new ());
1677   gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
1678   gtk_grid_set_row_spacing (grid, 6);
1679   gtk_grid_set_column_spacing (grid, 6);
1680
1681   /* We only display the number of personas in tooltips */
1682   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1683     {
1684       gchar *message;
1685       GtkWidget *label;
1686       GeeSet *personas;
1687       GeeIterator *iter;
1688       guint num_personas = 0;
1689
1690       /* Meta-contacts message displaying how many Telepathy personas we have */
1691       personas = folks_individual_get_personas (priv->individual);
1692       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1693       while (gee_iterator_next (iter))
1694         {
1695           FolksPersona *persona = gee_iterator_get (iter);
1696           if (empathy_folks_persona_is_interesting (persona))
1697             num_personas++;
1698
1699           g_clear_object (&persona);
1700         }
1701       g_clear_object (&iter);
1702
1703       /* Translators: the plurality applies to both instances of the word
1704        * "contact" */
1705       message = g_strdup_printf (
1706           ngettext ("Linked contact containing %u contact",
1707               "Linked contacts containing %u contacts", num_personas),
1708           num_personas);
1709       label = gtk_label_new (message);
1710       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1711       g_free (message);
1712
1713       gtk_grid_attach (grid, label, 0, current_row, 2, 1);
1714       gtk_widget_show (label);
1715
1716       current_row++;
1717     }
1718
1719   alias_presence_avatar_favourite_set_up (self, grid, current_row);
1720
1721   /* Display the grid */
1722   gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (grid),
1723       FALSE, TRUE, 0);
1724   gtk_widget_show (GTK_WIDGET (grid));
1725
1726   priv->individual_grid = grid;
1727
1728   /* Update the grid */
1729   update_individual_grid (self);
1730 }
1731
1732 static void
1733 individual_grid_destroy (EmpathyIndividualWidget *self)
1734 {
1735   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1736
1737   if (priv->individual_grid == NULL)
1738     return;
1739
1740   gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1741       GTK_WIDGET (priv->individual_grid));
1742
1743 #ifdef HAVE_LIBCHAMPLAIN
1744   if (priv->map_view_embed != NULL)
1745     {
1746       gtk_container_remove (GTK_CONTAINER (priv->viewport_map),
1747           priv->map_view_embed);
1748
1749       priv->map_view_embed = NULL;
1750     }
1751 #endif
1752
1753   priv->individual_grid = NULL;
1754 }
1755
1756 static void
1757 personas_changed_cb (FolksIndividual *individual,
1758     GeeSet *added,
1759     GeeSet *removed,
1760     EmpathyIndividualWidget *self)
1761 {
1762   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1763   GList *l, *children;
1764   GeeSet *personas;
1765   GeeIterator *iter;
1766   gboolean show_personas, was_showing_personas, will_show_personas, is_last;
1767   guint old_num_personas, new_num_personas = 0;
1768
1769   personas = folks_individual_get_personas (individual);
1770   /* we'll re-use this iterator throughout */
1771   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1772
1773   /* Note that old_num_personas is the number of persona gridss we were
1774    * displaying, not the number of Personas which were in the Individual
1775    * before. */
1776   old_num_personas = g_hash_table_size (priv->persona_grids);
1777
1778   while (gee_iterator_next (iter))
1779     {
1780       FolksPersona *persona = gee_iterator_get (iter);
1781       if (empathy_folks_persona_is_interesting (persona))
1782         new_num_personas++;
1783
1784       g_clear_object (&persona);
1785     }
1786
1787   /*
1788    * What we display for various conditions:
1789    *  - "Personas": display the alias, avatar, presence account and identifier
1790    *                for each of the Individual's Personas. (i.e. One grid per
1791    *                Persona.)
1792    *  - "Individual": display the alias, avatar and presence for the Individual,
1793    *                  and a label saying "Meta-contact containing x contacts".
1794    *                  (i.e. One grid in total.)
1795    *
1796    *              | SHOW_PERSONAS | !SHOW_PERSONAS
1797    * -------------+---------------+---------------
1798    * > 1 Persona  | Personas      | Individual
1799    * -------------+---------------+---------------
1800    * == 1 Persona | Personas      | Personas
1801    */
1802   show_personas = (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) != 0;
1803   was_showing_personas = show_personas || old_num_personas == 1;
1804   will_show_personas = show_personas || new_num_personas == 1;
1805
1806   /* If both @added and @removed are NULL, we're being called manually, and we
1807    * need to set up the gridss for the first time. We do this simply by
1808    * ensuring was_showing_personas and will_show_personas are different so that
1809    * the code resets the UI.
1810    */
1811   if (added == NULL && removed == NULL)
1812     was_showing_personas = !will_show_personas;
1813
1814   if (was_showing_personas && will_show_personas)
1815     {
1816       GeeIterator *iter_changed;
1817
1818       /* Remove outdated Personas */
1819       iter_changed = gee_iterable_iterator (GEE_ITERABLE (removed));
1820       while (gee_iterator_next (iter_changed))
1821         {
1822           FolksPersona *persona = gee_iterator_get (iter_changed);
1823           remove_persona (self, persona);
1824           g_clear_object (&persona);
1825         }
1826       g_clear_object (&iter_changed);
1827
1828       /* Add new Personas */
1829       iter_changed = gee_iterable_iterator (GEE_ITERABLE (added));
1830       while (gee_iterator_next (iter_changed))
1831         {
1832           FolksPersona *persona = gee_iterator_get (iter_changed);
1833           add_persona (self, persona);
1834           g_clear_object (&persona);
1835         }
1836       g_clear_object (&iter_changed);
1837     }
1838   else if (!was_showing_personas && will_show_personas)
1839     {
1840       /* Remove the old Individual grid */
1841       individual_grid_destroy (self);
1842
1843       /* Set up all the Persona grids instead */
1844       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1845       while (gee_iterator_next (iter))
1846         {
1847           FolksPersona *persona = gee_iterator_get (iter);
1848           add_persona (self, persona);
1849           g_clear_object (&persona);
1850         }
1851     }
1852   else if (was_showing_personas && !will_show_personas)
1853     {
1854       /* Remove all Personas */
1855       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1856       while (gee_iterator_next (iter))
1857         {
1858           FolksPersona *persona = gee_iterator_get (iter);
1859           remove_persona (self, persona);
1860           g_clear_object (&persona);
1861         }
1862
1863       if (removed != NULL)
1864         {
1865           GeeIterator *iter_changed;
1866
1867           iter_changed = gee_iterable_iterator (GEE_ITERABLE (removed));
1868           while (gee_iterator_next (iter_changed))
1869             {
1870               FolksPersona *persona = gee_iterator_get (iter_changed);
1871               remove_persona (self, persona);
1872               g_clear_object (&persona);
1873             }
1874           g_clear_object (&iter_changed);
1875         }
1876
1877       /* Set up the Individual grid instead */
1878       individual_grid_set_up (self);
1879     }
1880   g_clear_object (&iter);
1881
1882   /* Hide the last separator and show the others */
1883   children = gtk_container_get_children (GTK_CONTAINER (priv->vbox_individual));
1884   children = g_list_reverse (children);
1885   is_last = TRUE;
1886
1887   for (l = children; l != NULL; l = l->next)
1888     {
1889       if (GTK_IS_SEPARATOR (l->data))
1890         {
1891           gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1892           is_last = FALSE;
1893         }
1894     }
1895
1896   g_list_free (children);
1897 }
1898
1899 static void
1900 individual_removed_cb (FolksIndividual *individual,
1901     FolksIndividual *replacement_individual,
1902     EmpathyIndividualWidget *self)
1903 {
1904   empathy_individual_widget_set_individual (self, replacement_individual);
1905 }
1906
1907 static void
1908 remove_individual (EmpathyIndividualWidget *self)
1909 {
1910   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1911   if (priv->individual != NULL)
1912     {
1913       GeeSet *personas;
1914       GeeIterator *iter;
1915
1916       g_signal_handlers_disconnect_by_func (priv->individual,
1917           notify_alias_cb, self);
1918       g_signal_handlers_disconnect_by_func (priv->individual,
1919           notify_presence_cb, self);
1920       g_signal_handlers_disconnect_by_func (priv->individual,
1921           notify_avatar_cb, self);
1922       g_signal_handlers_disconnect_by_func (priv->individual,
1923           personas_changed_cb, self);
1924       g_signal_handlers_disconnect_by_func (priv->individual,
1925           individual_removed_cb, self);
1926
1927       if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1928         {
1929           g_signal_handlers_disconnect_by_func (priv->individual,
1930               notify_is_favourite_cb, self);
1931         }
1932
1933       personas = folks_individual_get_personas (priv->individual);
1934       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1935       while (gee_iterator_next (iter))
1936         {
1937           FolksPersona *persona = gee_iterator_get (iter);
1938           remove_persona (self, persona);
1939           g_clear_object (&persona);
1940         }
1941       g_clear_object (&iter);
1942       individual_grid_destroy (self);
1943
1944       if (priv->contact != NULL)
1945         remove_weak_contact (self);
1946
1947       tp_clear_object (&priv->individual);
1948     }
1949
1950   if (priv->details_cancellable != NULL)
1951     g_cancellable_cancel (priv->details_cancellable);
1952 }
1953
1954 static void
1955 individual_update (EmpathyIndividualWidget *self)
1956 {
1957   EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1958
1959   /* Connect and get info from new Individual */
1960   if (priv->individual != NULL)
1961     {
1962       g_signal_connect (priv->individual, "notify::alias",
1963           (GCallback) notify_alias_cb, self);
1964       g_signal_connect (priv->individual, "notify::presence-type",
1965           (GCallback) notify_presence_cb, self);
1966       g_signal_connect (priv->individual, "notify::presence-message",
1967           (GCallback) notify_presence_cb, self);
1968       g_signal_connect (priv->individual, "notify::avatar",
1969           (GCallback) notify_avatar_cb, self);
1970       g_signal_connect (priv->individual, "personas-changed",
1971           (GCallback) personas_changed_cb, self);
1972       g_signal_connect (priv->individual, "removed",
1973           (GCallback) individual_removed_cb, self);
1974
1975       if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1976         {
1977           g_signal_connect (priv->individual, "notify::is-favourite",
1978               (GCallback) notify_is_favourite_cb, self);
1979         }
1980
1981       /* Update individual grid */
1982       personas_changed_cb (priv->individual, NULL, NULL, self);
1983     }
1984
1985   if (priv->individual == NULL)
1986     {
1987       gtk_widget_hide (priv->vbox_individual);
1988     }
1989   else if (priv->individual_grid != NULL)
1990     {
1991       /* We only need to update the details for the Individual as a whole */
1992       update_individual_grid (self);
1993       gtk_widget_show (priv->vbox_individual);
1994     }
1995   else
1996     {
1997       /* We need to update the details for every Persona in the Individual */
1998       GeeSet *personas;
1999       GeeIterator *iter;
2000
2001       personas = folks_individual_get_personas (priv->individual);
2002       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2003       while (gee_iterator_next (iter))
2004         {
2005           FolksPersona *persona = gee_iterator_get (iter);
2006
2007           if (empathy_folks_persona_is_interesting (persona))
2008             update_persona (self, persona);
2009
2010           g_clear_object (&persona);
2011         }
2012       g_clear_object (&iter);
2013
2014       gtk_widget_show (priv->vbox_individual);
2015     }
2016 }
2017
2018 static void
2019 empathy_individual_widget_init (EmpathyIndividualWidget *self)
2020 {
2021   EmpathyIndividualWidgetPriv *priv;
2022   GtkBuilder *gui;
2023   gchar *filename;
2024
2025   priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
2026       EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
2027   self->priv = priv;
2028
2029   gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
2030       GTK_ORIENTATION_VERTICAL);
2031
2032   filename = empathy_file_lookup ("empathy-individual-widget.ui",
2033       "libempathy-gtk");
2034   gui = tpaw_builder_get_file (filename,
2035       "scrolled_window_individual", &priv->scrolled_window_individual,
2036       "viewport_individual", &priv->viewport_individual,
2037       "vbox_individual_widget", &priv->vbox_individual_widget,
2038       "vbox_individual", &priv->vbox_individual,
2039       "vbox_location", &priv->vbox_location,
2040       "subvbox_location", &priv->subvbox_location,
2041       "label_location", &priv->label_location,
2042 #ifdef HAVE_LIBCHAMPLAIN
2043       "viewport_map", &priv->viewport_map,
2044 #endif
2045       "groups_widget", &priv->groups_widget,
2046       "vbox_details", &priv->vbox_details,
2047       "grid_details", &priv->grid_details,
2048       "hbox_details_requested", &priv->hbox_details_requested,
2049       "hbox_client_types", &priv->hbox_client_types,
2050       NULL);
2051   g_free (filename);
2052
2053   priv->grid_location = NULL;
2054
2055   gtk_box_pack_start (GTK_BOX (self), priv->vbox_individual_widget, TRUE, TRUE,
2056       0);
2057   gtk_widget_show (priv->vbox_individual_widget);
2058
2059   priv->persona_grids = g_hash_table_new (NULL, NULL);
2060   priv->individual_grid = NULL;
2061
2062   /* Create widgets */
2063   details_set_up (self);
2064
2065   g_object_unref (gui);
2066 }
2067
2068 static void
2069 constructed (GObject *object)
2070 {
2071   GObjectClass *klass = G_OBJECT_CLASS (empathy_individual_widget_parent_class);
2072   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2073   GtkScrolledWindow *scrolled_window =
2074       GTK_SCROLLED_WINDOW (priv->scrolled_window_individual);
2075
2076   /* Allow scrolling of the list of Personas if we're showing Personas. */
2077   if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS)
2078     {
2079       gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
2080       gtk_scrolled_window_set_policy (scrolled_window,
2081           GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
2082       gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
2083           priv->scrolled_window_individual, TRUE, TRUE, 0, GTK_PACK_START);
2084
2085       gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
2086           6);
2087       gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
2088     }
2089   else
2090     {
2091       gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_NONE);
2092       gtk_scrolled_window_set_policy (scrolled_window,
2093           GTK_POLICY_NEVER, GTK_POLICY_NEVER);
2094       gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
2095           priv->scrolled_window_individual, FALSE, TRUE, 0, GTK_PACK_START);
2096
2097       gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
2098           0);
2099     }
2100
2101   /* Chain up */
2102   if (klass->constructed != NULL)
2103     klass->constructed (object);
2104 }
2105
2106 static void
2107 get_property (GObject *object,
2108     guint param_id,
2109     GValue *value,
2110     GParamSpec *pspec)
2111 {
2112   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2113
2114   switch (param_id)
2115     {
2116       case PROP_INDIVIDUAL:
2117         g_value_set_object (value, priv->individual);
2118         break;
2119       case PROP_FLAGS:
2120         g_value_set_flags (value, priv->flags);
2121         break;
2122       default:
2123         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2124         break;
2125     }
2126 }
2127
2128 static void
2129 set_property (GObject *object,
2130     guint param_id,
2131     const GValue *value,
2132     GParamSpec *pspec)
2133 {
2134   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2135
2136   switch (param_id)
2137     {
2138       case PROP_INDIVIDUAL:
2139         empathy_individual_widget_set_individual (
2140             EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
2141         break;
2142       case PROP_FLAGS:
2143         priv->flags = g_value_get_flags (value);
2144         break;
2145       default:
2146         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2147         break;
2148     }
2149 }
2150
2151 static void
2152 dispose (GObject *object)
2153 {
2154   remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
2155
2156   G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
2157 }
2158
2159 static void
2160 finalize (GObject *object)
2161 {
2162   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2163
2164   g_hash_table_unref (priv->persona_grids);
2165
2166   G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
2167 }
2168
2169 static void
2170 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
2171 {
2172   GObjectClass *object_class = G_OBJECT_CLASS (klass);
2173
2174   object_class->constructed = constructed;
2175   object_class->get_property = get_property;
2176   object_class->set_property = set_property;
2177   object_class->dispose = dispose;
2178   object_class->finalize = finalize;
2179
2180   /**
2181    * EmpathyIndividualWidget:individual:
2182    *
2183    * The #FolksIndividual to display in the widget.
2184    */
2185   g_object_class_install_property (object_class, PROP_INDIVIDUAL,
2186       g_param_spec_object ("individual",
2187           "Individual",
2188           "The #FolksIndividual to display in the widget.",
2189           FOLKS_TYPE_INDIVIDUAL,
2190           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2191
2192   /**
2193    * EmpathyIndividualWidget:flags:
2194    *
2195    * A set of flags which affect the widget's behaviour.
2196    */
2197   g_object_class_install_property (object_class, PROP_FLAGS,
2198       g_param_spec_flags ("flags",
2199           "Flags",
2200           "A set of flags which affect the widget's behaviour.",
2201           EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
2202           EMPATHY_INDIVIDUAL_WIDGET_NONE,
2203           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
2204
2205   g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
2206 }
2207
2208 /**
2209  * empathy_individual_widget_new:
2210  * @individual: the #FolksIndividual to display
2211  * @flags: flags affecting how the widget behaves and what it displays
2212  *
2213  * Creates a new #EmpathyIndividualWidget.
2214  *
2215  * Return value: a new #EmpathyIndividualWidget
2216  */
2217 GtkWidget *
2218 empathy_individual_widget_new (FolksIndividual *individual,
2219     EmpathyIndividualWidgetFlags flags)
2220 {
2221   g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
2222       NULL);
2223
2224   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
2225       "individual", individual,
2226       "flags", flags,
2227       NULL);
2228 }
2229
2230 /**
2231  * empathy_individual_widget_get_individual:
2232  * @self: an #EmpathyIndividualWidget
2233  *
2234  * Returns the #FolksIndividual being displayed by the widget.
2235  *
2236  * Return value: the #FolksIndividual being displayed, or %NULL
2237  */
2238 FolksIndividual *
2239 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
2240 {
2241   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
2242
2243   return GET_PRIV (self)->individual;
2244 }
2245
2246 /**
2247  * empathy_individual_widget_set_individual:
2248  * @self: an #EmpathyIndividualWidget
2249  * @individual: the #FolksIndividual to display, or %NULL
2250  *
2251  * Set the #FolksIndividual to be displayed by the widget:
2252  * #EmpathyIndividualWidget:individual.
2253  *
2254  * The @individual may be %NULL in order to display nothing in the widget.
2255  */
2256 void
2257 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
2258     FolksIndividual *individual)
2259 {
2260   EmpathyIndividualWidgetPriv *priv;
2261
2262   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
2263   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
2264
2265   priv = GET_PRIV (self);
2266
2267   if (individual == priv->individual)
2268     return;
2269
2270   /* Out with the old… */
2271   remove_individual (self);
2272
2273   /* …and in with the new. */
2274   if (individual != NULL)
2275     g_object_ref (individual);
2276   priv->individual = individual;
2277
2278   /* Update information for widgets */
2279   individual_update (self);
2280   groups_update (self);
2281   details_update (self);
2282   location_update (self);
2283   client_types_update (self);
2284 }