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 client_types_notify_cb (TpContact *contact,
205 EmpathyIndividualWidget *self)
207 client_types_update (self);
211 update_weak_contact (EmpathyIndividualWidget *self)
213 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
214 TpContact *tp_contact = NULL;
216 remove_weak_contact (self);
218 if (priv->individual != NULL)
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) */
226 FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
228 personas = folks_individual_get_personas (priv->individual);
229 for (l = personas; l != NULL; l = l->next)
231 FolksPresenceOwner *presence;
233 /* We only want personas which implement FolksPresence */
234 if (!FOLKS_IS_PRESENCE_OWNER (l->data))
237 presence = FOLKS_PRESENCE_OWNER (l->data);
239 if (folks_presence_owner_typecmp (
240 folks_presence_owner_get_presence_type (presence),
242 && TPF_IS_PERSONA (presence))
244 presence_type = folks_presence_owner_get_presence_type (presence);
245 tp_contact = tpf_persona_get_contact (TPF_PERSONA (l->data));
250 if (tp_contact != NULL)
252 priv->contact = tp_contact;
253 g_object_add_weak_pointer (G_OBJECT (tp_contact),
254 (gpointer *) &priv->contact);
256 g_signal_connect (priv->contact, "notify::client-types",
257 (GCallback) client_types_notify_cb, self);
262 EmpathyIndividualWidget *widget; /* weak */
263 TpContact *contact; /* owned */
267 details_data_free (DetailsData *data)
269 if (data->widget != NULL)
271 g_object_remove_weak_pointer (G_OBJECT (data->widget),
272 (gpointer *) &data->widget);
274 g_object_unref (data->contact);
275 g_slice_free (DetailsData, data);
279 details_update_show (EmpathyIndividualWidget *self,
282 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
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)
290 TpContactInfoField *field = l->data;
291 InfoFieldData *field_data;
295 if (field->field_value == NULL || field->field_value[0] == NULL)
298 value = field->field_value[0];
300 field_data = find_info_field_data (field->field_name);
301 if (field_data == NULL)
303 DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
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);
315 w = gtk_label_new (value);
316 if (field_data->linkify == TRUE)
320 markup = empathy_add_link_markup (value);
321 gtk_label_set_markup (GTK_LABEL (w), markup);
325 gtk_label_set_selectable (GTK_LABEL (w),
326 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
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);
341 details_notify_cb (TpContact *contact,
343 EmpathyIndividualWidget *self)
345 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
348 gtk_container_foreach (GTK_CONTAINER (priv->table_details),
349 (GtkCallback) gtk_widget_destroy, NULL);
351 n_rows = details_update_show (self, contact);
355 gtk_widget_show (priv->vbox_details);
356 gtk_widget_show (priv->table_details);
360 gtk_widget_hide (priv->vbox_details);
363 gtk_widget_hide (priv->hbox_details_requested);
364 gtk_spinner_stop (GTK_SPINNER (priv->details_spinner));
368 details_request_cb (TpContact *contact,
372 EmpathyIndividualWidget *self = data->widget;
373 gboolean hide_widget = FALSE;
374 GError *error = NULL;
376 if (tp_contact_request_contact_info_finish (contact, res, &error) == TRUE)
377 details_notify_cb (contact, NULL, self);
381 g_clear_error (&error);
385 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
387 if (hide_widget == TRUE)
388 gtk_widget_hide (GET_PRIV (self)->vbox_details);
390 tp_clear_object (&priv->details_cancellable);
392 g_signal_connect (contact, "notify::contact-info",
393 (GCallback) details_notify_cb, self);
396 details_data_free (data);
400 details_feature_prepared_cb (TpConnection *connection,
404 EmpathyIndividualWidget *self = data->widget;
405 EmpathyIndividualWidgetPriv *priv = NULL;
407 if (tp_proxy_prepare_finish (connection, res, NULL) == FALSE || self == NULL)
410 gtk_widget_hide (GET_PRIV (self)->vbox_details);
411 details_data_free (data);
415 priv = GET_PRIV (self);
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));
423 if (priv->details_cancellable == NULL)
425 priv->details_cancellable = g_cancellable_new ();
426 tp_contact_request_contact_info_async (data->contact,
427 priv->details_cancellable, (GAsyncReadyCallback) details_request_cb,
433 details_update (EmpathyIndividualWidget *self)
435 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
436 TpContact *tp_contact = NULL;
438 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS))
441 gtk_widget_hide (priv->vbox_details);
443 if (priv->contact == NULL)
444 update_weak_contact (self);
446 if (priv->contact != NULL)
448 GQuark features[] = { TP_CONNECTION_FEATURE_CONTACT_INFO, 0 };
449 TpConnection *connection;
452 data = g_slice_new (DetailsData);
454 g_object_add_weak_pointer (G_OBJECT (self), (gpointer *) &data->widget);
455 data->contact = g_object_ref (priv->contact);
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);
465 groups_update (EmpathyIndividualWidget *self)
467 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
469 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS &&
470 priv->individual != NULL)
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);
479 gtk_widget_hide (priv->groups_widget);
483 /* Converts the Location's GHashTable's key to a user readable string */
485 location_key_to_label (const gchar *key)
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)
493 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
495 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
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)
501 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
502 return _("Building:");
503 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
505 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
507 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
509 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
510 return _("Description:");
511 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
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)
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)
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:");
537 DEBUG ("Unexpected Location key: %s", key);
543 location_update (EmpathyIndividualWidget *self)
545 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
546 EmpathyContact *contact = NULL;
547 GHashTable *location = NULL;
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,
567 gboolean display_map = FALSE;
570 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION) ||
571 priv->individual == NULL)
573 gtk_widget_hide (priv->vbox_location);
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)
583 FolksPersona *persona = FOLKS_PERSONA (l->data);
585 if (TPF_IS_PERSONA (persona))
587 TpContact *tp_contact;
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);
596 /* Try and get a location */
597 location = empathy_contact_get_location (contact);
598 if (location != NULL && g_hash_table_size (location) > 0)
602 tp_clear_object (&contact);
606 if (contact == NULL || location == NULL)
608 gtk_widget_hide (priv->vbox_location);
609 tp_clear_object (&contact);
613 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
616 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
617 gtk_label_set_markup (GTK_LABEL (priv->label_location), loc);
628 stamp = g_value_get_int64 (value);
631 user_date = empathy_time_to_string_relative (time_);
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);
637 gtk_label_set_markup (GTK_LABEL (priv->label_location), text);
642 /* Prepare the location information table */
643 if (priv->table_location != NULL)
644 gtk_widget_destroy (priv->table_location);
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);
651 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
653 const gchar* user_label;
657 gvalue = g_hash_table_lookup (location, (gpointer) skey);
661 user_label = location_key_to_label (skey);
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);
669 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
672 dvalue = g_value_get_double (gvalue);
673 svalue = g_strdup_printf ("%f", dvalue);
675 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
677 svalue = g_value_dup_string (gvalue);
679 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
683 time_ = g_value_get_int64 (value);
684 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
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);
695 gtk_label_set_selectable (GTK_LABEL (label),
696 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE :
704 tp_clear_object (&contact);
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))
711 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
719 /* We can display some fields */
720 gtk_widget_show (priv->table_location);
722 else if (display_map == FALSE)
724 /* Can't display either fields or map */
725 gtk_widget_hide (priv->vbox_location);
729 #ifdef HAVE_LIBCHAMPLAIN
730 if (display_map == TRUE)
733 ChamplainLayer *layer;
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));
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,
747 layer = champlain_layer_new ();
748 champlain_view_add_layer (priv->map_view, layer);
749 markers = g_ptr_array_new ();
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)
756 FolksPersona *persona = FOLKS_PERSONA (l->data);
758 if (TPF_IS_PERSONA (persona))
760 gdouble lat = 0.0, lon = 0.0;
761 ClutterActor *marker;
762 TpContact *tp_contact;
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);
769 /* Try and get a location */
770 location = empathy_contact_get_location (contact);
771 if (location == NULL || g_hash_table_size (location) == 0)
773 g_object_unref (contact);
777 /* Get this persona's latitude and longitude */
778 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
781 g_object_unref (contact);
785 lat = g_value_get_double (value);
787 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
790 g_object_unref (contact);
794 lon = g_value_get_double (value);
796 /* Add a marker to the map */
797 marker = champlain_marker_new_with_text (
798 folks_aliasable_get_alias (FOLKS_ALIASABLE (persona)), NULL,
800 champlain_base_marker_set_position (
801 CHAMPLAIN_BASE_MARKER (marker), lat, lon);
802 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
804 g_ptr_array_add (markers, marker);
806 g_object_unref (contact);
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);
816 gtk_widget_show_all (priv->viewport_map);
820 gtk_widget_show (priv->vbox_location);
824 client_types_update (EmpathyIndividualWidget *self)
826 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
827 const gchar * const *types;
829 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_CLIENT_TYPES) ||
830 priv->individual == NULL)
832 gtk_widget_hide (priv->hbox_client_types);
836 if (priv->contact == NULL)
837 update_weak_contact (self);
839 /* let's try that again... */
840 if (priv->contact == NULL)
843 types = tp_contact_get_client_types (priv->contact);
846 && g_strv_length ((gchar **) types) > 0
847 && !tp_strdiff (types[0], "phone"))
849 gtk_widget_show (priv->hbox_client_types);
853 gtk_widget_hide (priv->hbox_client_types);
859 remove_weak_contact (EmpathyIndividualWidget *self)
861 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
863 if (priv->contact == NULL)
866 g_signal_handlers_disconnect_by_func (priv->contact, client_types_notify_cb,
869 g_object_remove_weak_pointer (G_OBJECT (priv->contact),
870 (gpointer *) &priv->contact);
871 priv->contact = NULL;
874 static EmpathyAvatar *
875 persona_dup_avatar (FolksPersona *persona)
877 TpContact *tp_contact;
878 EmpathyContact *contact;
879 EmpathyAvatar *avatar;
881 if (!TPF_IS_PERSONA (persona))
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);
888 avatar = empathy_contact_get_avatar (contact);
890 empathy_avatar_ref (avatar);
891 g_object_unref (contact);
896 static EmpathyAvatar *
897 individual_dup_avatar (FolksIndividual *individual)
900 EmpathyAvatar *avatar = NULL;
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.
906 personas = folks_individual_get_personas (individual);
907 for (l = personas; l != NULL; l = l->next)
909 avatar = persona_dup_avatar (FOLKS_PERSONA (l->data));
918 save_avatar_menu_activate_cb (GtkWidget *widget,
919 EmpathyIndividualWidget *self)
921 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
923 EmpathyAvatar *avatar;
924 gchar *ext = NULL, *filename;
926 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
928 GTK_FILE_CHOOSER_ACTION_SAVE,
929 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
930 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
933 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
936 avatar = individual_dup_avatar (priv->individual);
940 /* look for the avatar extension */
941 if (avatar->format != NULL)
945 splitted = g_strsplit (avatar->format, "/", 2);
946 if (splitted[0] != NULL && splitted[1] != NULL)
947 ext = g_strdup (splitted[1]);
949 g_strfreev (splitted);
953 /* Avatar was loaded from the cache so was converted to PNG */
954 ext = g_strdup ("png");
961 id = tp_escape_as_identifier (folks_individual_get_id (priv->individual));
963 filename = g_strdup_printf ("%s.%s", id, ext);
964 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
971 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
973 GError *error = NULL;
975 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
977 if (empathy_avatar_save_to_file (avatar, filename, &error) == FALSE)
980 GtkWidget *error_dialog;
982 error_dialog = gtk_message_dialog_new (NULL, 0,
983 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
984 _("Unable to save avatar"));
986 gtk_message_dialog_format_secondary_text (
987 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
989 g_signal_connect (error_dialog, "response",
990 (GCallback) gtk_widget_destroy, NULL);
992 gtk_window_present (GTK_WINDOW (error_dialog));
994 g_clear_error (&error);
1000 gtk_widget_destroy (dialog);
1001 empathy_avatar_unref (avatar);
1005 popup_avatar_menu (EmpathyIndividualWidget *self,
1007 GdkEventButton *event)
1009 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1010 GtkWidget *menu, *item;
1011 EmpathyAvatar *avatar;
1012 gint button, event_time;
1014 if (priv->individual == NULL)
1017 avatar = individual_dup_avatar (priv->individual);
1020 empathy_avatar_unref (avatar);
1022 menu = gtk_menu_new ();
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);
1029 g_signal_connect (item, "activate",
1030 (GCallback) save_avatar_menu_activate_cb, self);
1034 button = event->button;
1035 event_time = event->time;
1040 event_time = gtk_get_current_event_time ();
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);
1052 avatar_widget_popup_menu_cb (GtkWidget *widget,
1053 EmpathyIndividualWidget *self)
1055 return popup_avatar_menu (self, widget, NULL);
1059 avatar_widget_button_press_event_cb (GtkWidget *widget,
1060 GdkEventButton *event,
1061 EmpathyIndividualWidget *self)
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);
1070 /* Returns the TpAccount for the user as a convenience. Note that it has a ref
1073 individual_is_user (FolksIndividual *individual)
1075 GList *personas, *l;
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)
1082 FolksPersona *persona = FOLKS_PERSONA (l->data);
1084 if (TPF_IS_PERSONA (persona))
1086 TpContact *tp_contact;
1087 EmpathyContact *contact;
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);
1094 /* Determine if the contact is the user */
1095 if (empathy_contact_is_user (contact))
1097 g_object_unref (contact);
1098 return g_object_ref (empathy_contact_get_account (contact));
1101 g_object_unref (contact);
1109 set_nickname_cb (TpAccount *account,
1110 GAsyncResult *result,
1113 GError *error = NULL;
1115 if (tp_account_set_nickname_finish (account, result, &error) == FALSE)
1117 DEBUG ("Failed to set Account.Nickname: %s", error->message);
1118 g_error_free (error);
1123 entry_alias_focus_event_cb (GtkEditable *editable,
1124 GdkEventFocus *event,
1125 EmpathyIndividualWidget *self)
1127 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1129 if (priv->individual != NULL)
1134 alias = gtk_entry_get_text (GTK_ENTRY (editable));
1135 account = individual_is_user (priv->individual);
1137 if (account != NULL)
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);
1146 folks_aliasable_set_alias (FOLKS_ALIASABLE (priv->individual), alias);
1154 favourite_toggled_cb (GtkToggleButton *button,
1155 EmpathyIndividualWidget *self)
1157 gboolean active = gtk_toggle_button_get_active (button);
1158 folks_favouritable_set_is_favourite (
1159 FOLKS_FAVOURITABLE (GET_PRIV (self)->individual), active);
1163 notify_avatar_cb (gpointer folks_object,
1165 EmpathyIndividualWidget *self)
1167 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1168 EmpathyAvatar *avatar = NULL;
1170 GtkWidget *avatar_widget;
1172 if (FOLKS_IS_INDIVIDUAL (folks_object))
1174 avatar = individual_dup_avatar (FOLKS_INDIVIDUAL (folks_object));
1175 table = G_OBJECT (priv->individual_table);
1177 else if (FOLKS_IS_PERSONA (folks_object))
1179 avatar = persona_dup_avatar (FOLKS_PERSONA (folks_object));
1180 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1184 g_assert_not_reached ();
1190 avatar_widget = g_object_get_data (table, "avatar-widget");
1191 empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (avatar_widget), avatar);
1194 empathy_avatar_unref (avatar);
1198 notify_alias_cb (gpointer folks_object,
1200 EmpathyIndividualWidget *self)
1202 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1204 GtkWidget *alias_widget;
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);
1211 g_assert_not_reached ();
1216 alias_widget = g_object_get_data (table, "alias-widget");
1218 if (GTK_IS_ENTRY (alias_widget))
1220 gtk_entry_set_text (GTK_ENTRY (alias_widget),
1221 folks_aliasable_get_alias (FOLKS_ALIASABLE (folks_object)));
1225 gtk_label_set_label (GTK_LABEL (alias_widget),
1226 folks_aliasable_get_alias (FOLKS_ALIASABLE (folks_object)));
1231 notify_presence_cb (gpointer folks_object,
1233 EmpathyIndividualWidget *self)
1235 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1237 GtkWidget *status_label, *state_image;
1238 const gchar *message;
1239 gchar *markup_text = NULL;
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);
1246 g_assert_not_reached ();
1251 status_label = g_object_get_data (table, "status-label");
1252 state_image = g_object_get_data (table, "state-image");
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))
1259 message = empathy_presence_get_default_message (
1260 folks_presence_owner_get_presence_type (
1261 FOLKS_PRESENCE_OWNER (folks_object)));
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);
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);
1278 notify_is_favourite_cb (gpointer folks_object,
1280 EmpathyIndividualWidget *self)
1282 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1284 GtkWidget *favourite_widget;
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);
1291 g_assert_not_reached ();
1296 favourite_widget = g_object_get_data (table, "favourite-widget");
1298 if (GTK_IS_TOGGLE_BUTTON (favourite_widget))
1300 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (favourite_widget),
1301 folks_favouritable_get_is_favourite (
1302 FOLKS_FAVOURITABLE (folks_object)));
1307 alias_presence_avatar_favourite_set_up (EmpathyIndividualWidget *self,
1311 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1312 GtkWidget *label, *alias, *image, *avatar, *alignment;
1313 guint current_row = starting_row;
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,
1320 gtk_widget_show (label);
1322 /* Set up alias label/entry */
1323 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS)
1325 alias = gtk_entry_new ();
1327 g_signal_connect (alias, "focus-out-event",
1328 (GCallback) entry_alias_focus_event_cb, self);
1330 /* Make return activate the window default (the Close button) */
1331 gtk_entry_set_activates_default (GTK_ENTRY (alias), TRUE);
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);
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);
1349 priv->hbox_presence = gtk_hbox_new (FALSE, 6);
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,
1357 gtk_widget_show (image);
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);
1364 gtk_label_set_selectable (GTK_LABEL (label),
1365 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1367 g_object_set_data (G_OBJECT (table), "status-label", label);
1368 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, TRUE,
1370 gtk_widget_show (label);
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);
1378 /* Set up favourite toggle button */
1379 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1381 GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1383 g_signal_connect (favourite, "toggled",
1384 (GCallback) favourite_toggled_cb, self);
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);
1394 /* Set up avatar display */
1395 avatar = empathy_avatar_image_new ();
1397 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
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);
1405 g_object_set_data (G_OBJECT (table), "avatar-widget", avatar);
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);
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);
1417 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1419 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1420 TpContact *tp_contact;
1421 EmpathyContact *contact;
1428 table = g_hash_table_lookup (priv->persona_tables, persona);
1430 g_assert (table != NULL);
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);
1436 account = empathy_contact_get_account (contact);
1438 /* Update account widget */
1439 if (account != NULL)
1443 label = g_object_get_data (G_OBJECT (table), "account-label");
1444 image = g_object_get_data (G_OBJECT (table), "account-image");
1446 name = tp_account_get_display_name (account);
1447 gtk_label_set_label (label, name);
1449 name = tp_account_get_icon_name (account);
1450 gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
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 : "");
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);
1463 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1464 notify_is_favourite_cb (persona, NULL, self);
1466 g_object_unref (contact);
1470 add_persona (EmpathyIndividualWidget *self,
1471 FolksPersona *persona)
1473 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1476 GtkWidget *label, *account_label, *account_image, *separator;
1477 guint current_row = 0;
1479 if (!TPF_IS_PERSONA (persona))
1482 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1483 table = GTK_TABLE (gtk_table_new (5, 3, FALSE));
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);
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);
1496 /* Pack the protocol icon with the account name in an hbox */
1497 hbox = GTK_BOX (gtk_hbox_new (FALSE, 6));
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);
1505 account_image = gtk_image_new ();
1506 gtk_widget_show (account_image);
1508 gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1509 gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
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));
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);
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);
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);
1539 alias_presence_avatar_favourite_set_up (self, table, current_row);
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);
1551 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1553 g_signal_connect (persona, "notify::is-favourite",
1554 (GCallback) notify_is_favourite_cb, self);
1557 gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1558 GTK_WIDGET (table), FALSE, TRUE, 0);
1559 gtk_widget_show (GTK_WIDGET (table));
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,
1566 gtk_widget_show (separator);
1568 g_hash_table_replace (priv->persona_tables, persona, table);
1570 /* Update the new widgets */
1571 update_persona (self, persona);
1575 remove_persona (EmpathyIndividualWidget *self,
1576 FolksPersona *persona)
1578 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1579 GtkWidget *separator;
1582 if (!TPF_IS_PERSONA (persona))
1585 table = g_hash_table_lookup (priv->persona_tables, persona);
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);
1593 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1595 g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
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);
1604 /* Remove the widget */
1605 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1606 GTK_WIDGET (table));
1608 g_hash_table_remove (priv->persona_tables, persona);
1612 update_individual_table (EmpathyIndividualWidget *self)
1614 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1616 notify_alias_cb (priv->individual, NULL, self);
1617 notify_presence_cb (priv->individual, NULL, self);
1618 notify_avatar_cb (priv->individual, NULL, self);
1620 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1621 notify_is_favourite_cb (priv->individual, NULL, self);
1625 individual_table_set_up (EmpathyIndividualWidget *self)
1627 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1629 guint current_row = 0;
1632 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1635 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
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);
1642 /* We only display the number of personas in tooltips */
1643 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1647 GList *personas, *l;
1648 guint num_personas = 0;
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)
1654 if (TPF_IS_PERSONA (l->data))
1658 /* Translators: the plurality applies to both instances of the word
1660 message = g_strdup_printf (
1661 ngettext ("Linked contact containing %u contact",
1662 "Linked contacts containing %u contacts", num_personas),
1664 label = gtk_label_new (message);
1665 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
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);
1675 alias_presence_avatar_favourite_set_up (self, table, current_row);
1677 /* Display the table */
1678 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (table),
1680 gtk_widget_show (GTK_WIDGET (table));
1682 priv->individual_table = table;
1684 /* Update the table */
1685 update_individual_table (self);
1689 individual_table_destroy (EmpathyIndividualWidget *self)
1691 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1693 if (priv->individual_table == NULL)
1696 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1697 GTK_WIDGET (priv->individual_table));
1698 priv->individual_table = NULL;
1702 personas_changed_cb (FolksIndividual *individual,
1705 EmpathyIndividualWidget *self)
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;
1712 personas = folks_individual_get_personas (individual);
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
1717 old_num_personas = g_hash_table_size (priv->persona_tables);
1719 for (l = personas; l != NULL; l = l->next)
1721 if (TPF_IS_PERSONA (l->data))
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
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.)
1734 * | SHOW_PERSONAS | !SHOW_PERSONAS
1735 * -------------+---------------+---------------
1736 * > 1 Persona | Personas | Individual
1737 * -------------+---------------+---------------
1738 * == 1 Persona | Personas | Personas
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;
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.
1749 if (added == NULL && removed == NULL)
1750 was_showing_personas = !will_show_personas;
1752 if (was_showing_personas && will_show_personas)
1754 /* Remove outdated Personas */
1755 for (l = removed; l != NULL; l = l->next)
1756 remove_persona (self, FOLKS_PERSONA (l->data));
1758 /* Add new Personas */
1759 for (l = added; l != NULL; l = l->next)
1760 add_persona (self, FOLKS_PERSONA (l->data));
1762 else if (!was_showing_personas && will_show_personas)
1764 /* Remove the old Individual table */
1765 individual_table_destroy (self);
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));
1771 else if (was_showing_personas && !will_show_personas)
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));
1779 /* Set up the Individual table instead */
1780 individual_table_set_up (self);
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);
1788 for (l = children; l != NULL; l = l->next)
1790 if (GTK_IS_SEPARATOR (l->data))
1792 gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1797 g_list_free (children);
1801 individual_removed_cb (FolksIndividual *individual,
1802 FolksIndividual *replacement_individual,
1803 EmpathyIndividualWidget *self)
1805 empathy_individual_widget_set_individual (self, replacement_individual);
1809 remove_individual (EmpathyIndividualWidget *self)
1811 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1812 if (priv->individual != NULL)
1814 GList *personas, *l;
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);
1827 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1829 g_signal_handlers_disconnect_by_func (priv->individual,
1830 notify_is_favourite_cb, self);
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);
1838 if (priv->contact != NULL)
1839 remove_weak_contact (self);
1841 tp_clear_object (&priv->individual);
1844 if (priv->details_cancellable != NULL)
1845 g_cancellable_cancel (priv->details_cancellable);
1849 individual_update (EmpathyIndividualWidget *self)
1851 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1853 /* Connect and get info from new Individual */
1854 if (priv->individual != NULL)
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);
1869 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1871 g_signal_connect (priv->individual, "notify::is-favourite",
1872 (GCallback) notify_is_favourite_cb, self);
1875 /* Update individual table */
1876 personas_changed_cb (priv->individual, NULL, NULL, self);
1879 if (priv->individual == NULL)
1881 gtk_widget_hide (priv->vbox_individual);
1883 else if (priv->individual_table != NULL)
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);
1891 /* We need to update the details for every Persona in the Individual */
1892 GList *personas, *l;
1894 personas = folks_individual_get_personas (priv->individual);
1895 for (l = personas; l != NULL; l = l->next)
1897 if (!TPF_IS_PERSONA (l->data))
1900 update_persona (self, FOLKS_PERSONA (l->data));
1903 gtk_widget_show (priv->vbox_individual);
1908 empathy_individual_widget_init (EmpathyIndividualWidget *self)
1910 EmpathyIndividualWidgetPriv *priv;
1914 priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1915 EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
1918 gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
1919 GTK_ORIENTATION_VERTICAL);
1921 filename = empathy_file_lookup ("empathy-individual-widget.ui",
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,
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,
1942 priv->table_location = NULL;
1944 gtk_box_pack_start (GTK_BOX (self), priv->vbox_individual_widget, TRUE, TRUE,
1946 gtk_widget_show (priv->vbox_individual_widget);
1948 priv->persona_tables = g_hash_table_new (NULL, NULL);
1949 priv->individual_table = NULL;
1951 /* Create widgets */
1952 details_set_up (self);
1954 g_object_unref (gui);
1958 constructed (GObject *object)
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);
1965 /* Allow scrolling of the list of Personas if we're showing Personas. */
1966 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS)
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);
1974 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1976 gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
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);
1986 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1991 if (klass->constructed != NULL)
1992 klass->constructed (object);
1996 get_property (GObject *object,
2001 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2005 case PROP_INDIVIDUAL:
2006 g_value_set_object (value, priv->individual);
2009 g_value_set_flags (value, priv->flags);
2012 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2018 set_property (GObject *object,
2020 const GValue *value,
2023 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2027 case PROP_INDIVIDUAL:
2028 empathy_individual_widget_set_individual (
2029 EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
2032 priv->flags = g_value_get_flags (value);
2035 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2041 dispose (GObject *object)
2043 remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
2045 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
2049 finalize (GObject *object)
2051 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2053 g_hash_table_destroy (priv->persona_tables);
2055 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
2059 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
2061 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
2070 * EmpathyIndividualWidget:individual:
2072 * The #FolksIndividual to display in the widget.
2074 g_object_class_install_property (object_class, PROP_INDIVIDUAL,
2075 g_param_spec_object ("individual",
2077 "The #FolksIndividual to display in the widget.",
2078 FOLKS_TYPE_INDIVIDUAL,
2079 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2082 * EmpathyIndividualWidget:flags:
2084 * A set of flags which affect the widget's behaviour.
2086 g_object_class_install_property (object_class, PROP_FLAGS,
2087 g_param_spec_flags ("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));
2094 g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
2098 * empathy_individual_widget_new:
2099 * @individual: the #FolksIndividual to display
2100 * @flags: flags affecting how the widget behaves and what it displays
2102 * Creates a new #EmpathyIndividualWidget.
2104 * Return value: a new #EmpathyIndividualWidget
2107 empathy_individual_widget_new (FolksIndividual *individual,
2108 EmpathyIndividualWidgetFlags flags)
2110 g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
2113 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
2114 "individual", individual,
2120 * empathy_individual_widget_get_individual:
2121 * @self: an #EmpathyIndividualWidget
2123 * Returns the #FolksIndividual being displayed by the widget.
2125 * Return value: the #FolksIndividual being displayed, or %NULL
2128 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
2130 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
2132 return GET_PRIV (self)->individual;
2136 * empathy_individual_widget_set_individual:
2137 * @self: an #EmpathyIndividualWidget
2138 * @individual: the #FolksIndividual to display, or %NULL
2140 * Set the #FolksIndividual to be displayed by the widget:
2141 * #EmpathyIndividualWidget:individual.
2143 * The @individual may be %NULL in order to display nothing in the widget.
2146 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
2147 FolksIndividual *individual)
2149 EmpathyIndividualWidgetPriv *priv;
2151 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
2152 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
2154 priv = GET_PRIV (self);
2156 if (individual == priv->individual)
2159 /* Out with the old… */
2160 remove_individual (self);
2162 /* …and in with the new. */
2163 if (individual != NULL)
2164 g_object_ref (individual);
2165 priv->individual = individual;
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);