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