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