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) */
227 FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
229 personas = folks_individual_get_personas (priv->individual);
230 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
231 while (gee_iterator_next (iter))
233 FolksPersona *persona = gee_iterator_get (iter);
235 /* We only want personas which have presence and a TpContact */
236 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
238 FolksPresenceDetails *presence;
239 FolksPresenceType presence_type_cur;
241 presence = FOLKS_PRESENCE_DETAILS (persona);
242 presence_type_cur = folks_presence_details_get_presence_type (
245 if (folks_presence_details_typecmp (
246 presence_type_cur, presence_type) > 0)
248 presence_type = presence_type_cur;
249 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
253 g_clear_object (&persona);
255 g_clear_object (&iter);
258 if (tp_contact != NULL)
260 priv->contact = tp_contact;
261 g_object_add_weak_pointer (G_OBJECT (tp_contact),
262 (gpointer *) &priv->contact);
264 g_signal_connect (priv->contact, "notify::client-types",
265 (GCallback) client_types_notify_cb, self);
270 details_update_show (EmpathyIndividualWidget *self,
273 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
277 info = tp_contact_get_contact_info (contact);
278 info = g_list_sort (info, (GCompareFunc) contact_info_field_cmp);
279 for (l = info; l != NULL; l = l->next)
281 TpContactInfoField *field = l->data;
282 InfoFieldData *field_data;
286 if (field->field_value == NULL || field->field_value[0] == NULL)
289 value = field->field_value[0];
291 field_data = find_info_field_data (field->field_name);
292 if (field_data == NULL)
294 DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
299 w = gtk_label_new (_(field_data->title));
300 gtk_table_attach (GTK_TABLE (priv->table_details),
301 w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
302 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
306 w = gtk_label_new (value);
307 if (field_data->linkify == TRUE)
311 markup = empathy_add_link_markup (value);
312 gtk_label_set_markup (GTK_LABEL (w), markup);
316 gtk_label_set_selectable (GTK_LABEL (w),
317 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
319 gtk_table_attach_defaults (GTK_TABLE (priv->table_details),
320 w, 1, 2, n_rows, n_rows + 1);
321 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
332 details_notify_cb (TpContact *contact,
334 EmpathyIndividualWidget *self)
336 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
339 gtk_container_foreach (GTK_CONTAINER (priv->table_details),
340 (GtkCallback) gtk_widget_destroy, NULL);
342 n_rows = details_update_show (self, contact);
346 gtk_widget_show (priv->vbox_details);
347 gtk_widget_show (priv->table_details);
351 gtk_widget_hide (priv->vbox_details);
354 gtk_widget_hide (priv->hbox_details_requested);
355 gtk_spinner_stop (GTK_SPINNER (priv->details_spinner));
359 details_request_cb (GObject *source,
363 EmpathyIndividualWidget *self = user_data;
364 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
365 TpContact *contact = (TpContact *) source;
366 gboolean hide_widget = FALSE;
367 GError *error = NULL;
369 if (tp_contact_request_contact_info_finish (contact, res, &error) == TRUE)
371 details_notify_cb (contact, NULL, self);
375 /* If the request got cancelled it could mean the contact widget is
376 * destroyed, so we should not dereference information */
377 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
379 g_error_free (error);
384 g_error_free (error);
387 if (hide_widget == TRUE)
388 gtk_widget_hide (GET_PRIV (self)->vbox_details);
390 tp_clear_object (&priv->details_cancellable);
392 tp_g_signal_connect_object (contact, "notify::contact-info",
393 (GCallback) details_notify_cb, self, 0);
397 fetch_contact_information (EmpathyIndividualWidget *self)
399 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
400 TpConnection *connection;
402 connection = tp_contact_get_connection (priv->contact);
404 if (!tp_proxy_has_interface_by_id (connection,
405 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_INFO))
407 gtk_widget_hide (GET_PRIV (self)->vbox_details);
411 /* Request the Individual's info */
412 gtk_widget_show (priv->vbox_details);
413 gtk_widget_show (priv->hbox_details_requested);
414 gtk_widget_hide (priv->table_details);
415 gtk_spinner_start (GTK_SPINNER (priv->details_spinner));
417 if (priv->details_cancellable == NULL)
419 priv->details_cancellable = g_cancellable_new ();
421 tp_contact_request_contact_info_async (priv->contact,
422 priv->details_cancellable, details_request_cb, self);
427 details_update (EmpathyIndividualWidget *self)
429 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
431 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS))
434 gtk_widget_hide (priv->vbox_details);
436 if (priv->contact == NULL)
437 update_weak_contact (self);
439 if (priv->contact != NULL)
441 fetch_contact_information (self);
446 groups_update (EmpathyIndividualWidget *self)
448 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
450 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS &&
451 priv->individual != NULL)
453 empathy_groups_widget_set_group_details (
454 EMPATHY_GROUPS_WIDGET (priv->groups_widget),
455 FOLKS_GROUP_DETAILS (priv->individual));
456 gtk_widget_show (priv->groups_widget);
460 gtk_widget_hide (priv->groups_widget);
464 /* Converts the Location's GHashTable's key to a user readable string */
466 location_key_to_label (const gchar *key)
468 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
469 return _("Country ISO Code:");
470 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
471 return _("Country:");
472 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
474 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
476 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
478 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
479 return _("Postal Code:");
480 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
482 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
483 return _("Building:");
484 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
486 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
488 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
490 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
491 return _("Description:");
492 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
494 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
495 return _("Accuracy Level:");
496 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
498 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
499 return _("Vertical Error (meters):");
500 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
501 return _("Horizontal Error (meters):");
502 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
504 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
505 return _("Bearing:");
506 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
507 return _("Climb Speed:");
508 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
509 return _("Last Updated on:");
510 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
511 return _("Longitude:");
512 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
513 return _("Latitude:");
514 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
515 return _("Altitude:");
518 DEBUG ("Unexpected Location key: %s", key);
524 location_update (EmpathyIndividualWidget *self)
526 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
527 EmpathyContact *contact = NULL;
528 GHashTable *location = NULL;
532 static const gchar* ordered_geolocation_keys[] = {
533 EMPATHY_LOCATION_TEXT,
534 EMPATHY_LOCATION_URI,
535 EMPATHY_LOCATION_DESCRIPTION,
536 EMPATHY_LOCATION_BUILDING,
537 EMPATHY_LOCATION_FLOOR,
538 EMPATHY_LOCATION_ROOM,
539 EMPATHY_LOCATION_STREET,
540 EMPATHY_LOCATION_AREA,
541 EMPATHY_LOCATION_LOCALITY,
542 EMPATHY_LOCATION_REGION,
543 EMPATHY_LOCATION_COUNTRY,
548 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) */
563 personas = folks_individual_get_personas (priv->individual);
564 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
565 while (location == NULL && gee_iterator_next (iter))
567 FolksPersona *persona = gee_iterator_get (iter);
569 if (empathy_folks_persona_is_interesting (persona))
571 TpContact *tp_contact;
573 /* Get the contact. If it turns out to have location information, we
574 * have to keep it alive for the duration of the function, since we're
575 * accessing its private data. */
576 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
577 contact = empathy_contact_dup_from_tp_contact (tp_contact);
578 empathy_contact_set_persona (contact, persona);
580 /* Try and get a location */
581 location = empathy_contact_get_location (contact);
582 /* if location isn't fully valid, treat the contact as insufficient */
583 if (location != NULL && g_hash_table_size (location) <= 0)
586 g_clear_object (&contact);
589 g_clear_object (&persona);
591 g_clear_object (&iter);
593 if (contact == NULL || location == NULL)
595 gtk_widget_hide (priv->vbox_location);
596 tp_clear_object (&contact);
600 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
603 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
604 gtk_label_set_markup (GTK_LABEL (priv->label_location), loc);
614 stamp = g_value_get_int64 (value);
616 user_date = empathy_time_to_string_relative (stamp);
618 tmp = g_strdup_printf ("<b>%s</b>", _("Location"));
619 /* translators: format is "Location, $date" */
620 text = g_strdup_printf (_("%s, %s"), tmp, user_date);
622 gtk_label_set_markup (GTK_LABEL (priv->label_location), text);
627 /* Prepare the location information table */
628 if (priv->table_location != NULL)
629 gtk_widget_destroy (priv->table_location);
631 priv->table_location = gtk_table_new (1, 2, FALSE);
632 gtk_box_pack_start (GTK_BOX (priv->subvbox_location),
633 priv->table_location, FALSE, FALSE, 5);
636 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
638 const gchar* user_label;
642 gvalue = g_hash_table_lookup (location, (gpointer) skey);
646 user_label = location_key_to_label (skey);
648 label = gtk_label_new (user_label);
649 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
650 gtk_table_attach (GTK_TABLE (priv->table_location),
651 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
652 gtk_widget_show (label);
654 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
657 dvalue = g_value_get_double (gvalue);
658 svalue = g_strdup_printf ("%f", dvalue);
660 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
662 svalue = g_value_dup_string (gvalue);
664 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
668 time_ = g_value_get_int64 (value);
669 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
674 label = gtk_label_new (svalue);
675 gtk_table_attach_defaults (GTK_TABLE (priv->table_location),
676 label, 1, 2, row, row + 1);
677 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
678 gtk_widget_show (label);
680 gtk_label_set_selectable (GTK_LABEL (label),
681 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE :
689 tp_clear_object (&contact);
691 #ifdef HAVE_LIBCHAMPLAIN
692 if ((g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL) &&
693 (g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL) &&
694 !(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
696 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
704 /* We can display some fields */
705 gtk_widget_show (priv->table_location);
707 else if (display_map == FALSE)
709 /* Can't display either fields or map */
710 gtk_widget_hide (priv->vbox_location);
714 #ifdef HAVE_LIBCHAMPLAIN
715 if (display_map == TRUE)
717 ChamplainMarkerLayer *layer;
719 priv->map_view_embed = gtk_champlain_embed_new ();
720 priv->map_view = gtk_champlain_embed_get_view (
721 GTK_CHAMPLAIN_EMBED (priv->map_view_embed));
723 gtk_container_add (GTK_CONTAINER (priv->viewport_map),
724 priv->map_view_embed);
725 g_object_set (G_OBJECT (priv->map_view),
726 "kinetic-mode", TRUE,
730 layer = champlain_marker_layer_new ();
731 champlain_view_add_layer (priv->map_view, CHAMPLAIN_LAYER (layer));
733 /* FIXME: For now, we have to do this manually. Once libfolks grows a
734 * location interface, we can use that. (bgo#627400) */
736 personas = folks_individual_get_personas (priv->individual);
737 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
738 while (gee_iterator_next (iter))
740 FolksPersona *persona = gee_iterator_get (iter);
742 if (empathy_folks_persona_is_interesting (persona))
744 gdouble lat = 0.0, lon = 0.0;
745 ClutterActor *marker;
746 TpContact *tp_contact;
748 /* Get the contact */
749 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
750 contact = empathy_contact_dup_from_tp_contact (tp_contact);
751 empathy_contact_set_persona (contact, persona);
753 /* Try and get a location */
754 location = empathy_contact_get_location (contact);
755 if (location == NULL || g_hash_table_size (location) == 0)
758 /* Get this persona's latitude and longitude */
759 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
763 lat = g_value_get_double (value);
765 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
769 lon = g_value_get_double (value);
771 /* Add a marker to the map */
772 marker = champlain_label_new_with_text (
773 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona)),
775 champlain_location_set_location (CHAMPLAIN_LOCATION (marker),
777 champlain_marker_layer_add_marker (layer,
778 CHAMPLAIN_MARKER (marker));
782 g_clear_object (&persona);
783 g_clear_object (&contact);
785 g_clear_object (&iter);
787 /* Zoom to show all of the markers */
788 champlain_view_ensure_layers_visible (priv->map_view, FALSE);
790 gtk_widget_show_all (priv->viewport_map);
794 gtk_widget_show (priv->vbox_location);
798 client_types_update (EmpathyIndividualWidget *self)
800 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
801 const gchar * const *types;
803 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_CLIENT_TYPES) ||
804 priv->individual == NULL)
806 gtk_widget_hide (priv->hbox_client_types);
810 if (priv->contact == NULL)
811 update_weak_contact (self);
813 /* let's try that again... */
814 if (priv->contact == NULL)
817 types = tp_contact_get_client_types (priv->contact);
820 && g_strv_length ((gchar **) types) > 0
821 && !tp_strdiff (types[0], "phone"))
823 gtk_widget_show (priv->hbox_client_types);
827 gtk_widget_hide (priv->hbox_client_types);
833 remove_weak_contact (EmpathyIndividualWidget *self)
835 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
837 if (priv->contact == NULL)
840 g_signal_handlers_disconnect_by_func (priv->contact, client_types_notify_cb,
843 g_object_remove_weak_pointer (G_OBJECT (priv->contact),
844 (gpointer *) &priv->contact);
845 priv->contact = NULL;
848 static EmpathyAvatar *
849 persona_dup_avatar (FolksPersona *persona)
851 TpContact *tp_contact;
852 EmpathyContact *contact;
853 EmpathyAvatar *avatar;
855 if (!empathy_folks_persona_is_interesting (persona))
858 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
859 contact = empathy_contact_dup_from_tp_contact (tp_contact);
860 empathy_contact_set_persona (contact, persona);
862 avatar = empathy_contact_get_avatar (contact);
864 empathy_avatar_ref (avatar);
865 g_object_unref (contact);
870 static EmpathyAvatar *
871 individual_dup_avatar (FolksIndividual *individual)
875 EmpathyAvatar *avatar = NULL;
877 /* FIXME: We just choose the first Persona which has an avatar, and save that.
878 * The avatar handling in EmpathyContact needs to be moved into libfolks as
879 * much as possible, and this code rewritten to use FolksHasAvatar.
882 personas = folks_individual_get_personas (individual);
883 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
884 while (avatar == NULL && gee_iterator_next (iter))
886 FolksPersona *persona = gee_iterator_get (iter);
887 avatar = persona_dup_avatar (persona);
889 g_clear_object (&persona);
891 g_clear_object (&iter);
897 save_avatar_menu_activate_cb (GtkWidget *widget,
898 EmpathyIndividualWidget *self)
900 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
902 EmpathyAvatar *avatar;
903 gchar *ext = NULL, *filename;
905 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
907 GTK_FILE_CHOOSER_ACTION_SAVE,
908 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
909 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
912 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
915 avatar = individual_dup_avatar (priv->individual);
919 /* look for the avatar extension */
920 if (avatar->format != NULL)
924 splitted = g_strsplit (avatar->format, "/", 2);
925 if (splitted[0] != NULL && splitted[1] != NULL)
926 ext = g_strdup (splitted[1]);
928 g_strfreev (splitted);
932 /* Avatar was loaded from the cache so was converted to PNG */
933 ext = g_strdup ("png");
940 id = tp_escape_as_identifier (folks_individual_get_id (priv->individual));
942 filename = g_strdup_printf ("%s.%s", id, ext);
943 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
950 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
952 GError *error = NULL;
954 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
956 if (empathy_avatar_save_to_file (avatar, filename, &error) == FALSE)
959 GtkWidget *error_dialog;
961 error_dialog = gtk_message_dialog_new (NULL, 0,
962 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
963 _("Unable to save avatar"));
965 gtk_message_dialog_format_secondary_text (
966 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
968 g_signal_connect (error_dialog, "response",
969 (GCallback) gtk_widget_destroy, NULL);
971 gtk_window_present (GTK_WINDOW (error_dialog));
973 g_clear_error (&error);
979 gtk_widget_destroy (dialog);
980 empathy_avatar_unref (avatar);
984 popup_avatar_menu (EmpathyIndividualWidget *self,
986 GdkEventButton *event)
988 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
989 GtkWidget *menu, *item;
990 EmpathyAvatar *avatar;
991 gint button, event_time;
993 if (priv->individual == NULL)
996 avatar = individual_dup_avatar (priv->individual);
999 empathy_avatar_unref (avatar);
1001 menu = empathy_context_menu_new (parent);
1003 /* Add "Save as..." entry */
1004 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
1005 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1006 gtk_widget_show (item);
1008 g_signal_connect (item, "activate",
1009 (GCallback) save_avatar_menu_activate_cb, self);
1013 button = event->button;
1014 event_time = event->time;
1019 event_time = gtk_get_current_event_time ();
1022 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, button, event_time);
1028 avatar_widget_popup_menu_cb (GtkWidget *widget,
1029 EmpathyIndividualWidget *self)
1031 return popup_avatar_menu (self, widget, NULL);
1035 avatar_widget_button_press_event_cb (GtkWidget *widget,
1036 GdkEventButton *event,
1037 EmpathyIndividualWidget *self)
1039 /* Ignore double-clicks and triple-clicks */
1040 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1041 return popup_avatar_menu (self, widget, event);
1046 /* Returns the TpAccount for the user as a convenience. Note that it has a ref
1049 individual_is_user (FolksIndividual *individual)
1053 TpAccount *retval = NULL;
1055 /* FIXME: This should move into libfolks when libfolks grows a way of
1056 * determining "self". (bgo#627402) */
1057 personas = folks_individual_get_personas (individual);
1058 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1059 while (gee_iterator_next (iter))
1061 FolksPersona *persona = gee_iterator_get (iter);
1063 if (TPF_IS_PERSONA (persona))
1065 TpContact *tp_contact;
1066 EmpathyContact *contact;
1068 /* Get the contact */
1069 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1070 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1071 empathy_contact_set_persona (contact, persona);
1073 /* Determine if the contact is the user */
1074 if (empathy_contact_is_user (contact))
1075 retval = g_object_ref (empathy_contact_get_account (contact));
1077 g_object_unref (contact);
1079 g_clear_object (&persona);
1081 g_clear_object (&iter);
1087 set_nickname_cb (TpAccount *account,
1088 GAsyncResult *result,
1091 GError *error = NULL;
1093 if (tp_account_set_nickname_finish (account, result, &error) == FALSE)
1095 DEBUG ("Failed to set Account.Nickname: %s", error->message);
1096 g_error_free (error);
1101 entry_alias_focus_event_cb (GtkEditable *editable,
1102 GdkEventFocus *event,
1103 EmpathyIndividualWidget *self)
1105 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1107 if (priv->individual != NULL)
1112 alias = gtk_entry_get_text (GTK_ENTRY (editable));
1113 account = individual_is_user (priv->individual);
1115 if (account != NULL)
1117 DEBUG ("Set Account.Nickname to %s", alias);
1118 tp_account_set_nickname_async (account, alias,
1119 (GAsyncReadyCallback) set_nickname_cb, NULL);
1120 g_object_unref (account);
1124 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (priv->individual),
1133 favourite_toggled_cb (GtkToggleButton *button,
1134 EmpathyIndividualWidget *self)
1136 gboolean active = gtk_toggle_button_get_active (button);
1137 folks_favourite_details_set_is_favourite (
1138 FOLKS_FAVOURITE_DETAILS (GET_PRIV (self)->individual), active);
1142 notify_avatar_cb (gpointer folks_object,
1144 EmpathyIndividualWidget *self)
1146 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1147 EmpathyAvatar *avatar = NULL;
1149 GtkWidget *avatar_widget;
1151 if (FOLKS_IS_INDIVIDUAL (folks_object))
1153 avatar = individual_dup_avatar (FOLKS_INDIVIDUAL (folks_object));
1154 table = G_OBJECT (priv->individual_table);
1156 else if (FOLKS_IS_PERSONA (folks_object))
1158 avatar = persona_dup_avatar (FOLKS_PERSONA (folks_object));
1159 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1163 g_assert_not_reached ();
1169 avatar_widget = g_object_get_data (table, "avatar-widget");
1170 empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (avatar_widget), avatar);
1173 empathy_avatar_unref (avatar);
1177 notify_alias_cb (gpointer folks_object,
1179 EmpathyIndividualWidget *self)
1181 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1183 GtkWidget *alias_widget;
1185 if (FOLKS_IS_INDIVIDUAL (folks_object))
1186 table = G_OBJECT (priv->individual_table);
1187 else if (FOLKS_IS_PERSONA (folks_object))
1188 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1190 g_assert_not_reached ();
1195 alias_widget = g_object_get_data (table, "alias-widget");
1197 if (GTK_IS_ENTRY (alias_widget))
1199 gtk_entry_set_text (GTK_ENTRY (alias_widget),
1200 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (folks_object)));
1204 gtk_label_set_label (GTK_LABEL (alias_widget),
1205 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (folks_object)));
1210 notify_presence_cb (gpointer folks_object,
1212 EmpathyIndividualWidget *self)
1214 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1216 GtkWidget *status_label, *state_image;
1217 const gchar *message;
1218 gchar *markup_text = NULL;
1220 if (FOLKS_IS_INDIVIDUAL (folks_object))
1221 table = G_OBJECT (priv->individual_table);
1222 else if (FOLKS_IS_PERSONA (folks_object))
1223 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1225 g_assert_not_reached ();
1230 status_label = g_object_get_data (table, "status-label");
1231 state_image = g_object_get_data (table, "state-image");
1233 /* FIXME: Default messages should be moved into libfolks (bgo#627403) */
1234 message = folks_presence_details_get_presence_message (
1235 FOLKS_PRESENCE_DETAILS (folks_object));
1236 if (EMP_STR_EMPTY (message))
1238 message = empathy_presence_get_default_message (
1239 folks_presence_details_get_presence_type (
1240 FOLKS_PRESENCE_DETAILS (folks_object)));
1243 if (message != NULL)
1244 markup_text = empathy_add_link_markup (message);
1245 gtk_label_set_markup (GTK_LABEL (status_label), markup_text);
1246 g_free (markup_text);
1248 gtk_image_set_from_icon_name (GTK_IMAGE (state_image),
1249 empathy_icon_name_for_presence (
1250 folks_presence_details_get_presence_type (
1251 FOLKS_PRESENCE_DETAILS (folks_object))),
1252 GTK_ICON_SIZE_BUTTON);
1253 gtk_widget_show (state_image);
1257 notify_is_favourite_cb (gpointer folks_object,
1259 EmpathyIndividualWidget *self)
1261 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1263 GtkWidget *favourite_widget;
1265 if (FOLKS_IS_INDIVIDUAL (folks_object))
1266 table = G_OBJECT (priv->individual_table);
1267 else if (FOLKS_IS_PERSONA (folks_object))
1268 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1270 g_assert_not_reached ();
1275 favourite_widget = g_object_get_data (table, "favourite-widget");
1277 if (GTK_IS_TOGGLE_BUTTON (favourite_widget))
1279 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (favourite_widget),
1280 folks_favourite_details_get_is_favourite (
1281 FOLKS_FAVOURITE_DETAILS (folks_object)));
1286 alias_presence_avatar_favourite_set_up (EmpathyIndividualWidget *self,
1290 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1291 GtkWidget *label, *alias, *image, *avatar, *alignment;
1292 guint current_row = starting_row;
1295 label = gtk_label_new (_("Alias:"));
1296 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1297 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1, GTK_FILL,
1299 gtk_widget_show (label);
1301 /* Set up alias label/entry */
1302 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS)
1304 alias = gtk_entry_new ();
1306 g_signal_connect (alias, "focus-out-event",
1307 (GCallback) entry_alias_focus_event_cb, self);
1309 /* Make return activate the window default (the Close button) */
1310 gtk_entry_set_activates_default (GTK_ENTRY (alias), TRUE);
1314 alias = gtk_label_new (NULL);
1315 gtk_label_set_selectable (GTK_LABEL (alias),
1316 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1317 gtk_misc_set_alignment (GTK_MISC (alias), 0.0, 0.5);
1320 g_object_set_data (G_OBJECT (table), "alias-widget", alias);
1321 gtk_table_attach (table, alias, 1, 2, current_row, current_row + 1,
1322 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1323 gtk_widget_show (alias);
1328 priv->hbox_presence = gtk_hbox_new (FALSE, 6);
1330 /* Presence image */
1331 image = gtk_image_new_from_stock (GTK_STOCK_MISSING_IMAGE,
1332 GTK_ICON_SIZE_BUTTON);
1333 g_object_set_data (G_OBJECT (table), "state-image", image);
1334 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), image, FALSE,
1336 gtk_widget_show (image);
1338 label = gtk_label_new ("");
1339 gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
1340 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1341 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1343 gtk_label_set_selectable (GTK_LABEL (label),
1344 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1346 g_object_set_data (G_OBJECT (table), "status-label", label);
1347 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, TRUE,
1349 gtk_widget_show (label);
1351 gtk_table_attach (table, priv->hbox_presence, 0, 2, current_row,
1352 current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1353 gtk_widget_show (priv->hbox_presence);
1357 /* Set up favourite toggle button */
1358 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1360 GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1362 g_signal_connect (favourite, "toggled",
1363 (GCallback) favourite_toggled_cb, self);
1365 g_object_set_data (G_OBJECT (table), "favourite-widget", favourite);
1366 gtk_table_attach (table, favourite, 0, 2, current_row, current_row + 1,
1367 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1368 gtk_widget_show (favourite);
1373 /* Set up avatar display */
1374 avatar = empathy_avatar_image_new ();
1376 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1378 g_signal_connect (avatar, "popup-menu",
1379 (GCallback) avatar_widget_popup_menu_cb, self);
1380 g_signal_connect (avatar, "button-press-event",
1381 (GCallback) avatar_widget_button_press_event_cb, self);
1384 g_object_set_data (G_OBJECT (table), "avatar-widget", avatar);
1386 alignment = gtk_alignment_new (1.0, 0.0, 0.0, 0.0);
1387 gtk_container_add (GTK_CONTAINER (alignment), avatar);
1388 gtk_widget_show (avatar);
1390 gtk_table_attach (table, alignment, 2, 3, 0, current_row,
1391 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 6, 6);
1392 gtk_widget_show (alignment);
1396 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1398 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1399 TpContact *tp_contact;
1400 EmpathyContact *contact;
1407 table = g_hash_table_lookup (priv->persona_tables, persona);
1409 g_assert (table != NULL);
1411 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1412 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1413 empathy_contact_set_persona (contact, persona);
1415 account = empathy_contact_get_account (contact);
1417 /* Update account widget */
1418 if (account != NULL)
1422 label = g_object_get_data (G_OBJECT (table), "account-label");
1423 image = g_object_get_data (G_OBJECT (table), "account-image");
1425 name = tp_account_get_display_name (account);
1426 gtk_label_set_label (label, name);
1428 name = tp_account_get_icon_name (account);
1429 gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
1432 /* Update id widget */
1433 label = g_object_get_data (G_OBJECT (table), "id-widget");
1434 id = folks_persona_get_display_id (persona);
1435 gtk_label_set_label (label, (id != NULL) ? id : "");
1437 /* Update other widgets */
1438 notify_alias_cb (persona, NULL, self);
1439 notify_presence_cb (persona, NULL, self);
1440 notify_avatar_cb (persona, NULL, self);
1442 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1443 notify_is_favourite_cb (persona, NULL, self);
1445 g_object_unref (contact);
1449 add_persona (EmpathyIndividualWidget *self,
1450 FolksPersona *persona)
1452 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1455 GtkWidget *label, *account_label, *account_image, *separator;
1456 guint current_row = 0;
1458 if (!empathy_folks_persona_is_interesting (persona))
1461 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1462 table = GTK_TABLE (gtk_table_new (5, 3, FALSE));
1464 table = GTK_TABLE (gtk_table_new (4, 3, FALSE));
1465 gtk_table_set_row_spacings (table, 6);
1466 gtk_table_set_col_spacings (table, 6);
1468 /* Account and Identifier */
1469 label = gtk_label_new (_("Account:"));
1470 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1471 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1472 GTK_FILL, GTK_FILL, 0, 0);
1473 gtk_widget_show (label);
1475 /* Pack the protocol icon with the account name in an hbox */
1476 hbox = GTK_BOX (gtk_hbox_new (FALSE, 6));
1478 account_label = gtk_label_new (NULL);
1479 gtk_label_set_selectable (GTK_LABEL (account_label),
1480 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1481 gtk_misc_set_alignment (GTK_MISC (account_label), 0.0, 0.5);
1482 gtk_widget_show (account_label);
1484 account_image = gtk_image_new ();
1485 gtk_widget_show (account_image);
1487 gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1488 gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
1490 g_object_set_data (G_OBJECT (table), "account-image", account_image);
1491 g_object_set_data (G_OBJECT (table), "account-label", account_label);
1492 gtk_table_attach (table, GTK_WIDGET (hbox), 1, 2, current_row,
1493 current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1494 gtk_widget_show (GTK_WIDGET (hbox));
1498 /* Translators: Identifier to connect to Instant Messaging network */
1499 label = gtk_label_new (_("Identifier:"));
1500 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1501 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1502 GTK_FILL, GTK_FILL, 0, 0);
1503 gtk_widget_show (label);
1505 /* Set up ID label */
1506 label = gtk_label_new (NULL);
1507 gtk_label_set_selectable (GTK_LABEL (label),
1508 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1509 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1511 g_object_set_data (G_OBJECT (table), "id-widget", label);
1512 gtk_table_attach (table, label, 1, 2, current_row, current_row + 1,
1513 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1514 gtk_widget_show (label);
1518 alias_presence_avatar_favourite_set_up (self, table, current_row);
1520 /* Connect to signals and display the table */
1521 g_signal_connect (persona, "notify::alias",
1522 (GCallback) notify_alias_cb, self);
1523 g_signal_connect (persona, "notify::avatar",
1524 (GCallback) notify_avatar_cb, self);
1525 g_signal_connect (persona, "notify::presence-type",
1526 (GCallback) notify_presence_cb, self);
1527 g_signal_connect (persona, "notify::presence-message",
1528 (GCallback) notify_presence_cb, self);
1530 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1532 g_signal_connect (persona, "notify::is-favourite",
1533 (GCallback) notify_is_favourite_cb, self);
1536 gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1537 GTK_WIDGET (table), FALSE, TRUE, 0);
1538 gtk_widget_show (GTK_WIDGET (table));
1540 /* Pack a separator after the table */
1541 separator = gtk_hseparator_new ();
1542 g_object_set_data (G_OBJECT (table), "separator", separator);
1543 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), separator, FALSE, FALSE,
1545 gtk_widget_show (separator);
1547 g_hash_table_replace (priv->persona_tables, persona, table);
1549 /* Update the new widgets */
1550 update_persona (self, persona);
1554 remove_persona (EmpathyIndividualWidget *self,
1555 FolksPersona *persona)
1557 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1558 GtkWidget *separator;
1561 if (!empathy_folks_persona_is_interesting (persona))
1564 table = g_hash_table_lookup (priv->persona_tables, persona);
1568 g_signal_handlers_disconnect_by_func (persona, notify_alias_cb, self);
1569 g_signal_handlers_disconnect_by_func (persona, notify_avatar_cb, self);
1570 g_signal_handlers_disconnect_by_func (persona, notify_presence_cb, self);
1572 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1574 g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
1578 /* Remove the separator */
1579 separator = g_object_get_data (G_OBJECT (table), "separator");
1580 if (separator != NULL)
1581 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual), separator);
1583 /* Remove the widget */
1584 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1585 GTK_WIDGET (table));
1587 g_hash_table_remove (priv->persona_tables, persona);
1591 update_individual_table (EmpathyIndividualWidget *self)
1593 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1595 notify_alias_cb (priv->individual, NULL, self);
1596 notify_presence_cb (priv->individual, NULL, self);
1597 notify_avatar_cb (priv->individual, NULL, self);
1599 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1600 notify_is_favourite_cb (priv->individual, NULL, self);
1604 individual_table_set_up (EmpathyIndividualWidget *self)
1606 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1608 guint current_row = 0;
1611 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1614 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1617 table = GTK_TABLE (gtk_table_new (nb_rows, 3, FALSE));
1618 gtk_table_set_row_spacings (table, 6);
1619 gtk_table_set_col_spacings (table, 6);
1621 /* We only display the number of personas in tooltips */
1622 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1628 guint num_personas = 0;
1630 /* Meta-contacts message displaying how many Telepathy personas we have */
1631 personas = folks_individual_get_personas (priv->individual);
1632 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1633 while (gee_iterator_next (iter))
1635 FolksPersona *persona = gee_iterator_get (iter);
1636 if (empathy_folks_persona_is_interesting (persona))
1639 g_clear_object (&persona);
1641 g_clear_object (&iter);
1643 /* Translators: the plurality applies to both instances of the word
1645 message = g_strdup_printf (
1646 ngettext ("Linked contact containing %u contact",
1647 "Linked contacts containing %u contacts", num_personas),
1649 label = gtk_label_new (message);
1650 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1653 gtk_table_attach (table, label, 0, 2, current_row, current_row + 1,
1654 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1655 gtk_widget_show (label);
1660 alias_presence_avatar_favourite_set_up (self, table, current_row);
1662 /* Display the table */
1663 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (table),
1665 gtk_widget_show (GTK_WIDGET (table));
1667 priv->individual_table = table;
1669 /* Update the table */
1670 update_individual_table (self);
1674 individual_table_destroy (EmpathyIndividualWidget *self)
1676 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1678 if (priv->individual_table == NULL)
1681 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1682 GTK_WIDGET (priv->individual_table));
1683 priv->individual_table = NULL;
1687 personas_changed_cb (FolksIndividual *individual,
1690 EmpathyIndividualWidget *self)
1692 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1693 GList *l, *children;
1696 gboolean show_personas, was_showing_personas, will_show_personas, is_last;
1697 guint old_num_personas, new_num_personas = 0;
1699 personas = folks_individual_get_personas (individual);
1700 /* we'll re-use this iterator throughout */
1701 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1703 /* Note that old_num_personas is the number of persona tables we were
1704 * displaying, not the number of Personas which were in the Individual
1706 old_num_personas = g_hash_table_size (priv->persona_tables);
1708 while (gee_iterator_next (iter))
1710 FolksPersona *persona = gee_iterator_get (iter);
1711 if (empathy_folks_persona_is_interesting (persona))
1714 g_clear_object (&persona);
1718 * What we display for various conditions:
1719 * - "Personas": display the alias, avatar, presence account and identifier
1720 * for each of the Individual's Personas. (i.e. One table per
1722 * - "Individual": display the alias, avatar and presence for the Individual,
1723 * and a label saying "Meta-contact containing x contacts".
1724 * (i.e. One table in total.)
1726 * | SHOW_PERSONAS | !SHOW_PERSONAS
1727 * -------------+---------------+---------------
1728 * > 1 Persona | Personas | Individual
1729 * -------------+---------------+---------------
1730 * == 1 Persona | Personas | Personas
1732 show_personas = (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) != 0;
1733 was_showing_personas = show_personas || old_num_personas == 1;
1734 will_show_personas = show_personas || new_num_personas == 1;
1736 /* If both @added and @removed are NULL, we're being called manually, and we
1737 * need to set up the tables for the first time. We do this simply by
1738 * ensuring was_showing_personas and will_show_personas are different so that
1739 * the code resets the UI.
1741 if (added == NULL && removed == NULL)
1742 was_showing_personas = !will_show_personas;
1744 if (was_showing_personas && will_show_personas)
1746 GeeIterator *iter_changed;
1748 /* Remove outdated Personas */
1749 iter_changed = gee_iterable_iterator (GEE_ITERABLE (removed));
1750 while (gee_iterator_next (iter_changed))
1752 FolksPersona *persona = gee_iterator_get (iter_changed);
1753 remove_persona (self, persona);
1754 g_clear_object (&persona);
1756 g_clear_object (&iter_changed);
1758 /* Add new Personas */
1759 iter_changed = gee_iterable_iterator (GEE_ITERABLE (added));
1760 while (gee_iterator_next (iter_changed))
1762 FolksPersona *persona = gee_iterator_get (iter_changed);
1763 add_persona (self, persona);
1764 g_clear_object (&persona);
1766 g_clear_object (&iter_changed);
1768 else if (!was_showing_personas && will_show_personas)
1772 /* Remove the old Individual table */
1773 individual_table_destroy (self);
1775 /* Set up all the Persona tables instead */
1776 for (c = gee_iterator_first (iter); c; c = gee_iterator_next (iter))
1778 FolksPersona *persona = gee_iterator_get (iter);
1779 add_persona (self, persona);
1780 g_clear_object (&persona);
1783 else if (was_showing_personas && !will_show_personas)
1787 /* Remove all Personas */
1788 for (c = gee_iterator_first (iter); c; c = gee_iterator_next (iter))
1790 FolksPersona *persona = gee_iterator_get (iter);
1791 remove_persona (self, persona);
1792 g_clear_object (&persona);
1795 if (removed != NULL)
1797 GeeIterator *iter_changed;
1799 iter_changed = gee_iterable_iterator (GEE_ITERABLE (removed));
1800 while (gee_iterator_next (iter_changed))
1802 FolksPersona *persona = gee_iterator_get (iter_changed);
1803 remove_persona (self, persona);
1804 g_clear_object (&persona);
1806 g_clear_object (&iter_changed);
1809 /* Set up the Individual table instead */
1810 individual_table_set_up (self);
1812 g_clear_object (&iter);
1814 /* Hide the last separator and show the others */
1815 children = gtk_container_get_children (GTK_CONTAINER (priv->vbox_individual));
1816 children = g_list_reverse (children);
1819 for (l = children; l != NULL; l = l->next)
1821 if (GTK_IS_SEPARATOR (l->data))
1823 gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1828 g_list_free (children);
1832 individual_removed_cb (FolksIndividual *individual,
1833 FolksIndividual *replacement_individual,
1834 EmpathyIndividualWidget *self)
1836 empathy_individual_widget_set_individual (self, replacement_individual);
1840 remove_individual (EmpathyIndividualWidget *self)
1842 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1843 if (priv->individual != NULL)
1848 g_signal_handlers_disconnect_by_func (priv->individual,
1849 notify_alias_cb, self);
1850 g_signal_handlers_disconnect_by_func (priv->individual,
1851 notify_presence_cb, self);
1852 g_signal_handlers_disconnect_by_func (priv->individual,
1853 notify_avatar_cb, self);
1854 g_signal_handlers_disconnect_by_func (priv->individual,
1855 personas_changed_cb, self);
1856 g_signal_handlers_disconnect_by_func (priv->individual,
1857 individual_removed_cb, self);
1859 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1861 g_signal_handlers_disconnect_by_func (priv->individual,
1862 notify_is_favourite_cb, self);
1865 personas = folks_individual_get_personas (priv->individual);
1866 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1867 while (gee_iterator_next (iter))
1869 FolksPersona *persona = gee_iterator_get (iter);
1870 remove_persona (self, persona);
1871 g_clear_object (&persona);
1873 g_clear_object (&iter);
1874 individual_table_destroy (self);
1876 if (priv->contact != NULL)
1877 remove_weak_contact (self);
1879 tp_clear_object (&priv->individual);
1882 if (priv->details_cancellable != NULL)
1883 g_cancellable_cancel (priv->details_cancellable);
1887 individual_update (EmpathyIndividualWidget *self)
1889 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1891 /* Connect and get info from new Individual */
1892 if (priv->individual != NULL)
1894 g_signal_connect (priv->individual, "notify::alias",
1895 (GCallback) notify_alias_cb, self);
1896 g_signal_connect (priv->individual, "notify::presence-type",
1897 (GCallback) notify_presence_cb, self);
1898 g_signal_connect (priv->individual, "notify::presence-message",
1899 (GCallback) notify_presence_cb, self);
1900 g_signal_connect (priv->individual, "notify::avatar",
1901 (GCallback) notify_avatar_cb, self);
1902 g_signal_connect (priv->individual, "personas-changed",
1903 (GCallback) personas_changed_cb, self);
1904 g_signal_connect (priv->individual, "removed",
1905 (GCallback) individual_removed_cb, self);
1907 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1909 g_signal_connect (priv->individual, "notify::is-favourite",
1910 (GCallback) notify_is_favourite_cb, self);
1913 /* Update individual table */
1914 personas_changed_cb (priv->individual, NULL, NULL, self);
1917 if (priv->individual == NULL)
1919 gtk_widget_hide (priv->vbox_individual);
1921 else if (priv->individual_table != NULL)
1923 /* We only need to update the details for the Individual as a whole */
1924 update_individual_table (self);
1925 gtk_widget_show (priv->vbox_individual);
1929 /* We need to update the details for every Persona in the Individual */
1933 personas = folks_individual_get_personas (priv->individual);
1934 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1935 while (gee_iterator_next (iter))
1937 FolksPersona *persona = gee_iterator_get (iter);
1939 if (empathy_folks_persona_is_interesting (persona))
1940 update_persona (self, persona);
1942 g_clear_object (&persona);
1944 g_clear_object (&iter);
1946 gtk_widget_show (priv->vbox_individual);
1951 empathy_individual_widget_init (EmpathyIndividualWidget *self)
1953 EmpathyIndividualWidgetPriv *priv;
1957 priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1958 EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
1961 gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
1962 GTK_ORIENTATION_VERTICAL);
1964 filename = empathy_file_lookup ("empathy-individual-widget.ui",
1966 gui = empathy_builder_get_file (filename,
1967 "scrolled_window_individual", &priv->scrolled_window_individual,
1968 "viewport_individual", &priv->viewport_individual,
1969 "vbox_individual_widget", &priv->vbox_individual_widget,
1970 "vbox_individual", &priv->vbox_individual,
1971 "vbox_location", &priv->vbox_location,
1972 "subvbox_location", &priv->subvbox_location,
1973 "label_location", &priv->label_location,
1974 #ifdef HAVE_LIBCHAMPLAIN
1975 "viewport_map", &priv->viewport_map,
1977 "groups_widget", &priv->groups_widget,
1978 "vbox_details", &priv->vbox_details,
1979 "table_details", &priv->table_details,
1980 "hbox_details_requested", &priv->hbox_details_requested,
1981 "hbox_client_types", &priv->hbox_client_types,
1985 priv->table_location = NULL;
1987 gtk_box_pack_start (GTK_BOX (self), priv->vbox_individual_widget, TRUE, TRUE,
1989 gtk_widget_show (priv->vbox_individual_widget);
1991 priv->persona_tables = g_hash_table_new (NULL, NULL);
1992 priv->individual_table = NULL;
1994 /* Create widgets */
1995 details_set_up (self);
1997 g_object_unref (gui);
2001 constructed (GObject *object)
2003 GObjectClass *klass = G_OBJECT_CLASS (empathy_individual_widget_parent_class);
2004 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2005 GtkScrolledWindow *scrolled_window =
2006 GTK_SCROLLED_WINDOW (priv->scrolled_window_individual);
2008 /* Allow scrolling of the list of Personas if we're showing Personas. */
2009 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS)
2011 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
2012 gtk_scrolled_window_set_policy (scrolled_window,
2013 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
2014 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
2015 priv->scrolled_window_individual, TRUE, TRUE, 0, GTK_PACK_START);
2017 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
2019 gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
2023 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_NONE);
2024 gtk_scrolled_window_set_policy (scrolled_window,
2025 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
2026 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
2027 priv->scrolled_window_individual, FALSE, TRUE, 0, GTK_PACK_START);
2029 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
2034 if (klass->constructed != NULL)
2035 klass->constructed (object);
2039 get_property (GObject *object,
2044 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2048 case PROP_INDIVIDUAL:
2049 g_value_set_object (value, priv->individual);
2052 g_value_set_flags (value, priv->flags);
2055 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2061 set_property (GObject *object,
2063 const GValue *value,
2066 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2070 case PROP_INDIVIDUAL:
2071 empathy_individual_widget_set_individual (
2072 EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
2075 priv->flags = g_value_get_flags (value);
2078 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2084 dispose (GObject *object)
2086 remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
2088 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
2092 finalize (GObject *object)
2094 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2096 g_hash_table_destroy (priv->persona_tables);
2098 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
2102 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
2104 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2106 object_class->constructed = constructed;
2107 object_class->get_property = get_property;
2108 object_class->set_property = set_property;
2109 object_class->dispose = dispose;
2110 object_class->finalize = finalize;
2113 * EmpathyIndividualWidget:individual:
2115 * The #FolksIndividual to display in the widget.
2117 g_object_class_install_property (object_class, PROP_INDIVIDUAL,
2118 g_param_spec_object ("individual",
2120 "The #FolksIndividual to display in the widget.",
2121 FOLKS_TYPE_INDIVIDUAL,
2122 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2125 * EmpathyIndividualWidget:flags:
2127 * A set of flags which affect the widget's behaviour.
2129 g_object_class_install_property (object_class, PROP_FLAGS,
2130 g_param_spec_flags ("flags",
2132 "A set of flags which affect the widget's behaviour.",
2133 EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
2134 EMPATHY_INDIVIDUAL_WIDGET_NONE,
2135 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
2137 g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
2141 * empathy_individual_widget_new:
2142 * @individual: the #FolksIndividual to display
2143 * @flags: flags affecting how the widget behaves and what it displays
2145 * Creates a new #EmpathyIndividualWidget.
2147 * Return value: a new #EmpathyIndividualWidget
2150 empathy_individual_widget_new (FolksIndividual *individual,
2151 EmpathyIndividualWidgetFlags flags)
2153 g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
2156 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
2157 "individual", individual,
2163 * empathy_individual_widget_get_individual:
2164 * @self: an #EmpathyIndividualWidget
2166 * Returns the #FolksIndividual being displayed by the widget.
2168 * Return value: the #FolksIndividual being displayed, or %NULL
2171 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
2173 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
2175 return GET_PRIV (self)->individual;
2179 * empathy_individual_widget_set_individual:
2180 * @self: an #EmpathyIndividualWidget
2181 * @individual: the #FolksIndividual to display, or %NULL
2183 * Set the #FolksIndividual to be displayed by the widget:
2184 * #EmpathyIndividualWidget:individual.
2186 * The @individual may be %NULL in order to display nothing in the widget.
2189 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
2190 FolksIndividual *individual)
2192 EmpathyIndividualWidgetPriv *priv;
2194 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
2195 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
2197 priv = GET_PRIV (self);
2199 if (individual == priv->individual)
2202 /* Out with the old… */
2203 remove_individual (self);
2205 /* …and in with the new. */
2206 if (individual != NULL)
2207 g_object_ref (individual);
2208 priv->individual = individual;
2210 /* Update information for widgets */
2211 individual_update (self);
2212 groups_update (self);
2213 details_update (self);
2214 location_update (self);
2215 client_types_update (self);