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 = FOLKS_PRESENCE (l->data);
225 if (folks_presence_typecmp (folks_presence_get_presence_type (presence),
227 && TPF_IS_PERSONA (presence))
229 presence_type = folks_presence_get_presence_type (presence);
230 tp_contact = tpf_persona_get_contact (TPF_PERSONA (l->data));
235 if (tp_contact != NULL)
237 priv->contact = tp_contact;
238 g_object_add_weak_pointer (G_OBJECT (tp_contact),
239 (gpointer *) &priv->contact);
244 EmpathyIndividualWidget *widget; /* weak */
245 TpContact *contact; /* owned */
249 details_data_free (DetailsData *data)
251 if (data->widget != NULL)
253 g_object_remove_weak_pointer (G_OBJECT (data->widget),
254 (gpointer *) &data->widget);
256 g_object_unref (data->contact);
257 g_slice_free (DetailsData, data);
261 details_update_show (EmpathyIndividualWidget *self,
264 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
268 info = tp_contact_get_contact_info (contact);
269 info = g_list_sort (info, (GCompareFunc) contact_info_field_cmp);
270 for (l = info; l != NULL; l = l->next)
272 TpContactInfoField *field = l->data;
273 InfoFieldData *field_data;
277 if (field->field_value == NULL || field->field_value[0] == NULL)
280 value = field->field_value[0];
282 field_data = find_info_field_data (field->field_name);
283 if (field_data == NULL)
285 DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
290 w = gtk_label_new (_(field_data->title));
291 gtk_table_attach (GTK_TABLE (priv->table_details),
292 w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
293 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
297 w = gtk_label_new (value);
298 if (field_data->linkify == TRUE)
302 markup = empathy_add_link_markup (value);
303 gtk_label_set_markup (GTK_LABEL (w), markup);
307 gtk_label_set_selectable (GTK_LABEL (w),
308 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
310 gtk_table_attach_defaults (GTK_TABLE (priv->table_details),
311 w, 1, 2, n_rows, n_rows + 1);
312 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
323 details_notify_cb (TpContact *contact,
325 EmpathyIndividualWidget *self)
327 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
330 gtk_container_foreach (GTK_CONTAINER (priv->table_details),
331 (GtkCallback) gtk_widget_destroy, NULL);
333 n_rows = details_update_show (self, contact);
337 gtk_widget_show (priv->vbox_details);
338 gtk_widget_show (priv->table_details);
342 gtk_widget_hide (priv->vbox_details);
345 gtk_widget_hide (priv->hbox_details_requested);
346 gtk_spinner_stop (GTK_SPINNER (priv->details_spinner));
350 details_request_cb (TpContact *contact,
354 EmpathyIndividualWidget *self = data->widget;
355 gboolean hide_widget = FALSE;
356 GError *error = NULL;
358 if (tp_contact_request_contact_info_finish (contact, res, &error) == TRUE)
359 details_notify_cb (contact, NULL, self);
363 g_clear_error (&error);
367 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
369 if (hide_widget == TRUE)
370 gtk_widget_hide (GET_PRIV (self)->vbox_details);
372 tp_clear_object (&priv->details_cancellable);
374 g_signal_connect (contact, "notify::contact-info",
375 (GCallback) details_notify_cb, self);
378 details_data_free (data);
382 details_feature_prepared_cb (TpConnection *connection,
386 EmpathyIndividualWidget *self = data->widget;
387 EmpathyIndividualWidgetPriv *priv = NULL;
389 if (tp_proxy_prepare_finish (connection, res, NULL) == FALSE || self == NULL)
392 gtk_widget_hide (GET_PRIV (self)->vbox_details);
393 details_data_free (data);
397 priv = GET_PRIV (self);
399 /* Request the Individual's info */
400 gtk_widget_show (priv->vbox_details);
401 gtk_widget_show (priv->hbox_details_requested);
402 gtk_widget_hide (priv->table_details);
403 gtk_spinner_start (GTK_SPINNER (priv->details_spinner));
405 if (priv->details_cancellable == NULL)
407 priv->details_cancellable = g_cancellable_new ();
408 tp_contact_request_contact_info_async (data->contact,
409 priv->details_cancellable, (GAsyncReadyCallback) details_request_cb,
415 details_update (EmpathyIndividualWidget *self)
417 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
418 TpContact *tp_contact = NULL;
420 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS))
423 gtk_widget_hide (priv->vbox_details);
425 if (priv->contact == NULL)
426 update_weak_contact (self);
428 if (priv->contact != NULL)
430 GQuark features[] = { TP_CONNECTION_FEATURE_CONTACT_INFO, 0 };
431 TpConnection *connection;
434 data = g_slice_new (DetailsData);
436 g_object_add_weak_pointer (G_OBJECT (self), (gpointer *) &data->widget);
437 data->contact = g_object_ref (priv->contact);
439 /* First, make sure the CONTACT_INFO feature is ready on the connection */
440 connection = tp_contact_get_connection (tp_contact);
441 tp_proxy_prepare_async (connection, features,
442 (GAsyncReadyCallback) details_feature_prepared_cb, data);
447 groups_update (EmpathyIndividualWidget *self)
449 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
451 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS &&
452 priv->individual != NULL)
454 empathy_groups_widget_set_groupable (
455 EMPATHY_GROUPS_WIDGET (priv->groups_widget),
456 FOLKS_GROUPABLE (priv->individual));
457 gtk_widget_show (priv->groups_widget);
461 gtk_widget_hide (priv->groups_widget);
465 /* Converts the Location's GHashTable's key to a user readable string */
467 location_key_to_label (const gchar *key)
469 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
470 return _("Country ISO Code:");
471 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
472 return _("Country:");
473 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
475 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
477 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
479 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
480 return _("Postal Code:");
481 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
483 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
484 return _("Building:");
485 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
487 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
489 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
491 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
492 return _("Description:");
493 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
495 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
496 return _("Accuracy Level:");
497 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
499 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
500 return _("Vertical Error (meters):");
501 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
502 return _("Horizontal Error (meters):");
503 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
505 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
506 return _("Bearing:");
507 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
508 return _("Climb Speed:");
509 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
510 return _("Last Updated on:");
511 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
512 return _("Longitude:");
513 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
514 return _("Latitude:");
515 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
516 return _("Altitude:");
519 DEBUG ("Unexpected Location key: %s", key);
525 location_update (EmpathyIndividualWidget *self)
527 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
528 EmpathyContact *contact = NULL;
529 GHashTable *location = NULL;
533 static const gchar* ordered_geolocation_keys[] = {
534 EMPATHY_LOCATION_TEXT,
535 EMPATHY_LOCATION_URI,
536 EMPATHY_LOCATION_DESCRIPTION,
537 EMPATHY_LOCATION_BUILDING,
538 EMPATHY_LOCATION_FLOOR,
539 EMPATHY_LOCATION_ROOM,
540 EMPATHY_LOCATION_STREET,
541 EMPATHY_LOCATION_AREA,
542 EMPATHY_LOCATION_LOCALITY,
543 EMPATHY_LOCATION_REGION,
544 EMPATHY_LOCATION_COUNTRY,
549 gboolean display_map = FALSE;
552 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION) ||
553 priv->individual == NULL)
555 gtk_widget_hide (priv->vbox_location);
559 /* FIXME: For the moment, we just display the first location data we can
560 * find amongst the Individual's Personas. Once libfolks grows a location
561 * interface, we can use that. (bgo#627400) */
562 personas = folks_individual_get_personas (priv->individual);
563 for (l = personas; l != NULL; l = l->next)
565 FolksPersona *persona = FOLKS_PERSONA (l->data);
567 if (TPF_IS_PERSONA (persona))
569 TpContact *tp_contact;
571 /* Get the contact. If it turns out to have location information, we
572 * have to keep it alive for the duration of the function, since we're
573 * accessing its private data. */
574 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
575 contact = empathy_contact_dup_from_tp_contact (tp_contact);
576 empathy_contact_set_persona (contact, persona);
578 /* Try and get a location */
579 location = empathy_contact_get_location (contact);
580 if (location != NULL && g_hash_table_size (location) > 0)
584 tp_clear_object (&contact);
588 if (contact == NULL || location == NULL)
590 gtk_widget_hide (priv->vbox_location);
591 tp_clear_object (&contact);
595 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
598 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
599 gtk_label_set_markup (GTK_LABEL (priv->label_location), loc);
610 stamp = g_value_get_int64 (value);
613 user_date = empathy_time_to_string_relative (time_);
615 tmp = g_strdup_printf ("<b>%s</b>", _("Location"));
616 /* translators: format is "Location, $date" */
617 text = g_strdup_printf (_("%s, %s"), tmp, user_date);
619 gtk_label_set_markup (GTK_LABEL (priv->label_location), text);
624 /* Prepare the location information table */
625 if (priv->table_location != NULL)
626 gtk_widget_destroy (priv->table_location);
628 priv->table_location = gtk_table_new (1, 2, FALSE);
629 gtk_box_pack_start (GTK_BOX (priv->subvbox_location),
630 priv->table_location, FALSE, FALSE, 5);
633 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
635 const gchar* user_label;
639 gvalue = g_hash_table_lookup (location, (gpointer) skey);
643 user_label = location_key_to_label (skey);
645 label = gtk_label_new (user_label);
646 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
647 gtk_table_attach (GTK_TABLE (priv->table_location),
648 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
649 gtk_widget_show (label);
651 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
654 dvalue = g_value_get_double (gvalue);
655 svalue = g_strdup_printf ("%f", dvalue);
657 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
659 svalue = g_value_dup_string (gvalue);
661 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
665 time_ = g_value_get_int64 (value);
666 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
671 label = gtk_label_new (svalue);
672 gtk_table_attach_defaults (GTK_TABLE (priv->table_location),
673 label, 1, 2, row, row + 1);
674 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
675 gtk_widget_show (label);
677 gtk_label_set_selectable (GTK_LABEL (label),
678 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE :
686 tp_clear_object (&contact);
688 #ifdef HAVE_LIBCHAMPLAIN
689 if ((g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL) &&
690 (g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL) &&
691 !(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
693 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
701 /* We can display some fields */
702 gtk_widget_show (priv->table_location);
704 else if (display_map == FALSE)
706 /* Can't display either fields or map */
707 gtk_widget_hide (priv->vbox_location);
711 #ifdef HAVE_LIBCHAMPLAIN
712 if (display_map == TRUE)
715 ChamplainLayer *layer;
717 priv->map_view_embed = gtk_champlain_embed_new ();
718 priv->map_view = gtk_champlain_embed_get_view (
719 GTK_CHAMPLAIN_EMBED (priv->map_view_embed));
721 gtk_container_add (GTK_CONTAINER (priv->viewport_map),
722 priv->map_view_embed);
723 g_object_set (G_OBJECT (priv->map_view),
724 "show-license", TRUE,
725 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
729 layer = champlain_layer_new ();
730 champlain_view_add_layer (priv->map_view, layer);
731 markers = g_ptr_array_new ();
733 /* FIXME: For now, we have to do this manually. Once libfolks grows a
734 * location interface, we can use that. (bgo#627400) */
735 personas = folks_individual_get_personas (priv->individual);
736 for (l = personas; l != NULL; l = l->next)
738 FolksPersona *persona = FOLKS_PERSONA (l->data);
740 if (TPF_IS_PERSONA (persona))
742 gdouble lat = 0.0, lon = 0.0;
743 ClutterActor *marker;
744 TpContact *tp_contact;
746 /* Get the contact */
747 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
748 contact = empathy_contact_dup_from_tp_contact (tp_contact);
749 empathy_contact_set_persona (contact, persona);
751 /* Try and get a location */
752 location = empathy_contact_get_location (contact);
753 if (location == NULL || g_hash_table_size (location) == 0)
755 g_object_unref (contact);
759 /* Get this persona's latitude and longitude */
760 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
763 g_object_unref (contact);
767 lat = g_value_get_double (value);
769 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
772 g_object_unref (contact);
776 lon = g_value_get_double (value);
778 /* Add a marker to the map */
779 marker = champlain_marker_new_with_text (
780 folks_aliasable_get_alias (FOLKS_ALIASABLE (persona)), NULL,
782 champlain_base_marker_set_position (
783 CHAMPLAIN_BASE_MARKER (marker), lat, lon);
784 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
786 g_ptr_array_add (markers, marker);
788 g_object_unref (contact);
792 /* Zoom to show all of the markers */
793 g_ptr_array_add (markers, NULL); /* NULL-terminate the array */
794 champlain_view_ensure_markers_visible (priv->map_view,
795 (ChamplainBaseMarker **) markers->pdata, FALSE);
796 g_ptr_array_free (markers, TRUE);
798 gtk_widget_show_all (priv->viewport_map);
802 gtk_widget_show (priv->vbox_location);
806 client_types_notify_cb (TpContact *contact,
808 EmpathyIndividualWidget *self)
810 client_types_update (self);
814 client_types_update (EmpathyIndividualWidget *self)
816 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
817 const gchar * const *types;
819 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_CLIENT_TYPES) ||
820 priv->individual == NULL)
822 gtk_widget_hide (priv->hbox_client_types);
826 if (priv->contact == NULL)
827 update_weak_contact (self);
829 /* let's try that again... */
830 if (priv->contact == NULL)
833 types = tp_contact_get_client_types (priv->contact);
836 && g_strv_length ((gchar **) types) > 0
837 && !tp_strdiff (types[0], "phone"))
839 gtk_widget_show (priv->hbox_client_types);
843 gtk_widget_hide (priv->hbox_client_types);
846 g_signal_connect (priv->contact, "notify::client-types",
847 (GCallback) client_types_notify_cb, self);
851 remove_weak_contact (EmpathyIndividualWidget *self)
853 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
855 if (priv->contact == NULL)
858 g_object_remove_weak_pointer (G_OBJECT (priv->contact),
859 (gpointer *) &priv->contact);
860 priv->contact = NULL;
863 static EmpathyAvatar *
864 persona_dup_avatar (FolksPersona *persona)
866 TpContact *tp_contact;
867 EmpathyContact *contact;
868 EmpathyAvatar *avatar;
870 if (!TPF_IS_PERSONA (persona))
873 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
874 contact = empathy_contact_dup_from_tp_contact (tp_contact);
875 empathy_contact_set_persona (contact, persona);
877 avatar = empathy_contact_get_avatar (contact);
879 empathy_avatar_ref (avatar);
880 g_object_unref (contact);
885 static EmpathyAvatar *
886 individual_dup_avatar (FolksIndividual *individual)
889 EmpathyAvatar *avatar = NULL;
891 /* FIXME: We just choose the first Persona which has an avatar, and save that.
892 * The avatar handling in EmpathyContact needs to be moved into libfolks as
893 * much as possible, and this code rewritten to use FolksAvatar.
895 personas = folks_individual_get_personas (individual);
896 for (l = personas; l != NULL; l = l->next)
898 avatar = persona_dup_avatar (FOLKS_PERSONA (l->data));
907 save_avatar_menu_activate_cb (GtkWidget *widget,
908 EmpathyIndividualWidget *self)
910 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
912 EmpathyAvatar *avatar;
913 gchar *ext = NULL, *filename;
915 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
917 GTK_FILE_CHOOSER_ACTION_SAVE,
918 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
919 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
922 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
925 avatar = individual_dup_avatar (priv->individual);
929 /* look for the avatar extension */
930 if (avatar->format != NULL)
934 splitted = g_strsplit (avatar->format, "/", 2);
935 if (splitted[0] != NULL && splitted[1] != NULL)
936 ext = g_strdup (splitted[1]);
938 g_strfreev (splitted);
942 /* Avatar was loaded from the cache so was converted to PNG */
943 ext = g_strdup ("png");
950 id = tp_escape_as_identifier (folks_individual_get_id (priv->individual));
952 filename = g_strdup_printf ("%s.%s", id, ext);
953 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
960 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
962 GError *error = NULL;
964 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
966 if (empathy_avatar_save_to_file (avatar, filename, &error) == FALSE)
969 GtkWidget *error_dialog;
971 error_dialog = gtk_message_dialog_new (NULL, 0,
972 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
973 _("Unable to save avatar"));
975 gtk_message_dialog_format_secondary_text (
976 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
978 g_signal_connect (error_dialog, "response",
979 (GCallback) gtk_widget_destroy, NULL);
981 gtk_window_present (GTK_WINDOW (error_dialog));
983 g_clear_error (&error);
989 gtk_widget_destroy (dialog);
990 empathy_avatar_unref (avatar);
994 popup_avatar_menu (EmpathyIndividualWidget *self,
996 GdkEventButton *event)
998 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
999 GtkWidget *menu, *item;
1000 EmpathyAvatar *avatar;
1001 gint button, event_time;
1003 if (priv->individual == NULL)
1006 avatar = individual_dup_avatar (priv->individual);
1009 empathy_avatar_unref (avatar);
1011 menu = gtk_menu_new ();
1013 /* Add "Save as..." entry */
1014 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
1015 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1016 gtk_widget_show (item);
1018 g_signal_connect (item, "activate",
1019 (GCallback) save_avatar_menu_activate_cb, self);
1023 button = event->button;
1024 event_time = event->time;
1029 event_time = gtk_get_current_event_time ();
1032 gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
1033 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, button, event_time);
1034 g_object_ref_sink (menu);
1035 g_object_unref (menu);
1041 avatar_widget_popup_menu_cb (GtkWidget *widget,
1042 EmpathyIndividualWidget *self)
1044 return popup_avatar_menu (self, widget, NULL);
1048 avatar_widget_button_press_event_cb (GtkWidget *widget,
1049 GdkEventButton *event,
1050 EmpathyIndividualWidget *self)
1052 /* Ignore double-clicks and triple-clicks */
1053 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1054 return popup_avatar_menu (self, widget, event);
1059 /* Returns the TpAccount for the user as a convenience. Note that it has a ref
1062 individual_is_user (FolksIndividual *individual)
1064 GList *personas, *l;
1066 /* FIXME: This should move into libfolks when libfolks grows a way of
1067 * determining "self". (bgo#627402) */
1068 personas = folks_individual_get_personas (individual);
1069 for (l = personas; l != NULL; l = l->next)
1071 FolksPersona *persona = FOLKS_PERSONA (l->data);
1073 if (TPF_IS_PERSONA (persona))
1075 TpContact *tp_contact;
1076 EmpathyContact *contact;
1078 /* Get the contact */
1079 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1080 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1081 empathy_contact_set_persona (contact, persona);
1083 /* Determine if the contact is the user */
1084 if (empathy_contact_is_user (contact))
1086 g_object_unref (contact);
1087 return g_object_ref (empathy_contact_get_account (contact));
1090 g_object_unref (contact);
1098 set_nickname_cb (TpAccount *account,
1099 GAsyncResult *result,
1102 GError *error = NULL;
1104 if (tp_account_set_nickname_finish (account, result, &error) == FALSE)
1106 DEBUG ("Failed to set Account.Nickname: %s", error->message);
1107 g_error_free (error);
1112 entry_alias_focus_event_cb (GtkEditable *editable,
1113 GdkEventFocus *event,
1114 EmpathyIndividualWidget *self)
1116 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1118 if (priv->individual != NULL)
1123 alias = gtk_entry_get_text (GTK_ENTRY (editable));
1124 account = individual_is_user (priv->individual);
1126 if (account != NULL)
1128 DEBUG ("Set Account.Nickname to %s", alias);
1129 tp_account_set_nickname_async (account, alias,
1130 (GAsyncReadyCallback) set_nickname_cb, NULL);
1131 g_object_unref (account);
1135 folks_aliasable_set_alias (FOLKS_ALIASABLE (priv->individual), alias);
1143 favourite_toggled_cb (GtkToggleButton *button,
1144 EmpathyIndividualWidget *self)
1146 gboolean active = gtk_toggle_button_get_active (button);
1147 folks_favourite_set_is_favourite (
1148 FOLKS_FAVOURITE (GET_PRIV (self)->individual), active);
1152 notify_avatar_cb (gpointer folks_object,
1154 EmpathyIndividualWidget *self)
1156 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1157 EmpathyAvatar *avatar = NULL;
1159 GtkWidget *avatar_widget;
1161 if (FOLKS_IS_INDIVIDUAL (folks_object))
1163 avatar = individual_dup_avatar (FOLKS_INDIVIDUAL (folks_object));
1164 table = G_OBJECT (priv->individual_table);
1166 else if (FOLKS_IS_PERSONA (folks_object))
1168 avatar = persona_dup_avatar (FOLKS_PERSONA (folks_object));
1169 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1173 g_assert_not_reached ();
1179 avatar_widget = g_object_get_data (table, "avatar-widget");
1180 empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (avatar_widget), avatar);
1183 empathy_avatar_unref (avatar);
1187 notify_alias_cb (gpointer folks_object,
1189 EmpathyIndividualWidget *self)
1191 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1193 GtkWidget *alias_widget;
1195 if (FOLKS_IS_INDIVIDUAL (folks_object))
1196 table = G_OBJECT (priv->individual_table);
1197 else if (FOLKS_IS_PERSONA (folks_object))
1198 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1200 g_assert_not_reached ();
1205 alias_widget = g_object_get_data (table, "alias-widget");
1207 if (GTK_IS_ENTRY (alias_widget))
1209 gtk_entry_set_text (GTK_ENTRY (alias_widget),
1210 folks_aliasable_get_alias (FOLKS_ALIASABLE (folks_object)));
1214 gtk_label_set_label (GTK_LABEL (alias_widget),
1215 folks_aliasable_get_alias (FOLKS_ALIASABLE (folks_object)));
1220 notify_presence_cb (gpointer folks_object,
1222 EmpathyIndividualWidget *self)
1224 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1226 GtkWidget *status_label, *state_image;
1227 const gchar *message;
1228 gchar *markup_text = NULL;
1230 if (FOLKS_IS_INDIVIDUAL (folks_object))
1231 table = G_OBJECT (priv->individual_table);
1232 else if (FOLKS_IS_PERSONA (folks_object))
1233 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1235 g_assert_not_reached ();
1240 status_label = g_object_get_data (table, "status-label");
1241 state_image = g_object_get_data (table, "state-image");
1243 /* FIXME: Default messages should be moved into libfolks (bgo#627403) */
1244 message = folks_presence_get_presence_message (FOLKS_PRESENCE (folks_object));
1245 if (EMP_STR_EMPTY (message))
1247 message = empathy_presence_get_default_message (
1248 folks_presence_get_presence_type (FOLKS_PRESENCE (folks_object)));
1251 if (message != NULL)
1252 markup_text = empathy_add_link_markup (message);
1253 gtk_label_set_markup (GTK_LABEL (status_label), markup_text);
1254 g_free (markup_text);
1256 gtk_image_set_from_icon_name (GTK_IMAGE (state_image),
1257 empathy_icon_name_for_presence (
1258 folks_presence_get_presence_type (FOLKS_PRESENCE (folks_object))),
1259 GTK_ICON_SIZE_BUTTON);
1260 gtk_widget_show (state_image);
1264 notify_is_favourite_cb (gpointer folks_object,
1266 EmpathyIndividualWidget *self)
1268 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1270 GtkWidget *favourite_widget;
1272 if (FOLKS_IS_INDIVIDUAL (folks_object))
1273 table = G_OBJECT (priv->individual_table);
1274 else if (FOLKS_IS_PERSONA (folks_object))
1275 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1277 g_assert_not_reached ();
1282 favourite_widget = g_object_get_data (table, "favourite-widget");
1284 if (GTK_IS_TOGGLE_BUTTON (favourite_widget))
1286 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (favourite_widget),
1287 folks_favourite_get_is_favourite (FOLKS_FAVOURITE (folks_object)));
1292 alias_presence_avatar_favourite_set_up (EmpathyIndividualWidget *self,
1296 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1297 GtkWidget *label, *alias, *image, *avatar, *alignment;
1298 guint current_row = starting_row;
1301 label = gtk_label_new (_("Alias:"));
1302 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1303 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1, GTK_FILL,
1305 gtk_widget_show (label);
1307 /* Set up alias label/entry */
1308 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS)
1310 alias = gtk_entry_new ();
1312 g_signal_connect (alias, "focus-out-event",
1313 (GCallback) entry_alias_focus_event_cb, self);
1315 /* Make return activate the window default (the Close button) */
1316 gtk_entry_set_activates_default (GTK_ENTRY (alias), TRUE);
1320 alias = gtk_label_new (NULL);
1321 gtk_label_set_selectable (GTK_LABEL (alias),
1322 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1323 gtk_misc_set_alignment (GTK_MISC (alias), 0.0, 0.5);
1326 g_object_set_data (G_OBJECT (table), "alias-widget", alias);
1327 gtk_table_attach (table, alias, 1, 2, current_row, current_row + 1,
1328 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1329 gtk_widget_show (alias);
1334 priv->hbox_presence = gtk_hbox_new (FALSE, 6);
1336 /* Presence image */
1337 image = gtk_image_new_from_stock (GTK_STOCK_MISSING_IMAGE,
1338 GTK_ICON_SIZE_BUTTON);
1339 g_object_set_data (G_OBJECT (table), "state-image", image);
1340 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), image, FALSE,
1342 gtk_widget_show (image);
1344 label = gtk_label_new ("");
1345 gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
1346 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1347 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1349 gtk_label_set_selectable (GTK_LABEL (label),
1350 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1352 g_object_set_data (G_OBJECT (table), "status-label", label);
1353 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, TRUE,
1355 gtk_widget_show (label);
1357 gtk_table_attach (table, priv->hbox_presence, 0, 2, current_row,
1358 current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1359 gtk_widget_show (priv->hbox_presence);
1363 /* Set up favourite toggle button */
1364 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1366 GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1368 g_signal_connect (favourite, "toggled",
1369 (GCallback) favourite_toggled_cb, self);
1371 g_object_set_data (G_OBJECT (table), "favourite-widget", favourite);
1372 gtk_table_attach (table, favourite, 0, 2, current_row, current_row + 1,
1373 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1374 gtk_widget_show (favourite);
1379 /* Set up avatar display */
1380 avatar = empathy_avatar_image_new ();
1382 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1384 g_signal_connect (avatar, "popup-menu",
1385 (GCallback) avatar_widget_popup_menu_cb, self);
1386 g_signal_connect (avatar, "button-press-event",
1387 (GCallback) avatar_widget_button_press_event_cb, self);
1390 g_object_set_data (G_OBJECT (table), "avatar-widget", avatar);
1392 alignment = gtk_alignment_new (1.0, 0.0, 0.0, 0.0);
1393 gtk_container_add (GTK_CONTAINER (alignment), avatar);
1394 gtk_widget_show (avatar);
1396 gtk_table_attach (table, alignment, 2, 3, 0, current_row,
1397 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 6, 6);
1398 gtk_widget_show (alignment);
1402 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1404 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1405 TpContact *tp_contact;
1406 EmpathyContact *contact;
1413 table = g_hash_table_lookup (priv->persona_tables, persona);
1415 g_assert (table != NULL);
1417 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1418 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1419 empathy_contact_set_persona (contact, persona);
1421 account = empathy_contact_get_account (contact);
1423 /* Update account widget */
1424 if (account != NULL)
1428 label = g_object_get_data (G_OBJECT (table), "account-label");
1429 image = g_object_get_data (G_OBJECT (table), "account-image");
1431 name = tp_account_get_display_name (account);
1432 gtk_label_set_label (label, name);
1434 name = tp_account_get_icon_name (account);
1435 gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
1438 /* Update id widget */
1439 label = g_object_get_data (G_OBJECT (table), "id-widget");
1440 id = folks_persona_get_display_id (persona);
1441 gtk_label_set_label (label, (id != NULL) ? id : "");
1443 /* Update other widgets */
1444 notify_alias_cb (persona, NULL, self);
1445 notify_presence_cb (persona, NULL, self);
1446 notify_avatar_cb (persona, NULL, self);
1448 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1449 notify_is_favourite_cb (persona, NULL, self);
1451 g_object_unref (contact);
1455 add_persona (EmpathyIndividualWidget *self,
1456 FolksPersona *persona)
1458 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1461 GtkWidget *label, *account_label, *account_image, *separator;
1462 guint current_row = 0;
1464 if (!TPF_IS_PERSONA (persona))
1467 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1468 table = GTK_TABLE (gtk_table_new (5, 3, FALSE));
1470 table = GTK_TABLE (gtk_table_new (4, 3, FALSE));
1471 gtk_table_set_row_spacings (table, 6);
1472 gtk_table_set_col_spacings (table, 6);
1474 /* Account and Identifier */
1475 label = gtk_label_new (_("Account:"));
1476 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1477 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1478 GTK_FILL, GTK_FILL, 0, 0);
1479 gtk_widget_show (label);
1481 /* Pack the protocol icon with the account name in an hbox */
1482 hbox = GTK_BOX (gtk_hbox_new (FALSE, 6));
1484 account_label = gtk_label_new (NULL);
1485 gtk_label_set_selectable (GTK_LABEL (account_label),
1486 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1487 gtk_misc_set_alignment (GTK_MISC (account_label), 0.0, 0.5);
1488 gtk_widget_show (account_label);
1490 account_image = gtk_image_new ();
1491 gtk_widget_show (account_image);
1493 gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1494 gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
1496 g_object_set_data (G_OBJECT (table), "account-image", account_image);
1497 g_object_set_data (G_OBJECT (table), "account-label", account_label);
1498 gtk_table_attach (table, GTK_WIDGET (hbox), 1, 2, current_row,
1499 current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1500 gtk_widget_show (GTK_WIDGET (hbox));
1504 /* Translators: Identifier to connect to Instant Messaging network */
1505 label = gtk_label_new (_("Identifier:"));
1506 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1507 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1508 GTK_FILL, GTK_FILL, 0, 0);
1509 gtk_widget_show (label);
1511 /* Set up ID label */
1512 label = gtk_label_new (NULL);
1513 gtk_label_set_selectable (GTK_LABEL (label),
1514 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1515 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1517 g_object_set_data (G_OBJECT (table), "id-widget", label);
1518 gtk_table_attach (table, label, 1, 2, current_row, current_row + 1,
1519 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1520 gtk_widget_show (label);
1524 alias_presence_avatar_favourite_set_up (self, table, current_row);
1526 /* Connect to signals and display the table */
1527 g_signal_connect (persona, "notify::alias",
1528 (GCallback) notify_alias_cb, self);
1529 g_signal_connect (persona, "notify::avatar",
1530 (GCallback) notify_avatar_cb, self);
1531 g_signal_connect (persona, "notify::presence-type",
1532 (GCallback) notify_presence_cb, self);
1533 g_signal_connect (persona, "notify::presence-message",
1534 (GCallback) notify_presence_cb, self);
1536 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1538 g_signal_connect (persona, "notify::is-favourite",
1539 (GCallback) notify_is_favourite_cb, self);
1542 gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1543 GTK_WIDGET (table), FALSE, TRUE, 0);
1544 gtk_widget_show (GTK_WIDGET (table));
1546 /* Pack a separator after the table */
1547 separator = gtk_hseparator_new ();
1548 g_object_set_data (G_OBJECT (table), "separator", separator);
1549 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), separator, FALSE, FALSE,
1551 gtk_widget_show (separator);
1553 g_hash_table_replace (priv->persona_tables, persona, table);
1555 /* Update the new widgets */
1556 update_persona (self, persona);
1560 remove_persona (EmpathyIndividualWidget *self,
1561 FolksPersona *persona)
1563 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1564 GtkWidget *separator;
1567 if (!TPF_IS_PERSONA (persona))
1570 table = g_hash_table_lookup (priv->persona_tables, persona);
1574 g_signal_handlers_disconnect_by_func (persona, notify_alias_cb, self);
1575 g_signal_handlers_disconnect_by_func (persona, notify_avatar_cb, self);
1576 g_signal_handlers_disconnect_by_func (persona, notify_presence_cb, self);
1578 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1580 g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
1584 /* Remove the separator */
1585 separator = g_object_get_data (G_OBJECT (table), "separator");
1586 if (separator != NULL)
1587 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual), separator);
1589 /* Remove the widget */
1590 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1591 GTK_WIDGET (table));
1593 g_hash_table_remove (priv->persona_tables, persona);
1597 update_individual_table (EmpathyIndividualWidget *self)
1599 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1601 notify_alias_cb (priv->individual, NULL, self);
1602 notify_presence_cb (priv->individual, NULL, self);
1603 notify_avatar_cb (priv->individual, NULL, self);
1605 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1606 notify_is_favourite_cb (priv->individual, NULL, self);
1610 individual_table_set_up (EmpathyIndividualWidget *self)
1612 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1614 guint current_row = 0;
1617 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1620 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1623 table = GTK_TABLE (gtk_table_new (nb_rows, 3, FALSE));
1624 gtk_table_set_row_spacings (table, 6);
1625 gtk_table_set_col_spacings (table, 6);
1627 /* We only display the number of personas in tooltips */
1628 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1632 GList *personas, *l;
1633 guint num_personas = 0;
1635 /* Meta-contacts message displaying how many Telepathy personas we have */
1636 personas = folks_individual_get_personas (priv->individual);
1637 for (l = personas; l != NULL; l = l->next)
1639 if (TPF_IS_PERSONA (l->data))
1643 message = g_strdup_printf (ngettext ("Meta-contact containing %u contact",
1644 "Meta-contact containing %u contacts", num_personas), num_personas);
1645 label = gtk_label_new (message);
1646 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1649 gtk_table_attach (table, label, 0, 2, current_row, current_row + 1,
1650 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1651 gtk_widget_show (label);
1656 alias_presence_avatar_favourite_set_up (self, table, current_row);
1658 /* Display the table */
1659 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (table),
1661 gtk_widget_show (GTK_WIDGET (table));
1663 priv->individual_table = table;
1665 /* Update the table */
1666 update_individual_table (self);
1670 individual_table_destroy (EmpathyIndividualWidget *self)
1672 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1674 if (priv->individual_table == NULL)
1677 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1678 GTK_WIDGET (priv->individual_table));
1679 priv->individual_table = NULL;
1683 personas_changed_cb (FolksIndividual *individual,
1686 EmpathyIndividualWidget *self)
1688 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1689 GList *personas, *l, *children;
1690 gboolean show_personas, was_showing_personas, will_show_personas, is_last;
1691 guint old_num_personas, new_num_personas = 0;
1693 personas = folks_individual_get_personas (individual);
1695 /* Note that old_num_personas is the number of persona tables we were
1696 * displaying, not the number of Personas which were in the Individual
1698 old_num_personas = g_hash_table_size (priv->persona_tables);
1700 for (l = personas; l != NULL; l = l->next)
1702 if (TPF_IS_PERSONA (l->data))
1707 * What we display for various conditions:
1708 * - "Personas": display the alias, avatar, presence account and identifier
1709 * for each of the Individual's Personas. (i.e. One table per
1711 * - "Individual": display the alias, avatar and presence for the Individual,
1712 * and a label saying "Meta-contact containing x contacts".
1713 * (i.e. One table in total.)
1715 * | SHOW_PERSONAS | !SHOW_PERSONAS
1716 * -------------+---------------+---------------
1717 * > 1 Persona | Personas | Individual
1718 * -------------+---------------+---------------
1719 * == 1 Persona | Personas | Personas
1721 show_personas = (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) != 0;
1722 was_showing_personas = show_personas || old_num_personas == 1;
1723 will_show_personas = show_personas || new_num_personas == 1;
1725 /* If both @added and @removed are NULL, we're being called manually, and we
1726 * need to set up the tables for the first time. We do this simply by
1727 * ensuring was_showing_personas and will_show_personas are different so that
1728 * the code resets the UI.
1730 if (added == NULL && removed == NULL)
1731 was_showing_personas = !will_show_personas;
1733 if (was_showing_personas && will_show_personas)
1735 /* Remove outdated Personas */
1736 for (l = removed; l != NULL; l = l->next)
1737 remove_persona (self, FOLKS_PERSONA (l->data));
1739 /* Add new Personas */
1740 for (l = added; l != NULL; l = l->next)
1741 add_persona (self, FOLKS_PERSONA (l->data));
1743 else if (!was_showing_personas && will_show_personas)
1745 /* Remove the old Individual table */
1746 individual_table_destroy (self);
1748 /* Set up all the Persona tables instead */
1749 for (l = personas; l != NULL; l = l->next)
1750 add_persona (self, FOLKS_PERSONA (l->data));
1752 else if (was_showing_personas && !will_show_personas)
1754 /* Remove all Personas */
1755 for (l = personas; l != NULL; l = l->next)
1756 remove_persona (self, FOLKS_PERSONA (l->data));
1757 for (l = removed; l != NULL; l = l->next)
1758 remove_persona (self, FOLKS_PERSONA (l->data));
1760 /* Set up the Individual table instead */
1761 individual_table_set_up (self);
1764 /* Hide the last separator and show the others */
1765 children = gtk_container_get_children (GTK_CONTAINER (priv->vbox_individual));
1766 children = g_list_reverse (children);
1769 for (l = children; l != NULL; l = l->next)
1771 if (GTK_IS_SEPARATOR (l->data))
1773 gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1778 g_list_free (children);
1782 individual_removed_cb (FolksIndividual *individual,
1783 FolksIndividual *replacement_individual,
1784 EmpathyIndividualWidget *self)
1786 empathy_individual_widget_set_individual (self, replacement_individual);
1790 remove_individual (EmpathyIndividualWidget *self)
1792 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1793 if (priv->individual != NULL)
1795 GList *personas, *l;
1797 g_signal_handlers_disconnect_by_func (priv->individual,
1798 notify_alias_cb, self);
1799 g_signal_handlers_disconnect_by_func (priv->individual,
1800 notify_presence_cb, self);
1801 g_signal_handlers_disconnect_by_func (priv->individual,
1802 notify_avatar_cb, self);
1803 g_signal_handlers_disconnect_by_func (priv->individual,
1804 personas_changed_cb, self);
1805 g_signal_handlers_disconnect_by_func (priv->individual,
1806 individual_removed_cb, self);
1808 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1810 g_signal_handlers_disconnect_by_func (priv->individual,
1811 notify_is_favourite_cb, self);
1814 personas = folks_individual_get_personas (priv->individual);
1815 for (l = personas; l != NULL; l = l->next)
1816 remove_persona (self, FOLKS_PERSONA (l->data));
1817 individual_table_destroy (self);
1819 if (priv->contact != NULL)
1820 remove_weak_contact (self);
1822 tp_clear_object (&priv->individual);
1825 if (priv->details_cancellable != NULL)
1826 g_cancellable_cancel (priv->details_cancellable);
1830 individual_update (EmpathyIndividualWidget *self)
1832 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1834 /* Connect and get info from new Individual */
1835 if (priv->individual != NULL)
1837 g_signal_connect (priv->individual, "notify::alias",
1838 (GCallback) notify_alias_cb, self);
1839 g_signal_connect (priv->individual, "notify::presence-type",
1840 (GCallback) notify_presence_cb, self);
1841 g_signal_connect (priv->individual, "notify::presence-message",
1842 (GCallback) notify_presence_cb, self);
1843 g_signal_connect (priv->individual, "notify::avatar",
1844 (GCallback) notify_avatar_cb, self);
1845 g_signal_connect (priv->individual, "personas-changed",
1846 (GCallback) personas_changed_cb, self);
1847 g_signal_connect (priv->individual, "removed",
1848 (GCallback) individual_removed_cb, self);
1850 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1852 g_signal_connect (priv->individual, "notify::is-favourite",
1853 (GCallback) notify_is_favourite_cb, self);
1856 /* Update individual table */
1857 personas_changed_cb (priv->individual, NULL, NULL, self);
1860 if (priv->individual == NULL)
1862 gtk_widget_hide (priv->vbox_individual);
1864 else if (priv->individual_table != NULL)
1866 /* We only need to update the details for the Individual as a whole */
1867 update_individual_table (self);
1868 gtk_widget_show (priv->vbox_individual);
1872 /* We need to update the details for every Persona in the Individual */
1873 GList *personas, *l;
1875 personas = folks_individual_get_personas (priv->individual);
1876 for (l = personas; l != NULL; l = l->next)
1878 if (!TPF_IS_PERSONA (l->data))
1881 update_persona (self, FOLKS_PERSONA (l->data));
1884 gtk_widget_show (priv->vbox_individual);
1889 empathy_individual_widget_init (EmpathyIndividualWidget *self)
1891 EmpathyIndividualWidgetPriv *priv;
1895 priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1896 EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
1899 gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
1900 GTK_ORIENTATION_VERTICAL);
1902 filename = empathy_file_lookup ("empathy-individual-widget.ui",
1904 gui = empathy_builder_get_file (filename,
1905 "scrolled_window_individual", &priv->scrolled_window_individual,
1906 "viewport_individual", &priv->viewport_individual,
1907 "vbox_individual_widget", &priv->vbox_individual_widget,
1908 "vbox_individual", &priv->vbox_individual,
1909 "vbox_location", &priv->vbox_location,
1910 "subvbox_location", &priv->subvbox_location,
1911 "label_location", &priv->label_location,
1912 #ifdef HAVE_LIBCHAMPLAIN
1913 "viewport_map", &priv->viewport_map,
1915 "groups_widget", &priv->groups_widget,
1916 "vbox_details", &priv->vbox_details,
1917 "table_details", &priv->table_details,
1918 "hbox_details_requested", &priv->hbox_details_requested,
1919 "hbox_client_types", &priv->hbox_client_types,
1923 priv->table_location = NULL;
1925 gtk_box_pack_start (GTK_BOX (self), priv->vbox_individual_widget, TRUE, TRUE,
1927 gtk_widget_show (priv->vbox_individual_widget);
1929 priv->persona_tables = g_hash_table_new (NULL, NULL);
1930 priv->individual_table = NULL;
1932 /* Create widgets */
1933 details_set_up (self);
1935 g_object_unref (gui);
1939 constructed (GObject *object)
1941 GObjectClass *klass = G_OBJECT_CLASS (empathy_individual_widget_parent_class);
1942 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1943 GtkScrolledWindow *scrolled_window =
1944 GTK_SCROLLED_WINDOW (priv->scrolled_window_individual);
1946 /* Allow scrolling of the list of Personas if we're showing Personas. */
1947 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS)
1949 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
1950 gtk_scrolled_window_set_policy (scrolled_window,
1951 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1952 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
1953 priv->scrolled_window_individual, TRUE, TRUE, 0, GTK_PACK_START);
1955 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1957 gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
1961 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_NONE);
1962 gtk_scrolled_window_set_policy (scrolled_window,
1963 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1964 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
1965 priv->scrolled_window_individual, FALSE, TRUE, 0, GTK_PACK_START);
1967 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1972 if (klass->constructed != NULL)
1973 klass->constructed (object);
1977 get_property (GObject *object,
1982 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1986 case PROP_INDIVIDUAL:
1987 g_value_set_object (value, priv->individual);
1990 g_value_set_flags (value, priv->flags);
1993 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1999 set_property (GObject *object,
2001 const GValue *value,
2004 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2008 case PROP_INDIVIDUAL:
2009 empathy_individual_widget_set_individual (
2010 EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
2013 priv->flags = g_value_get_flags (value);
2016 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2022 dispose (GObject *object)
2024 remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
2026 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
2030 finalize (GObject *object)
2032 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2034 g_hash_table_destroy (priv->persona_tables);
2036 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
2040 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
2042 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2044 object_class->constructed = constructed;
2045 object_class->get_property = get_property;
2046 object_class->set_property = set_property;
2047 object_class->dispose = dispose;
2048 object_class->finalize = finalize;
2051 * EmpathyIndividualWidget:individual:
2053 * The #FolksIndividual to display in the widget.
2055 g_object_class_install_property (object_class, PROP_INDIVIDUAL,
2056 g_param_spec_object ("individual",
2058 "The #FolksIndividual to display in the widget.",
2059 FOLKS_TYPE_INDIVIDUAL,
2060 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2063 * EmpathyIndividualWidget:flags:
2065 * A set of flags which affect the widget's behaviour.
2067 g_object_class_install_property (object_class, PROP_FLAGS,
2068 g_param_spec_flags ("flags",
2070 "A set of flags which affect the widget's behaviour.",
2071 EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
2072 EMPATHY_INDIVIDUAL_WIDGET_NONE,
2073 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
2075 g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
2079 * empathy_individual_widget_new:
2080 * @individual: the #FolksIndividual to display
2081 * @flags: flags affecting how the widget behaves and what it displays
2083 * Creates a new #EmpathyIndividualWidget.
2085 * Return value: a new #EmpathyIndividualWidget
2088 empathy_individual_widget_new (FolksIndividual *individual,
2089 EmpathyIndividualWidgetFlags flags)
2091 g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
2094 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
2095 "individual", individual,
2101 * empathy_individual_widget_get_individual:
2102 * @self: an #EmpathyIndividualWidget
2104 * Returns the #FolksIndividual being displayed by the widget.
2106 * Return value: the #FolksIndividual being displayed, or %NULL
2109 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
2111 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
2113 return GET_PRIV (self)->individual;
2117 * empathy_individual_widget_set_individual:
2118 * @self: an #EmpathyIndividualWidget
2119 * @individual: the #FolksIndividual to display, or %NULL
2121 * Set the #FolksIndividual to be displayed by the widget:
2122 * #EmpathyIndividualWidget:individual.
2124 * The @individual may be %NULL in order to display nothing in the widget.
2127 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
2128 FolksIndividual *individual)
2130 EmpathyIndividualWidgetPriv *priv;
2132 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
2133 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
2135 priv = GET_PRIV (self);
2137 if (individual == priv->individual)
2140 /* Out with the old… */
2141 remove_individual (self);
2143 /* …and in with the new. */
2144 if (individual != NULL)
2145 g_object_ref (individual);
2146 priv->individual = individual;
2148 /* Update information for widgets */
2149 individual_update (self);
2150 groups_update (self);
2151 details_update (self);
2152 location_update (self);
2153 client_types_update (self);