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>
23 #include "empathy-individual-widget.h"
25 #include <glib/gi18n-lib.h>
26 #include <tp-account-widgets/tpaw-builder.h>
27 #include <tp-account-widgets/tpaw-contactinfo-utils.h>
28 #include <tp-account-widgets/tpaw-time.h>
29 #include <tp-account-widgets/tpaw-utils.h>
31 #ifdef HAVE_LIBCHAMPLAIN
32 #include <champlain/champlain.h>
33 #include <champlain-gtk/champlain-gtk.h>
36 #include "empathy-avatar-image.h"
37 #include "empathy-groups-widget.h"
38 #include "empathy-gtk-enum-types.h"
39 #include "empathy-location.h"
40 #include "empathy-request-util.h"
41 #include "empathy-ui-utils.h"
42 #include "empathy-utils.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
45 #include "empathy-debug.h"
48 * SECTION:empathy-individual-widget
49 * @title:EmpathyIndividualWidget
50 * @short_description: A widget used to display and edit details about an
52 * @include: libempathy-empathy-individual-widget.h
54 * #EmpathyIndividualWidget is a widget which displays appropriate widgets
55 * with details about an individual, also allowing changing these details,
60 * EmpathyIndividualWidget:
61 * @parent: parent object
63 * Widget which displays appropriate widgets with details about an individual,
64 * also allowing changing these details, if desired.
67 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualWidget)
70 FolksIndividual *individual; /* owned */
71 EmpathyIndividualWidgetFlags flags;
73 /* weak pointer to the contact whose contact details we're displaying */
76 /* unowned Persona (borrowed from priv->individual) -> GtkGrid child */
77 GHashTable *persona_grids;
78 /* Table containing the information for the individual as whole, or NULL */
79 GtkGrid *individual_grid;
82 GtkWidget *hbox_presence;
83 GtkWidget *vbox_individual_widget;
84 GtkWidget *scrolled_window_individual;
85 GtkWidget *viewport_individual;
86 GtkWidget *vbox_individual;
89 GtkWidget *vbox_location;
90 GtkWidget *subvbox_location;
91 GtkWidget *grid_location;
92 GtkWidget *label_location;
93 #ifdef HAVE_LIBCHAMPLAIN
94 GtkWidget *viewport_map;
95 GtkWidget *map_view_embed;
96 ChamplainView *map_view;
100 GtkWidget *groups_widget;
103 GtkWidget *hbox_client_types;
106 GtkWidget *vbox_details;
107 GtkWidget *grid_details;
108 GtkWidget *hbox_details_requested;
109 GtkWidget *details_spinner;
110 GCancellable *details_cancellable; /* owned */
111 } EmpathyIndividualWidgetPriv;
113 G_DEFINE_TYPE (EmpathyIndividualWidget, empathy_individual_widget,
121 static void client_types_update (EmpathyIndividualWidget *self);
122 static void remove_weak_contact (EmpathyIndividualWidget *self);
125 details_set_up (EmpathyIndividualWidget *self)
127 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
129 gtk_widget_hide (priv->vbox_details);
131 priv->details_spinner = gtk_spinner_new ();
132 gtk_box_pack_end (GTK_BOX (priv->hbox_details_requested),
133 priv->details_spinner, TRUE, TRUE, 0);
134 gtk_widget_show (priv->details_spinner);
138 client_types_notify_cb (TpContact *contact,
140 EmpathyIndividualWidget *self)
142 client_types_update (self);
146 EmpathyIndividualWidget *widget; /* weak */
147 TpContact *contact; /* owned */
151 update_weak_contact (EmpathyIndividualWidget *self)
153 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
154 TpContact *tp_contact = NULL;
156 remove_weak_contact (self);
158 if (priv->individual != NULL)
160 /* FIXME: We take the most available TpContact we find and only
161 * use its details. It would be a lot better if we would get the
162 * details for every TpContact in the Individual and merge them
163 * all, but that requires vCard support in libfolks for it to
164 * not be hideously complex. (bgo#627399) */
167 FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
169 personas = folks_individual_get_personas (priv->individual);
170 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
171 while (gee_iterator_next (iter))
173 FolksPersona *persona = gee_iterator_get (iter);
175 /* We only want personas which have presence and a TpContact */
176 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
178 FolksPresenceDetails *presence;
179 FolksPresenceType presence_type_cur;
181 presence = FOLKS_PRESENCE_DETAILS (persona);
182 presence_type_cur = folks_presence_details_get_presence_type (
185 if (tp_contact == NULL || folks_presence_details_typecmp (
186 presence_type_cur, presence_type) > 0)
188 presence_type = presence_type_cur;
189 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
193 g_clear_object (&persona);
195 g_clear_object (&iter);
198 if (tp_contact != NULL)
200 priv->contact = tp_contact;
201 g_object_add_weak_pointer (G_OBJECT (tp_contact),
202 (gpointer *) &priv->contact);
204 g_signal_connect (priv->contact, "notify::client-types",
205 (GCallback) client_types_notify_cb, self);
210 add_row (GtkGrid *grid,
215 gtk_grid_attach (grid, title, 0, row, 1, 1);
216 gtk_misc_set_alignment (GTK_MISC (title), 0, 0.5);
217 gtk_widget_show (title);
219 gtk_grid_attach (grid, value, 1, row, 1, 1);
220 gtk_misc_set_alignment (GTK_MISC (value), 0, 0.5);
221 gtk_widget_show (value);
225 channel_name_activated_cb (
230 empathy_join_muc (account, uri, empathy_get_current_action_time ());
235 create_channel_list_label (TpAccount *account,
239 GtkWidget *label = NULL;
240 GString *label_markup = g_string_new ("");
245 /* Is there channels? */
246 channels = g_ptr_array_new ();
248 for (l = info; l != NULL; l = l->next)
250 TpContactInfoField *field = l->data;
252 if (!tp_strdiff (field->field_name, "x-irc-channel"))
253 g_ptr_array_add (channels, (gpointer) field->field_value[0]);
256 if (channels->len == 0)
259 for (i = 0; i < channels->len; i++)
261 const gchar *channel_name = g_ptr_array_index (channels, i);
262 /* We abuse the URI of the link to hold the channel name. It seems to
263 * be okay to just use it essentially verbatim, rather than trying to
264 * ensure it's actually a valid URI. */
265 gchar *escaped = g_markup_escape_text (channel_name, -1);
268 g_string_append (label_markup, ", ");
270 g_string_append_printf (label_markup, "<a href='%s'>%s</a>",
275 label = gtk_label_new (NULL);
276 gtk_label_set_markup (GTK_LABEL (label), label_markup->str);
277 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
279 g_signal_connect (label, "activate-link",
280 (GCallback) channel_name_activated_cb, account);
283 g_ptr_array_unref (channels);
284 g_string_free (label_markup, TRUE);
289 details_update_show (EmpathyIndividualWidget *self,
292 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
295 GtkWidget *channels_label;
299 info = tp_contact_dup_contact_info (contact);
300 info = g_list_sort (info, (GCompareFunc) tpaw_contact_info_field_cmp);
301 for (l = info; l != NULL; l = l->next)
303 TpContactInfoField *field = l->data;
306 TpawContactInfoFormatFunc format;
307 GtkWidget *title_widget, *value_widget;
309 if (field->field_value == NULL || field->field_value[0] == NULL)
312 value = field->field_value[0];
314 if (!tpaw_contact_info_lookup_field (field->field_name,
317 DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
321 /* Skip empty field */
322 if (tp_str_empty (value))
326 title = tpaw_contact_info_field_label (field->field_name,
327 field->parameters, TRUE);
328 title_widget = gtk_label_new (title);
331 value_widget = gtk_label_new (value);
337 markup = format (field->field_value);
338 gtk_label_set_markup (GTK_LABEL (value_widget), markup);
342 gtk_label_set_selectable (GTK_LABEL (value_widget),
343 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
345 add_row (GTK_GRID (priv->grid_details), n_rows, title_widget,
351 conn = tp_contact_get_connection (contact);
352 account = tp_connection_get_account (conn);
354 channels_label = create_channel_list_label (account, info, n_rows);
356 if (channels_label != NULL)
358 GtkWidget *title_widget;
360 title_widget = gtk_label_new (_("Channels:"));
362 add_row (GTK_GRID (priv->grid_details), n_rows, title_widget,
368 tp_contact_info_list_free (info);
374 details_notify_cb (TpContact *contact,
376 EmpathyIndividualWidget *self)
378 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
381 gtk_container_foreach (GTK_CONTAINER (priv->grid_details),
382 (GtkCallback) gtk_widget_destroy, NULL);
384 n_rows = details_update_show (self, contact);
388 gtk_widget_show (priv->vbox_details);
389 gtk_widget_show (priv->grid_details);
393 gtk_widget_hide (priv->vbox_details);
396 gtk_widget_hide (priv->hbox_details_requested);
397 gtk_spinner_stop (GTK_SPINNER (priv->details_spinner));
401 details_request_cb (GObject *source,
405 EmpathyIndividualWidget *self = user_data;
406 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
407 TpContact *contact = (TpContact *) source;
408 gboolean hide_widget = FALSE;
409 GError *error = NULL;
411 if (tp_contact_request_contact_info_finish (contact, res, &error) == TRUE)
413 details_notify_cb (contact, NULL, self);
417 /* If the request got cancelled it could mean the contact widget is
418 * destroyed, so we should not dereference information */
419 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
421 g_error_free (error);
426 g_error_free (error);
429 if (hide_widget == TRUE)
430 gtk_widget_hide (GET_PRIV (self)->vbox_details);
432 tp_clear_object (&priv->details_cancellable);
434 tp_g_signal_connect_object (contact, "notify::contact-info",
435 (GCallback) details_notify_cb, self, 0);
439 fetch_contact_information (EmpathyIndividualWidget *self)
441 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
442 TpConnection *connection;
444 connection = tp_contact_get_connection (priv->contact);
446 if (!tp_proxy_has_interface_by_id (connection,
447 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_INFO))
449 gtk_widget_hide (GET_PRIV (self)->vbox_details);
453 /* Request the Individual's info */
454 gtk_widget_show (priv->vbox_details);
455 gtk_widget_show (priv->hbox_details_requested);
456 gtk_widget_hide (priv->grid_details);
457 gtk_spinner_start (GTK_SPINNER (priv->details_spinner));
459 if (priv->details_cancellable == NULL)
461 priv->details_cancellable = g_cancellable_new ();
463 tp_contact_request_contact_info_async (priv->contact,
464 priv->details_cancellable, details_request_cb, self);
469 details_update (EmpathyIndividualWidget *self)
471 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
473 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS))
476 gtk_widget_hide (priv->vbox_details);
478 if (priv->contact == NULL)
479 update_weak_contact (self);
481 if (priv->contact != NULL)
483 fetch_contact_information (self);
488 groups_update (EmpathyIndividualWidget *self)
490 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
492 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS &&
493 priv->individual != NULL)
495 empathy_groups_widget_set_group_details (
496 EMPATHY_GROUPS_WIDGET (priv->groups_widget),
497 FOLKS_GROUP_DETAILS (priv->individual));
498 gtk_widget_show (priv->groups_widget);
502 gtk_widget_hide (priv->groups_widget);
506 /* Converts the Location's GHashTable's key to a user readable string */
508 location_key_to_label (const gchar *key)
510 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
511 return _("Country ISO Code:");
512 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
513 return _("Country:");
514 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
516 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
518 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
520 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
521 return _("Postal Code:");
522 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
524 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
525 return _("Building:");
526 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
528 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
530 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
532 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
533 return _("Description:");
534 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
536 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
537 return _("Accuracy Level:");
538 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
540 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
541 return _("Vertical Error (meters):");
542 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
543 return _("Horizontal Error (meters):");
544 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
546 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
547 return _("Bearing:");
548 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
549 return _("Climb Speed:");
550 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
551 return _("Last Updated on:");
552 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
553 return _("Longitude:");
554 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
555 return _("Latitude:");
556 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
557 return _("Altitude:");
560 DEBUG ("Unexpected Location key: %s", key);
566 location_update (EmpathyIndividualWidget *self)
568 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
569 EmpathyContact *contact = NULL;
570 GHashTable *location = NULL;
574 static const gchar* ordered_geolocation_keys[] = {
575 EMPATHY_LOCATION_TEXT,
576 EMPATHY_LOCATION_URI,
577 EMPATHY_LOCATION_DESCRIPTION,
578 EMPATHY_LOCATION_BUILDING,
579 EMPATHY_LOCATION_FLOOR,
580 EMPATHY_LOCATION_ROOM,
581 EMPATHY_LOCATION_STREET,
582 EMPATHY_LOCATION_AREA,
583 EMPATHY_LOCATION_LOCALITY,
584 EMPATHY_LOCATION_REGION,
585 EMPATHY_LOCATION_COUNTRY,
590 gboolean display_map = FALSE;
594 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION) ||
595 priv->individual == NULL)
597 gtk_widget_hide (priv->vbox_location);
601 /* FIXME: For the moment, we just display the first location data we can
602 * find amongst the Individual's Personas. Once libfolks grows a location
603 * interface, we can use that. (bgo#627400) */
605 personas = folks_individual_get_personas (priv->individual);
606 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
607 while (location == NULL && gee_iterator_next (iter))
609 FolksPersona *persona = gee_iterator_get (iter);
611 if (empathy_folks_persona_is_interesting (persona))
613 TpContact *tp_contact;
615 /* Get the contact. If it turns out to have location information, we
616 * have to keep it alive for the duration of the function, since we're
617 * accessing its private data. */
618 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
619 if (tp_contact != NULL)
621 contact = empathy_contact_dup_from_tp_contact (tp_contact);
622 empathy_contact_set_persona (contact, persona);
624 /* Try and get a location */
625 location = empathy_contact_get_location (contact);
626 /* if location isn't fully valid, treat the contact as
628 if (location != NULL && g_hash_table_size (location) <= 0)
631 g_clear_object (&contact);
635 g_clear_object (&persona);
637 g_clear_object (&iter);
639 if (contact == NULL || location == NULL)
641 gtk_widget_hide (priv->vbox_location);
642 tp_clear_object (&contact);
646 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
649 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
650 gtk_label_set_markup (GTK_LABEL (priv->label_location), loc);
660 stamp = g_value_get_int64 (value);
662 user_date = tpaw_time_to_string_relative (stamp);
664 tmp = g_strdup_printf ("<b>%s</b>", _("Location"));
665 /* translators: format is "Location, $date" */
666 text = g_strdup_printf (_("%s, %s"), tmp, user_date);
668 gtk_label_set_markup (GTK_LABEL (priv->label_location), text);
673 /* Prepare the location information table */
674 if (priv->grid_location != NULL)
675 gtk_widget_destroy (priv->grid_location);
677 priv->grid_location = gtk_grid_new ();
678 gtk_box_pack_start (GTK_BOX (priv->subvbox_location),
679 priv->grid_location, FALSE, FALSE, 5);
682 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
684 const gchar* user_label;
688 gvalue = g_hash_table_lookup (location, (gpointer) skey);
692 user_label = location_key_to_label (skey);
694 label = gtk_label_new (user_label);
695 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
696 gtk_grid_attach (GTK_GRID (priv->grid_location),
697 label, 0, row, 1, 1);
698 gtk_widget_show (label);
700 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
703 dvalue = g_value_get_double (gvalue);
704 svalue = g_strdup_printf ("%f", dvalue);
706 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
708 svalue = g_value_dup_string (gvalue);
710 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
714 time_ = g_value_get_int64 (value);
715 svalue = tpaw_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
720 label = gtk_label_new (svalue);
721 gtk_grid_attach (GTK_GRID (priv->grid_location),
722 label, 1, row, 1, 1);
723 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
724 gtk_widget_show (label);
726 gtk_label_set_selectable (GTK_LABEL (label),
727 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE :
735 tp_clear_object (&contact);
737 #ifdef HAVE_LIBCHAMPLAIN
738 if ((g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL) &&
739 (g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL))
741 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
749 /* We can display some fields */
750 gtk_widget_show (priv->grid_location);
752 else if (display_map == FALSE)
754 /* Can't display either fields or map */
755 gtk_widget_hide (priv->vbox_location);
759 #ifdef HAVE_LIBCHAMPLAIN
760 if (display_map == TRUE)
762 ChamplainMarkerLayer *layer;
764 priv->map_view_embed = gtk_champlain_embed_new ();
765 priv->map_view = gtk_champlain_embed_get_view (
766 GTK_CHAMPLAIN_EMBED (priv->map_view_embed));
768 gtk_container_add (GTK_CONTAINER (priv->viewport_map),
769 priv->map_view_embed);
770 g_object_set (G_OBJECT (priv->map_view),
771 "kinetic-mode", TRUE,
775 layer = champlain_marker_layer_new ();
776 champlain_view_add_layer (priv->map_view, CHAMPLAIN_LAYER (layer));
778 /* FIXME: For now, we have to do this manually. Once libfolks grows a
779 * location interface, we can use that. (bgo#627400) */
781 personas = folks_individual_get_personas (priv->individual);
782 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
783 while (gee_iterator_next (iter))
785 FolksPersona *persona = gee_iterator_get (iter);
787 if (empathy_folks_persona_is_interesting (persona))
789 gdouble lat = 0.0, lon = 0.0;
790 ClutterActor *marker;
791 TpContact *tp_contact;
793 /* Get the contact */
794 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
795 if (tp_contact == NULL)
798 contact = empathy_contact_dup_from_tp_contact (tp_contact);
799 empathy_contact_set_persona (contact, persona);
801 /* Try and get a location */
802 location = empathy_contact_get_location (contact);
803 if (location == NULL || g_hash_table_size (location) == 0)
806 /* Get this persona's latitude and longitude */
807 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
811 lat = g_value_get_double (value);
813 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
817 lon = g_value_get_double (value);
819 /* Add a marker to the map */
820 marker = champlain_label_new_with_text (
821 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona)),
823 champlain_location_set_location (CHAMPLAIN_LOCATION (marker),
825 champlain_marker_layer_add_marker (layer,
826 CHAMPLAIN_MARKER (marker));
830 g_clear_object (&persona);
831 g_clear_object (&contact);
833 g_clear_object (&iter);
835 /* Zoom to show all of the markers */
836 champlain_view_ensure_layers_visible (priv->map_view, FALSE);
838 gtk_widget_show_all (priv->viewport_map);
842 gtk_widget_show (priv->vbox_location);
846 client_types_update (EmpathyIndividualWidget *self)
848 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
849 const gchar * const *types;
851 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_CLIENT_TYPES) ||
852 priv->individual == NULL)
854 gtk_widget_hide (priv->hbox_client_types);
858 if (priv->contact == NULL)
859 update_weak_contact (self);
861 /* let's try that again... */
862 if (priv->contact == NULL)
865 types = tp_contact_get_client_types (priv->contact);
867 if (empathy_client_types_contains_mobile_device ((GStrv) types))
869 gtk_widget_show (priv->hbox_client_types);
873 gtk_widget_hide (priv->hbox_client_types);
879 remove_weak_contact (EmpathyIndividualWidget *self)
881 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
883 if (priv->contact == NULL)
886 g_signal_handlers_disconnect_by_func (priv->contact, client_types_notify_cb,
889 g_object_remove_weak_pointer (G_OBJECT (priv->contact),
890 (gpointer *) &priv->contact);
891 priv->contact = NULL;
894 static EmpathyAvatar *
895 persona_dup_avatar (FolksPersona *persona)
897 TpContact *tp_contact;
898 EmpathyContact *contact;
899 EmpathyAvatar *avatar;
901 if (!empathy_folks_persona_is_interesting (persona))
904 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
905 if (tp_contact == NULL)
908 contact = empathy_contact_dup_from_tp_contact (tp_contact);
909 empathy_contact_set_persona (contact, persona);
911 avatar = empathy_contact_get_avatar (contact);
913 empathy_avatar_ref (avatar);
914 g_object_unref (contact);
919 static EmpathyAvatar *
920 individual_dup_avatar (FolksIndividual *individual)
924 EmpathyAvatar *avatar = NULL;
926 /* FIXME: We just choose the first Persona which has an avatar, and save that.
927 * The avatar handling in EmpathyContact needs to be moved into libfolks as
928 * much as possible, and this code rewritten to use FolksHasAvatar.
931 personas = folks_individual_get_personas (individual);
932 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
933 while (avatar == NULL && gee_iterator_next (iter))
935 FolksPersona *persona = gee_iterator_get (iter);
936 avatar = persona_dup_avatar (persona);
938 g_clear_object (&persona);
940 g_clear_object (&iter);
946 save_avatar_menu_activate_cb (GtkWidget *widget,
947 EmpathyIndividualWidget *self)
949 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
951 EmpathyAvatar *avatar;
952 gchar *ext = NULL, *filename;
954 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
956 GTK_FILE_CHOOSER_ACTION_SAVE,
957 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
958 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
961 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
964 avatar = individual_dup_avatar (priv->individual);
968 /* look for the avatar extension */
969 if (avatar->format != NULL)
973 splitted = g_strsplit (avatar->format, "/", 2);
974 if (splitted[0] != NULL && splitted[1] != NULL)
975 ext = g_strdup (splitted[1]);
977 g_strfreev (splitted);
981 /* Avatar was loaded from the cache so was converted to PNG */
982 ext = g_strdup ("png");
989 id = tp_escape_as_identifier (folks_individual_get_id (priv->individual));
991 filename = g_strdup_printf ("%s.%s", id, ext);
992 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
999 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
1001 GError *error = NULL;
1003 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1005 if (empathy_avatar_save_to_file (avatar, filename, &error) == FALSE)
1008 GtkWidget *error_dialog;
1010 error_dialog = gtk_message_dialog_new (NULL, 0,
1011 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1012 _("Unable to save avatar"));
1014 gtk_message_dialog_format_secondary_text (
1015 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
1017 g_signal_connect (error_dialog, "response",
1018 (GCallback) gtk_widget_destroy, NULL);
1020 gtk_window_present (GTK_WINDOW (error_dialog));
1022 g_clear_error (&error);
1028 gtk_widget_destroy (dialog);
1029 empathy_avatar_unref (avatar);
1033 popup_avatar_menu (EmpathyIndividualWidget *self,
1035 GdkEventButton *event)
1037 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1038 GtkWidget *menu, *item;
1039 EmpathyAvatar *avatar;
1040 gint button, event_time;
1042 if (priv->individual == NULL)
1045 avatar = individual_dup_avatar (priv->individual);
1048 empathy_avatar_unref (avatar);
1050 menu = empathy_context_menu_new (parent);
1052 /* Add "Save as..." entry */
1053 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
1054 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1055 gtk_widget_show (item);
1057 g_signal_connect (item, "activate",
1058 (GCallback) save_avatar_menu_activate_cb, self);
1062 button = event->button;
1063 event_time = event->time;
1068 event_time = gtk_get_current_event_time ();
1071 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, button, event_time);
1077 avatar_widget_popup_menu_cb (GtkWidget *widget,
1078 EmpathyIndividualWidget *self)
1080 return popup_avatar_menu (self, widget, NULL);
1084 avatar_widget_button_press_event_cb (GtkWidget *widget,
1085 GdkEventButton *event,
1086 EmpathyIndividualWidget *self)
1088 /* Ignore double-clicks and triple-clicks */
1089 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1090 return popup_avatar_menu (self, widget, event);
1095 /* Returns the TpAccount for the user as a convenience. Note that it has a ref
1098 individual_is_user (FolksIndividual *individual)
1102 TpAccount *retval = NULL;
1104 /* FIXME: This should move into libfolks when libfolks grows a way of
1105 * determining "self". (bgo#627402) */
1106 personas = folks_individual_get_personas (individual);
1107 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1108 while (gee_iterator_next (iter))
1110 FolksPersona *persona = gee_iterator_get (iter);
1112 if (TPF_IS_PERSONA (persona))
1114 TpContact *tp_contact;
1115 EmpathyContact *contact = NULL;
1117 /* Get the contact */
1118 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1119 if (tp_contact != NULL)
1121 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1122 empathy_contact_set_persona (contact, persona);
1124 /* Determine if the contact is the user */
1125 if (empathy_contact_is_user (contact))
1126 retval = g_object_ref (empathy_contact_get_account (contact));
1129 g_object_unref (contact);
1131 g_clear_object (&persona);
1133 g_clear_object (&iter);
1139 set_nickname_cb (TpAccount *account,
1140 GAsyncResult *result,
1143 GError *error = NULL;
1145 if (tp_account_set_nickname_finish (account, result, &error) == FALSE)
1147 DEBUG ("Failed to set Account.Nickname: %s", error->message);
1148 g_error_free (error);
1153 entry_alias_focus_event_cb (GtkEditable *editable,
1154 GdkEventFocus *event,
1155 EmpathyIndividualWidget *self)
1157 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1159 if (priv->individual != NULL)
1164 alias = gtk_entry_get_text (GTK_ENTRY (editable));
1165 account = individual_is_user (priv->individual);
1167 if (account != NULL)
1169 DEBUG ("Set Account.Nickname to %s", alias);
1170 tp_account_set_nickname_async (account, alias,
1171 (GAsyncReadyCallback) set_nickname_cb, NULL);
1172 g_object_unref (account);
1176 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (priv->individual),
1185 favourite_toggled_cb (GtkToggleButton *button,
1186 EmpathyIndividualWidget *self)
1188 gboolean active = gtk_toggle_button_get_active (button);
1189 folks_favourite_details_set_is_favourite (
1190 FOLKS_FAVOURITE_DETAILS (GET_PRIV (self)->individual), active);
1194 notify_avatar_cb (gpointer folks_object,
1196 EmpathyIndividualWidget *self)
1198 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1199 EmpathyAvatar *avatar = NULL;
1201 GtkWidget *avatar_widget;
1203 if (FOLKS_IS_INDIVIDUAL (folks_object))
1205 avatar = individual_dup_avatar (FOLKS_INDIVIDUAL (folks_object));
1206 grid = G_OBJECT (priv->individual_grid);
1208 else if (FOLKS_IS_PERSONA (folks_object))
1210 avatar = persona_dup_avatar (FOLKS_PERSONA (folks_object));
1211 grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1215 g_assert_not_reached ();
1221 avatar_widget = g_object_get_data (grid, "avatar-widget");
1222 empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (avatar_widget), avatar);
1225 empathy_avatar_unref (avatar);
1229 notify_alias_cb (gpointer folks_object,
1231 EmpathyIndividualWidget *self)
1233 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1235 GtkWidget *alias_widget;
1237 if (FOLKS_IS_INDIVIDUAL (folks_object))
1238 grid = G_OBJECT (priv->individual_grid);
1239 else if (FOLKS_IS_PERSONA (folks_object))
1240 grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1242 g_assert_not_reached ();
1247 alias_widget = g_object_get_data (grid, "alias-widget");
1249 if (GTK_IS_ENTRY (alias_widget))
1251 gtk_entry_set_text (GTK_ENTRY (alias_widget),
1252 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (folks_object)));
1256 gtk_label_set_label (GTK_LABEL (alias_widget),
1257 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (folks_object)));
1262 notify_presence_cb (gpointer folks_object,
1264 EmpathyIndividualWidget *self)
1266 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1268 GtkWidget *status_label, *state_image;
1269 const gchar *message;
1270 gchar *markup_text = NULL;
1271 FolksPresenceType presence;
1272 gboolean visible = TRUE;
1274 if (FOLKS_IS_INDIVIDUAL (folks_object))
1275 grid = G_OBJECT (priv->individual_grid);
1276 else if (FOLKS_IS_PERSONA (folks_object))
1277 grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1279 g_assert_not_reached ();
1284 status_label = g_object_get_data (grid, "status-label");
1285 state_image = g_object_get_data (grid, "state-image");
1287 presence = folks_presence_details_get_presence_type (
1288 FOLKS_PRESENCE_DETAILS (folks_object));
1289 if (presence == FOLKS_PRESENCE_TYPE_UNKNOWN ||
1290 presence == FOLKS_PRESENCE_TYPE_ERROR)
1292 /* Don't display anything if we don't know the presence */
1297 /* FIXME: Default messages should be moved into libfolks (bgo#627403) */
1298 message = folks_presence_details_get_presence_message (
1299 FOLKS_PRESENCE_DETAILS (folks_object));
1300 if (TPAW_STR_EMPTY (message))
1302 message = empathy_presence_get_default_message (presence);
1305 if (message != NULL)
1306 markup_text = tpaw_add_link_markup (message);
1307 gtk_label_set_markup (GTK_LABEL (status_label), markup_text);
1308 g_free (markup_text);
1310 gtk_image_set_from_icon_name (GTK_IMAGE (state_image),
1311 empathy_icon_name_for_presence (presence),
1312 GTK_ICON_SIZE_BUTTON);
1315 gtk_widget_set_visible (status_label, visible);
1316 gtk_widget_set_visible (state_image, visible);
1320 notify_is_favourite_cb (gpointer folks_object,
1322 EmpathyIndividualWidget *self)
1324 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1326 GtkWidget *favourite_widget;
1328 if (FOLKS_IS_INDIVIDUAL (folks_object))
1329 grid = G_OBJECT (priv->individual_grid);
1330 else if (FOLKS_IS_PERSONA (folks_object))
1331 grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1333 g_assert_not_reached ();
1338 favourite_widget = g_object_get_data (grid, "favourite-widget");
1340 if (GTK_IS_TOGGLE_BUTTON (favourite_widget))
1342 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (favourite_widget),
1343 folks_favourite_details_get_is_favourite (
1344 FOLKS_FAVOURITE_DETAILS (folks_object)));
1349 alias_presence_avatar_favourite_set_up (EmpathyIndividualWidget *self,
1353 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1354 GtkWidget *label, *alias, *image, *avatar;
1355 guint current_row = starting_row;
1358 label = gtk_label_new (_("Alias:"));
1359 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1360 gtk_grid_attach (grid, label, 0, current_row, 1, 1);
1361 gtk_widget_show (label);
1363 /* Set up alias label/entry */
1364 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS)
1366 alias = gtk_entry_new ();
1368 g_signal_connect (alias, "focus-out-event",
1369 (GCallback) entry_alias_focus_event_cb, self);
1371 /* Make return activate the window default (the Close button) */
1372 gtk_entry_set_activates_default (GTK_ENTRY (alias), TRUE);
1376 alias = gtk_label_new (NULL);
1377 gtk_label_set_selectable (GTK_LABEL (alias),
1378 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1379 gtk_misc_set_alignment (GTK_MISC (alias), 0.0, 0.5);
1382 g_object_set_data (G_OBJECT (grid), "alias-widget", alias);
1383 gtk_grid_attach_next_to (grid, alias, label,
1384 GTK_POS_RIGHT, 1, 1);
1385 gtk_widget_show (alias);
1390 priv->hbox_presence = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
1392 /* Presence image */
1393 image = gtk_image_new_from_stock (GTK_STOCK_MISSING_IMAGE,
1394 GTK_ICON_SIZE_BUTTON);
1395 g_object_set_data (G_OBJECT (grid), "state-image", image);
1396 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), image, FALSE,
1398 gtk_widget_show (image);
1400 label = gtk_label_new ("");
1401 gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
1402 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1403 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1405 gtk_label_set_selectable (GTK_LABEL (label),
1406 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1408 g_object_set_data (G_OBJECT (grid), "status-label", label);
1409 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, FALSE,
1411 gtk_widget_show (label);
1413 gtk_grid_attach (grid, priv->hbox_presence,
1414 0, current_row, 2, 1);
1415 gtk_widget_show (priv->hbox_presence);
1419 /* Set up favourite toggle button */
1420 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1422 GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1424 g_signal_connect (favourite, "toggled",
1425 (GCallback) favourite_toggled_cb, self);
1427 g_object_set_data (G_OBJECT (grid), "favourite-widget", favourite);
1428 gtk_grid_attach (grid, favourite,
1429 0, current_row, 2, 1);
1430 gtk_widget_show (favourite);
1435 /* Set up avatar display */
1436 avatar = empathy_avatar_image_new ();
1438 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1440 g_signal_connect (avatar, "popup-menu",
1441 (GCallback) avatar_widget_popup_menu_cb, self);
1442 g_signal_connect (avatar, "button-press-event",
1443 (GCallback) avatar_widget_button_press_event_cb, self);
1446 g_object_set_data (G_OBJECT (grid), "avatar-widget", avatar);
1447 g_object_set (avatar,
1448 "valign", GTK_ALIGN_START,
1455 gtk_grid_attach (grid, avatar,
1456 2, 0, 1, current_row);
1457 gtk_widget_show (avatar);
1461 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1463 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1464 TpContact *tp_contact;
1465 EmpathyContact *contact;
1472 grid = g_hash_table_lookup (priv->persona_grids, persona);
1474 g_assert (grid != NULL);
1476 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1477 if (tp_contact == NULL)
1480 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1481 empathy_contact_set_persona (contact, persona);
1483 account = empathy_contact_get_account (contact);
1485 /* Update account widget */
1486 if (account != NULL)
1490 label = g_object_get_data (G_OBJECT (grid), "account-label");
1491 image = g_object_get_data (G_OBJECT (grid), "account-image");
1493 name = tp_account_get_display_name (account);
1494 gtk_label_set_label (label, name);
1496 name = tp_account_get_icon_name (account);
1497 gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
1500 /* Update id widget */
1501 label = g_object_get_data (G_OBJECT (grid), "id-widget");
1502 id = folks_persona_get_display_id (persona);
1503 gtk_label_set_label (label, (id != NULL) ? id : "");
1505 /* Update other widgets */
1506 notify_alias_cb (persona, NULL, self);
1507 notify_presence_cb (persona, NULL, self);
1508 notify_avatar_cb (persona, NULL, self);
1510 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1511 notify_is_favourite_cb (persona, NULL, self);
1513 g_object_unref (contact);
1517 add_persona (EmpathyIndividualWidget *self,
1518 FolksPersona *persona)
1520 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1523 GtkWidget *label, *account_label, *account_image, *separator;
1524 guint current_row = 0;
1526 if (!empathy_folks_persona_is_interesting (persona))
1529 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1530 grid = GTK_GRID (gtk_grid_new ());
1532 grid = GTK_GRID (gtk_grid_new ());
1534 gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
1535 gtk_grid_set_row_spacing (grid, 6);
1536 gtk_grid_set_column_spacing (grid, 6);
1538 /* Account and Identifier */
1539 label = gtk_label_new (_("Account:"));
1540 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1541 gtk_grid_attach (grid, label, 0, current_row, 1, 1);
1542 gtk_widget_show (label);
1544 /* Pack the protocol icon with the account name in an hbox */
1545 hbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6));
1547 account_label = gtk_label_new (NULL);
1548 gtk_label_set_selectable (GTK_LABEL (account_label),
1549 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1550 gtk_misc_set_alignment (GTK_MISC (account_label), 0.0, 0.5);
1551 gtk_widget_show (account_label);
1553 account_image = gtk_image_new ();
1554 gtk_widget_show (account_image);
1556 gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1557 gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
1559 g_object_set_data (G_OBJECT (grid), "account-image", account_image);
1560 g_object_set_data (G_OBJECT (grid), "account-label", account_label);
1561 gtk_grid_attach_next_to (grid, GTK_WIDGET (hbox), label, GTK_POS_RIGHT, 1, 1);
1562 gtk_widget_show (GTK_WIDGET (hbox));
1566 /* Translators: Identifier to connect to Instant Messaging network */
1567 label = gtk_label_new (_("Identifier:"));
1568 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1569 gtk_grid_attach (grid, label, 0, current_row, 1, 1);
1570 gtk_widget_show (label);
1572 /* Set up ID label */
1573 label = gtk_label_new (NULL);
1574 gtk_label_set_selectable (GTK_LABEL (label),
1575 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1576 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1578 g_object_set_data (G_OBJECT (grid), "id-widget", label);
1579 gtk_grid_attach (grid, label, 1, current_row, 1, 1);
1580 gtk_widget_show (label);
1584 alias_presence_avatar_favourite_set_up (self, grid, current_row);
1586 /* Connect to signals and display the grid */
1587 g_signal_connect (persona, "notify::alias",
1588 (GCallback) notify_alias_cb, self);
1589 g_signal_connect (persona, "notify::avatar",
1590 (GCallback) notify_avatar_cb, self);
1591 g_signal_connect (persona, "notify::presence-type",
1592 (GCallback) notify_presence_cb, self);
1593 g_signal_connect (persona, "notify::presence-message",
1594 (GCallback) notify_presence_cb, self);
1596 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1598 g_signal_connect (persona, "notify::is-favourite",
1599 (GCallback) notify_is_favourite_cb, self);
1602 gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1603 GTK_WIDGET (grid), FALSE, TRUE, 0);
1604 gtk_widget_show (GTK_WIDGET (grid));
1606 /* Pack a separator after the grid */
1607 separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
1608 g_object_set_data (G_OBJECT (grid), "separator", separator);
1609 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), separator, FALSE, FALSE,
1611 gtk_widget_show (separator);
1613 g_hash_table_replace (priv->persona_grids, persona, grid);
1615 /* Update the new widgets */
1616 update_persona (self, persona);
1620 remove_persona (EmpathyIndividualWidget *self,
1621 FolksPersona *persona)
1623 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1624 GtkWidget *separator;
1627 if (!empathy_folks_persona_is_interesting (persona))
1630 grid = g_hash_table_lookup (priv->persona_grids, persona);
1634 g_signal_handlers_disconnect_by_func (persona, notify_alias_cb, self);
1635 g_signal_handlers_disconnect_by_func (persona, notify_avatar_cb, self);
1636 g_signal_handlers_disconnect_by_func (persona, notify_presence_cb, self);
1638 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1640 g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
1644 /* Remove the separator */
1645 separator = g_object_get_data (G_OBJECT (grid), "separator");
1646 if (separator != NULL)
1647 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual), separator);
1649 /* Remove the widget */
1650 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1653 g_hash_table_remove (priv->persona_grids, persona);
1657 update_individual_grid (EmpathyIndividualWidget *self)
1659 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1661 notify_alias_cb (priv->individual, NULL, self);
1662 notify_presence_cb (priv->individual, NULL, self);
1663 notify_avatar_cb (priv->individual, NULL, self);
1665 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1666 notify_is_favourite_cb (priv->individual, NULL, self);
1670 individual_grid_set_up (EmpathyIndividualWidget *self)
1672 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1673 guint current_row = 0;
1676 grid = GTK_GRID (gtk_grid_new ());
1677 gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
1678 gtk_grid_set_row_spacing (grid, 6);
1679 gtk_grid_set_column_spacing (grid, 6);
1681 /* We only display the number of personas in tooltips */
1682 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1688 guint num_personas = 0;
1690 /* Meta-contacts message displaying how many Telepathy personas we have */
1691 personas = folks_individual_get_personas (priv->individual);
1692 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1693 while (gee_iterator_next (iter))
1695 FolksPersona *persona = gee_iterator_get (iter);
1696 if (empathy_folks_persona_is_interesting (persona))
1699 g_clear_object (&persona);
1701 g_clear_object (&iter);
1703 /* Translators: the plurality applies to both instances of the word
1705 message = g_strdup_printf (
1706 ngettext ("Linked contact containing %u contact",
1707 "Linked contacts containing %u contacts", num_personas),
1709 label = gtk_label_new (message);
1710 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1713 gtk_grid_attach (grid, label, 0, current_row, 2, 1);
1714 gtk_widget_show (label);
1719 alias_presence_avatar_favourite_set_up (self, grid, current_row);
1721 /* Display the grid */
1722 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (grid),
1724 gtk_widget_show (GTK_WIDGET (grid));
1726 priv->individual_grid = grid;
1728 /* Update the grid */
1729 update_individual_grid (self);
1733 individual_grid_destroy (EmpathyIndividualWidget *self)
1735 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1737 if (priv->individual_grid == NULL)
1740 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1741 GTK_WIDGET (priv->individual_grid));
1743 #ifdef HAVE_LIBCHAMPLAIN
1744 if (priv->map_view_embed != NULL)
1746 gtk_container_remove (GTK_CONTAINER (priv->viewport_map),
1747 priv->map_view_embed);
1749 priv->map_view_embed = NULL;
1753 priv->individual_grid = NULL;
1757 personas_changed_cb (FolksIndividual *individual,
1760 EmpathyIndividualWidget *self)
1762 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1763 GList *l, *children;
1766 gboolean show_personas, was_showing_personas, will_show_personas, is_last;
1767 guint old_num_personas, new_num_personas = 0;
1769 personas = folks_individual_get_personas (individual);
1770 /* we'll re-use this iterator throughout */
1771 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1773 /* Note that old_num_personas is the number of persona gridss we were
1774 * displaying, not the number of Personas which were in the Individual
1776 old_num_personas = g_hash_table_size (priv->persona_grids);
1778 while (gee_iterator_next (iter))
1780 FolksPersona *persona = gee_iterator_get (iter);
1781 if (empathy_folks_persona_is_interesting (persona))
1784 g_clear_object (&persona);
1788 * What we display for various conditions:
1789 * - "Personas": display the alias, avatar, presence account and identifier
1790 * for each of the Individual's Personas. (i.e. One grid per
1792 * - "Individual": display the alias, avatar and presence for the Individual,
1793 * and a label saying "Meta-contact containing x contacts".
1794 * (i.e. One grid in total.)
1796 * | SHOW_PERSONAS | !SHOW_PERSONAS
1797 * -------------+---------------+---------------
1798 * > 1 Persona | Personas | Individual
1799 * -------------+---------------+---------------
1800 * == 1 Persona | Personas | Personas
1802 show_personas = (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) != 0;
1803 was_showing_personas = show_personas || old_num_personas == 1;
1804 will_show_personas = show_personas || new_num_personas == 1;
1806 /* If both @added and @removed are NULL, we're being called manually, and we
1807 * need to set up the gridss for the first time. We do this simply by
1808 * ensuring was_showing_personas and will_show_personas are different so that
1809 * the code resets the UI.
1811 if (added == NULL && removed == NULL)
1812 was_showing_personas = !will_show_personas;
1814 if (was_showing_personas && will_show_personas)
1816 GeeIterator *iter_changed;
1818 /* Remove outdated Personas */
1819 iter_changed = gee_iterable_iterator (GEE_ITERABLE (removed));
1820 while (gee_iterator_next (iter_changed))
1822 FolksPersona *persona = gee_iterator_get (iter_changed);
1823 remove_persona (self, persona);
1824 g_clear_object (&persona);
1826 g_clear_object (&iter_changed);
1828 /* Add new Personas */
1829 iter_changed = gee_iterable_iterator (GEE_ITERABLE (added));
1830 while (gee_iterator_next (iter_changed))
1832 FolksPersona *persona = gee_iterator_get (iter_changed);
1833 add_persona (self, persona);
1834 g_clear_object (&persona);
1836 g_clear_object (&iter_changed);
1838 else if (!was_showing_personas && will_show_personas)
1840 /* Remove the old Individual grid */
1841 individual_grid_destroy (self);
1843 /* Set up all the Persona grids instead */
1844 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1845 while (gee_iterator_next (iter))
1847 FolksPersona *persona = gee_iterator_get (iter);
1848 add_persona (self, persona);
1849 g_clear_object (&persona);
1852 else if (was_showing_personas && !will_show_personas)
1854 /* Remove all Personas */
1855 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1856 while (gee_iterator_next (iter))
1858 FolksPersona *persona = gee_iterator_get (iter);
1859 remove_persona (self, persona);
1860 g_clear_object (&persona);
1863 if (removed != NULL)
1865 GeeIterator *iter_changed;
1867 iter_changed = gee_iterable_iterator (GEE_ITERABLE (removed));
1868 while (gee_iterator_next (iter_changed))
1870 FolksPersona *persona = gee_iterator_get (iter_changed);
1871 remove_persona (self, persona);
1872 g_clear_object (&persona);
1874 g_clear_object (&iter_changed);
1877 /* Set up the Individual grid instead */
1878 individual_grid_set_up (self);
1880 g_clear_object (&iter);
1882 /* Hide the last separator and show the others */
1883 children = gtk_container_get_children (GTK_CONTAINER (priv->vbox_individual));
1884 children = g_list_reverse (children);
1887 for (l = children; l != NULL; l = l->next)
1889 if (GTK_IS_SEPARATOR (l->data))
1891 gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1896 g_list_free (children);
1900 individual_removed_cb (FolksIndividual *individual,
1901 FolksIndividual *replacement_individual,
1902 EmpathyIndividualWidget *self)
1904 empathy_individual_widget_set_individual (self, replacement_individual);
1908 remove_individual (EmpathyIndividualWidget *self)
1910 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1911 if (priv->individual != NULL)
1916 g_signal_handlers_disconnect_by_func (priv->individual,
1917 notify_alias_cb, self);
1918 g_signal_handlers_disconnect_by_func (priv->individual,
1919 notify_presence_cb, self);
1920 g_signal_handlers_disconnect_by_func (priv->individual,
1921 notify_avatar_cb, self);
1922 g_signal_handlers_disconnect_by_func (priv->individual,
1923 personas_changed_cb, self);
1924 g_signal_handlers_disconnect_by_func (priv->individual,
1925 individual_removed_cb, self);
1927 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1929 g_signal_handlers_disconnect_by_func (priv->individual,
1930 notify_is_favourite_cb, self);
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);
1938 remove_persona (self, persona);
1939 g_clear_object (&persona);
1941 g_clear_object (&iter);
1942 individual_grid_destroy (self);
1944 if (priv->contact != NULL)
1945 remove_weak_contact (self);
1947 tp_clear_object (&priv->individual);
1950 if (priv->details_cancellable != NULL)
1951 g_cancellable_cancel (priv->details_cancellable);
1955 individual_update (EmpathyIndividualWidget *self)
1957 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1959 /* Connect and get info from new Individual */
1960 if (priv->individual != NULL)
1962 g_signal_connect (priv->individual, "notify::alias",
1963 (GCallback) notify_alias_cb, self);
1964 g_signal_connect (priv->individual, "notify::presence-type",
1965 (GCallback) notify_presence_cb, self);
1966 g_signal_connect (priv->individual, "notify::presence-message",
1967 (GCallback) notify_presence_cb, self);
1968 g_signal_connect (priv->individual, "notify::avatar",
1969 (GCallback) notify_avatar_cb, self);
1970 g_signal_connect (priv->individual, "personas-changed",
1971 (GCallback) personas_changed_cb, self);
1972 g_signal_connect (priv->individual, "removed",
1973 (GCallback) individual_removed_cb, self);
1975 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1977 g_signal_connect (priv->individual, "notify::is-favourite",
1978 (GCallback) notify_is_favourite_cb, self);
1981 /* Update individual grid */
1982 personas_changed_cb (priv->individual, NULL, NULL, self);
1985 if (priv->individual == NULL)
1987 gtk_widget_hide (priv->vbox_individual);
1989 else if (priv->individual_grid != NULL)
1991 /* We only need to update the details for the Individual as a whole */
1992 update_individual_grid (self);
1993 gtk_widget_show (priv->vbox_individual);
1997 /* We need to update the details for every Persona in the Individual */
2001 personas = folks_individual_get_personas (priv->individual);
2002 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2003 while (gee_iterator_next (iter))
2005 FolksPersona *persona = gee_iterator_get (iter);
2007 if (empathy_folks_persona_is_interesting (persona))
2008 update_persona (self, persona);
2010 g_clear_object (&persona);
2012 g_clear_object (&iter);
2014 gtk_widget_show (priv->vbox_individual);
2019 empathy_individual_widget_init (EmpathyIndividualWidget *self)
2021 EmpathyIndividualWidgetPriv *priv;
2025 priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
2026 EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
2029 gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
2030 GTK_ORIENTATION_VERTICAL);
2032 filename = empathy_file_lookup ("empathy-individual-widget.ui",
2034 gui = tpaw_builder_get_file (filename,
2035 "scrolled_window_individual", &priv->scrolled_window_individual,
2036 "viewport_individual", &priv->viewport_individual,
2037 "vbox_individual_widget", &priv->vbox_individual_widget,
2038 "vbox_individual", &priv->vbox_individual,
2039 "vbox_location", &priv->vbox_location,
2040 "subvbox_location", &priv->subvbox_location,
2041 "label_location", &priv->label_location,
2042 #ifdef HAVE_LIBCHAMPLAIN
2043 "viewport_map", &priv->viewport_map,
2045 "groups_widget", &priv->groups_widget,
2046 "vbox_details", &priv->vbox_details,
2047 "grid_details", &priv->grid_details,
2048 "hbox_details_requested", &priv->hbox_details_requested,
2049 "hbox_client_types", &priv->hbox_client_types,
2053 priv->grid_location = NULL;
2055 gtk_box_pack_start (GTK_BOX (self), priv->vbox_individual_widget, TRUE, TRUE,
2057 gtk_widget_show (priv->vbox_individual_widget);
2059 priv->persona_grids = g_hash_table_new (NULL, NULL);
2060 priv->individual_grid = NULL;
2062 /* Create widgets */
2063 details_set_up (self);
2065 g_object_unref (gui);
2069 constructed (GObject *object)
2071 GObjectClass *klass = G_OBJECT_CLASS (empathy_individual_widget_parent_class);
2072 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2073 GtkScrolledWindow *scrolled_window =
2074 GTK_SCROLLED_WINDOW (priv->scrolled_window_individual);
2076 /* Allow scrolling of the list of Personas if we're showing Personas. */
2077 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS)
2079 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
2080 gtk_scrolled_window_set_policy (scrolled_window,
2081 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
2082 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
2083 priv->scrolled_window_individual, TRUE, TRUE, 0, GTK_PACK_START);
2085 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
2087 gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
2091 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_NONE);
2092 gtk_scrolled_window_set_policy (scrolled_window,
2093 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
2094 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
2095 priv->scrolled_window_individual, FALSE, TRUE, 0, GTK_PACK_START);
2097 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
2102 if (klass->constructed != NULL)
2103 klass->constructed (object);
2107 get_property (GObject *object,
2112 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2116 case PROP_INDIVIDUAL:
2117 g_value_set_object (value, priv->individual);
2120 g_value_set_flags (value, priv->flags);
2123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2129 set_property (GObject *object,
2131 const GValue *value,
2134 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2138 case PROP_INDIVIDUAL:
2139 empathy_individual_widget_set_individual (
2140 EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
2143 priv->flags = g_value_get_flags (value);
2146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2152 dispose (GObject *object)
2154 remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
2156 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
2160 finalize (GObject *object)
2162 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2164 g_hash_table_unref (priv->persona_grids);
2166 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
2170 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
2172 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2174 object_class->constructed = constructed;
2175 object_class->get_property = get_property;
2176 object_class->set_property = set_property;
2177 object_class->dispose = dispose;
2178 object_class->finalize = finalize;
2181 * EmpathyIndividualWidget:individual:
2183 * The #FolksIndividual to display in the widget.
2185 g_object_class_install_property (object_class, PROP_INDIVIDUAL,
2186 g_param_spec_object ("individual",
2188 "The #FolksIndividual to display in the widget.",
2189 FOLKS_TYPE_INDIVIDUAL,
2190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2193 * EmpathyIndividualWidget:flags:
2195 * A set of flags which affect the widget's behaviour.
2197 g_object_class_install_property (object_class, PROP_FLAGS,
2198 g_param_spec_flags ("flags",
2200 "A set of flags which affect the widget's behaviour.",
2201 EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
2202 EMPATHY_INDIVIDUAL_WIDGET_NONE,
2203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
2205 g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
2209 * empathy_individual_widget_new:
2210 * @individual: the #FolksIndividual to display
2211 * @flags: flags affecting how the widget behaves and what it displays
2213 * Creates a new #EmpathyIndividualWidget.
2215 * Return value: a new #EmpathyIndividualWidget
2218 empathy_individual_widget_new (FolksIndividual *individual,
2219 EmpathyIndividualWidgetFlags flags)
2221 g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
2224 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
2225 "individual", individual,
2231 * empathy_individual_widget_get_individual:
2232 * @self: an #EmpathyIndividualWidget
2234 * Returns the #FolksIndividual being displayed by the widget.
2236 * Return value: the #FolksIndividual being displayed, or %NULL
2239 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
2241 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
2243 return GET_PRIV (self)->individual;
2247 * empathy_individual_widget_set_individual:
2248 * @self: an #EmpathyIndividualWidget
2249 * @individual: the #FolksIndividual to display, or %NULL
2251 * Set the #FolksIndividual to be displayed by the widget:
2252 * #EmpathyIndividualWidget:individual.
2254 * The @individual may be %NULL in order to display nothing in the widget.
2257 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
2258 FolksIndividual *individual)
2260 EmpathyIndividualWidgetPriv *priv;
2262 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
2263 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
2265 priv = GET_PRIV (self);
2267 if (individual == priv->individual)
2270 /* Out with the old… */
2271 remove_individual (self);
2273 /* …and in with the new. */
2274 if (individual != NULL)
2275 g_object_ref (individual);
2276 priv->individual = individual;
2278 /* Update information for widgets */
2279 individual_update (self);
2280 groups_update (self);
2281 details_update (self);
2282 location_update (self);
2283 client_types_update (self);