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