2 * Copyright (C) 2007-2010 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Philip Withnall <philip.withnall@collabora.co.uk>
28 #include <glib/gi18n-lib.h>
30 #include <telepathy-glib/util.h>
32 #include <folks/folks.h>
33 #include <folks/folks-telepathy.h>
35 #ifdef HAVE_LIBCHAMPLAIN
36 #include <champlain/champlain.h>
37 #include <champlain-gtk/champlain-gtk.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-location.h>
42 #include <libempathy/empathy-time.h>
44 #include "empathy-avatar-image.h"
45 #include "empathy-groups-widget.h"
46 #include "empathy-gtk-enum-types.h"
47 #include "empathy-individual-widget.h"
48 #include "empathy-kludge-label.h"
49 #include "empathy-string-parser.h"
50 #include "empathy-ui-utils.h"
52 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
53 #include <libempathy/empathy-debug.h>
56 * SECTION:empathy-individual-widget
57 * @title:EmpathyIndividualWidget
58 * @short_description: A widget used to display and edit details about an
60 * @include: libempathy-empathy-individual-widget.h
62 * #EmpathyIndividualWidget is a widget which displays appropriate widgets
63 * with details about an individual, also allowing changing these details,
68 * EmpathyIndividualWidget:
69 * @parent: parent object
71 * Widget which displays appropriate widgets with details about an individual,
72 * also allowing changing these details, if desired.
75 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualWidget)
78 FolksIndividual *individual; /* owned */
79 EmpathyIndividualWidgetFlags flags;
81 /* weak pointer to the contact whose contact details we're displaying */
82 TpContact *contact_info_contact;
84 /* unowned Persona (borrowed from priv->individual) -> GtkTable child */
85 GHashTable *persona_tables;
86 /* Table containing the information for the individual as whole, or NULL */
87 GtkTable *individual_table;
90 GtkWidget *hbox_presence;
91 GtkWidget *vbox_individual_widget;
92 GtkWidget *scrolled_window_individual;
93 GtkWidget *viewport_individual;
94 GtkWidget *vbox_individual;
97 GtkWidget *vbox_location;
98 GtkWidget *subvbox_location;
99 GtkWidget *table_location;
100 GtkWidget *label_location;
101 #ifdef HAVE_LIBCHAMPLAIN
102 GtkWidget *viewport_map;
103 GtkWidget *map_view_embed;
104 ChamplainView *map_view;
108 GtkWidget *groups_widget;
111 GtkWidget *vbox_details;
112 GtkWidget *table_details;
113 GtkWidget *hbox_details_requested;
114 GtkWidget *details_spinner;
115 GCancellable *details_cancellable; /* owned */
116 } EmpathyIndividualWidgetPriv;
118 G_DEFINE_TYPE (EmpathyIndividualWidget, empathy_individual_widget,
127 details_set_up (EmpathyIndividualWidget *self)
129 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
131 gtk_widget_hide (priv->vbox_details);
133 priv->details_spinner = gtk_spinner_new ();
134 gtk_box_pack_end (GTK_BOX (priv->hbox_details_requested),
135 priv->details_spinner, TRUE, TRUE, 0);
136 gtk_widget_show (priv->details_spinner);
141 const gchar *field_name;
146 static InfoFieldData info_field_data[] =
148 { "fn", N_("Full name:"), FALSE },
149 { "tel", N_("Phone number:"), FALSE },
150 { "email", N_("E-mail address:"), TRUE },
151 { "url", N_("Website:"), TRUE },
152 { "bday", N_("Birthday:"), FALSE },
156 static InfoFieldData *
157 find_info_field_data (const gchar *field_name)
161 for (i = 0; info_field_data[i].field_name != NULL; i++)
163 if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
164 return info_field_data + i;
170 contact_info_field_name_cmp (const gchar *name1,
175 if (tp_strdiff (name1, name2) == FALSE)
178 /* We use the order of info_field_data */
179 for (i = 0; info_field_data[i].field_name != NULL; i++)
181 if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
183 if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
187 return g_strcmp0 (name1, name2);
191 contact_info_field_cmp (TpContactInfoField *field1,
192 TpContactInfoField *field2)
194 return contact_info_field_name_cmp (field1->field_name, field2->field_name);
198 EmpathyIndividualWidget *widget; /* weak */
199 TpContact *contact; /* owned */
203 details_data_free (DetailsData *data)
205 if (data->widget != NULL)
207 g_object_remove_weak_pointer (G_OBJECT (data->widget),
208 (gpointer *) &data->widget);
210 g_object_unref (data->contact);
211 g_slice_free (DetailsData, data);
215 details_update_show (EmpathyIndividualWidget *self,
218 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
222 info = tp_contact_get_contact_info (contact);
223 info = g_list_sort (info, (GCompareFunc) contact_info_field_cmp);
224 for (l = info; l != NULL; l = l->next)
226 TpContactInfoField *field = l->data;
227 InfoFieldData *field_data;
231 if (field->field_value == NULL || field->field_value[0] == NULL)
234 value = field->field_value[0];
236 field_data = find_info_field_data (field->field_name);
237 if (field_data == NULL)
239 DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
244 w = gtk_label_new (_(field_data->title));
245 gtk_table_attach (GTK_TABLE (priv->table_details),
246 w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
247 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
251 w = gtk_label_new (value);
252 if (field_data->linkify == TRUE)
256 markup = empathy_add_link_markup (value);
257 gtk_label_set_markup (GTK_LABEL (w), markup);
261 gtk_label_set_selectable (GTK_LABEL (w),
262 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
264 gtk_table_attach_defaults (GTK_TABLE (priv->table_details),
265 w, 1, 2, n_rows, n_rows + 1);
266 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
277 details_notify_cb (TpContact *contact,
279 EmpathyIndividualWidget *self)
281 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
284 gtk_container_foreach (GTK_CONTAINER (priv->table_details),
285 (GtkCallback) gtk_widget_destroy, NULL);
287 n_rows = details_update_show (self, contact);
291 gtk_widget_show (priv->vbox_details);
292 gtk_widget_show (priv->table_details);
296 gtk_widget_hide (priv->vbox_details);
299 gtk_widget_hide (priv->hbox_details_requested);
300 gtk_spinner_stop (GTK_SPINNER (priv->details_spinner));
304 details_request_cb (TpContact *contact,
308 EmpathyIndividualWidget *self = data->widget;
309 gboolean hide_widget = FALSE;
310 GError *error = NULL;
312 if (tp_contact_request_contact_info_finish (contact, res, &error) == TRUE)
313 details_notify_cb (contact, NULL, self);
317 g_clear_error (&error);
321 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
323 if (hide_widget == TRUE)
324 gtk_widget_hide (GET_PRIV (self)->vbox_details);
326 tp_clear_object (&priv->details_cancellable);
328 /* We need a (weak) pointer to the contact so that we can disconnect the
329 * signal handler on deconstruction. */
330 if (priv->contact_info_contact != NULL)
332 g_object_remove_weak_pointer (G_OBJECT (priv->contact_info_contact),
333 (gpointer *) &priv->contact_info_contact);
336 priv->contact_info_contact = contact;
337 g_object_add_weak_pointer (G_OBJECT (contact),
338 (gpointer *) &priv->contact_info_contact);
340 g_signal_connect (contact, "notify::contact-info",
341 (GCallback) details_notify_cb, self);
344 details_data_free (data);
348 details_feature_prepared_cb (TpConnection *connection,
352 EmpathyIndividualWidget *self = data->widget;
353 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
355 if (tp_proxy_prepare_finish (connection, res, NULL) == FALSE)
357 gtk_widget_hide (priv->vbox_details);
358 details_data_free (data);
362 /* Request the Individual's info */
363 gtk_widget_show (priv->vbox_details);
364 gtk_widget_show (priv->hbox_details_requested);
365 gtk_widget_hide (priv->table_details);
366 gtk_spinner_start (GTK_SPINNER (priv->details_spinner));
368 if (priv->details_cancellable == NULL)
370 priv->details_cancellable = g_cancellable_new ();
371 tp_contact_request_contact_info_async (data->contact,
372 priv->details_cancellable, (GAsyncReadyCallback) details_request_cb,
378 details_update (EmpathyIndividualWidget *self)
380 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
381 TpContact *tp_contact = NULL;
383 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS))
386 gtk_widget_hide (priv->vbox_details);
388 if (priv->individual != NULL)
390 /* FIXME: We take the first TpContact we find and only use its details.
391 * It would be a lot better if we would get the details for every
392 * TpContact in the Individual and merge them all, but that requires
393 * vCard support in libfolks for it to not be hideously complex.
397 personas = folks_individual_get_personas (priv->individual);
398 for (l = personas; l != NULL; l = l->next)
400 if (TPF_IS_PERSONA (l->data))
402 tp_contact = tpf_persona_get_contact (TPF_PERSONA (l->data));
403 if (tp_contact != NULL)
409 if (tp_contact != NULL)
411 GQuark features[] = { TP_CONNECTION_FEATURE_CONTACT_INFO, 0 };
412 TpConnection *connection;
415 data = g_slice_new (DetailsData);
417 g_object_add_weak_pointer (G_OBJECT (self), (gpointer *) &data->widget);
418 data->contact = g_object_ref (tp_contact);
420 /* First, make sure the CONTACT_INFO feature is ready on the connection */
421 connection = tp_contact_get_connection (tp_contact);
422 tp_proxy_prepare_async (connection, features,
423 (GAsyncReadyCallback) details_feature_prepared_cb, data);
428 groups_update (EmpathyIndividualWidget *self)
430 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
432 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS &&
433 priv->individual != NULL)
435 empathy_groups_widget_set_groupable (
436 EMPATHY_GROUPS_WIDGET (priv->groups_widget),
437 FOLKS_GROUPS (priv->individual));
438 gtk_widget_show (priv->groups_widget);
442 gtk_widget_hide (priv->groups_widget);
446 /* Converts the Location's GHashTable's key to a user readable string */
448 location_key_to_label (const gchar *key)
450 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
451 return _("Country ISO Code:");
452 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
453 return _("Country:");
454 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
456 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
458 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
460 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
461 return _("Postal Code:");
462 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
464 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
465 return _("Building:");
466 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
468 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
470 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
472 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
473 return _("Description:");
474 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
476 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
477 return _("Accuracy Level:");
478 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
480 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
481 return _("Vertical Error (meters):");
482 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
483 return _("Horizontal Error (meters):");
484 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
486 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
487 return _("Bearing:");
488 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
489 return _("Climb Speed:");
490 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
491 return _("Last Updated on:");
492 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
493 return _("Longitude:");
494 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
495 return _("Latitude:");
496 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
497 return _("Altitude:");
500 DEBUG ("Unexpected Location key: %s", key);
506 location_update (EmpathyIndividualWidget *self)
508 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
509 EmpathyContact *contact = NULL;
510 GHashTable *location = NULL;
514 static const gchar* ordered_geolocation_keys[] = {
515 EMPATHY_LOCATION_TEXT,
516 EMPATHY_LOCATION_URI,
517 EMPATHY_LOCATION_DESCRIPTION,
518 EMPATHY_LOCATION_BUILDING,
519 EMPATHY_LOCATION_FLOOR,
520 EMPATHY_LOCATION_ROOM,
521 EMPATHY_LOCATION_STREET,
522 EMPATHY_LOCATION_AREA,
523 EMPATHY_LOCATION_LOCALITY,
524 EMPATHY_LOCATION_REGION,
525 EMPATHY_LOCATION_COUNTRY,
530 gboolean display_map = FALSE;
533 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION))
535 gtk_widget_hide (priv->vbox_location);
539 /* FIXME: For the moment, we just display the first location data we can
540 * find amongst the Individual's Personas. Once libfolks grows a location
541 * interface, we can use that. (bgo#627400) */
542 personas = folks_individual_get_personas (priv->individual);
543 for (l = personas; l != NULL; l = l->next)
545 FolksPersona *persona = FOLKS_PERSONA (l->data);
547 if (TPF_IS_PERSONA (persona))
549 TpContact *tp_contact;
551 /* Get the contact. If it turns out to have location information, we
552 * have to keep it alive for the duration of the function, since we're
553 * accessing its private data. */
554 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
555 contact = empathy_contact_dup_from_tp_contact (tp_contact);
556 empathy_contact_set_persona (contact, persona);
558 /* Try and get a location */
559 location = empathy_contact_get_location (contact);
560 if (location != NULL && g_hash_table_size (location) > 0)
564 tp_clear_object (&contact);
568 if (contact == NULL || location == NULL)
570 gtk_widget_hide (priv->vbox_location);
571 tp_clear_object (&contact);
575 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
578 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
579 gtk_label_set_markup (GTK_LABEL (priv->label_location), loc);
590 stamp = g_value_get_int64 (value);
593 user_date = empathy_time_to_string_relative (time_);
595 tmp = g_strdup_printf ("<b>%s</b>", _("Location"));
596 /* translators: format is "Location, $date" */
597 text = g_strdup_printf (_("%s, %s"), tmp, user_date);
599 gtk_label_set_markup (GTK_LABEL (priv->label_location), text);
604 /* Prepare the location information table */
605 if (priv->table_location != NULL)
606 gtk_widget_destroy (priv->table_location);
608 priv->table_location = gtk_table_new (1, 2, FALSE);
609 gtk_box_pack_start (GTK_BOX (priv->subvbox_location),
610 priv->table_location, FALSE, FALSE, 5);
613 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
615 const gchar* user_label;
619 gvalue = g_hash_table_lookup (location, (gpointer) skey);
623 user_label = location_key_to_label (skey);
625 label = gtk_label_new (user_label);
626 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
627 gtk_table_attach (GTK_TABLE (priv->table_location),
628 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
629 gtk_widget_show (label);
631 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
634 dvalue = g_value_get_double (gvalue);
635 svalue = g_strdup_printf ("%f", dvalue);
637 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
639 svalue = g_value_dup_string (gvalue);
641 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
645 time_ = g_value_get_int64 (value);
646 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
651 label = gtk_label_new (svalue);
652 gtk_table_attach_defaults (GTK_TABLE (priv->table_location),
653 label, 1, 2, row, row + 1);
654 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
655 gtk_widget_show (label);
657 gtk_label_set_selectable (GTK_LABEL (label),
658 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE :
666 tp_clear_object (&contact);
668 #ifdef HAVE_LIBCHAMPLAIN
669 if ((g_hash_table_lookup (location, EMPATHY_LOCATION_LAT) != NULL) &&
670 (g_hash_table_lookup (location, EMPATHY_LOCATION_LON) != NULL) &&
671 !(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
673 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
681 /* We can display some fields */
682 gtk_widget_show (priv->table_location);
684 else if (display_map == FALSE)
686 /* Can't display either fields or map */
687 gtk_widget_hide (priv->vbox_location);
691 #ifdef HAVE_LIBCHAMPLAIN
692 if (display_map == TRUE)
695 ChamplainLayer *layer;
697 priv->map_view_embed = gtk_champlain_embed_new ();
698 priv->map_view = gtk_champlain_embed_get_view (
699 GTK_CHAMPLAIN_EMBED (priv->map_view_embed));
701 gtk_container_add (GTK_CONTAINER (priv->viewport_map),
702 priv->map_view_embed);
703 g_object_set (G_OBJECT (priv->map_view),
704 "show-license", TRUE,
705 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
709 layer = champlain_layer_new ();
710 champlain_view_add_layer (priv->map_view, layer);
711 markers = g_ptr_array_new ();
713 /* FIXME: For now, we have to do this manually. Once libfolks grows a
714 * location interface, we can use that. (bgo#627400) */
715 personas = folks_individual_get_personas (priv->individual);
716 for (l = personas; l != NULL; l = l->next)
718 FolksPersona *persona = FOLKS_PERSONA (l->data);
720 if (TPF_IS_PERSONA (persona))
722 gdouble lat = 0.0, lon = 0.0;
723 ClutterActor *marker;
724 TpContact *tp_contact;
726 /* Get the contact */
727 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
728 contact = empathy_contact_dup_from_tp_contact (tp_contact);
729 empathy_contact_set_persona (contact, persona);
731 /* Try and get a location */
732 location = empathy_contact_get_location (contact);
733 if (location == NULL || g_hash_table_size (location) == 0)
735 g_object_unref (contact);
739 /* Get this persona's latitude and longitude */
740 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
743 g_object_unref (contact);
747 lat = g_value_get_double (value);
749 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
752 g_object_unref (contact);
756 lon = g_value_get_double (value);
758 /* Add a marker to the map */
759 marker = champlain_marker_new_with_text (
760 folks_alias_get_alias (FOLKS_ALIAS (persona)), NULL, NULL,
762 champlain_base_marker_set_position (
763 CHAMPLAIN_BASE_MARKER (marker), lat, lon);
764 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
766 g_ptr_array_add (markers, marker);
768 g_object_unref (contact);
772 /* Zoom to show all of the markers */
773 g_ptr_array_add (markers, NULL); /* NULL-terminate the array */
774 champlain_view_ensure_markers_visible (priv->map_view,
775 (ChamplainBaseMarker **) markers->pdata, FALSE);
776 g_ptr_array_free (markers, TRUE);
778 gtk_widget_show_all (priv->viewport_map);
782 gtk_widget_show (priv->vbox_location);
785 static EmpathyAvatar *
786 persona_dup_avatar (FolksPersona *persona)
788 TpContact *tp_contact;
789 EmpathyContact *contact;
790 EmpathyAvatar *avatar;
792 if (!TPF_IS_PERSONA (persona))
795 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
796 contact = empathy_contact_dup_from_tp_contact (tp_contact);
797 empathy_contact_set_persona (contact, persona);
799 avatar = empathy_contact_get_avatar (contact);
801 empathy_avatar_ref (avatar);
802 g_object_unref (contact);
807 static EmpathyAvatar *
808 individual_dup_avatar (FolksIndividual *individual)
811 EmpathyAvatar *avatar = NULL;
813 /* FIXME: We just choose the first Persona which has an avatar, and save that.
814 * The avatar handling in EmpathyContact needs to be moved into libfolks as
815 * much as possible, and this code rewritten to use FolksAvatar.
817 personas = folks_individual_get_personas (individual);
818 for (l = personas; l != NULL; l = l->next)
820 avatar = persona_dup_avatar (FOLKS_PERSONA (l->data));
829 save_avatar_menu_activate_cb (GtkWidget *widget,
830 EmpathyIndividualWidget *self)
832 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
834 EmpathyAvatar *avatar;
835 gchar *ext = NULL, *filename;
837 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
839 GTK_FILE_CHOOSER_ACTION_SAVE,
840 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
841 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
844 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
847 avatar = individual_dup_avatar (priv->individual);
851 /* look for the avatar extension */
852 if (avatar->format != NULL)
856 splitted = g_strsplit (avatar->format, "/", 2);
857 if (splitted[0] != NULL && splitted[1] != NULL)
858 ext = g_strdup (splitted[1]);
860 g_strfreev (splitted);
864 /* Avatar was loaded from the cache so was converted to PNG */
865 ext = g_strdup ("png");
872 id = tp_escape_as_identifier (folks_individual_get_id (priv->individual));
874 filename = g_strdup_printf ("%s.%s", id, ext);
875 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
882 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
884 GError *error = NULL;
886 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
888 if (empathy_avatar_save_to_file (avatar, filename, &error) == FALSE)
891 GtkWidget *error_dialog;
893 error_dialog = gtk_message_dialog_new (NULL, 0,
894 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
895 _("Unable to save avatar"));
897 gtk_message_dialog_format_secondary_text (
898 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
900 g_signal_connect (error_dialog, "response",
901 (GCallback) gtk_widget_destroy, NULL);
903 gtk_window_present (GTK_WINDOW (error_dialog));
905 g_clear_error (&error);
911 gtk_widget_destroy (dialog);
912 empathy_avatar_unref (avatar);
916 popup_avatar_menu (EmpathyIndividualWidget *self,
918 GdkEventButton *event)
920 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
921 GtkWidget *menu, *item;
922 EmpathyAvatar *avatar;
923 gint button, event_time;
925 if (priv->individual == NULL)
928 avatar = individual_dup_avatar (priv->individual);
931 empathy_avatar_unref (avatar);
933 menu = gtk_menu_new ();
935 /* Add "Save as..." entry */
936 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
937 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
938 gtk_widget_show (item);
940 g_signal_connect (item, "activate",
941 (GCallback) save_avatar_menu_activate_cb, self);
945 button = event->button;
946 event_time = event->time;
951 event_time = gtk_get_current_event_time ();
954 gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
955 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, button, event_time);
956 g_object_ref_sink (menu);
957 g_object_unref (menu);
963 avatar_widget_popup_menu_cb (GtkWidget *widget,
964 EmpathyIndividualWidget *self)
966 return popup_avatar_menu (self, widget, NULL);
970 avatar_widget_button_press_event_cb (GtkWidget *widget,
971 GdkEventButton *event,
972 EmpathyIndividualWidget *self)
974 /* Ignore double-clicks and triple-clicks */
975 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
976 return popup_avatar_menu (self, widget, event);
981 /* Returns the TpAccount for the user as a convenience. Note that it has a ref
984 individual_is_user (FolksIndividual *individual)
988 /* FIXME: This should move into libfolks when libfolks grows a way of
989 * determining "self". (bgo#627402) */
990 personas = folks_individual_get_personas (individual);
991 for (l = personas; l != NULL; l = l->next)
993 FolksPersona *persona = FOLKS_PERSONA (l->data);
995 if (TPF_IS_PERSONA (persona))
997 TpContact *tp_contact;
998 EmpathyContact *contact;
1000 /* Get the contact */
1001 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1002 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1003 empathy_contact_set_persona (contact, persona);
1005 /* Determine if the contact is the user */
1006 if (empathy_contact_is_user (contact))
1008 g_object_unref (contact);
1009 return g_object_ref (empathy_contact_get_account (contact));
1012 g_object_unref (contact);
1020 set_nickname_cb (TpAccount *account,
1021 GAsyncResult *result,
1024 GError *error = NULL;
1026 if (tp_account_set_nickname_finish (account, result, &error) == FALSE)
1028 DEBUG ("Failed to set Account.Nickname: %s", error->message);
1029 g_error_free (error);
1034 entry_alias_focus_event_cb (GtkEditable *editable,
1035 GdkEventFocus *event,
1036 EmpathyIndividualWidget *self)
1038 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1040 if (priv->individual != NULL)
1045 alias = gtk_entry_get_text (GTK_ENTRY (editable));
1046 account = individual_is_user (priv->individual);
1048 if (account != NULL)
1050 DEBUG ("Set Account.Nickname to %s", alias);
1051 tp_account_set_nickname_async (account, alias,
1052 (GAsyncReadyCallback) set_nickname_cb, NULL);
1053 g_object_unref (account);
1057 folks_alias_set_alias (FOLKS_ALIAS (priv->individual), alias);
1065 favourite_toggled_cb (GtkToggleButton *button,
1066 EmpathyIndividualWidget *self)
1068 gboolean active = gtk_toggle_button_get_active (button);
1069 folks_favourite_set_is_favourite (
1070 FOLKS_FAVOURITE (GET_PRIV (self)->individual), active);
1074 notify_avatar_cb (gpointer folks_object,
1076 EmpathyIndividualWidget *self)
1078 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1079 EmpathyAvatar *avatar = NULL;
1081 GtkWidget *avatar_widget;
1083 if (FOLKS_IS_INDIVIDUAL (folks_object))
1085 avatar = individual_dup_avatar (FOLKS_INDIVIDUAL (folks_object));
1086 table = G_OBJECT (priv->individual_table);
1088 else if (FOLKS_IS_PERSONA (folks_object))
1090 avatar = persona_dup_avatar (FOLKS_PERSONA (folks_object));
1091 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1095 g_assert_not_reached ();
1101 avatar_widget = g_object_get_data (table, "avatar-widget");
1102 empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (avatar_widget), avatar);
1105 empathy_avatar_unref (avatar);
1109 notify_alias_cb (gpointer folks_object,
1111 EmpathyIndividualWidget *self)
1113 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1115 GtkWidget *alias_widget;
1117 if (FOLKS_IS_INDIVIDUAL (folks_object))
1118 table = G_OBJECT (priv->individual_table);
1119 else if (FOLKS_IS_PERSONA (folks_object))
1120 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1122 g_assert_not_reached ();
1127 alias_widget = g_object_get_data (table, "alias-widget");
1129 if (GTK_IS_ENTRY (alias_widget))
1131 gtk_entry_set_text (GTK_ENTRY (alias_widget),
1132 folks_alias_get_alias (FOLKS_ALIAS (folks_object)));
1136 gtk_label_set_label (GTK_LABEL (alias_widget),
1137 folks_alias_get_alias (FOLKS_ALIAS (folks_object)));
1142 notify_presence_cb (gpointer folks_object,
1144 EmpathyIndividualWidget *self)
1146 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1148 GtkWidget *status_label, *state_image;
1149 const gchar *message;
1150 gchar *markup_text = NULL;
1152 if (FOLKS_IS_INDIVIDUAL (folks_object))
1153 table = G_OBJECT (priv->individual_table);
1154 else if (FOLKS_IS_PERSONA (folks_object))
1155 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1157 g_assert_not_reached ();
1162 status_label = g_object_get_data (table, "status-label");
1163 state_image = g_object_get_data (table, "state-image");
1165 /* FIXME: Default messages should be moved into libfolks (bgo#627403) */
1166 message = folks_presence_get_presence_message (FOLKS_PRESENCE (folks_object));
1167 if (EMP_STR_EMPTY (message))
1169 message = empathy_presence_get_default_message (
1170 folks_presence_get_presence_type (FOLKS_PRESENCE (folks_object)));
1173 if (message != NULL)
1174 markup_text = empathy_add_link_markup (message);
1175 gtk_label_set_markup (GTK_LABEL (status_label), markup_text);
1176 g_free (markup_text);
1178 gtk_image_set_from_icon_name (GTK_IMAGE (state_image),
1179 empathy_icon_name_for_presence (
1180 folks_presence_get_presence_type (FOLKS_PRESENCE (folks_object))),
1181 GTK_ICON_SIZE_BUTTON);
1182 gtk_widget_show (state_image);
1186 notify_is_favourite_cb (gpointer folks_object,
1188 EmpathyIndividualWidget *self)
1190 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1192 GtkWidget *favourite_widget;
1194 if (FOLKS_IS_INDIVIDUAL (folks_object))
1195 table = G_OBJECT (priv->individual_table);
1196 else if (FOLKS_IS_PERSONA (folks_object))
1197 table = g_hash_table_lookup (priv->persona_tables, folks_object);
1199 g_assert_not_reached ();
1204 favourite_widget = g_object_get_data (table, "favourite-widget");
1206 if (GTK_IS_TOGGLE_BUTTON (favourite_widget))
1208 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (favourite_widget),
1209 folks_favourite_get_is_favourite (FOLKS_FAVOURITE (folks_object)));
1214 alias_presence_avatar_favourite_set_up (EmpathyIndividualWidget *self,
1218 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1219 GtkWidget *label, *alias, *image, *avatar, *alignment;
1220 guint current_row = starting_row;
1223 label = gtk_label_new (_("Alias:"));
1224 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1225 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1, GTK_FILL,
1227 gtk_widget_show (label);
1229 /* Set up alias label/entry */
1230 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS)
1232 alias = gtk_entry_new ();
1234 g_signal_connect (alias, "focus-out-event",
1235 (GCallback) entry_alias_focus_event_cb, self);
1237 /* Make return activate the window default (the Close button) */
1238 gtk_entry_set_activates_default (GTK_ENTRY (alias), TRUE);
1242 alias = gtk_label_new (NULL);
1243 gtk_label_set_selectable (GTK_LABEL (alias),
1244 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1245 gtk_misc_set_alignment (GTK_MISC (alias), 0.0, 0.5);
1248 g_object_set_data (G_OBJECT (table), "alias-widget", alias);
1249 gtk_table_attach (table, alias, 1, 2, current_row, current_row + 1,
1250 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1251 gtk_widget_show (alias);
1256 priv->hbox_presence = gtk_hbox_new (FALSE, 6);
1258 /* Presence image */
1259 image = gtk_image_new_from_stock (GTK_STOCK_MISSING_IMAGE,
1260 GTK_ICON_SIZE_BUTTON);
1261 g_object_set_data (G_OBJECT (table), "state-image", image);
1262 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), image, FALSE,
1264 gtk_widget_show (image);
1266 /* Set up status_label as a KludgeLabel */
1267 label = empathy_kludge_label_new ("");
1268 gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
1269 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1271 gtk_label_set_selectable (GTK_LABEL (label),
1272 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1274 g_object_set_data (G_OBJECT (table), "status-label", label);
1275 gtk_box_pack_start (GTK_BOX (priv->hbox_presence), label, TRUE,
1277 gtk_widget_show (label);
1279 gtk_table_attach (table, priv->hbox_presence, 0, 2, current_row,
1280 current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1281 gtk_widget_show (priv->hbox_presence);
1285 /* Set up favourite toggle button */
1286 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1288 GtkWidget *favourite = gtk_check_button_new_with_label (_("Favorite"));
1290 g_signal_connect (favourite, "toggled",
1291 (GCallback) favourite_toggled_cb, self);
1293 g_object_set_data (G_OBJECT (table), "favourite-widget", favourite);
1294 gtk_table_attach (table, favourite, 0, 2, current_row, current_row + 1,
1295 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1296 gtk_widget_show (favourite);
1301 /* Set up avatar display */
1302 avatar = empathy_avatar_image_new ();
1304 if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP))
1306 g_signal_connect (avatar, "popup-menu",
1307 (GCallback) avatar_widget_popup_menu_cb, self);
1308 g_signal_connect (avatar, "button-press-event",
1309 (GCallback) avatar_widget_button_press_event_cb, self);
1312 g_object_set_data (G_OBJECT (table), "avatar-widget", avatar);
1314 alignment = gtk_alignment_new (1.0, 0.0, 0.0, 0.0);
1315 gtk_container_add (GTK_CONTAINER (alignment), avatar);
1316 gtk_widget_show (avatar);
1318 gtk_table_attach (table, alignment, 2, 3, 0, current_row,
1319 GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 6, 6);
1320 gtk_widget_show (alignment);
1324 update_persona (EmpathyIndividualWidget *self, FolksPersona *persona)
1326 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1327 TpContact *tp_contact;
1328 EmpathyContact *contact;
1335 table = g_hash_table_lookup (priv->persona_tables, persona);
1337 g_assert (table != NULL);
1339 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1340 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1341 empathy_contact_set_persona (contact, persona);
1343 account = empathy_contact_get_account (contact);
1345 /* Update account widget */
1346 if (account != NULL)
1350 label = g_object_get_data (G_OBJECT (table), "account-label");
1351 image = g_object_get_data (G_OBJECT (table), "account-image");
1353 name = tp_account_get_display_name (account);
1354 gtk_label_set_label (label, name);
1356 name = tp_account_get_icon_name (account);
1357 gtk_image_set_from_icon_name (image, name, GTK_ICON_SIZE_MENU);
1360 /* Update id widget */
1361 label = g_object_get_data (G_OBJECT (table), "id-widget");
1362 id = folks_persona_get_display_id (persona);
1363 gtk_label_set_label (label, (id != NULL) ? id : "");
1365 /* Update other widgets */
1366 notify_alias_cb (persona, NULL, self);
1367 notify_presence_cb (persona, NULL, self);
1368 notify_avatar_cb (persona, NULL, self);
1370 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1371 notify_is_favourite_cb (persona, NULL, self);
1373 g_object_unref (contact);
1377 add_persona (EmpathyIndividualWidget *self,
1378 FolksPersona *persona)
1380 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1383 GtkWidget *label, *account_label, *account_image, *separator;
1384 guint current_row = 0;
1386 if (!TPF_IS_PERSONA (persona))
1389 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1390 table = GTK_TABLE (gtk_table_new (5, 3, FALSE));
1392 table = GTK_TABLE (gtk_table_new (4, 3, FALSE));
1393 gtk_table_set_row_spacings (table, 6);
1394 gtk_table_set_col_spacings (table, 6);
1396 /* Account and Identifier */
1397 label = gtk_label_new (_("Account:"));
1398 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1399 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1400 GTK_FILL, GTK_FILL, 0, 0);
1401 gtk_widget_show (label);
1403 /* Pack the protocol icon with the account name in an hbox */
1404 hbox = GTK_BOX (gtk_hbox_new (FALSE, 6));
1406 account_label = gtk_label_new (NULL);
1407 gtk_label_set_selectable (GTK_LABEL (account_label),
1408 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1409 gtk_misc_set_alignment (GTK_MISC (account_label), 0.0, 0.5);
1410 gtk_widget_show (account_label);
1412 account_image = gtk_image_new ();
1413 gtk_widget_show (account_image);
1415 gtk_box_pack_start (hbox, account_image, FALSE, FALSE, 0);
1416 gtk_box_pack_start (hbox, account_label, FALSE, TRUE, 0);
1418 g_object_set_data (G_OBJECT (table), "account-image", account_image);
1419 g_object_set_data (G_OBJECT (table), "account-label", account_label);
1420 gtk_table_attach (table, GTK_WIDGET (hbox), 1, 2, current_row,
1421 current_row + 1, GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1422 gtk_widget_show (GTK_WIDGET (hbox));
1426 /* Translators: Identifier to connect to Instant Messaging network */
1427 label = gtk_label_new (_("Identifier:"));
1428 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1429 gtk_table_attach (table, label, 0, 1, current_row, current_row + 1,
1430 GTK_FILL, GTK_FILL, 0, 0);
1431 gtk_widget_show (label);
1433 /* Set up ID label */
1434 label = gtk_label_new (NULL);
1435 gtk_label_set_selectable (GTK_LABEL (label),
1436 (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP) ? FALSE : TRUE);
1437 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1439 g_object_set_data (G_OBJECT (table), "id-widget", label);
1440 gtk_table_attach (table, label, 1, 2, current_row, current_row + 1,
1441 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1442 gtk_widget_show (label);
1446 alias_presence_avatar_favourite_set_up (self, table, current_row);
1448 /* Connect to signals and display the table */
1449 g_signal_connect (persona, "notify::alias",
1450 (GCallback) notify_alias_cb, self);
1451 g_signal_connect (persona, "notify::avatar",
1452 (GCallback) notify_avatar_cb, self);
1453 g_signal_connect (persona, "notify::presence-type",
1454 (GCallback) notify_presence_cb, self);
1455 g_signal_connect (persona, "notify::presence-message",
1456 (GCallback) notify_presence_cb, self);
1458 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1460 g_signal_connect (persona, "notify::is-favourite",
1461 (GCallback) notify_is_favourite_cb, self);
1464 gtk_box_pack_start (GTK_BOX (priv->vbox_individual),
1465 GTK_WIDGET (table), FALSE, TRUE, 0);
1466 gtk_widget_show (GTK_WIDGET (table));
1468 /* Pack a separator after the table */
1469 separator = gtk_hseparator_new ();
1470 g_object_set_data (G_OBJECT (table), "separator", separator);
1471 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), separator, FALSE, FALSE,
1473 gtk_widget_show (separator);
1475 g_hash_table_replace (priv->persona_tables, persona, table);
1477 /* Update the new widgets */
1478 update_persona (self, persona);
1482 remove_persona (EmpathyIndividualWidget *self,
1483 FolksPersona *persona)
1485 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1486 GtkWidget *separator;
1489 if (!TPF_IS_PERSONA (persona))
1492 table = g_hash_table_lookup (priv->persona_tables, persona);
1496 g_signal_handlers_disconnect_by_func (persona, notify_alias_cb, self);
1497 g_signal_handlers_disconnect_by_func (persona, notify_avatar_cb, self);
1498 g_signal_handlers_disconnect_by_func (persona, notify_presence_cb, self);
1500 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1502 g_signal_handlers_disconnect_by_func (persona, notify_is_favourite_cb,
1506 /* Remove the separator */
1507 separator = g_object_get_data (G_OBJECT (table), "separator");
1508 if (separator != NULL)
1509 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual), separator);
1511 /* Remove the widget */
1512 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1513 GTK_WIDGET (table));
1515 g_hash_table_remove (priv->persona_tables, persona);
1519 update_individual_table (EmpathyIndividualWidget *self)
1521 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1523 notify_alias_cb (priv->individual, NULL, self);
1524 notify_presence_cb (priv->individual, NULL, self);
1525 notify_avatar_cb (priv->individual, NULL, self);
1527 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1528 notify_is_favourite_cb (priv->individual, NULL, self);
1532 individual_table_set_up (EmpathyIndividualWidget *self)
1534 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1536 guint current_row = 0;
1539 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1542 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1545 table = GTK_TABLE (gtk_table_new (nb_rows, 3, FALSE));
1546 gtk_table_set_row_spacings (table, 6);
1547 gtk_table_set_col_spacings (table, 6);
1549 /* We only display the number of personas in tooltips */
1550 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP)
1554 GList *personas, *l;
1555 guint num_personas = 0;
1557 /* Meta-contacts message displaying how many Telepathy personas we have */
1558 personas = folks_individual_get_personas (priv->individual);
1559 for (l = personas; l != NULL; l = l->next)
1561 if (TPF_IS_PERSONA (l->data))
1565 message = g_strdup_printf (ngettext ("Meta-contact containing %u contact",
1566 "Meta-contact containing %u contacts", num_personas), num_personas);
1567 label = gtk_label_new (message);
1568 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1571 gtk_table_attach (table, label, 0, 2, current_row, current_row + 1,
1572 GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
1573 gtk_widget_show (label);
1578 alias_presence_avatar_favourite_set_up (self, table, current_row);
1580 /* Display the table */
1581 gtk_box_pack_start (GTK_BOX (priv->vbox_individual), GTK_WIDGET (table),
1583 gtk_widget_show (GTK_WIDGET (table));
1585 priv->individual_table = table;
1587 /* Update the table */
1588 update_individual_table (self);
1592 individual_table_destroy (EmpathyIndividualWidget *self)
1594 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1596 if (priv->individual_table == NULL)
1599 gtk_container_remove (GTK_CONTAINER (priv->vbox_individual),
1600 GTK_WIDGET (priv->individual_table));
1601 priv->individual_table = NULL;
1605 personas_changed_cb (FolksIndividual *individual,
1608 EmpathyIndividualWidget *self)
1610 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1611 GList *personas, *l, *children;
1612 gboolean show_personas, was_showing_personas, will_show_personas, is_last;
1613 guint old_num_personas, new_num_personas;
1615 personas = folks_individual_get_personas (individual);
1617 /* Note that old_num_personas is the number of persona tables we were
1618 * displaying, not the number of Personas which were in the Individual
1620 old_num_personas = g_hash_table_size (priv->persona_tables);
1621 new_num_personas = g_list_length (personas);
1624 * What we display for various conditions:
1625 * - "Personas": display the alias, avatar, presence account and identifier
1626 * for each of the Individual's Personas. (i.e. One table per
1628 * - "Individual": display the alias, avatar and presence for the Individual,
1629 * and a label saying "Meta-contact containing x contacts".
1630 * (i.e. One table in total.)
1632 * | SHOW_PERSONAS | !SHOW_PERSONAS
1633 * -------------+---------------+---------------
1634 * > 1 Persona | Personas | Individual
1635 * -------------+---------------+---------------
1636 * == 1 Persona | Personas | Personas
1638 show_personas = (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS) != 0;
1639 was_showing_personas = show_personas || old_num_personas == 1;
1640 will_show_personas = show_personas || new_num_personas == 1;
1642 /* If both @added and @removed are NULL, we're being called manually, and we
1643 * need to set up the tables for the first time. We do this simply by
1644 * ensuring was_showing_personas and will_show_personas are different so that
1645 * the code resets the UI.
1647 if (added == NULL && removed == NULL)
1648 was_showing_personas = !will_show_personas;
1650 if (was_showing_personas && will_show_personas)
1652 /* Remove outdated Personas */
1653 for (l = removed; l != NULL; l = l->next)
1654 remove_persona (self, FOLKS_PERSONA (l->data));
1656 /* Add new Personas */
1657 for (l = added; l != NULL; l = l->next)
1658 add_persona (self, FOLKS_PERSONA (l->data));
1660 else if (!was_showing_personas && will_show_personas)
1662 /* Remove the old Individual table */
1663 individual_table_destroy (self);
1665 /* Set up all the Persona tables instead */
1666 for (l = personas; l != NULL; l = l->next)
1667 add_persona (self, FOLKS_PERSONA (l->data));
1669 else if (was_showing_personas && !will_show_personas)
1671 /* Remove all Personas */
1672 for (l = personas; l != NULL; l = l->next)
1673 remove_persona (self, FOLKS_PERSONA (l->data));
1675 /* Set up the Individual table instead */
1676 individual_table_set_up (self);
1679 /* Hide the last separator and show the others */
1680 children = gtk_container_get_children (GTK_CONTAINER (priv->vbox_individual));
1681 children = g_list_reverse (children);
1684 for (l = children; l != NULL; l = l->next)
1686 if (GTK_IS_SEPARATOR (l->data))
1688 gtk_widget_set_visible (GTK_WIDGET (l->data), !is_last);
1693 g_list_free (children);
1697 remove_individual (EmpathyIndividualWidget *self)
1699 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1700 if (priv->individual != NULL)
1702 GList *personas, *l;
1704 g_signal_handlers_disconnect_by_func (priv->individual,
1705 notify_alias_cb, self);
1706 g_signal_handlers_disconnect_by_func (priv->individual,
1707 notify_presence_cb, self);
1708 g_signal_handlers_disconnect_by_func (priv->individual,
1709 notify_avatar_cb, self);
1710 g_signal_handlers_disconnect_by_func (priv->individual,
1711 personas_changed_cb, self);
1713 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1715 g_signal_handlers_disconnect_by_func (priv->individual,
1716 notify_is_favourite_cb, self);
1719 personas = folks_individual_get_personas (priv->individual);
1720 for (l = personas; l != NULL; l = l->next)
1721 remove_persona (self, FOLKS_PERSONA (l->data));
1722 individual_table_destroy (self);
1724 if (priv->contact_info_contact != NULL)
1726 g_signal_handlers_disconnect_by_func (priv->contact_info_contact,
1727 details_notify_cb, self);
1728 g_object_remove_weak_pointer (G_OBJECT (priv->contact_info_contact),
1729 (gpointer *) &priv->contact_info_contact);
1730 priv->contact_info_contact = NULL;
1733 tp_clear_object (&priv->individual);
1736 if (priv->details_cancellable != NULL)
1737 g_cancellable_cancel (priv->details_cancellable);
1741 individual_update (EmpathyIndividualWidget *self)
1743 EmpathyIndividualWidgetPriv *priv = GET_PRIV (self);
1745 /* Connect and get info from new Individual */
1746 if (priv->individual != NULL)
1748 g_signal_connect (priv->individual, "notify::alias",
1749 (GCallback) notify_alias_cb, self);
1750 g_signal_connect (priv->individual, "notify::presence-type",
1751 (GCallback) notify_presence_cb, self);
1752 g_signal_connect (priv->individual, "notify::presence-message",
1753 (GCallback) notify_presence_cb, self);
1754 g_signal_connect (priv->individual, "notify::avatar",
1755 (GCallback) notify_avatar_cb, self);
1756 g_signal_connect (priv->individual, "personas-changed",
1757 (GCallback) personas_changed_cb, self);
1759 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE)
1761 g_signal_connect (priv->individual, "notify::is-favourite",
1762 (GCallback) notify_is_favourite_cb, self);
1765 /* Update individual table */
1766 personas_changed_cb (priv->individual, NULL, NULL, self);
1769 if (priv->individual == NULL)
1771 gtk_widget_hide (priv->vbox_individual);
1773 else if (priv->individual_table != NULL)
1775 /* We only need to update the details for the Individual as a whole */
1776 update_individual_table (self);
1777 gtk_widget_show (priv->vbox_individual);
1781 /* We need to update the details for every Persona in the Individual */
1782 GList *personas, *l;
1784 personas = folks_individual_get_personas (priv->individual);
1785 for (l = personas; l != NULL; l = l->next)
1787 if (!TPF_IS_PERSONA (l->data))
1790 update_persona (self, FOLKS_PERSONA (l->data));
1793 gtk_widget_show (priv->vbox_individual);
1798 empathy_individual_widget_init (EmpathyIndividualWidget *self)
1800 EmpathyIndividualWidgetPriv *priv;
1804 priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1805 EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
1808 gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
1809 GTK_ORIENTATION_VERTICAL);
1811 filename = empathy_file_lookup ("empathy-individual-widget.ui",
1813 gui = empathy_builder_get_file (filename,
1814 "scrolled_window_individual", &priv->scrolled_window_individual,
1815 "viewport_individual", &priv->viewport_individual,
1816 "vbox_individual_widget", &priv->vbox_individual_widget,
1817 "vbox_individual", &priv->vbox_individual,
1818 "vbox_location", &priv->vbox_location,
1819 "subvbox_location", &priv->subvbox_location,
1820 "label_location", &priv->label_location,
1821 #ifdef HAVE_LIBCHAMPLAIN
1822 "viewport_map", &priv->viewport_map,
1824 "groups_widget", &priv->groups_widget,
1825 "vbox_details", &priv->vbox_details,
1826 "table_details", &priv->table_details,
1827 "hbox_details_requested", &priv->hbox_details_requested,
1831 priv->table_location = NULL;
1833 gtk_box_pack_start (GTK_BOX (self), priv->vbox_individual_widget, TRUE, TRUE,
1835 gtk_widget_show (priv->vbox_individual_widget);
1837 priv->persona_tables = g_hash_table_new (NULL, NULL);
1838 priv->individual_table = NULL;
1840 /* Create widgets */
1841 details_set_up (self);
1843 g_object_unref (gui);
1847 constructed (GObject *object)
1849 GObjectClass *klass = G_OBJECT_CLASS (empathy_individual_widget_parent_class);
1850 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1851 GtkScrolledWindow *scrolled_window =
1852 GTK_SCROLLED_WINDOW (priv->scrolled_window_individual);
1854 /* Allow scrolling of the list of Personas if we're showing Personas. */
1855 if (priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS)
1857 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
1858 gtk_scrolled_window_set_policy (scrolled_window,
1859 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1860 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
1861 priv->scrolled_window_individual, TRUE, TRUE, 0, GTK_PACK_START);
1863 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1865 gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
1869 gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_NONE);
1870 gtk_scrolled_window_set_policy (scrolled_window,
1871 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1872 gtk_box_set_child_packing (GTK_BOX (priv->vbox_individual_widget),
1873 priv->scrolled_window_individual, FALSE, TRUE, 0, GTK_PACK_START);
1875 gtk_container_set_border_width (GTK_CONTAINER (priv->viewport_individual),
1880 if (klass->constructed != NULL)
1881 klass->constructed (object);
1885 get_property (GObject *object,
1890 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1894 case PROP_INDIVIDUAL:
1895 g_value_set_object (value, priv->individual);
1898 g_value_set_flags (value, priv->flags);
1901 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1907 set_property (GObject *object,
1909 const GValue *value,
1912 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1916 case PROP_INDIVIDUAL:
1917 empathy_individual_widget_set_individual (
1918 EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
1921 priv->flags = g_value_get_flags (value);
1924 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1930 dispose (GObject *object)
1932 remove_individual (EMPATHY_INDIVIDUAL_WIDGET (object));
1934 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
1938 finalize (GObject *object)
1940 EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
1942 g_hash_table_destroy (priv->persona_tables);
1944 G_OBJECT_CLASS (empathy_individual_widget_parent_class)->finalize (object);
1948 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
1950 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1952 object_class->constructed = constructed;
1953 object_class->get_property = get_property;
1954 object_class->set_property = set_property;
1955 object_class->dispose = dispose;
1956 object_class->finalize = finalize;
1959 * EmpathyIndividualWidget:individual:
1961 * The #FolksIndividual to display in the widget.
1963 g_object_class_install_property (object_class, PROP_INDIVIDUAL,
1964 g_param_spec_object ("individual",
1966 "The #FolksIndividual to display in the widget.",
1967 FOLKS_TYPE_INDIVIDUAL,
1968 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1971 * EmpathyIndividualWidget:flags:
1973 * A set of flags which affect the widget's behaviour.
1975 g_object_class_install_property (object_class, PROP_FLAGS,
1976 g_param_spec_flags ("flags",
1978 "A set of flags which affect the widget's behaviour.",
1979 EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
1980 EMPATHY_INDIVIDUAL_WIDGET_NONE,
1981 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
1983 g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
1987 * empathy_individual_widget_new:
1988 * @individual: the #FolksIndividual to display
1989 * @flags: flags affecting how the widget behaves and what it displays
1991 * Creates a new #EmpathyIndividualWidget.
1993 * Return value: a new #EmpathyIndividualWidget
1996 empathy_individual_widget_new (FolksIndividual *individual,
1997 EmpathyIndividualWidgetFlags flags)
1999 g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
2002 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
2003 "individual", individual,
2009 * empathy_individual_widget_get_individual:
2010 * @self: an #EmpathyIndividualWidget
2012 * Returns the #FolksIndividual being displayed by the widget.
2014 * Return value: the #FolksIndividual being displayed, or %NULL
2017 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
2019 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
2021 return GET_PRIV (self)->individual;
2025 * empathy_individual_widget_set_individual:
2026 * @self: an #EmpathyIndividualWidget
2027 * @individual: the #FolksIndividual to display, or %NULL
2029 * Set the #FolksIndividual to be displayed by the widget:
2030 * #EmpathyIndividualWidget:individual.
2032 * The @individual may be %NULL in order to display nothing in the widget.
2035 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
2036 FolksIndividual *individual)
2038 EmpathyIndividualWidgetPriv *priv;
2040 g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
2041 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
2043 priv = GET_PRIV (self);
2045 if (individual == priv->individual)
2048 /* Out with the old… */
2049 remove_individual (self);
2051 /* …and in with the new. */
2052 if (individual != NULL)
2053 g_object_ref (individual);
2054 priv->individual = individual;
2056 /* Update information for widgets */
2057 individual_update (self);
2058 groups_update (self);
2059 details_update (self);
2060 location_update (self);