2 * Copyright (C) 2007-2010 Collabora Ltd.
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.
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.
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
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Philip Withnall <philip.withnall@collabora.co.uk>
28 #include <glib/gi18n-lib.h>
30 #include <telepathy-glib/util.h>
32 #include <folks/folks.h>
33 #include <folks/folks-telepathy.h>
35 #ifdef HAVE_LIBCHAMPLAIN
36 #include <champlain/champlain.h>
37 #include <champlain-gtk/champlain-gtk.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-location.h>
42 #include <libempathy/empathy-time.h>
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"
51 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
52 #include <libempathy/empathy-debug.h>
55 * SECTION:empathy-individual-widget
56 * @title:EmpathyIndividualWidget
57 * @short_description: A widget used to display and edit details about an
59 * @include: libempathy-empathy-individual-widget.h
61 * #EmpathyIndividualWidget is a widget which displays appropriate widgets
62 * with details about an individual, also allowing changing these details,
67 * EmpathyIndividualWidget:
68 * @parent: parent object
70 * Widget which displays appropriate widgets with details about an individual,
71 * also allowing changing these details, if desired.
74 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualWidget)
77 FolksIndividual *individual; /* owned */
78 EmpathyIndividualWidgetFlags flags;
80 /* weak pointer to the contact whose contact details we're displaying */
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;
89 GtkWidget *hbox_presence;
90 GtkWidget *vbox_individual_widget;
91 GtkWidget *scrolled_window_individual;
92 GtkWidget *viewport_individual;
93 GtkWidget *vbox_individual;
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;
107 GtkWidget *groups_widget;
110 GtkWidget *hbox_client_types;
113 GtkWidget *vbox_details;
114 GtkWidget *table_details;
115 GtkWidget *hbox_details_requested;
116 GtkWidget *details_spinner;
117 GCancellable *details_cancellable; /* owned */
118 } EmpathyIndividualWidgetPriv;
120 G_DEFINE_TYPE (EmpathyIndividualWidget, empathy_individual_widget,
128 static void client_types_update (EmpathyIndividualWidget *self);
129 static void remove_weak_contact (EmpathyIndividualWidget *self);
132 details_set_up (EmpathyIndividualWidget *self)
134 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
136 gtk_widget_hide (priv->vbox_details);
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);
146 const gchar *field_name;
151 static InfoFieldData info_field_data[] =
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 },
161 static InfoFieldData *
162 find_info_field_data (const gchar *field_name)
166 for (i = 0; info_field_data[i].field_name != NULL; i++)
168 if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
169 return info_field_data + i;
175 contact_info_field_name_cmp (const gchar *name1,
180 if (tp_strdiff (name1, name2) == FALSE)
183 /* We use the order of info_field_data */
184 for (i = 0; info_field_data[i].field_name != NULL; i++)
186 if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
188 if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
192 return g_strcmp0 (name1, name2);
196 contact_info_field_cmp (TpContactInfoField *field1,
197 TpContactInfoField *field2)
199 return contact_info_field_name_cmp (field1->field_name, field2->field_name);
203 update_weak_contact (EmpathyIndividualWidget *self)
205 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
206 TpContact *tp_contact = NULL;
208 remove_weak_contact (self);
210 if (priv->individual != NULL)
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) */
218 FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
220 personas = folks_individual_get_personas (priv->individual);
221 for (l = personas; l != NULL; l = l->next)
223 FolksPresence *presence;
225 /* We only want personas which implement FolksPresence */
226 if (!FOLKS_IS_PRESENCE (l->data))
229 presence = FOLKS_PRESENCE (l->data);
231 if (folks_presence_typecmp (folks_presence_get_presence_type (presence),
233 && TPF_IS_PERSONA (presence))
235 presence_type = folks_presence_get_presence_type (presence);
236 tp_contact = tpf_persona_get_contact (TPF_PERSONA (l->data));
241 if (tp_contact != NULL)
243 priv->contact = tp_contact;
244 g_object_add_weak_pointer (G_OBJECT (tp_contact),
245 (gpointer *) &priv->contact);
250 EmpathyIndividualWidget *widget; /* weak */
251 TpContact *contact; /* owned */
255 details_data_free (DetailsData *data)
257 if (data->widget != NULL)
259 g_object_remove_weak_pointer (G_OBJECT (data->widget),
260 (gpointer *) &data->widget);
262 g_object_unref (data->contact);
263 g_slice_free (DetailsData, data);
267 details_update_show (EmpathyIndividualWidget *self,
270 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
274 info = tp_contact_get_contact_info (contact);
275 info = g_list_sort (info, (GCompareFunc) contact_info_field_cmp);
276 for (l = info; l != NULL; l = l->next)
278 TpContactInfoField *field = l->data;
279 InfoFieldData *field_data;
283 if (field->field_value == NULL || field->field_value[0] == NULL)
286 value = field->field_value[0];
288 field_data = find_info_field_data (field->field_name);
289 if (field_data == NULL)
291 DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
296 w = gtk_label_new (_(field_data->title));
297 gtk_table_attach (GTK_TABLE (priv->table_details),
298 w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
299 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
303 w = gtk_label_new (value);
304 if (field_data->linkify == TRUE)
308 markup = empathy_add_link_markup (value);
309 gtk_label_set_markup (GTK_LABEL (w), markup);
313 gtk_label_set_selectable (GTK_LABEL (w),
314 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
316 gtk_table_attach_defaults (GTK_TABLE (priv->table_details),
317 w, 1, 2, n_rows, n_rows + 1);
318 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
329 details_notify_cb (TpContact *contact,
331 EmpathyIndividualWidget *self)
333 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
336 gtk_container_foreach (GTK_CONTAINER (priv->table_details),
337 (GtkCallback) gtk_widget_destroy, NULL);
339 n_rows = details_update_show (self, contact);
343 gtk_widget_show (priv->vbox_details);
344 gtk_widget_show (priv->table_details);
348 gtk_widget_hide (priv->vbox_details);
351 gtk_widget_hide (priv->hbox_details_requested);
352 gtk_spinner_stop (GTK_SPINNER (priv->details_spinner));
356 details_request_cb (TpContact *contact,
360 EmpathyIndividualWidget *self = data->widget;
361 gboolean hide_widget = FALSE;
362 GError *error = NULL;
364 if (tp_contact_request_contact_info_finish (contact, res, &error) == TRUE)
365 details_notify_cb (contact, NULL, self);
369 g_clear_error (&error);
373 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
375 if (hide_widget == TRUE)
376 gtk_widget_hide (GET_PRIV (self)->vbox_details);
378 tp_clear_object (&priv->details_cancellable);
380 g_signal_connect (contact, "notify::contact-info",
381 (GCallback) details_notify_cb, self);
384 details_data_free (data);
388 details_feature_prepared_cb (TpConnection *connection,
392 EmpathyIndividualWidget *self = data->widget;
393 EmpathyIndividualWidgetPriv *priv = NULL;
395 if (tp_proxy_prepare_finish (connection, res, NULL) == FALSE || self == NULL)
398 gtk_widget_hide (GET_PRIV (self)->vbox_details);
399 details_data_free (data);
403 priv = GET_PRIV (self);
405 /* Request the Individual's info */
406 gtk_widget_show (priv->vbox_details);
407 gtk_widget_show (priv->hbox_details_requested);
408 gtk_widget_hide (priv->table_details);
409 gtk_spinner_start (GTK_SPINNER (priv->details_spinner));
411 if (priv->details_cancellable == NULL)
413 priv->details_cancellable = g_cancellable_new ();
414 tp_contact_request_contact_info_async (data->contact,
415 priv->details_cancellable, (GAsyncReadyCallback) details_request_cb,
421 details_update (EmpathyIndividualWidget *self)
423 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
424 TpContact *tp_contact = NULL;
426 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS))
429 gtk_widget_hide (priv->vbox_details);
431 if (priv->contact == NULL)
432 update_weak_contact (self);
434 if (priv->contact != NULL)
436 GQuark features[] = { TP_CONNECTION_FEATURE_CONTACT_INFO, 0 };
437 TpConnection *connection;
440 data = g_slice_new (DetailsData);
442 g_object_add_weak_pointer (G_OBJECT (self), (gpointer *) &data->widget);
443 data->contact = g_object_ref (priv->contact);
445 /* First, make sure the CONTACT_INFO feature is ready on the connection */
446 connection = tp_contact_get_connection (tp_contact);
447 tp_proxy_prepare_async (connection, features,
448 (GAsyncReadyCallback) details_feature_prepared_cb, data);
453 groups_update (EmpathyIndividualWidget *self)
455 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
457 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS &&
458 priv->individual != NULL)
460 empathy_groups_widget_set_groupable (
461 EMPATHY_GROUPS_WIDGET (priv->groups_widget),
462 FOLKS_GROUPABLE (priv->individual));
463 gtk_widget_show (priv->groups_widget);
467 gtk_widget_hide (priv->groups_widget);
471 /* Converts the Location's GHashTable's key to a user readable string */
473 location_key_to_label (const gchar *key)
475 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
476 return _("Country ISO Code:");
477 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
478 return _("Country:");
479 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
481 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
483 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
485 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
486 return _("Postal Code:");
487 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
489 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
490 return _("Building:");
491 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
493 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
495 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
497 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
498 return _("Description:");
499 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
501 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
502 return _("Accuracy Level:");
503 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
505 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
506 return _("Vertical Error (meters):");
507 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
508 return _("Horizontal Error (meters):");
509 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
511 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
512 return _("Bearing:");
513 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
514 return _("Climb Speed:");
515 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
516 return _("Last Updated on:");
517 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
518 return _("Longitude:");
519 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
520 return _("Latitude:");
521 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
522 return _("Altitude:");
525 DEBUG ("Unexpected Location key: %s", key);
531 location_update (EmpathyIndividualWidget *self)
533 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
534 EmpathyContact *contact = NULL;
535 GHashTable *location = NULL;
539 static const gchar* ordered_geolocation_keys[] = {
540 EMPATHY_LOCATION_TEXT,
541 EMPATHY_LOCATION_URI,
542 EMPATHY_LOCATION_DESCRIPTION,
543 EMPATHY_LOCATION_BUILDING,
544 EMPATHY_LOCATION_FLOOR,
545 EMPATHY_LOCATION_ROOM,
546 EMPATHY_LOCATION_STREET,
547 EMPATHY_LOCATION_AREA,
548 EMPATHY_LOCATION_LOCALITY,
549 EMPATHY_LOCATION_REGION,
550 EMPATHY_LOCATION_COUNTRY,
555 gboolean display_map = FALSE;
558 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION) ||
559 priv->individual == NULL)
561 gtk_widget_hide (priv->vbox_location);
565 /* FIXME: For the moment, we just display the first location data we can
566 * find amongst the Individual's Personas. Once libfolks grows a location
567 * interface, we can use that. (bgo#627400) */
568 personas = folks_individual_get_personas (priv->individual);
569 for (l = personas; l != NULL; l = l->next)
571 FolksPersona *persona = FOLKS_PERSONA (l->data);
573 if (TPF_IS_PERSONA (persona))
575 TpContact *tp_contact;
577 /* Get the contact. If it turns out to have location information, we
578 * have to keep it alive for the duration of the function, since we're
579 * accessing its private data. */
580 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
581 contact = empathy_contact_dup_from_tp_contact (tp_contact);
582 empathy_contact_set_persona (contact, persona);
584 /* Try and get a location */
585 location = empathy_contact_get_location (contact);
586 if (location != NULL && g_hash_table_size (location) > 0)
590 tp_clear_object (&contact);
594 if (contact == NULL || location == NULL)
596 gtk_widget_hide (priv->vbox_location);
597 tp_clear_object (&contact);
601 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
604 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
605 gtk_label_set_markup (GTK_LABEL (priv->label_location), loc);
616 stamp = g_value_get_int64 (value);
619 user_date = empathy_time_to_string_relative (time_);
621 tmp = g_strdup_printf ("<b>%s</b>", _("Location"));
622 /* translators: format is "Location, $date" */
623 text = g_strdup_printf (_("%s, %s"), tmp, user_date);
625 gtk_label_set_markup (GTK_LABEL (priv->label_location), text);
630 /* Prepare the location information table */
631 if (priv->table_location != NULL)
632 gtk_widget_destroy (priv->table_location);
634 priv->table_location = gtk_table_new (1, 2, FALSE);
635 gtk_box_pack_start (GTK_BOX (priv->subvbox_location),
636 priv->table_location, FALSE, FALSE, 5);
639 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
641 const gchar* user_label;
645 gvalue = g_hash_table_lookup (location, (gpointer) skey);
649 user_label = location_key_to_label (skey);
651 label = gtk_label_new (user_label);
652 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
653 gtk_table_attach (GTK_TABLE (priv->table_location),
654 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
655 gtk_widget_show (label);
657 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
660 dvalue = g_value_get_double (gvalue);
661 svalue = g_strdup_printf ("%f", dvalue);
663 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
665 svalue = g_value_dup_string (gvalue);
667 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
671 time_ = g_value_get_int64 (value);
672 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
677 label = gtk_label_new (svalue);
678 gtk_table_attach_defaults (GTK_TABLE (priv->table_location),
679 label, 1, 2, row, row + 1);
680 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
681 gtk_widget_show (label);
683 gtk_label_set_selectable (GTK_LABEL (label),
684 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE :
692 tp_clear_object (&contact);
694 #ifdef HAVE_LIBCHAMPLAIN
695 if ((g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL) &&
696 (g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL) &&
697 !(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
699 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
707 /* We can display some fields */
708 gtk_widget_show (priv->table_location);
710 else if (display_map == FALSE)
712 /* Can't display either fields or map */
713 gtk_widget_hide (priv->vbox_location);
717 #ifdef HAVE_LIBCHAMPLAIN
718 if (display_map == TRUE)
721 ChamplainLayer *layer;
723 priv->map_view_embed = gtk_champlain_embed_new ();
724 priv->map_view = gtk_champlain_embed_get_view (
725 GTK_CHAMPLAIN_EMBED (priv->map_view_embed));
727 gtk_container_add (GTK_CONTAINER (priv->viewport_map),
728 priv->map_view_embed);
729 g_object_set (G_OBJECT (priv->map_view),
730 "show-license", TRUE,
731 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
735 layer = champlain_layer_new ();
736 champlain_view_add_layer (priv->map_view, layer);
737 markers = g_ptr_array_new ();
739 /* FIXME: For now, we have to do this manually. Once libfolks grows a
740 * location interface, we can use that. (bgo#627400) */
741 personas = folks_individual_get_personas (priv->individual);
742 for (l = personas; l != NULL; l = l->next)
744 FolksPersona *persona = FOLKS_PERSONA (l->data);
746 if (TPF_IS_PERSONA (persona))
748 gdouble lat = 0.0, lon = 0.0;
749 ClutterActor *marker;
750 TpContact *tp_contact;
752 /* Get the contact */
753 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
754 contact = empathy_contact_dup_from_tp_contact (tp_contact);
755 empathy_contact_set_persona (contact, persona);
757 /* Try and get a location */
758 location = empathy_contact_get_location (contact);
759 if (location == NULL || g_hash_table_size (location) == 0)
761 g_object_unref (contact);
765 /* Get this persona's latitude and longitude */
766 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
769 g_object_unref (contact);
773 lat = g_value_get_double (value);
775 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
778 g_object_unref (contact);
782 lon = g_value_get_double (value);
784 /* Add a marker to the map */
785 marker = champlain_marker_new_with_text (
786 folks_aliasable_get_alias (FOLKS_ALIASABLE (persona)), NULL,
788 champlain_base_marker_set_position (
789 CHAMPLAIN_BASE_MARKER (marker), lat, lon);
790 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
792 g_ptr_array_add (markers, marker);
794 g_object_unref (contact);
798 /* Zoom to show all of the markers */
799 g_ptr_array_add (markers, NULL); /* NULL-terminate the array */
800 champlain_view_ensure_markers_visible (priv->map_view,
801 (ChamplainBaseMarker **) markers->pdata, FALSE);
802 g_ptr_array_free (markers, TRUE);
804 gtk_widget_show_all (priv->viewport_map);
808 gtk_widget_show (priv->vbox_location);
812 client_types_notify_cb (TpContact *contact,
814 EmpathyIndividualWidget *self)
816 client_types_update (self);
820 client_types_update (EmpathyIndividualWidget *self)
822 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
823 const gchar * const *types;
825 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_CLIENT_TYPES) ||
826 priv->individual == NULL)
828 gtk_widget_hide (priv->hbox_client_types);
832 if (priv->contact == NULL)
833 update_weak_contact (self);
835 /* let's try that again... */
836 if (priv->contact == NULL)
839 types = tp_contact_get_client_types (priv->contact);
842 && g_strv_length ((gchar **) types) > 0
843 && !tp_strdiff (types[0], "phone"))
845 gtk_widget_show (priv->hbox_client_types);
849 gtk_widget_hide (priv->hbox_client_types);
852 g_signal_connect (priv->contact, "notify::client-types",
853 (GCallback) client_types_notify_cb, self);
857 remove_weak_contact (EmpathyIndividualWidget *self)
859 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
861 if (priv->contact == NULL)
864 g_object_remove_weak_pointer (G_OBJECT (priv->contact),
865 (gpointer *) &priv->contact);
866 priv->contact = NULL;
869 static EmpathyAvatar *
870 persona_dup_avatar (FolksPersona *persona)
872 TpContact *tp_contact;
873 EmpathyContact *contact;
874 EmpathyAvatar *avatar;
876 if (!TPF_IS_PERSONA (persona))
879 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
880 contact = empathy_contact_dup_from_tp_contact (tp_contact);
881 empathy_contact_set_persona (contact, persona);
883 avatar = empathy_contact_get_avatar (contact);
885 empathy_avatar_ref (avatar);
886 g_object_unref (contact);
891 static EmpathyAvatar *
892 individual_dup_avatar (FolksIndividual *individual)
895 EmpathyAvatar *avatar = NULL;
897 /* FIXME: We just choose the first Persona which has an avatar, and save that.
898 * The avatar handling in EmpathyContact needs to be moved into libfolks as
899 * much as possible, and this code rewritten to use FolksAvatar.
901 personas = folks_individual_get_personas (individual);
902 for (l = personas; l != NULL; l = l->next)
904 avatar = persona_dup_avatar (FOLKS_PERSONA (l->data));
913 save_avatar_menu_activate_cb (GtkWidget *widget,
914 EmpathyIndividualWidget *self)
916 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
918 EmpathyAvatar *avatar;
919 gchar *ext = NULL, *filename;
921 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
923 GTK_FILE_CHOOSER_ACTION_SAVE,
924 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
925 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
928 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
931 avatar = individual_dup_avatar (priv->individual);
935 /* look for the avatar extension */
936 if (avatar->format != NULL)
940 splitted = g_strsplit (avatar->format, "/", 2);
941 if (splitted[0] != NULL && splitted[1] != NULL)
942 ext = g_strdup (splitted[1]);
944 g_strfreev (splitted);
948 /* Avatar was loaded from the cache so was converted to PNG */
949 ext = g_strdup ("png");
956 id = tp_escape_as_identifier (folks_individual_get_id (priv->individual));
958 filename = g_strdup_printf ("%s.%s", id, ext);
959 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
966 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
968 GError *error = NULL;
970 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
972 if (empathy_avatar_save_to_file (avatar, filename, &error) == FALSE)
975 GtkWidget *error_dialog;
977 error_dialog = gtk_message_dialog_new (NULL, 0,
978 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
979 _("Unable to save avatar"));
981 gtk_message_dialog_format_secondary_text (
982 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
984 g_signal_connect (error_dialog, "response",
985 (GCallback) gtk_widget_destroy, NULL);
987 gtk_window_present (GTK_WINDOW (error_dialog));
989 g_clear_error (&error);
995 gtk_widget_destroy (dialog);
996 empathy_avatar_unref (avatar);
1000 popup_avatar_menu (EmpathyIndividualWidget *self,
1002 GdkEventButton *event)
1004 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1005 GtkWidget *menu, *item;
1006 EmpathyAvatar *avatar;
1007 gint button, event_time;
1009 if (priv->individual == NULL)
1012 avatar = individual_dup_avatar (priv->individual);
1015 empathy_avatar_unref (avatar);
1017 menu = gtk_menu_new ();
1019 /* Add "Save as..." entry */
1020 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
1021 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1022 gtk_widget_show (item);
1024 g_signal_connect (item, "activate",
1025 (GCallback) save_avatar_menu_activate_cb, self);
1029 button = event->button;
1030 event_time = event->time;
1035 event_time = gtk_get_current_event_time ();
1038 gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
1039 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, button, event_time);
1040 g_object_ref_sink (menu);
1041 g_object_unref (menu);
1047 avatar_widget_popup_menu_cb (GtkWidget *widget,
1048 EmpathyIndividualWidget *self)
1050 return popup_avatar_menu (self, widget, NULL);
1054 avatar_widget_button_press_event_cb (GtkWidget *widget,
1055 GdkEventButton *event,
1056 EmpathyIndividualWidget *self)
1058 /* Ignore double-clicks and triple-clicks */
1059 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1060 return popup_avatar_menu (self, widget, event);
1065 /* Returns the TpAccount for the user as a convenience. Note that it has a ref
1068 individual_is_user (FolksIndividual *individual)
1070 GList *personas, *l;
1072 /* FIXME: This should move into libfolks when libfolks grows a way of
1073 * determining "self". (bgo#627402) */
1074 personas = folks_individual_get_personas (individual);
1075 for (l = personas; l != NULL; l = l->next)
1077 FolksPersona *persona = FOLKS_PERSONA (l->data);
1079 if (TPF_IS_PERSONA (persona))
1081 TpContact *tp_contact;
1082 EmpathyContact *contact;
1084 /* Get the contact */
1085 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1086 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1087 empathy_contact_set_persona (contact, persona);
1089 /* Determine if the contact is the user */
1090 if (empathy_contact_is_user (contact))
1092 g_object_unref (contact);
1093 return g_object_ref (empathy_contact_get_account (contact));
1096 g_object_unref (contact);
1104 set_nickname_cb (TpAccount *account,
1105 GAsyncResult *result,
1108 GError *error = NULL;
1110 if (tp_account_set_nickname_finish (account, result, &error) == FALSE)
1112 DEBUG ("Failed to set Account.Nickname: %s", error->message);
1113 g_error_free (error);
1118 entry_alias_focus_event_cb (GtkEditable *editable,
1119 GdkEventFocus *event,
1120 EmpathyIndividualWidget *self)
1122 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1124 if (priv->individual != NULL)
1129 alias = gtk_entry_get_text (GTK_ENTRY (editable));
1130 account = individual_is_user (priv->individual);
1132 if (account != NULL)
1134 DEBUG ("Set Account.Nickname to %s", alias);
1135 tp_account_set_nickname_async (account, alias,
1136 (GAsyncReadyCallback) set_nickname_cb, NULL);
1137 g_object_unref (account);
1141 folks_aliasable_set_alias (FOLKS_ALIASABLE (priv->individual), alias);
1149 favourite_toggled_cb (GtkToggleButton *button,
1150 EmpathyIndividualWidget *self)
1152 gboolean active = gtk_toggle_button_get_active (button);
1153 folks_favourite_set_is_favourite (
1154 FOLKS_FAVOURITE (GET_PRIV (self)->individual), active);
1158 notify_avatar_cb (gpointer folks_object,
1160 EmpathyIndividualWidget *self)
1162 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1163 EmpathyAvatar *avatar = NULL;
1165 GtkWidget *avatar_widget;
1167 if (FOLKS_IS_INDIVIDUAL (folks_object))
1169 avatar = individual_dup_avatar (FOLKS_INDIVIDUAL (folks_object));
1170 table = G_OBJECT (priv->individual_table);
1172 else if (FOLKS_IS_PERSONA (folks_object))
1174 avatar = persona_dup_avatar (FOLKS_PERSONA (folks_object));
1175 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1179 g_assert_not_reached ();
1185 avatar_widget = g_object_get_data (table, "avatar-widget");
1186 empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (avatar_widget), avatar);
1189 empathy_avatar_unref (avatar);
1193 notify_alias_cb (gpointer folks_object,
1195 EmpathyIndividualWidget *self)
1197 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1199 GtkWidget *alias_widget;
1201 if (FOLKS_IS_INDIVIDUAL (folks_object))
1202 table = G_OBJECT (priv->individual_table);
1203 else if (FOLKS_IS_PERSONA (folks_object))
1204 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1206 g_assert_not_reached ();
1211 alias_widget = g_object_get_data (table, "alias-widget");
1213 if (GTK_IS_ENTRY (alias_widget))
1215 gtk_entry_set_text (GTK_ENTRY (alias_widget),
1216 folks_aliasable_get_alias (FOLKS_ALIASABLE (folks_object)));
1220 gtk_label_set_label (GTK_LABEL (alias_widget),
1221 folks_aliasable_get_alias (FOLKS_ALIASABLE (folks_object)));
1226 notify_presence_cb (gpointer folks_object,
1228 EmpathyIndividualWidget *self)
1230 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1232 GtkWidget *status_label, *state_image;
1233 const gchar *message;
1234 gchar *markup_text = NULL;
1236 if (FOLKS_IS_INDIVIDUAL (folks_object))
1237 table = G_OBJECT (priv->individual_table);
1238 else if (FOLKS_IS_PERSONA (folks_object))
1239 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1241 g_assert_not_reached ();
1246 status_label = g_object_get_data (table, "status-label");
1247 state_image = g_object_get_data (table, "state-image");
1249 /* FIXME: Default messages should be moved into libfolks (bgo#627403) */
1250 message = folks_presence_get_presence_message (FOLKS_PRESENCE (folks_object));
1251 if (EMP_STR_EMPTY (message))
1253 message = empathy_presence_get_default_message (
1254 folks_presence_get_presence_type (FOLKS_PRESENCE (folks_object)));
1257 if (message != NULL)
1258 markup_text = empathy_add_link_markup (message);
1259 gtk_label_set_markup (GTK_LABEL (status_label), markup_text);
1260 g_free (markup_text);
1262 gtk_image_set_from_icon_name (GTK_IMAGE (state_image),
1263 empathy_icon_name_for_presence (
1264 folks_presence_get_presence_type (FOLKS_PRESENCE (folks_object))),
1265 GTK_ICON_SIZE_BUTTON);
1266 gtk_widget_show (state_image);
1270 notify_is_favourite_cb (gpointer folks_object,
1272 EmpathyIndividualWidget *self)
1274 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1276 GtkWidget *favourite_widget;
1278 if (FOLKS_IS_INDIVIDUAL (folks_object))
1279 table = G_OBJECT (priv->individual_table);
1280 else if (FOLKS_IS_PERSONA (folks_object))
1281 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1283 g_assert_not_reached ();
1288 favourite_widget = g_object_get_data (table, "favourite-widget");
1290 if (GTK_IS_TOGGLE_BUTTON (favourite_widget))
1292 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (favourite_widget),
1293 folks_favourite_get_is_favourite (FOLKS_FAVOURITE (folks_object)));
1298 alias_presence_avatar_favourite_set_up (EmpathyIndividualWidget *self,
1302 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1303 GtkWidget *label, *alias, *image, *avatar, *alignment;
1304 guint current_row = starting_row;
1307 label = gtk_label_new (_("Alias:"));
1308 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1309 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1, GTK_FILL,
1311 gtk_widget_show (label);
1313 /* Set up alias label/entry */
1314 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS)
1316 alias = gtk_entry_new ();
1318 g_signal_connect (alias, "focus-out-event",
1319 (GCallback) entry_alias_focus_event_cb, self);
1321 /* Make return activate the window default (the Close button) */
1322 gtk_entry_set_activates_default (GTK_ENTRY (alias), TRUE);
1326 alias = gtk_label_new (NULL);
1327 gtk_label_set_selectable (GTK_LABEL (alias),
1328 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1329 gtk_misc_set_alignment (GTK_MISC (alias), 0.0, 0.5);
1332 g_object_set_data (G_OBJECT (table), "alias-widget", alias);
1333 gtk_table_attach (table, alias, 1, 2, current_row, current_row + 1,
1334 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1335 gtk_widget_show (alias);
1340 priv->hbox_presence = gtk_hbox_new (FALSE, 6);
1342 /* Presence image */
1343 image = gtk_image_new_from_stock (GTK_STOCK_MISSING_IMAGE,
1344 GTK_ICON_SIZE_BUTTON);
1345 g_object_set_data (G_OBJECT (table), "state-image", image);
1346 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), image, FALSE,
1348 gtk_widget_show (image);
1350 label = gtk_label_new ("");
1351 gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
1352 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1353 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1355 gtk_label_set_selectable (GTK_LABEL (label),
1356 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1358 g_object_set_data (G_OBJECT (table), "status-label", label);
1359 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, TRUE,
1361 gtk_widget_show (label);
1363 gtk_table_attach (table, priv->hbox_presence, 0, 2, current_row,
1364 current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1365 gtk_widget_show (priv->hbox_presence);
1369 /* Set up favourite toggle button */
1370 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1372 GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1374 g_signal_connect (favourite, "toggled",
1375 (GCallback) favourite_toggled_cb, self);
1377 g_object_set_data (G_OBJECT (table), "favourite-widget", favourite);
1378 gtk_table_attach (table, favourite, 0, 2, current_row, current_row + 1,
1379 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1380 gtk_widget_show (favourite);
1385 /* Set up avatar display */
1386 avatar = empathy_avatar_image_new ();
1388 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1390 g_signal_connect (avatar, "popup-menu",
1391 (GCallback) avatar_widget_popup_menu_cb, self);
1392 g_signal_connect (avatar, "button-press-event",
1393 (GCallback) avatar_widget_button_press_event_cb, self);
1396 g_object_set_data (G_OBJECT (table), "avatar-widget", avatar);
1398 alignment = gtk_alignment_new (1.0, 0.0, 0.0, 0.0);
1399 gtk_container_add (GTK_CONTAINER (alignment), avatar);
1400 gtk_widget_show (avatar);
1402 gtk_table_attach (table, alignment, 2, 3, 0, current_row,
1403 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 6, 6);
1404 gtk_widget_show (alignment);
1408 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1410 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1411 TpContact *tp_contact;
1412 EmpathyContact *contact;
1419 table = g_hash_table_lookup (priv->persona_tables, persona);
1421 g_assert (table != NULL);
1423 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1424 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1425 empathy_contact_set_persona (contact, persona);
1427 account = empathy_contact_get_account (contact);
1429 /* Update account widget */
1430 if (account != NULL)
1434 label = g_object_get_data (G_OBJECT (table), "account-label");
1435 image = g_object_get_data (G_OBJECT (table), "account-image");
1437 name = tp_account_get_display_name (account);
1438 gtk_label_set_label (label, name);
1440 name = tp_account_get_icon_name (account);
1441 gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
1444 /* Update id widget */
1445 label = g_object_get_data (G_OBJECT (table), "id-widget");
1446 id = folks_persona_get_display_id (persona);
1447 gtk_label_set_label (label, (id != NULL) ? id : "");
1449 /* Update other widgets */
1450 notify_alias_cb (persona, NULL, self);
1451 notify_presence_cb (persona, NULL, self);
1452 notify_avatar_cb (persona, NULL, self);
1454 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1455 notify_is_favourite_cb (persona, NULL, self);
1457 g_object_unref (contact);
1461 add_persona (EmpathyIndividualWidget *self,
1462 FolksPersona *persona)
1464 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1467 GtkWidget *label, *account_label, *account_image, *separator;
1468 guint current_row = 0;
1470 if (!TPF_IS_PERSONA (persona))
1473 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1474 table = GTK_TABLE (gtk_table_new (5, 3, FALSE));
1476 table = GTK_TABLE (gtk_table_new (4, 3, FALSE));
1477 gtk_table_set_row_spacings (table, 6);
1478 gtk_table_set_col_spacings (table, 6);
1480 /* Account and Identifier */
1481 label = gtk_label_new (_("Account:"));
1482 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1483 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1484 GTK_FILL, GTK_FILL, 0, 0);
1485 gtk_widget_show (label);
1487 /* Pack the protocol icon with the account name in an hbox */
1488 hbox = GTK_BOX (gtk_hbox_new (FALSE, 6));
1490 account_label = gtk_label_new (NULL);
1491 gtk_label_set_selectable (GTK_LABEL (account_label),
1492 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1493 gtk_misc_set_alignment (GTK_MISC (account_label), 0.0, 0.5);
1494 gtk_widget_show (account_label);
1496 account_image = gtk_image_new ();
1497 gtk_widget_show (account_image);
1499 gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1500 gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
1502 g_object_set_data (G_OBJECT (table), "account-image", account_image);
1503 g_object_set_data (G_OBJECT (table), "account-label", account_label);
1504 gtk_table_attach (table, GTK_WIDGET (hbox), 1, 2, current_row,
1505 current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1506 gtk_widget_show (GTK_WIDGET (hbox));
1510 /* Translators: Identifier to connect to Instant Messaging network */
1511 label = gtk_label_new (_("Identifier:"));
1512 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1513 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1514 GTK_FILL, GTK_FILL, 0, 0);
1515 gtk_widget_show (label);
1517 /* Set up ID label */
1518 label = gtk_label_new (NULL);
1519 gtk_label_set_selectable (GTK_LABEL (label),
1520 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1521 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1523 g_object_set_data (G_OBJECT (table), "id-widget", label);
1524 gtk_table_attach (table, label, 1, 2, current_row, current_row + 1,
1525 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1526 gtk_widget_show (label);
1530 alias_presence_avatar_favourite_set_up (self, table, current_row);
1532 /* Connect to signals and display the table */
1533 g_signal_connect (persona, "notify::alias",
1534 (GCallback) notify_alias_cb, self);
1535 g_signal_connect (persona, "notify::avatar",
1536 (GCallback) notify_avatar_cb, self);
1537 g_signal_connect (persona, "notify::presence-type",
1538 (GCallback) notify_presence_cb, self);
1539 g_signal_connect (persona, "notify::presence-message",
1540 (GCallback) notify_presence_cb, self);
1542 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1544 g_signal_connect (persona, "notify::is-favourite",
1545 (GCallback) notify_is_favourite_cb, self);
1548 gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1549 GTK_WIDGET (table), FALSE, TRUE, 0);
1550 gtk_widget_show (GTK_WIDGET (table));
1552 /* Pack a separator after the table */
1553 separator = gtk_hseparator_new ();
1554 g_object_set_data (G_OBJECT (table), "separator", separator);
1555 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), separator, FALSE, FALSE,
1557 gtk_widget_show (separator);
1559 g_hash_table_replace (priv->persona_tables, persona, table);
1561 /* Update the new widgets */
1562 update_persona (self, persona);
1566 remove_persona (EmpathyIndividualWidget *self,
1567 FolksPersona *persona)
1569 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1570 GtkWidget *separator;
1573 if (!TPF_IS_PERSONA (persona))
1576 table = g_hash_table_lookup (priv->persona_tables, persona);
1580 g_signal_handlers_disconnect_by_func (persona, notify_alias_cb, self);
1581 g_signal_handlers_disconnect_by_func (persona, notify_avatar_cb, self);
1582 g_signal_handlers_disconnect_by_func (persona, notify_presence_cb, self);
1584 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1586 g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
1590 /* Remove the separator */
1591 separator = g_object_get_data (G_OBJECT (table), "separator");
1592 if (separator != NULL)
1593 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual), separator);
1595 /* Remove the widget */
1596 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1597 GTK_WIDGET (table));
1599 g_hash_table_remove (priv->persona_tables, persona);
1603 update_individual_table (EmpathyIndividualWidget *self)
1605 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1607 notify_alias_cb (priv->individual, NULL, self);
1608 notify_presence_cb (priv->individual, NULL, self);
1609 notify_avatar_cb (priv->individual, NULL, self);
1611 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1612 notify_is_favourite_cb (priv->individual, NULL, self);
1616 individual_table_set_up (EmpathyIndividualWidget *self)
1618 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1620 guint current_row = 0;
1623 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1626 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1629 table = GTK_TABLE (gtk_table_new (nb_rows, 3, FALSE));
1630 gtk_table_set_row_spacings (table, 6);
1631 gtk_table_set_col_spacings (table, 6);
1633 /* We only display the number of personas in tooltips */
1634 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1638 GList *personas, *l;
1639 guint num_personas = 0;
1641 /* Meta-contacts message displaying how many Telepathy personas we have */
1642 personas = folks_individual_get_personas (priv->individual);
1643 for (l = personas; l != NULL; l = l->next)
1645 if (TPF_IS_PERSONA (l->data))
1649 /* Translators: the plurality applies to both instances of the word
1651 message = g_strdup_printf (
1652 ngettext ("Linked contact containing %u contact",
1653 "Linked contacts containing %u contacts", num_personas),
1655 label = gtk_label_new (message);
1656 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1659 gtk_table_attach (table, label, 0, 2, current_row, current_row + 1,
1660 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1661 gtk_widget_show (label);
1666 alias_presence_avatar_favourite_set_up (self, table, current_row);
1668 /* Display the table */
1669 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (table),
1671 gtk_widget_show (GTK_WIDGET (table));
1673 priv->individual_table = table;
1675 /* Update the table */
1676 update_individual_table (self);
1680 individual_table_destroy (EmpathyIndividualWidget *self)
1682 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1684 if (priv->individual_table == NULL)
1687 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1688 GTK_WIDGET (priv->individual_table));
1689 priv->individual_table = NULL;
1693 personas_changed_cb (FolksIndividual *individual,
1696 EmpathyIndividualWidget *self)
1698 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1699 GList *personas, *l, *children;
1700 gboolean show_personas, was_showing_personas, will_show_personas, is_last;
1701 guint old_num_personas, new_num_personas = 0;
1703 personas = folks_individual_get_personas (individual);
1705 /* Note that old_num_personas is the number of persona tables we were
1706 * displaying, not the number of Personas which were in the Individual
1708 old_num_personas = g_hash_table_size (priv->persona_tables);
1710 for (l = personas; l != NULL; l = l->next)
1712 if (TPF_IS_PERSONA (l->data))
1717 * What we display for various conditions:
1718 * - "Personas": display the alias, avatar, presence account and identifier
1719 * for each of the Individual's Personas. (i.e. One table per
1721 * - "Individual": display the alias, avatar and presence for the Individual,
1722 * and a label saying "Meta-contact containing x contacts".
1723 * (i.e. One table in total.)
1725 * | SHOW_PERSONAS | !SHOW_PERSONAS
1726 * -------------+---------------+---------------
1727 * > 1 Persona | Personas | Individual
1728 * -------------+---------------+---------------
1729 * == 1 Persona | Personas | Personas
1731 show_personas = (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) != 0;
1732 was_showing_personas = show_personas || old_num_personas == 1;
1733 will_show_personas = show_personas || new_num_personas == 1;
1735 /* If both @added and @removed are NULL, we're being called manually, and we
1736 * need to set up the tables for the first time. We do this simply by
1737 * ensuring was_showing_personas and will_show_personas are different so that
1738 * the code resets the UI.
1740 if (added == NULL && removed == NULL)
1741 was_showing_personas = !will_show_personas;
1743 if (was_showing_personas && will_show_personas)
1745 /* Remove outdated Personas */
1746 for (l = removed; l != NULL; l = l->next)
1747 remove_persona (self, FOLKS_PERSONA (l->data));
1749 /* Add new Personas */
1750 for (l = added; l != NULL; l = l->next)
1751 add_persona (self, FOLKS_PERSONA (l->data));
1753 else if (!was_showing_personas && will_show_personas)
1755 /* Remove the old Individual table */
1756 individual_table_destroy (self);
1758 /* Set up all the Persona tables instead */
1759 for (l = personas; l != NULL; l = l->next)
1760 add_persona (self, FOLKS_PERSONA (l->data));
1762 else if (was_showing_personas && !will_show_personas)
1764 /* Remove all Personas */
1765 for (l = personas; l != NULL; l = l->next)
1766 remove_persona (self, FOLKS_PERSONA (l->data));
1767 for (l = removed; l != NULL; l = l->next)
1768 remove_persona (self, FOLKS_PERSONA (l->data));
1770 /* Set up the Individual table instead */
1771 individual_table_set_up (self);
1774 /* Hide the last separator and show the others */
1775 children = gtk_container_get_children (GTK_CONTAINER (priv->vbox_individual));
1776 children = g_list_reverse (children);
1779 for (l = children; l != NULL; l = l->next)
1781 if (GTK_IS_SEPARATOR (l->data))
1783 gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1788 g_list_free (children);
1792 individual_removed_cb (FolksIndividual *individual,
1793 FolksIndividual *replacement_individual,
1794 EmpathyIndividualWidget *self)
1796 empathy_individual_widget_set_individual (self, replacement_individual);
1800 remove_individual (EmpathyIndividualWidget *self)
1802 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1803 if (priv->individual != NULL)
1805 GList *personas, *l;
1807 g_signal_handlers_disconnect_by_func (priv->individual,
1808 notify_alias_cb, self);
1809 g_signal_handlers_disconnect_by_func (priv->individual,
1810 notify_presence_cb, self);
1811 g_signal_handlers_disconnect_by_func (priv->individual,
1812 notify_avatar_cb, self);
1813 g_signal_handlers_disconnect_by_func (priv->individual,
1814 personas_changed_cb, self);
1815 g_signal_handlers_disconnect_by_func (priv->individual,
1816 individual_removed_cb, self);
1818 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1820 g_signal_handlers_disconnect_by_func (priv->individual,
1821 notify_is_favourite_cb, self);
1824 personas = folks_individual_get_personas (priv->individual);
1825 for (l = personas; l != NULL; l = l->next)
1826 remove_persona (self, FOLKS_PERSONA (l->data));
1827 individual_table_destroy (self);
1829 if (priv->contact != NULL)
1830 remove_weak_contact (self);
1832 tp_clear_object (&priv->individual);
1835 if (priv->details_cancellable != NULL)
1836 g_cancellable_cancel (priv->details_cancellable);
1840 individual_update (EmpathyIndividualWidget *self)
1842 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1844 /* Connect and get info from new Individual */
1845 if (priv->individual != NULL)
1847 g_signal_connect (priv->individual, "notify::alias",
1848 (GCallback) notify_alias_cb, self);
1849 g_signal_connect (priv->individual, "notify::presence-type",
1850 (GCallback) notify_presence_cb, self);
1851 g_signal_connect (priv->individual, "notify::presence-message",
1852 (GCallback) notify_presence_cb, self);
1853 g_signal_connect (priv->individual, "notify::avatar",
1854 (GCallback) notify_avatar_cb, self);
1855 g_signal_connect (priv->individual, "personas-changed",
1856 (GCallback) personas_changed_cb, self);
1857 g_signal_connect (priv->individual, "removed",
1858 (GCallback) individual_removed_cb, self);
1860 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1862 g_signal_connect (priv->individual, "notify::is-favourite",
1863 (GCallback) notify_is_favourite_cb, self);
1866 /* Update individual table */
1867 personas_changed_cb (priv->individual, NULL, NULL, self);
1870 if (priv->individual == NULL)
1872 gtk_widget_hide (priv->vbox_individual);
1874 else if (priv->individual_table != NULL)
1876 /* We only need to update the details for the Individual as a whole */
1877 update_individual_table (self);
1878 gtk_widget_show (priv->vbox_individual);
1882 /* We need to update the details for every Persona in the Individual */
1883 GList *personas, *l;
1885 personas = folks_individual_get_personas (priv->individual);
1886 for (l = personas; l != NULL; l = l->next)
1888 if (!TPF_IS_PERSONA (l->data))
1891 update_persona (self, FOLKS_PERSONA (l->data));
1894 gtk_widget_show (priv->vbox_individual);
1899 empathy_individual_widget_init (EmpathyIndividualWidget *self)
1901 EmpathyIndividualWidgetPriv *priv;
1905 priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1906 EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
1909 gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
1910 GTK_ORIENTATION_VERTICAL);
1912 filename = empathy_file_lookup ("empathy-individual-widget.ui",
1914 gui = empathy_builder_get_file (filename,
1915 "scrolled_window_individual", &priv->scrolled_window_individual,
1916 "viewport_individual", &priv->viewport_individual,
1917 "vbox_individual_widget", &priv->vbox_individual_widget,
1918 "vbox_individual", &priv->vbox_individual,
1919 "vbox_location", &priv->vbox_location,
1920 "subvbox_location", &priv->subvbox_location,
1921 "label_location", &priv->label_location,
1922 #ifdef HAVE_LIBCHAMPLAIN
1923 "viewport_map", &priv->viewport_map,
1925 "groups_widget", &priv->groups_widget,
1926 "vbox_details", &priv->vbox_details,
1927 "table_details", &priv->table_details,
1928 "hbox_details_requested", &priv->hbox_details_requested,
1929 "hbox_client_types", &priv->hbox_client_types,
1933 priv->table_location = NULL;
1935 gtk_box_pack_start (GTK_BOX (self), priv->vbox_individual_widget, TRUE, TRUE,
1937 gtk_widget_show (priv->vbox_individual_widget);
1939 priv->persona_tables = g_hash_table_new (NULL, NULL);
1940 priv->individual_table = NULL;
1942 /* Create widgets */
1943 details_set_up (self);
1945 g_object_unref (gui);
1949 constructed (GObject *object)
1951 GObjectClass *klass = G_OBJECT_CLASS (empathy_individual_widget_parent_class);
1952 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1953 GtkScrolledWindow *scrolled_window =
1954 GTK_SCROLLED_WINDOW (priv->scrolled_window_individual);
1956 /* Allow scrolling of the list of Personas if we're showing Personas. */
1957 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS)
1959 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
1960 gtk_scrolled_window_set_policy (scrolled_window,
1961 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1962 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
1963 priv->scrolled_window_individual, TRUE, TRUE, 0, GTK_PACK_START);
1965 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1967 gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
1971 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_NONE);
1972 gtk_scrolled_window_set_policy (scrolled_window,
1973 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1974 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
1975 priv->scrolled_window_individual, FALSE, TRUE, 0, GTK_PACK_START);
1977 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1982 if (klass->constructed != NULL)
1983 klass->constructed (object);
1987 get_property (GObject *object,
1992 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1996 case PROP_INDIVIDUAL:
1997 g_value_set_object (value, priv->individual);
2000 g_value_set_flags (value, priv->flags);
2003 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2009 set_property (GObject *object,
2011 const GValue *value,
2014 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2018 case PROP_INDIVIDUAL:
2019 empathy_individual_widget_set_individual (
2020 EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
2023 priv->flags = g_value_get_flags (value);
2026 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2032 dispose (GObject *object)
2034 remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
2036 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
2040 finalize (GObject *object)
2042 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2044 g_hash_table_destroy (priv->persona_tables);
2046 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
2050 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
2052 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2054 object_class->constructed = constructed;
2055 object_class->get_property = get_property;
2056 object_class->set_property = set_property;
2057 object_class->dispose = dispose;
2058 object_class->finalize = finalize;
2061 * EmpathyIndividualWidget:individual:
2063 * The #FolksIndividual to display in the widget.
2065 g_object_class_install_property (object_class, PROP_INDIVIDUAL,
2066 g_param_spec_object ("individual",
2068 "The #FolksIndividual to display in the widget.",
2069 FOLKS_TYPE_INDIVIDUAL,
2070 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2073 * EmpathyIndividualWidget:flags:
2075 * A set of flags which affect the widget's behaviour.
2077 g_object_class_install_property (object_class, PROP_FLAGS,
2078 g_param_spec_flags ("flags",
2080 "A set of flags which affect the widget's behaviour.",
2081 EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
2082 EMPATHY_INDIVIDUAL_WIDGET_NONE,
2083 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
2085 g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
2089 * empathy_individual_widget_new:
2090 * @individual: the #FolksIndividual to display
2091 * @flags: flags affecting how the widget behaves and what it displays
2093 * Creates a new #EmpathyIndividualWidget.
2095 * Return value: a new #EmpathyIndividualWidget
2098 empathy_individual_widget_new (FolksIndividual *individual,
2099 EmpathyIndividualWidgetFlags flags)
2101 g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
2104 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
2105 "individual", individual,
2111 * empathy_individual_widget_get_individual:
2112 * @self: an #EmpathyIndividualWidget
2114 * Returns the #FolksIndividual being displayed by the widget.
2116 * Return value: the #FolksIndividual being displayed, or %NULL
2119 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
2121 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
2123 return GET_PRIV (self)->individual;
2127 * empathy_individual_widget_set_individual:
2128 * @self: an #EmpathyIndividualWidget
2129 * @individual: the #FolksIndividual to display, or %NULL
2131 * Set the #FolksIndividual to be displayed by the widget:
2132 * #EmpathyIndividualWidget:individual.
2134 * The @individual may be %NULL in order to display nothing in the widget.
2137 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
2138 FolksIndividual *individual)
2140 EmpathyIndividualWidgetPriv *priv;
2142 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
2143 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
2145 priv = GET_PRIV (self);
2147 if (individual == priv->individual)
2150 /* Out with the old… */
2151 remove_individual (self);
2153 /* …and in with the new. */
2154 if (individual != NULL)
2155 g_object_ref (individual);
2156 priv->individual = individual;
2158 /* Update information for widgets */
2159 individual_update (self);
2160 groups_update (self);
2161 details_update (self);
2162 location_update (self);
2163 client_types_update (self);