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>
30 #ifdef HAVE_LIBCHAMPLAIN
31 #include <champlain/champlain.h>
32 #include <champlain-gtk/champlain-gtk.h>
35 #include "empathy-avatar-image.h"
36 #include "empathy-groups-widget.h"
37 #include "empathy-gtk-enum-types.h"
38 #include "empathy-location.h"
39 #include "empathy-request-util.h"
40 #include "empathy-ui-utils.h"
41 #include "empathy-utils.h"
43 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
44 #include "empathy-debug.h"
47 * SECTION:empathy-individual-widget
48 * @title:EmpathyIndividualWidget
49 * @short_description: A widget used to display and edit details about an
51 * @include: libempathy-empathy-individual-widget.h
53 * #EmpathyIndividualWidget is a widget which displays appropriate widgets
54 * with details about an individual, also allowing changing these details,
59 * EmpathyIndividualWidget:
60 * @parent: parent object
62 * Widget which displays appropriate widgets with details about an individual,
63 * also allowing changing these details, if desired.
66 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualWidget)
69 FolksIndividual *individual; /* owned */
70 EmpathyIndividualWidgetFlags flags;
72 /* weak pointer to the contact whose contact details we're displaying */
75 /* unowned Persona (borrowed from priv->individual) -> GtkGrid child */
76 GHashTable *persona_grids;
77 /* Table containing the information for the individual as whole, or NULL */
78 GtkGrid *individual_grid;
81 GtkWidget *hbox_presence;
82 GtkWidget *vbox_individual_widget;
83 GtkWidget *scrolled_window_individual;
84 GtkWidget *viewport_individual;
85 GtkWidget *vbox_individual;
88 GtkWidget *vbox_location;
89 GtkWidget *subvbox_location;
90 GtkWidget *grid_location;
91 GtkWidget *label_location;
92 #ifdef HAVE_LIBCHAMPLAIN
93 GtkWidget *viewport_map;
94 GtkWidget *map_view_embed;
95 ChamplainView *map_view;
99 GtkWidget *groups_widget;
102 GtkWidget *hbox_client_types;
105 GtkWidget *vbox_details;
106 GtkWidget *grid_details;
107 GtkWidget *hbox_details_requested;
108 GtkWidget *details_spinner;
109 GCancellable *details_cancellable; /* owned */
110 } EmpathyIndividualWidgetPriv;
112 G_DEFINE_TYPE (EmpathyIndividualWidget, empathy_individual_widget,
120 static void client_types_update (EmpathyIndividualWidget *self);
121 static void remove_weak_contact (EmpathyIndividualWidget *self);
124 details_set_up (EmpathyIndividualWidget *self)
126 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
128 gtk_widget_hide (priv->vbox_details);
130 priv->details_spinner = gtk_spinner_new ();
131 gtk_box_pack_end (GTK_BOX (priv->hbox_details_requested),
132 priv->details_spinner, TRUE, TRUE, 0);
133 gtk_widget_show (priv->details_spinner);
137 client_types_notify_cb (TpContact *contact,
139 EmpathyIndividualWidget *self)
141 client_types_update (self);
145 EmpathyIndividualWidget *widget; /* weak */
146 TpContact *contact; /* owned */
150 update_weak_contact (EmpathyIndividualWidget *self)
152 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
153 TpContact *tp_contact = NULL;
155 remove_weak_contact (self);
157 if (priv->individual != NULL)
159 /* FIXME: We take the most available TpContact we find and only
160 * use its details. It would be a lot better if we would get the
161 * details for every TpContact in the Individual and merge them
162 * all, but that requires vCard support in libfolks for it to
163 * not be hideously complex. (bgo#627399) */
166 FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
168 personas = folks_individual_get_personas (priv->individual);
169 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
170 while (gee_iterator_next (iter))
172 FolksPersona *persona = gee_iterator_get (iter);
174 /* We only want personas which have presence and a TpContact */
175 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
177 FolksPresenceDetails *presence;
178 FolksPresenceType presence_type_cur;
180 presence = FOLKS_PRESENCE_DETAILS (persona);
181 presence_type_cur = folks_presence_details_get_presence_type (
184 if (tp_contact == NULL || folks_presence_details_typecmp (
185 presence_type_cur, presence_type) > 0)
187 presence_type = presence_type_cur;
188 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
192 g_clear_object (&persona);
194 g_clear_object (&iter);
197 if (tp_contact != NULL)
199 priv->contact = tp_contact;
200 g_object_add_weak_pointer (G_OBJECT (tp_contact),
201 (gpointer *) &priv->contact);
203 g_signal_connect (priv->contact, "notify::client-types",
204 (GCallback) client_types_notify_cb, self);
209 add_row (GtkGrid *grid,
214 gtk_grid_attach (grid, title, 0, row, 1, 1);
215 gtk_misc_set_alignment (GTK_MISC (title), 0, 0.5);
216 gtk_widget_show (title);
218 gtk_grid_attach (grid, value, 1, row, 1, 1);
219 gtk_misc_set_alignment (GTK_MISC (value), 0, 0.5);
220 gtk_widget_show (value);
224 channel_name_activated_cb (
229 empathy_join_muc (account, uri, empathy_get_current_action_time ());
234 create_channel_list_label (TpAccount *account,
238 GtkWidget *label = NULL;
239 GString *label_markup = g_string_new ("");
244 /* Is there channels? */
245 channels = g_ptr_array_new ();
247 for (l = info; l != NULL; l = l->next)
249 TpContactInfoField *field = l->data;
251 if (!tp_strdiff (field->field_name, "x-irc-channel"))
252 g_ptr_array_add (channels, (gpointer) field->field_value[0]);
255 if (channels->len == 0)
258 for (i = 0; i < channels->len; i++)
260 const gchar *channel_name = g_ptr_array_index (channels, i);
261 /* We abuse the URI of the link to hold the channel name. It seems to
262 * be okay to just use it essentially verbatim, rather than trying to
263 * ensure it's actually a valid URI. */
264 gchar *escaped = g_markup_escape_text (channel_name, -1);
267 g_string_append (label_markup, ", ");
269 g_string_append_printf (label_markup, "<a href='%s'>%s</a>",
274 label = gtk_label_new (NULL);
275 gtk_label_set_markup (GTK_LABEL (label), label_markup->str);
276 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
278 g_signal_connect (label, "activate-link",
279 (GCallback) channel_name_activated_cb, account);
282 g_ptr_array_unref (channels);
283 g_string_free (label_markup, TRUE);
288 details_update_show (EmpathyIndividualWidget *self,
291 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
294 GtkWidget *channels_label;
298 info = tp_contact_dup_contact_info (contact);
299 info = g_list_sort (info, (GCompareFunc) tpaw_contact_info_field_cmp);
300 for (l = info; l != NULL; l = l->next)
302 TpContactInfoField *field = l->data;
305 TpawContactInfoFormatFunc format;
306 GtkWidget *title_widget, *value_widget;
308 if (field->field_value == NULL || field->field_value[0] == NULL)
311 value = field->field_value[0];
313 if (!tpaw_contact_info_lookup_field (field->field_name,
316 DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
320 /* Skip empty field */
321 if (tp_str_empty (value))
325 title = tpaw_contact_info_field_label (field->field_name,
326 field->parameters, TRUE);
327 title_widget = gtk_label_new (title);
330 value_widget = gtk_label_new (value);
336 markup = format (field->field_value);
337 gtk_label_set_markup (GTK_LABEL (value_widget), markup);
341 gtk_label_set_selectable (GTK_LABEL (value_widget),
342 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
344 add_row (GTK_GRID (priv->grid_details), n_rows, title_widget,
350 conn = tp_contact_get_connection (contact);
351 account = tp_connection_get_account (conn);
353 channels_label = create_channel_list_label (account, info, n_rows);
355 if (channels_label != NULL)
357 GtkWidget *title_widget;
359 title_widget = gtk_label_new (_("Channels:"));
361 add_row (GTK_GRID (priv->grid_details), n_rows, title_widget,
367 tp_contact_info_list_free (info);
373 details_notify_cb (TpContact *contact,
375 EmpathyIndividualWidget *self)
377 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
380 gtk_container_foreach (GTK_CONTAINER (priv->grid_details),
381 (GtkCallback) gtk_widget_destroy, NULL);
383 n_rows = details_update_show (self, contact);
387 gtk_widget_show (priv->vbox_details);
388 gtk_widget_show (priv->grid_details);
392 gtk_widget_hide (priv->vbox_details);
395 gtk_widget_hide (priv->hbox_details_requested);
396 gtk_spinner_stop (GTK_SPINNER (priv->details_spinner));
400 details_request_cb (GObject *source,
404 EmpathyIndividualWidget *self = user_data;
405 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
406 TpContact *contact = (TpContact *) source;
407 gboolean hide_widget = FALSE;
408 GError *error = NULL;
410 if (tp_contact_request_contact_info_finish (contact, res, &error) == TRUE)
412 details_notify_cb (contact, NULL, self);
416 /* If the request got cancelled it could mean the contact widget is
417 * destroyed, so we should not dereference information */
418 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
420 g_error_free (error);
425 g_error_free (error);
428 if (hide_widget == TRUE)
429 gtk_widget_hide (GET_PRIV (self)->vbox_details);
431 tp_clear_object (&priv->details_cancellable);
433 tp_g_signal_connect_object (contact, "notify::contact-info",
434 (GCallback) details_notify_cb, self, 0);
438 fetch_contact_information (EmpathyIndividualWidget *self)
440 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
441 TpConnection *connection;
443 connection = tp_contact_get_connection (priv->contact);
445 if (!tp_proxy_has_interface_by_id (connection,
446 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_INFO))
448 gtk_widget_hide (GET_PRIV (self)->vbox_details);
452 /* Request the Individual's info */
453 gtk_widget_show (priv->vbox_details);
454 gtk_widget_show (priv->hbox_details_requested);
455 gtk_widget_hide (priv->grid_details);
456 gtk_spinner_start (GTK_SPINNER (priv->details_spinner));
458 if (priv->details_cancellable == NULL)
460 priv->details_cancellable = g_cancellable_new ();
462 tp_contact_request_contact_info_async (priv->contact,
463 priv->details_cancellable, details_request_cb, self);
468 details_update (EmpathyIndividualWidget *self)
470 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
472 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS))
475 gtk_widget_hide (priv->vbox_details);
477 if (priv->contact == NULL)
478 update_weak_contact (self);
480 if (priv->contact != NULL)
482 fetch_contact_information (self);
487 groups_update (EmpathyIndividualWidget *self)
489 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
491 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS &&
492 priv->individual != NULL)
494 empathy_groups_widget_set_group_details (
495 EMPATHY_GROUPS_WIDGET (priv->groups_widget),
496 FOLKS_GROUP_DETAILS (priv->individual));
497 gtk_widget_show (priv->groups_widget);
501 gtk_widget_hide (priv->groups_widget);
505 /* Converts the Location's GHashTable's key to a user readable string */
507 location_key_to_label (const gchar *key)
509 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
510 return _("Country ISO Code:");
511 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
512 return _("Country:");
513 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
515 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
517 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
519 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
520 return _("Postal Code:");
521 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
523 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
524 return _("Building:");
525 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
527 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
529 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
531 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
532 return _("Description:");
533 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
535 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
536 return _("Accuracy Level:");
537 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
539 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
540 return _("Vertical Error (meters):");
541 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
542 return _("Horizontal Error (meters):");
543 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
545 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
546 return _("Bearing:");
547 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
548 return _("Climb Speed:");
549 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
550 return _("Last Updated on:");
551 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
552 return _("Longitude:");
553 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
554 return _("Latitude:");
555 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
556 return _("Altitude:");
559 DEBUG ("Unexpected Location key: %s", key);
565 location_update (EmpathyIndividualWidget *self)
567 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
568 EmpathyContact *contact = NULL;
569 GHashTable *location = NULL;
573 static const gchar* ordered_geolocation_keys[] = {
574 EMPATHY_LOCATION_TEXT,
575 EMPATHY_LOCATION_URI,
576 EMPATHY_LOCATION_DESCRIPTION,
577 EMPATHY_LOCATION_BUILDING,
578 EMPATHY_LOCATION_FLOOR,
579 EMPATHY_LOCATION_ROOM,
580 EMPATHY_LOCATION_STREET,
581 EMPATHY_LOCATION_AREA,
582 EMPATHY_LOCATION_LOCALITY,
583 EMPATHY_LOCATION_REGION,
584 EMPATHY_LOCATION_COUNTRY,
589 gboolean display_map = FALSE;
593 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION) ||
594 priv->individual == NULL)
596 gtk_widget_hide (priv->vbox_location);
600 /* FIXME: For the moment, we just display the first location data we can
601 * find amongst the Individual's Personas. Once libfolks grows a location
602 * interface, we can use that. (bgo#627400) */
604 personas = folks_individual_get_personas (priv->individual);
605 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
606 while (location == NULL && gee_iterator_next (iter))
608 FolksPersona *persona = gee_iterator_get (iter);
610 if (empathy_folks_persona_is_interesting (persona))
612 TpContact *tp_contact;
614 /* Get the contact. If it turns out to have location information, we
615 * have to keep it alive for the duration of the function, since we're
616 * accessing its private data. */
617 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
618 if (tp_contact != NULL)
620 contact = empathy_contact_dup_from_tp_contact (tp_contact);
621 empathy_contact_set_persona (contact, persona);
623 /* Try and get a location */
624 location = empathy_contact_get_location (contact);
625 /* if location isn't fully valid, treat the contact as
627 if (location != NULL && g_hash_table_size (location) <= 0)
630 g_clear_object (&contact);
634 g_clear_object (&persona);
636 g_clear_object (&iter);
638 if (contact == NULL || location == NULL)
640 gtk_widget_hide (priv->vbox_location);
641 tp_clear_object (&contact);
645 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
648 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
649 gtk_label_set_markup (GTK_LABEL (priv->label_location), loc);
659 stamp = g_value_get_int64 (value);
661 user_date = tpaw_time_to_string_relative (stamp);
663 tmp = g_strdup_printf ("<b>%s</b>", _("Location"));
664 /* translators: format is "Location, $date" */
665 text = g_strdup_printf (_("%s, %s"), tmp, user_date);
667 gtk_label_set_markup (GTK_LABEL (priv->label_location), text);
672 /* Prepare the location information table */
673 if (priv->grid_location != NULL)
674 gtk_widget_destroy (priv->grid_location);
676 priv->grid_location = gtk_grid_new ();
677 gtk_box_pack_start (GTK_BOX (priv->subvbox_location),
678 priv->grid_location, FALSE, FALSE, 5);
681 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
683 const gchar* user_label;
687 gvalue = g_hash_table_lookup (location, (gpointer) skey);
691 user_label = location_key_to_label (skey);
693 label = gtk_label_new (user_label);
694 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
695 gtk_grid_attach (GTK_GRID (priv->grid_location),
696 label, 0, row, 1, 1);
697 gtk_widget_show (label);
699 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
702 dvalue = g_value_get_double (gvalue);
703 svalue = g_strdup_printf ("%f", dvalue);
705 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
707 svalue = g_value_dup_string (gvalue);
709 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
713 time_ = g_value_get_int64 (value);
714 svalue = tpaw_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
719 label = gtk_label_new (svalue);
720 gtk_grid_attach (GTK_GRID (priv->grid_location),
721 label, 1, row, 1, 1);
722 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
723 gtk_widget_show (label);
725 gtk_label_set_selectable (GTK_LABEL (label),
726 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE :
734 tp_clear_object (&contact);
736 #ifdef HAVE_LIBCHAMPLAIN
737 if ((g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL) &&
738 (g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL))
740 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
748 /* We can display some fields */
749 gtk_widget_show (priv->grid_location);
751 else if (display_map == FALSE)
753 /* Can't display either fields or map */
754 gtk_widget_hide (priv->vbox_location);
758 #ifdef HAVE_LIBCHAMPLAIN
759 if (display_map == TRUE)
761 ChamplainMarkerLayer *layer;
763 priv->map_view_embed = gtk_champlain_embed_new ();
764 priv->map_view = gtk_champlain_embed_get_view (
765 GTK_CHAMPLAIN_EMBED (priv->map_view_embed));
767 gtk_container_add (GTK_CONTAINER (priv->viewport_map),
768 priv->map_view_embed);
769 g_object_set (G_OBJECT (priv->map_view),
770 "kinetic-mode", TRUE,
774 layer = champlain_marker_layer_new ();
775 champlain_view_add_layer (priv->map_view, CHAMPLAIN_LAYER (layer));
777 /* FIXME: For now, we have to do this manually. Once libfolks grows a
778 * location interface, we can use that. (bgo#627400) */
780 personas = folks_individual_get_personas (priv->individual);
781 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
782 while (gee_iterator_next (iter))
784 FolksPersona *persona = gee_iterator_get (iter);
786 if (empathy_folks_persona_is_interesting (persona))
788 gdouble lat = 0.0, lon = 0.0;
789 ClutterActor *marker;
790 TpContact *tp_contact;
792 /* Get the contact */
793 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
794 if (tp_contact == NULL)
797 contact = empathy_contact_dup_from_tp_contact (tp_contact);
798 empathy_contact_set_persona (contact, persona);
800 /* Try and get a location */
801 location = empathy_contact_get_location (contact);
802 if (location == NULL || g_hash_table_size (location) == 0)
805 /* Get this persona's latitude and longitude */
806 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
810 lat = g_value_get_double (value);
812 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
816 lon = g_value_get_double (value);
818 /* Add a marker to the map */
819 marker = champlain_label_new_with_text (
820 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (persona)),
822 champlain_location_set_location (CHAMPLAIN_LOCATION (marker),
824 champlain_marker_layer_add_marker (layer,
825 CHAMPLAIN_MARKER (marker));
829 g_clear_object (&persona);
830 g_clear_object (&contact);
832 g_clear_object (&iter);
834 /* Zoom to show all of the markers */
835 champlain_view_ensure_layers_visible (priv->map_view, FALSE);
837 gtk_widget_show_all (priv->viewport_map);
841 gtk_widget_show (priv->vbox_location);
845 client_types_update (EmpathyIndividualWidget *self)
847 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
848 const gchar * const *types;
850 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_CLIENT_TYPES) ||
851 priv->individual == NULL)
853 gtk_widget_hide (priv->hbox_client_types);
857 if (priv->contact == NULL)
858 update_weak_contact (self);
860 /* let's try that again... */
861 if (priv->contact == NULL)
864 types = tp_contact_get_client_types (priv->contact);
866 if (empathy_client_types_contains_mobile_device ((GStrv) types))
868 gtk_widget_show (priv->hbox_client_types);
872 gtk_widget_hide (priv->hbox_client_types);
878 remove_weak_contact (EmpathyIndividualWidget *self)
880 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
882 if (priv->contact == NULL)
885 g_signal_handlers_disconnect_by_func (priv->contact, client_types_notify_cb,
888 g_object_remove_weak_pointer (G_OBJECT (priv->contact),
889 (gpointer *) &priv->contact);
890 priv->contact = NULL;
893 static EmpathyAvatar *
894 persona_dup_avatar (FolksPersona *persona)
896 TpContact *tp_contact;
897 EmpathyContact *contact;
898 EmpathyAvatar *avatar;
900 if (!empathy_folks_persona_is_interesting (persona))
903 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
904 if (tp_contact == NULL)
907 contact = empathy_contact_dup_from_tp_contact (tp_contact);
908 empathy_contact_set_persona (contact, persona);
910 avatar = empathy_contact_get_avatar (contact);
912 empathy_avatar_ref (avatar);
913 g_object_unref (contact);
918 static EmpathyAvatar *
919 individual_dup_avatar (FolksIndividual *individual)
923 EmpathyAvatar *avatar = NULL;
925 /* FIXME: We just choose the first Persona which has an avatar, and save that.
926 * The avatar handling in EmpathyContact needs to be moved into libfolks as
927 * much as possible, and this code rewritten to use FolksHasAvatar.
930 personas = folks_individual_get_personas (individual);
931 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
932 while (avatar == NULL && gee_iterator_next (iter))
934 FolksPersona *persona = gee_iterator_get (iter);
935 avatar = persona_dup_avatar (persona);
937 g_clear_object (&persona);
939 g_clear_object (&iter);
945 save_avatar_menu_activate_cb (GtkWidget *widget,
946 EmpathyIndividualWidget *self)
948 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
950 EmpathyAvatar *avatar;
951 gchar *ext = NULL, *filename;
953 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
955 GTK_FILE_CHOOSER_ACTION_SAVE,
956 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
957 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
960 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
963 avatar = individual_dup_avatar (priv->individual);
967 /* look for the avatar extension */
968 if (avatar->format != NULL)
972 splitted = g_strsplit (avatar->format, "/", 2);
973 if (splitted[0] != NULL && splitted[1] != NULL)
974 ext = g_strdup (splitted[1]);
976 g_strfreev (splitted);
980 /* Avatar was loaded from the cache so was converted to PNG */
981 ext = g_strdup ("png");
988 id = tp_escape_as_identifier (folks_individual_get_id (priv->individual));
990 filename = g_strdup_printf ("%s.%s", id, ext);
991 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
998 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
1000 GError *error = NULL;
1002 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1004 if (empathy_avatar_save_to_file (avatar, filename, &error) == FALSE)
1007 GtkWidget *error_dialog;
1009 error_dialog = gtk_message_dialog_new (NULL, 0,
1010 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1011 _("Unable to save avatar"));
1013 gtk_message_dialog_format_secondary_text (
1014 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
1016 g_signal_connect (error_dialog, "response",
1017 (GCallback) gtk_widget_destroy, NULL);
1019 gtk_window_present (GTK_WINDOW (error_dialog));
1021 g_clear_error (&error);
1027 gtk_widget_destroy (dialog);
1028 empathy_avatar_unref (avatar);
1032 popup_avatar_menu (EmpathyIndividualWidget *self,
1034 GdkEventButton *event)
1036 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1037 GtkWidget *menu, *item;
1038 EmpathyAvatar *avatar;
1039 gint button, event_time;
1041 if (priv->individual == NULL)
1044 avatar = individual_dup_avatar (priv->individual);
1047 empathy_avatar_unref (avatar);
1049 menu = empathy_context_menu_new (parent);
1051 /* Add "Save as..." entry */
1052 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
1053 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1054 gtk_widget_show (item);
1056 g_signal_connect (item, "activate",
1057 (GCallback) save_avatar_menu_activate_cb, self);
1061 button = event->button;
1062 event_time = event->time;
1067 event_time = gtk_get_current_event_time ();
1070 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, button, event_time);
1076 avatar_widget_popup_menu_cb (GtkWidget *widget,
1077 EmpathyIndividualWidget *self)
1079 return popup_avatar_menu (self, widget, NULL);
1083 avatar_widget_button_press_event_cb (GtkWidget *widget,
1084 GdkEventButton *event,
1085 EmpathyIndividualWidget *self)
1087 /* Ignore double-clicks and triple-clicks */
1088 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1089 return popup_avatar_menu (self, widget, event);
1094 /* Returns the TpAccount for the user as a convenience. Note that it has a ref
1097 individual_is_user (FolksIndividual *individual)
1101 TpAccount *retval = NULL;
1103 /* FIXME: This should move into libfolks when libfolks grows a way of
1104 * determining "self". (bgo#627402) */
1105 personas = folks_individual_get_personas (individual);
1106 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1107 while (gee_iterator_next (iter))
1109 FolksPersona *persona = gee_iterator_get (iter);
1111 if (TPF_IS_PERSONA (persona))
1113 TpContact *tp_contact;
1114 EmpathyContact *contact = NULL;
1116 /* Get the contact */
1117 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1118 if (tp_contact != NULL)
1120 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1121 empathy_contact_set_persona (contact, persona);
1123 /* Determine if the contact is the user */
1124 if (empathy_contact_is_user (contact))
1125 retval = g_object_ref (empathy_contact_get_account (contact));
1128 g_object_unref (contact);
1130 g_clear_object (&persona);
1132 g_clear_object (&iter);
1138 set_nickname_cb (TpAccount *account,
1139 GAsyncResult *result,
1142 GError *error = NULL;
1144 if (tp_account_set_nickname_finish (account, result, &error) == FALSE)
1146 DEBUG ("Failed to set Account.Nickname: %s", error->message);
1147 g_error_free (error);
1152 entry_alias_focus_event_cb (GtkEditable *editable,
1153 GdkEventFocus *event,
1154 EmpathyIndividualWidget *self)
1156 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1158 if (priv->individual != NULL)
1163 alias = gtk_entry_get_text (GTK_ENTRY (editable));
1164 account = individual_is_user (priv->individual);
1166 if (account != NULL)
1168 DEBUG ("Set Account.Nickname to %s", alias);
1169 tp_account_set_nickname_async (account, alias,
1170 (GAsyncReadyCallback) set_nickname_cb, NULL);
1171 g_object_unref (account);
1175 folks_alias_details_set_alias (FOLKS_ALIAS_DETAILS (priv->individual),
1184 favourite_toggled_cb (GtkToggleButton *button,
1185 EmpathyIndividualWidget *self)
1187 gboolean active = gtk_toggle_button_get_active (button);
1188 folks_favourite_details_set_is_favourite (
1189 FOLKS_FAVOURITE_DETAILS (GET_PRIV (self)->individual), active);
1193 notify_avatar_cb (gpointer folks_object,
1195 EmpathyIndividualWidget *self)
1197 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1198 EmpathyAvatar *avatar = NULL;
1200 GtkWidget *avatar_widget;
1202 if (FOLKS_IS_INDIVIDUAL (folks_object))
1204 avatar = individual_dup_avatar (FOLKS_INDIVIDUAL (folks_object));
1205 grid = G_OBJECT (priv->individual_grid);
1207 else if (FOLKS_IS_PERSONA (folks_object))
1209 avatar = persona_dup_avatar (FOLKS_PERSONA (folks_object));
1210 grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1214 g_assert_not_reached ();
1220 avatar_widget = g_object_get_data (grid, "avatar-widget");
1221 empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (avatar_widget), avatar);
1224 empathy_avatar_unref (avatar);
1228 notify_alias_cb (gpointer folks_object,
1230 EmpathyIndividualWidget *self)
1232 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1234 GtkWidget *alias_widget;
1236 if (FOLKS_IS_INDIVIDUAL (folks_object))
1237 grid = G_OBJECT (priv->individual_grid);
1238 else if (FOLKS_IS_PERSONA (folks_object))
1239 grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1241 g_assert_not_reached ();
1246 alias_widget = g_object_get_data (grid, "alias-widget");
1248 if (GTK_IS_ENTRY (alias_widget))
1250 gtk_entry_set_text (GTK_ENTRY (alias_widget),
1251 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (folks_object)));
1255 gtk_label_set_label (GTK_LABEL (alias_widget),
1256 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (folks_object)));
1261 notify_presence_cb (gpointer folks_object,
1263 EmpathyIndividualWidget *self)
1265 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1267 GtkWidget *status_label, *state_image;
1268 const gchar *message;
1269 gchar *markup_text = NULL;
1270 FolksPresenceType presence;
1271 gboolean visible = TRUE;
1273 if (FOLKS_IS_INDIVIDUAL (folks_object))
1274 grid = G_OBJECT (priv->individual_grid);
1275 else if (FOLKS_IS_PERSONA (folks_object))
1276 grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1278 g_assert_not_reached ();
1283 status_label = g_object_get_data (grid, "status-label");
1284 state_image = g_object_get_data (grid, "state-image");
1286 presence = folks_presence_details_get_presence_type (
1287 FOLKS_PRESENCE_DETAILS (folks_object));
1288 if (presence == FOLKS_PRESENCE_TYPE_UNKNOWN ||
1289 presence == FOLKS_PRESENCE_TYPE_ERROR)
1291 /* Don't display anything if we don't know the presence */
1296 /* FIXME: Default messages should be moved into libfolks (bgo#627403) */
1297 message = folks_presence_details_get_presence_message (
1298 FOLKS_PRESENCE_DETAILS (folks_object));
1299 if (EMP_STR_EMPTY (message))
1301 message = empathy_presence_get_default_message (presence);
1304 if (message != NULL)
1305 markup_text = tpaw_add_link_markup (message);
1306 gtk_label_set_markup (GTK_LABEL (status_label), markup_text);
1307 g_free (markup_text);
1309 gtk_image_set_from_icon_name (GTK_IMAGE (state_image),
1310 empathy_icon_name_for_presence (presence),
1311 GTK_ICON_SIZE_BUTTON);
1314 gtk_widget_set_visible (status_label, visible);
1315 gtk_widget_set_visible (state_image, visible);
1319 notify_is_favourite_cb (gpointer folks_object,
1321 EmpathyIndividualWidget *self)
1323 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1325 GtkWidget *favourite_widget;
1327 if (FOLKS_IS_INDIVIDUAL (folks_object))
1328 grid = G_OBJECT (priv->individual_grid);
1329 else if (FOLKS_IS_PERSONA (folks_object))
1330 grid = g_hash_table_lookup (priv->persona_grids, folks_object);
1332 g_assert_not_reached ();
1337 favourite_widget = g_object_get_data (grid, "favourite-widget");
1339 if (GTK_IS_TOGGLE_BUTTON (favourite_widget))
1341 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (favourite_widget),
1342 folks_favourite_details_get_is_favourite (
1343 FOLKS_FAVOURITE_DETAILS (folks_object)));
1348 alias_presence_avatar_favourite_set_up (EmpathyIndividualWidget *self,
1352 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1353 GtkWidget *label, *alias, *image, *avatar;
1354 guint current_row = starting_row;
1357 label = gtk_label_new (_("Alias:"));
1358 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1359 gtk_grid_attach (grid, label, 0, current_row, 1, 1);
1360 gtk_widget_show (label);
1362 /* Set up alias label/entry */
1363 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS)
1365 alias = gtk_entry_new ();
1367 g_signal_connect (alias, "focus-out-event",
1368 (GCallback) entry_alias_focus_event_cb, self);
1370 /* Make return activate the window default (the Close button) */
1371 gtk_entry_set_activates_default (GTK_ENTRY (alias), TRUE);
1375 alias = gtk_label_new (NULL);
1376 gtk_label_set_selectable (GTK_LABEL (alias),
1377 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1378 gtk_misc_set_alignment (GTK_MISC (alias), 0.0, 0.5);
1381 g_object_set_data (G_OBJECT (grid), "alias-widget", alias);
1382 gtk_grid_attach_next_to (grid, alias, label,
1383 GTK_POS_RIGHT, 1, 1);
1384 gtk_widget_show (alias);
1389 priv->hbox_presence = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
1391 /* Presence image */
1392 image = gtk_image_new_from_stock (GTK_STOCK_MISSING_IMAGE,
1393 GTK_ICON_SIZE_BUTTON);
1394 g_object_set_data (G_OBJECT (grid), "state-image", image);
1395 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), image, FALSE,
1397 gtk_widget_show (image);
1399 label = gtk_label_new ("");
1400 gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
1401 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1402 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1404 gtk_label_set_selectable (GTK_LABEL (label),
1405 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1407 g_object_set_data (G_OBJECT (grid), "status-label", label);
1408 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, FALSE,
1410 gtk_widget_show (label);
1412 gtk_grid_attach (grid, priv->hbox_presence,
1413 0, current_row, 2, 1);
1414 gtk_widget_show (priv->hbox_presence);
1418 /* Set up favourite toggle button */
1419 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1421 GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1423 g_signal_connect (favourite, "toggled",
1424 (GCallback) favourite_toggled_cb, self);
1426 g_object_set_data (G_OBJECT (grid), "favourite-widget", favourite);
1427 gtk_grid_attach (grid, favourite,
1428 0, current_row, 2, 1);
1429 gtk_widget_show (favourite);
1434 /* Set up avatar display */
1435 avatar = empathy_avatar_image_new ();
1437 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1439 g_signal_connect (avatar, "popup-menu",
1440 (GCallback) avatar_widget_popup_menu_cb, self);
1441 g_signal_connect (avatar, "button-press-event",
1442 (GCallback) avatar_widget_button_press_event_cb, self);
1445 g_object_set_data (G_OBJECT (grid), "avatar-widget", avatar);
1446 g_object_set (avatar,
1447 "valign", GTK_ALIGN_START,
1454 gtk_grid_attach (grid, avatar,
1455 2, 0, 1, current_row);
1456 gtk_widget_show (avatar);
1460 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1462 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1463 TpContact *tp_contact;
1464 EmpathyContact *contact;
1471 grid = g_hash_table_lookup (priv->persona_grids, persona);
1473 g_assert (grid != NULL);
1475 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1476 if (tp_contact == NULL)
1479 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1480 empathy_contact_set_persona (contact, persona);
1482 account = empathy_contact_get_account (contact);
1484 /* Update account widget */
1485 if (account != NULL)
1489 label = g_object_get_data (G_OBJECT (grid), "account-label");
1490 image = g_object_get_data (G_OBJECT (grid), "account-image");
1492 name = tp_account_get_display_name (account);
1493 gtk_label_set_label (label, name);
1495 name = tp_account_get_icon_name (account);
1496 gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
1499 /* Update id widget */
1500 label = g_object_get_data (G_OBJECT (grid), "id-widget");
1501 id = folks_persona_get_display_id (persona);
1502 gtk_label_set_label (label, (id != NULL) ? id : "");
1504 /* Update other widgets */
1505 notify_alias_cb (persona, NULL, self);
1506 notify_presence_cb (persona, NULL, self);
1507 notify_avatar_cb (persona, NULL, self);
1509 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1510 notify_is_favourite_cb (persona, NULL, self);
1512 g_object_unref (contact);
1516 add_persona (EmpathyIndividualWidget *self,
1517 FolksPersona *persona)
1519 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1522 GtkWidget *label, *account_label, *account_image, *separator;
1523 guint current_row = 0;
1525 if (!empathy_folks_persona_is_interesting (persona))
1528 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1529 grid = GTK_GRID (gtk_grid_new ());
1531 grid = GTK_GRID (gtk_grid_new ());
1533 gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
1534 gtk_grid_set_row_spacing (grid, 6);
1535 gtk_grid_set_column_spacing (grid, 6);
1537 /* Account and Identifier */
1538 label = gtk_label_new (_("Account:"));
1539 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1540 gtk_grid_attach (grid, label, 0, current_row, 1, 1);
1541 gtk_widget_show (label);
1543 /* Pack the protocol icon with the account name in an hbox */
1544 hbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6));
1546 account_label = gtk_label_new (NULL);
1547 gtk_label_set_selectable (GTK_LABEL (account_label),
1548 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1549 gtk_misc_set_alignment (GTK_MISC (account_label), 0.0, 0.5);
1550 gtk_widget_show (account_label);
1552 account_image = gtk_image_new ();
1553 gtk_widget_show (account_image);
1555 gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1556 gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
1558 g_object_set_data (G_OBJECT (grid), "account-image", account_image);
1559 g_object_set_data (G_OBJECT (grid), "account-label", account_label);
1560 gtk_grid_attach_next_to (grid, GTK_WIDGET (hbox), label, GTK_POS_RIGHT, 1, 1);
1561 gtk_widget_show (GTK_WIDGET (hbox));
1565 /* Translators: Identifier to connect to Instant Messaging network */
1566 label = gtk_label_new (_("Identifier:"));
1567 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1568 gtk_grid_attach (grid, label, 0, current_row, 1, 1);
1569 gtk_widget_show (label);
1571 /* Set up ID label */
1572 label = gtk_label_new (NULL);
1573 gtk_label_set_selectable (GTK_LABEL (label),
1574 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1575 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1577 g_object_set_data (G_OBJECT (grid), "id-widget", label);
1578 gtk_grid_attach (grid, label, 1, current_row, 1, 1);
1579 gtk_widget_show (label);
1583 alias_presence_avatar_favourite_set_up (self, grid, current_row);
1585 /* Connect to signals and display the grid */
1586 g_signal_connect (persona, "notify::alias",
1587 (GCallback) notify_alias_cb, self);
1588 g_signal_connect (persona, "notify::avatar",
1589 (GCallback) notify_avatar_cb, self);
1590 g_signal_connect (persona, "notify::presence-type",
1591 (GCallback) notify_presence_cb, self);
1592 g_signal_connect (persona, "notify::presence-message",
1593 (GCallback) notify_presence_cb, self);
1595 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1597 g_signal_connect (persona, "notify::is-favourite",
1598 (GCallback) notify_is_favourite_cb, self);
1601 gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1602 GTK_WIDGET (grid), FALSE, TRUE, 0);
1603 gtk_widget_show (GTK_WIDGET (grid));
1605 /* Pack a separator after the grid */
1606 separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
1607 g_object_set_data (G_OBJECT (grid), "separator", separator);
1608 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), separator, FALSE, FALSE,
1610 gtk_widget_show (separator);
1612 g_hash_table_replace (priv->persona_grids, persona, grid);
1614 /* Update the new widgets */
1615 update_persona (self, persona);
1619 remove_persona (EmpathyIndividualWidget *self,
1620 FolksPersona *persona)
1622 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1623 GtkWidget *separator;
1626 if (!empathy_folks_persona_is_interesting (persona))
1629 grid = g_hash_table_lookup (priv->persona_grids, persona);
1633 g_signal_handlers_disconnect_by_func (persona, notify_alias_cb, self);
1634 g_signal_handlers_disconnect_by_func (persona, notify_avatar_cb, self);
1635 g_signal_handlers_disconnect_by_func (persona, notify_presence_cb, self);
1637 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1639 g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
1643 /* Remove the separator */
1644 separator = g_object_get_data (G_OBJECT (grid), "separator");
1645 if (separator != NULL)
1646 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual), separator);
1648 /* Remove the widget */
1649 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1652 g_hash_table_remove (priv->persona_grids, persona);
1656 update_individual_grid (EmpathyIndividualWidget *self)
1658 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1660 notify_alias_cb (priv->individual, NULL, self);
1661 notify_presence_cb (priv->individual, NULL, self);
1662 notify_avatar_cb (priv->individual, NULL, self);
1664 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1665 notify_is_favourite_cb (priv->individual, NULL, self);
1669 individual_grid_set_up (EmpathyIndividualWidget *self)
1671 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1672 guint current_row = 0;
1675 grid = GTK_GRID (gtk_grid_new ());
1676 gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
1677 gtk_grid_set_row_spacing (grid, 6);
1678 gtk_grid_set_column_spacing (grid, 6);
1680 /* We only display the number of personas in tooltips */
1681 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1687 guint num_personas = 0;
1689 /* Meta-contacts message displaying how many Telepathy personas we have */
1690 personas = folks_individual_get_personas (priv->individual);
1691 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1692 while (gee_iterator_next (iter))
1694 FolksPersona *persona = gee_iterator_get (iter);
1695 if (empathy_folks_persona_is_interesting (persona))
1698 g_clear_object (&persona);
1700 g_clear_object (&iter);
1702 /* Translators: the plurality applies to both instances of the word
1704 message = g_strdup_printf (
1705 ngettext ("Linked contact containing %u contact",
1706 "Linked contacts containing %u contacts", num_personas),
1708 label = gtk_label_new (message);
1709 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1712 gtk_grid_attach (grid, label, 0, current_row, 2, 1);
1713 gtk_widget_show (label);
1718 alias_presence_avatar_favourite_set_up (self, grid, current_row);
1720 /* Display the grid */
1721 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (grid),
1723 gtk_widget_show (GTK_WIDGET (grid));
1725 priv->individual_grid = grid;
1727 /* Update the grid */
1728 update_individual_grid (self);
1732 individual_grid_destroy (EmpathyIndividualWidget *self)
1734 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1736 if (priv->individual_grid == NULL)
1739 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1740 GTK_WIDGET (priv->individual_grid));
1742 #ifdef HAVE_LIBCHAMPLAIN
1743 if (priv->map_view_embed != NULL)
1745 gtk_container_remove (GTK_CONTAINER (priv->viewport_map),
1746 priv->map_view_embed);
1748 priv->map_view_embed = NULL;
1752 priv->individual_grid = NULL;
1756 personas_changed_cb (FolksIndividual *individual,
1759 EmpathyIndividualWidget *self)
1761 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1762 GList *l, *children;
1765 gboolean show_personas, was_showing_personas, will_show_personas, is_last;
1766 guint old_num_personas, new_num_personas = 0;
1768 personas = folks_individual_get_personas (individual);
1769 /* we'll re-use this iterator throughout */
1770 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1772 /* Note that old_num_personas is the number of persona gridss we were
1773 * displaying, not the number of Personas which were in the Individual
1775 old_num_personas = g_hash_table_size (priv->persona_grids);
1777 while (gee_iterator_next (iter))
1779 FolksPersona *persona = gee_iterator_get (iter);
1780 if (empathy_folks_persona_is_interesting (persona))
1783 g_clear_object (&persona);
1787 * What we display for various conditions:
1788 * - "Personas": display the alias, avatar, presence account and identifier
1789 * for each of the Individual's Personas. (i.e. One grid per
1791 * - "Individual": display the alias, avatar and presence for the Individual,
1792 * and a label saying "Meta-contact containing x contacts".
1793 * (i.e. One grid in total.)
1795 * | SHOW_PERSONAS | !SHOW_PERSONAS
1796 * -------------+---------------+---------------
1797 * > 1 Persona | Personas | Individual
1798 * -------------+---------------+---------------
1799 * == 1 Persona | Personas | Personas
1801 show_personas = (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) != 0;
1802 was_showing_personas = show_personas || old_num_personas == 1;
1803 will_show_personas = show_personas || new_num_personas == 1;
1805 /* If both @added and @removed are NULL, we're being called manually, and we
1806 * need to set up the gridss for the first time. We do this simply by
1807 * ensuring was_showing_personas and will_show_personas are different so that
1808 * the code resets the UI.
1810 if (added == NULL && removed == NULL)
1811 was_showing_personas = !will_show_personas;
1813 if (was_showing_personas && will_show_personas)
1815 GeeIterator *iter_changed;
1817 /* Remove outdated Personas */
1818 iter_changed = gee_iterable_iterator (GEE_ITERABLE (removed));
1819 while (gee_iterator_next (iter_changed))
1821 FolksPersona *persona = gee_iterator_get (iter_changed);
1822 remove_persona (self, persona);
1823 g_clear_object (&persona);
1825 g_clear_object (&iter_changed);
1827 /* Add new Personas */
1828 iter_changed = gee_iterable_iterator (GEE_ITERABLE (added));
1829 while (gee_iterator_next (iter_changed))
1831 FolksPersona *persona = gee_iterator_get (iter_changed);
1832 add_persona (self, persona);
1833 g_clear_object (&persona);
1835 g_clear_object (&iter_changed);
1837 else if (!was_showing_personas && will_show_personas)
1839 /* Remove the old Individual grid */
1840 individual_grid_destroy (self);
1842 /* Set up all the Persona grids instead */
1843 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1844 while (gee_iterator_next (iter))
1846 FolksPersona *persona = gee_iterator_get (iter);
1847 add_persona (self, persona);
1848 g_clear_object (&persona);
1851 else if (was_showing_personas && !will_show_personas)
1853 /* Remove all Personas */
1854 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1855 while (gee_iterator_next (iter))
1857 FolksPersona *persona = gee_iterator_get (iter);
1858 remove_persona (self, persona);
1859 g_clear_object (&persona);
1862 if (removed != NULL)
1864 GeeIterator *iter_changed;
1866 iter_changed = gee_iterable_iterator (GEE_ITERABLE (removed));
1867 while (gee_iterator_next (iter_changed))
1869 FolksPersona *persona = gee_iterator_get (iter_changed);
1870 remove_persona (self, persona);
1871 g_clear_object (&persona);
1873 g_clear_object (&iter_changed);
1876 /* Set up the Individual grid instead */
1877 individual_grid_set_up (self);
1879 g_clear_object (&iter);
1881 /* Hide the last separator and show the others */
1882 children = gtk_container_get_children (GTK_CONTAINER (priv->vbox_individual));
1883 children = g_list_reverse (children);
1886 for (l = children; l != NULL; l = l->next)
1888 if (GTK_IS_SEPARATOR (l->data))
1890 gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1895 g_list_free (children);
1899 individual_removed_cb (FolksIndividual *individual,
1900 FolksIndividual *replacement_individual,
1901 EmpathyIndividualWidget *self)
1903 empathy_individual_widget_set_individual (self, replacement_individual);
1907 remove_individual (EmpathyIndividualWidget *self)
1909 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1910 if (priv->individual != NULL)
1915 g_signal_handlers_disconnect_by_func (priv->individual,
1916 notify_alias_cb, self);
1917 g_signal_handlers_disconnect_by_func (priv->individual,
1918 notify_presence_cb, self);
1919 g_signal_handlers_disconnect_by_func (priv->individual,
1920 notify_avatar_cb, self);
1921 g_signal_handlers_disconnect_by_func (priv->individual,
1922 personas_changed_cb, self);
1923 g_signal_handlers_disconnect_by_func (priv->individual,
1924 individual_removed_cb, self);
1926 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1928 g_signal_handlers_disconnect_by_func (priv->individual,
1929 notify_is_favourite_cb, self);
1932 personas = folks_individual_get_personas (priv->individual);
1933 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1934 while (gee_iterator_next (iter))
1936 FolksPersona *persona = gee_iterator_get (iter);
1937 remove_persona (self, persona);
1938 g_clear_object (&persona);
1940 g_clear_object (&iter);
1941 individual_grid_destroy (self);
1943 if (priv->contact != NULL)
1944 remove_weak_contact (self);
1946 tp_clear_object (&priv->individual);
1949 if (priv->details_cancellable != NULL)
1950 g_cancellable_cancel (priv->details_cancellable);
1954 individual_update (EmpathyIndividualWidget *self)
1956 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1958 /* Connect and get info from new Individual */
1959 if (priv->individual != NULL)
1961 g_signal_connect (priv->individual, "notify::alias",
1962 (GCallback) notify_alias_cb, self);
1963 g_signal_connect (priv->individual, "notify::presence-type",
1964 (GCallback) notify_presence_cb, self);
1965 g_signal_connect (priv->individual, "notify::presence-message",
1966 (GCallback) notify_presence_cb, self);
1967 g_signal_connect (priv->individual, "notify::avatar",
1968 (GCallback) notify_avatar_cb, self);
1969 g_signal_connect (priv->individual, "personas-changed",
1970 (GCallback) personas_changed_cb, self);
1971 g_signal_connect (priv->individual, "removed",
1972 (GCallback) individual_removed_cb, self);
1974 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1976 g_signal_connect (priv->individual, "notify::is-favourite",
1977 (GCallback) notify_is_favourite_cb, self);
1980 /* Update individual grid */
1981 personas_changed_cb (priv->individual, NULL, NULL, self);
1984 if (priv->individual == NULL)
1986 gtk_widget_hide (priv->vbox_individual);
1988 else if (priv->individual_grid != NULL)
1990 /* We only need to update the details for the Individual as a whole */
1991 update_individual_grid (self);
1992 gtk_widget_show (priv->vbox_individual);
1996 /* We need to update the details for every Persona in the Individual */
2000 personas = folks_individual_get_personas (priv->individual);
2001 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2002 while (gee_iterator_next (iter))
2004 FolksPersona *persona = gee_iterator_get (iter);
2006 if (empathy_folks_persona_is_interesting (persona))
2007 update_persona (self, persona);
2009 g_clear_object (&persona);
2011 g_clear_object (&iter);
2013 gtk_widget_show (priv->vbox_individual);
2018 empathy_individual_widget_init (EmpathyIndividualWidget *self)
2020 EmpathyIndividualWidgetPriv *priv;
2024 priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
2025 EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
2028 gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
2029 GTK_ORIENTATION_VERTICAL);
2031 filename = empathy_file_lookup ("empathy-individual-widget.ui",
2033 gui = tpaw_builder_get_file (filename,
2034 "scrolled_window_individual", &priv->scrolled_window_individual,
2035 "viewport_individual", &priv->viewport_individual,
2036 "vbox_individual_widget", &priv->vbox_individual_widget,
2037 "vbox_individual", &priv->vbox_individual,
2038 "vbox_location", &priv->vbox_location,
2039 "subvbox_location", &priv->subvbox_location,
2040 "label_location", &priv->label_location,
2041 #ifdef HAVE_LIBCHAMPLAIN
2042 "viewport_map", &priv->viewport_map,
2044 "groups_widget", &priv->groups_widget,
2045 "vbox_details", &priv->vbox_details,
2046 "grid_details", &priv->grid_details,
2047 "hbox_details_requested", &priv->hbox_details_requested,
2048 "hbox_client_types", &priv->hbox_client_types,
2052 priv->grid_location = NULL;
2054 gtk_box_pack_start (GTK_BOX (self), priv->vbox_individual_widget, TRUE, TRUE,
2056 gtk_widget_show (priv->vbox_individual_widget);
2058 priv->persona_grids = g_hash_table_new (NULL, NULL);
2059 priv->individual_grid = NULL;
2061 /* Create widgets */
2062 details_set_up (self);
2064 g_object_unref (gui);
2068 constructed (GObject *object)
2070 GObjectClass *klass = G_OBJECT_CLASS (empathy_individual_widget_parent_class);
2071 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2072 GtkScrolledWindow *scrolled_window =
2073 GTK_SCROLLED_WINDOW (priv->scrolled_window_individual);
2075 /* Allow scrolling of the list of Personas if we're showing Personas. */
2076 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS)
2078 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
2079 gtk_scrolled_window_set_policy (scrolled_window,
2080 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
2081 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
2082 priv->scrolled_window_individual, TRUE, TRUE, 0, GTK_PACK_START);
2084 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
2086 gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
2090 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_NONE);
2091 gtk_scrolled_window_set_policy (scrolled_window,
2092 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
2093 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
2094 priv->scrolled_window_individual, FALSE, TRUE, 0, GTK_PACK_START);
2096 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
2101 if (klass->constructed != NULL)
2102 klass->constructed (object);
2106 get_property (GObject *object,
2111 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2115 case PROP_INDIVIDUAL:
2116 g_value_set_object (value, priv->individual);
2119 g_value_set_flags (value, priv->flags);
2122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2128 set_property (GObject *object,
2130 const GValue *value,
2133 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2137 case PROP_INDIVIDUAL:
2138 empathy_individual_widget_set_individual (
2139 EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
2142 priv->flags = g_value_get_flags (value);
2145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
2151 dispose (GObject *object)
2153 remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
2155 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
2159 finalize (GObject *object)
2161 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
2163 g_hash_table_unref (priv->persona_grids);
2165 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
2169 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
2171 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2173 object_class->constructed = constructed;
2174 object_class->get_property = get_property;
2175 object_class->set_property = set_property;
2176 object_class->dispose = dispose;
2177 object_class->finalize = finalize;
2180 * EmpathyIndividualWidget:individual:
2182 * The #FolksIndividual to display in the widget.
2184 g_object_class_install_property (object_class, PROP_INDIVIDUAL,
2185 g_param_spec_object ("individual",
2187 "The #FolksIndividual to display in the widget.",
2188 FOLKS_TYPE_INDIVIDUAL,
2189 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2192 * EmpathyIndividualWidget:flags:
2194 * A set of flags which affect the widget's behaviour.
2196 g_object_class_install_property (object_class, PROP_FLAGS,
2197 g_param_spec_flags ("flags",
2199 "A set of flags which affect the widget's behaviour.",
2200 EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
2201 EMPATHY_INDIVIDUAL_WIDGET_NONE,
2202 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
2204 g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
2208 * empathy_individual_widget_new:
2209 * @individual: the #FolksIndividual to display
2210 * @flags: flags affecting how the widget behaves and what it displays
2212 * Creates a new #EmpathyIndividualWidget.
2214 * Return value: a new #EmpathyIndividualWidget
2217 empathy_individual_widget_new (FolksIndividual *individual,
2218 EmpathyIndividualWidgetFlags flags)
2220 g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
2223 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
2224 "individual", individual,
2230 * empathy_individual_widget_get_individual:
2231 * @self: an #EmpathyIndividualWidget
2233 * Returns the #FolksIndividual being displayed by the widget.
2235 * Return value: the #FolksIndividual being displayed, or %NULL
2238 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
2240 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
2242 return GET_PRIV (self)->individual;
2246 * empathy_individual_widget_set_individual:
2247 * @self: an #EmpathyIndividualWidget
2248 * @individual: the #FolksIndividual to display, or %NULL
2250 * Set the #FolksIndividual to be displayed by the widget:
2251 * #EmpathyIndividualWidget:individual.
2253 * The @individual may be %NULL in order to display nothing in the widget.
2256 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
2257 FolksIndividual *individual)
2259 EmpathyIndividualWidgetPriv *priv;
2261 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
2262 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
2264 priv = GET_PRIV (self);
2266 if (individual == priv->individual)
2269 /* Out with the old… */
2270 remove_individual (self);
2272 /* …and in with the new. */
2273 if (individual != NULL)
2274 g_object_ref (individual);
2275 priv->individual = individual;
2277 /* Update information for widgets */
2278 individual_update (self);
2279 groups_update (self);
2280 details_update (self);
2281 location_update (self);
2282 client_types_update (self);