1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n-lib.h>
31 #include <champlain/champlain.h>
32 #include <champlain-gtk/champlain-gtk.h>
35 #include <telepathy-glib/account.h>
36 #include <telepathy-glib/util.h>
38 #include <libempathy/empathy-tp-contact-factory.h>
39 #include <libempathy/empathy-contact-manager.h>
40 #include <libempathy/empathy-contact-list.h>
41 #include <libempathy/empathy-location.h>
42 #include <libempathy/empathy-time.h>
43 #include <libempathy/empathy-utils.h>
45 #include "empathy-contact-widget.h"
46 #include "empathy-account-chooser.h"
47 #include "empathy-avatar-chooser.h"
48 #include "empathy-avatar-image.h"
49 #include "empathy-ui-utils.h"
50 #include "empathy-kludge-label.h"
52 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
53 #include <libempathy/empathy-debug.h>
56 * SECTION:empathy-contact-widget
57 * @title:EmpathyContactWidget
58 * @short_description: A widget used to display and edit details about a contact
59 * @include: libempathy-empathy-contact-widget.h
61 * #EmpathyContactWidget is a widget which displays appropriate widgets
62 * with details about a contact, also allowing changing these details,
67 * EmpathyContactWidget:
68 * @parent: parent object
70 * Widget which displays appropriate widgets with details about a contact,
71 * also allowing changing these details, if desired.
74 /* Delay before updating the widget when the id entry changed (seconds) */
75 #define ID_CHANGED_TIMEOUT 1
79 EmpathyTpContactFactory *factory;
80 EmpathyContactManager *manager;
81 EmpathyContact *contact;
82 EmpathyContactWidgetFlags flags;
83 guint widget_id_timeout;
85 GtkWidget *vbox_contact_widget;
88 GtkWidget *hbox_contact;
89 GtkWidget *widget_avatar;
90 GtkWidget *widget_account;
91 GtkWidget *image_account;
92 GtkWidget *label_account;
94 GtkWidget *widget_alias;
95 GtkWidget *label_alias;
96 GtkWidget *entry_alias;
97 GtkWidget *hbox_presence;
98 GtkWidget *image_state;
99 GtkWidget *label_status;
100 GtkWidget *table_contact;
101 GtkWidget *vbox_avatar;
104 GtkWidget *vbox_location;
105 GtkWidget *subvbox_location;
106 GtkWidget *table_location;
107 GtkWidget *label_location;
108 #if HAVE_LIBCHAMPLAIN
109 GtkWidget *viewport_map;
110 GtkWidget *map_view_embed;
111 ChamplainView *map_view;
115 GtkWidget *vbox_groups;
116 GtkWidget *entry_group;
117 GtkWidget *button_group;
118 GtkWidget *treeview_groups;
121 GtkWidget *vbox_details;
122 GtkWidget *table_details;
123 GtkWidget *hbox_details_requested;
126 GtkWidget *vbox_client;
127 GtkWidget *table_client;
128 GtkWidget *hbox_client_requested;
129 } EmpathyContactWidget;
133 EmpathyContactWidget *information;
136 GtkTreeIter found_iter;
148 contact_widget_details_setup (EmpathyContactWidget *information)
150 /* FIXME: Needs new telepathy spec */
151 gtk_widget_hide (information->vbox_details);
155 contact_widget_details_update (EmpathyContactWidget *information)
157 /* FIXME: Needs new telepathy spec */
161 contact_widget_client_update (EmpathyContactWidget *information)
163 /* FIXME: Needs new telepathy spec */
167 contact_widget_client_setup (EmpathyContactWidget *information)
169 /* FIXME: Needs new telepathy spec */
170 gtk_widget_hide (information->vbox_client);
174 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
176 EmpathyContactWidget *information)
186 view = GTK_TREE_VIEW (information->treeview_groups);
187 model = gtk_tree_view_get_model (view);
188 store = GTK_LIST_STORE (model);
190 path = gtk_tree_path_new_from_string (path_string);
192 gtk_tree_model_get_iter (model, &iter, path);
193 gtk_tree_model_get (model, &iter,
194 COL_ENABLED, &enabled,
198 gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
199 gtk_tree_path_free (path);
205 empathy_contact_list_remove_from_group (
206 EMPATHY_CONTACT_LIST (information->manager), information->contact,
211 empathy_contact_list_add_to_group (
212 EMPATHY_CONTACT_LIST (information->manager), information->contact,
220 contact_widget_model_populate_columns (EmpathyContactWidget *information)
224 GtkTreeViewColumn *column;
225 GtkCellRenderer *renderer;
228 view = GTK_TREE_VIEW (information->treeview_groups);
229 model = gtk_tree_view_get_model (view);
231 renderer = gtk_cell_renderer_toggle_new ();
232 g_signal_connect (renderer, "toggled",
233 G_CALLBACK (contact_widget_cell_toggled), information);
235 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
236 "active", COL_ENABLED, NULL);
238 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
239 gtk_tree_view_column_set_fixed_width (column, 50);
240 gtk_tree_view_append_column (view, column);
242 renderer = gtk_cell_renderer_text_new ();
243 col_offset = gtk_tree_view_insert_column_with_attributes (view,
247 /* "editable", COL_EDITABLE, */
250 g_object_set_data (G_OBJECT (renderer),
251 "column", GINT_TO_POINTER (COL_NAME));
253 column = gtk_tree_view_get_column (view, col_offset - 1);
254 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
255 gtk_tree_view_column_set_resizable (column,FALSE);
256 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
260 contact_widget_model_setup (EmpathyContactWidget *information)
264 GtkTreeSelection *selection;
266 view = GTK_TREE_VIEW (information->treeview_groups);
268 store = gtk_list_store_new (COL_COUNT,
269 G_TYPE_STRING, /* name */
270 G_TYPE_BOOLEAN, /* enabled */
271 G_TYPE_BOOLEAN); /* editable */
273 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
275 selection = gtk_tree_view_get_selection (view);
276 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
278 contact_widget_model_populate_columns (information);
280 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
281 COL_NAME, GTK_SORT_ASCENDING);
283 g_object_unref (store);
287 contact_widget_groups_populate_data (EmpathyContactWidget *information)
292 GList *my_groups, *l;
295 view = GTK_TREE_VIEW (information->treeview_groups);
296 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
297 gtk_list_store_clear (store);
299 all_groups = empathy_contact_list_get_all_groups (
300 EMPATHY_CONTACT_LIST (information->manager));
301 my_groups = empathy_contact_list_get_groups (
302 EMPATHY_CONTACT_LIST (information->manager),
303 information->contact);
305 for (l = all_groups; l; l = l->next)
307 const gchar *group_str;
312 enabled = g_list_find_custom (my_groups,
313 group_str, (GCompareFunc) strcmp) != NULL;
315 gtk_list_store_append (store, &iter);
316 gtk_list_store_set (store, &iter,
319 COL_ENABLED, enabled,
323 g_list_foreach (all_groups, (GFunc) g_free, NULL);
324 g_list_foreach (my_groups, (GFunc) g_free, NULL);
325 g_list_free (all_groups);
326 g_list_free (my_groups);
330 contact_widget_model_find_name_foreach (GtkTreeModel *model,
337 gtk_tree_model_get (model, iter,
344 if (data->name && strcmp (data->name, name) == 0)
347 data->found_iter = *iter;
360 contact_widget_model_find_name (EmpathyContactWidget *information,
368 if (EMP_STR_EMPTY (name))
371 data.information = information;
375 view = GTK_TREE_VIEW (information->treeview_groups);
376 model = gtk_tree_view_get_model (view);
378 gtk_tree_model_foreach (model,
379 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
382 if (data.found == TRUE)
384 *iter = data.found_iter;
392 contact_widget_entry_group_changed_cb (GtkEditable *editable,
393 EmpathyContactWidget *information)
398 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
400 if (contact_widget_model_find_name (information, group, &iter))
401 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
403 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
404 !EMP_STR_EMPTY (group));
408 contact_widget_entry_group_activate_cb (GtkEntry *entry,
409 EmpathyContactWidget *information)
411 gtk_widget_activate (GTK_WIDGET (information->button_group));
415 contact_widget_button_group_clicked_cb (GtkButton *button,
416 EmpathyContactWidget *information)
423 view = GTK_TREE_VIEW (information->treeview_groups);
424 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
426 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
428 gtk_list_store_append (store, &iter);
429 gtk_list_store_set (store, &iter,
434 empathy_contact_list_add_to_group (
435 EMPATHY_CONTACT_LIST (information->manager), information->contact,
440 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
442 /* FIXME: not implemented */
446 contact_widget_groups_setup (EmpathyContactWidget *information)
448 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
450 information->manager = empathy_contact_manager_dup_singleton ();
451 contact_widget_model_setup (information);
456 contact_widget_groups_update (EmpathyContactWidget *information)
458 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
459 information->contact)
461 g_signal_connect_swapped (information->contact, "notify::groups",
462 G_CALLBACK (contact_widget_groups_notify_cb), information);
463 contact_widget_groups_populate_data (information);
465 gtk_widget_show (information->vbox_groups);
468 gtk_widget_hide (information->vbox_groups);
471 /* Converts the Location's GHashTable's key to a user readable string */
473 location_key_to_label (const gchar *key)
475 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
476 return _("Country ISO Code:");
477 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
478 return _("Country:");
479 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
481 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
483 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
485 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
486 return _("Postal Code:");
487 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
489 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
490 return _("Building:");
491 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
493 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
495 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
497 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
498 return _("Description:");
499 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
501 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
502 return _("Accuracy Level:");
503 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
505 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
506 return _("Vertical Error (meters):");
507 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
508 return _("Horizontal Error (meters):");
509 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
511 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
512 return _("Bearing:");
513 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
514 return _("Climb Speed:");
515 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
516 return _("Last Updated on:");
517 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
518 return _("Longitude:");
519 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
520 return _("Latitude:");
521 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
522 return _("Altitude:");
525 DEBUG ("Unexpected Location key: %s", key);
531 contact_widget_location_update (EmpathyContactWidget *information)
533 GHashTable *location;
535 gdouble lat = 0.0, lon = 0.0;
536 gboolean has_position = TRUE;
539 static const gchar* ordered_geolocation_keys[] = {
540 EMPATHY_LOCATION_TEXT,
541 EMPATHY_LOCATION_URI,
542 EMPATHY_LOCATION_DESCRIPTION,
543 EMPATHY_LOCATION_BUILDING,
544 EMPATHY_LOCATION_FLOOR,
545 EMPATHY_LOCATION_ROOM,
546 EMPATHY_LOCATION_STREET,
547 EMPATHY_LOCATION_AREA,
548 EMPATHY_LOCATION_LOCALITY,
549 EMPATHY_LOCATION_REGION,
550 EMPATHY_LOCATION_COUNTRY,
555 gboolean display_map = FALSE;
557 if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
559 gtk_widget_hide (information->vbox_location);
563 location = empathy_contact_get_location (information->contact);
564 if (location == NULL || g_hash_table_size (location) == 0)
566 gtk_widget_hide (information->vbox_location);
570 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
572 has_position = FALSE;
574 lat = g_value_get_double (value);
576 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
578 has_position = FALSE;
580 lon = g_value_get_double (value);
582 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
585 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
586 gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
596 stamp = g_value_get_int64 (value);
599 user_date = empathy_time_to_string_relative (time_);
601 text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
602 gtk_label_set_markup (GTK_LABEL (information->label_location), text);
607 /* Prepare the location information table */
608 if (information->table_location != NULL)
610 gtk_widget_destroy (information->table_location);
613 information->table_location = gtk_table_new (1, 2, FALSE);
614 gtk_box_pack_start (GTK_BOX (information->subvbox_location),
615 information->table_location, FALSE, FALSE, 5);
618 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
620 const gchar* user_label;
624 gvalue = g_hash_table_lookup (location, (gpointer) skey);
628 user_label = location_key_to_label (skey);
630 label = gtk_label_new (user_label);
631 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
632 gtk_table_attach (GTK_TABLE (information->table_location),
633 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
634 gtk_widget_show (label);
636 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
639 dvalue = g_value_get_double (gvalue);
640 svalue = g_strdup_printf ("%f", dvalue);
642 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
644 svalue = g_value_dup_string (gvalue);
646 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
650 time_ = g_value_get_int64 (value);
651 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
656 label = gtk_label_new (svalue);
657 gtk_table_attach_defaults (GTK_TABLE (information->table_location),
658 label, 1, 2, row, row + 1);
659 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
660 gtk_widget_show (label);
662 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
663 gtk_label_set_selectable (GTK_LABEL (label), TRUE);
670 #if HAVE_LIBCHAMPLAIN
672 !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
674 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
682 /* We can display some fields */
683 gtk_widget_show (information->table_location);
685 else if (!display_map)
687 /* Can't display either fields or map */
688 gtk_widget_hide (information->vbox_location);
692 #if HAVE_LIBCHAMPLAIN
695 ClutterActor *marker;
696 ChamplainLayer *layer;
698 information->map_view_embed = gtk_champlain_embed_new ();
699 information->map_view = gtk_champlain_embed_get_view (
700 GTK_CHAMPLAIN_EMBED (information->map_view_embed));
702 gtk_container_add (GTK_CONTAINER (information->viewport_map),
703 information->map_view_embed);
704 g_object_set (G_OBJECT (information->map_view), "show-license", FALSE,
705 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
708 layer = champlain_layer_new ();
709 champlain_view_add_layer (information->map_view, layer);
711 marker = champlain_marker_new_with_text (
712 empathy_contact_get_name (information->contact), NULL, NULL, NULL);
713 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
714 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
716 champlain_view_center_on (information->map_view, lat, lon);
717 gtk_widget_show_all (information->viewport_map);
721 gtk_widget_show (information->vbox_location);
725 save_avatar_menu_activate_cb (GtkWidget *widget,
726 EmpathyContactWidget *information)
729 EmpathyAvatar *avatar;
730 gchar *ext = NULL, *filename;
732 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
734 GTK_FILE_CHOOSER_ACTION_SAVE,
735 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
736 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
739 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
742 /* look for the avatar extension */
743 avatar = empathy_contact_get_avatar (information->contact);
744 if (avatar->format != NULL)
748 splitted = g_strsplit (avatar->format, "/", 2);
749 if (splitted[0] != NULL && splitted[1] != NULL)
750 ext = g_strdup (splitted[1]);
752 g_strfreev (splitted);
756 /* Avatar was loaded from the cache so was converted to PNG */
757 ext = g_strdup ("png");
764 id = tp_escape_as_identifier (empathy_contact_get_id (
765 information->contact));
767 filename = g_strdup_printf ("%s.%s", id, ext);
768 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
775 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
777 GError *error = NULL;
779 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
781 if (!empathy_avatar_save_to_file (avatar, filename, &error))
784 GtkWidget *error_dialog;
786 error_dialog = gtk_message_dialog_new (NULL, 0,
787 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
788 _("Unable to save avatar"));
790 gtk_message_dialog_format_secondary_text (
791 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
793 g_signal_connect (error_dialog, "response",
794 G_CALLBACK (gtk_widget_destroy), NULL);
796 gtk_window_present (GTK_WINDOW (error_dialog));
798 g_clear_error (&error);
804 gtk_widget_destroy (dialog);
808 popup_avatar_menu (EmpathyContactWidget *information,
810 GdkEventButton *event)
812 GtkWidget *menu, *item;
813 gint button, event_time;
815 if (information->contact == NULL ||
816 empathy_contact_get_avatar (information->contact) == NULL)
819 menu = gtk_menu_new ();
821 /* Add "Save as..." entry */
822 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
823 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
824 gtk_widget_show (item);
826 g_signal_connect (item, "activate",
827 G_CALLBACK (save_avatar_menu_activate_cb), information);
831 button = event->button;
832 event_time = event->time;
837 event_time = gtk_get_current_event_time ();
840 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
842 g_object_ref_sink (menu);
843 g_object_unref (menu);
847 widget_avatar_popup_menu_cb (GtkWidget *widget,
848 EmpathyContactWidget *information)
850 popup_avatar_menu (information, widget, NULL);
856 widget_avatar_button_press_event_cb (GtkWidget *widget,
857 GdkEventButton *event,
858 EmpathyContactWidget *information)
860 /* Ignore double-clicks and triple-clicks */
861 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
863 popup_avatar_menu (information, widget, event);
871 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
872 EmpathyContactWidget *information)
876 const gchar *mime_type;
878 empathy_avatar_chooser_get_image_data (
879 EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
880 &data, &size, &mime_type);
881 empathy_tp_contact_factory_set_avatar (information->factory,
882 data, size, mime_type);
886 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
887 GdkEventFocus *event,
888 EmpathyContactWidget *information)
890 if (information->contact)
894 alias = gtk_entry_get_text (GTK_ENTRY (editable));
895 empathy_tp_contact_factory_set_alias (information->factory,
896 information->contact, alias);
903 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
904 EmpathyAvatarChooser *avatar_chooser)
906 TpConnection *connection;
908 connection = empathy_account_chooser_get_connection (account_chooser);
909 g_object_set (avatar_chooser, "connection", connection, NULL);
913 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
915 EmpathyAvatar *avatar = NULL;
917 if (information->contact)
918 avatar = empathy_contact_get_avatar (information->contact);
920 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
922 g_signal_handlers_block_by_func (information->widget_avatar,
923 contact_widget_avatar_changed_cb,
925 empathy_avatar_chooser_set (
926 EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
927 g_signal_handlers_unblock_by_func (information->widget_avatar,
928 contact_widget_avatar_changed_cb, information);
931 empathy_avatar_image_set (
932 EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
936 contact_widget_name_notify_cb (EmpathyContactWidget *information)
938 if (GTK_IS_ENTRY (information->widget_alias))
939 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
940 empathy_contact_get_name (information->contact));
942 gtk_label_set_label (GTK_LABEL (information->widget_alias),
943 empathy_contact_get_name (information->contact));
947 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
949 gtk_label_set_text (GTK_LABEL (information->label_status),
950 empathy_contact_get_status (information->contact));
951 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
952 empathy_icon_name_for_contact (information->contact),
953 GTK_ICON_SIZE_BUTTON);
954 gtk_widget_show (information->image_state);
958 contact_widget_remove_contact (EmpathyContactWidget *information)
960 if (information->contact)
962 g_signal_handlers_disconnect_by_func (information->contact,
963 contact_widget_name_notify_cb, information);
964 g_signal_handlers_disconnect_by_func (information->contact,
965 contact_widget_presence_notify_cb, information);
966 g_signal_handlers_disconnect_by_func (information->contact,
967 contact_widget_avatar_notify_cb, information);
968 g_signal_handlers_disconnect_by_func (information->contact,
969 contact_widget_groups_notify_cb, information);
971 g_object_unref (information->contact);
972 g_object_unref (information->factory);
973 information->contact = NULL;
974 information->factory = NULL;
978 static void contact_widget_change_contact (EmpathyContactWidget *information);
981 contact_widget_contact_update (EmpathyContactWidget *information)
983 TpAccount *account = NULL;
984 const gchar *id = NULL;
986 /* Connect and get info from new contact */
987 if (information->contact)
989 g_signal_connect_swapped (information->contact, "notify::name",
990 G_CALLBACK (contact_widget_name_notify_cb), information);
991 g_signal_connect_swapped (information->contact, "notify::presence",
992 G_CALLBACK (contact_widget_presence_notify_cb), information);
993 g_signal_connect_swapped (information->contact,
994 "notify::presence-message",
995 G_CALLBACK (contact_widget_presence_notify_cb), information);
996 g_signal_connect_swapped (information->contact, "notify::avatar",
997 G_CALLBACK (contact_widget_avatar_notify_cb), information);
999 account = empathy_contact_get_account (information->contact);
1000 id = empathy_contact_get_id (information->contact);
1003 /* Update account widget */
1004 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1008 g_signal_handlers_block_by_func (information->widget_account,
1009 contact_widget_change_contact,
1011 empathy_account_chooser_set_account (
1012 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1013 g_signal_handlers_unblock_by_func (information->widget_account,
1014 contact_widget_change_contact, information);
1023 name = tp_account_get_display_name (account);
1024 gtk_label_set_label (GTK_LABEL (information->label_account), name);
1026 name = tp_account_get_icon_name (account);
1027 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1028 name, GTK_ICON_SIZE_MENU);
1032 /* Update id widget */
1033 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1034 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1036 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1038 /* Update other widgets */
1039 if (information->contact)
1041 contact_widget_name_notify_cb (information);
1042 contact_widget_presence_notify_cb (information);
1043 contact_widget_avatar_notify_cb (information);
1045 gtk_widget_show (information->label_alias);
1046 gtk_widget_show (information->widget_alias);
1047 gtk_widget_show (information->hbox_presence);
1048 gtk_widget_show (information->widget_avatar);
1052 gtk_widget_hide (information->label_alias);
1053 gtk_widget_hide (information->widget_alias);
1054 gtk_widget_hide (information->hbox_presence);
1055 gtk_widget_hide (information->widget_avatar);
1060 contact_widget_set_contact (EmpathyContactWidget *information,
1061 EmpathyContact *contact)
1063 if (contact == information->contact)
1066 contact_widget_remove_contact (information);
1069 TpConnection *connection;
1071 connection = empathy_contact_get_connection (contact);
1072 information->contact = g_object_ref (contact);
1073 information->factory = empathy_tp_contact_factory_dup_singleton (connection);
1076 /* set the selected account to be the account this contact came from */
1077 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1078 empathy_account_chooser_set_account (
1079 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1080 empathy_contact_get_account (contact));
1083 /* Update information for widgets */
1084 contact_widget_contact_update (information);
1085 contact_widget_groups_update (information);
1086 contact_widget_details_update (information);
1087 contact_widget_client_update (information);
1088 contact_widget_location_update (information);
1092 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
1093 EmpathyContact *contact,
1094 const GError *error,
1096 GObject *weak_object)
1098 EmpathyContactWidget *information = user_data;
1102 DEBUG ("Error: %s", error->message);
1106 contact_widget_set_contact (information, contact);
1110 contact_widget_change_contact (EmpathyContactWidget *information)
1112 EmpathyTpContactFactory *factory;
1113 TpConnection *connection;
1115 connection = empathy_account_chooser_get_connection (
1116 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1120 factory = empathy_tp_contact_factory_dup_singleton (connection);
1121 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1125 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1126 if (!EMP_STR_EMPTY (id))
1128 empathy_tp_contact_factory_get_from_id (factory, id,
1129 contact_widget_got_contact_cb, information, NULL,
1130 G_OBJECT (information->vbox_contact_widget));
1135 empathy_tp_contact_factory_get_from_handle (factory,
1136 tp_connection_get_self_handle (connection),
1137 contact_widget_got_contact_cb, information, NULL,
1138 G_OBJECT (information->vbox_contact_widget));
1141 g_object_unref (factory);
1145 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1147 contact_widget_change_contact (self);
1152 contact_widget_id_changed_cb (GtkEntry *entry,
1153 EmpathyContactWidget *self)
1155 if (self->widget_id_timeout != 0)
1157 g_source_remove (self->widget_id_timeout);
1160 self->widget_id_timeout =
1161 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1162 (GSourceFunc) contact_widget_id_activate_timeout, self);
1166 contact_widget_id_focus_out_cb (GtkWidget *widget,
1167 GdkEventFocus *event,
1168 EmpathyContactWidget *information)
1170 contact_widget_change_contact (information);
1175 contact_widget_contact_setup (EmpathyContactWidget *information)
1177 /* Setup label_status as a KludgeLabel */
1178 information->label_status = empathy_kludge_label_new ("");
1179 gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1180 PANGO_WRAP_WORD_CHAR);
1181 gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1184 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1185 gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1187 gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1188 information->label_status, TRUE, TRUE, 0);
1189 gtk_widget_show (information->label_status);
1191 /* Setup account label/chooser */
1192 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1194 information->widget_account = empathy_account_chooser_new ();
1196 g_signal_connect_swapped (information->widget_account, "changed",
1197 G_CALLBACK (contact_widget_change_contact),
1202 /* Pack the protocol icon with the account name in an hbox */
1203 information->widget_account = gtk_hbox_new (FALSE, 6);
1205 information->label_account = gtk_label_new (NULL);
1206 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1207 gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1209 gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1210 gtk_widget_show (information->label_account);
1212 information->image_account = gtk_image_new ();
1213 gtk_widget_show (information->image_account);
1215 gtk_box_pack_start (GTK_BOX (information->widget_account),
1216 information->image_account, FALSE, FALSE, 0);
1217 gtk_box_pack_start (GTK_BOX (information->widget_account),
1218 information->label_account, FALSE, TRUE, 0);
1220 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1221 information->widget_account,
1223 gtk_widget_show (information->widget_account);
1225 /* Set up avatar chooser/display */
1226 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1228 information->widget_avatar = empathy_avatar_chooser_new ();
1229 g_signal_connect (information->widget_avatar, "changed",
1230 G_CALLBACK (contact_widget_avatar_changed_cb),
1232 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1234 g_signal_connect (information->widget_account, "changed",
1235 G_CALLBACK (update_avatar_chooser_account_cb),
1236 information->widget_avatar);
1237 update_avatar_chooser_account_cb (
1238 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1239 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1244 information->widget_avatar = empathy_avatar_image_new ();
1246 g_signal_connect (information->widget_avatar, "popup-menu",
1247 G_CALLBACK (widget_avatar_popup_menu_cb), information);
1248 g_signal_connect (information->widget_avatar, "button-press-event",
1249 G_CALLBACK (widget_avatar_button_press_event_cb), information);
1252 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1253 information->widget_avatar,
1256 gtk_widget_show (information->widget_avatar);
1258 /* Setup id label/entry */
1259 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1261 information->widget_id = gtk_entry_new ();
1262 g_signal_connect (information->widget_id, "focus-out-event",
1263 G_CALLBACK (contact_widget_id_focus_out_cb),
1265 g_signal_connect (information->widget_id, "changed",
1266 G_CALLBACK (contact_widget_id_changed_cb),
1271 information->widget_id = gtk_label_new (NULL);
1272 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1273 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1275 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1277 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1278 information->widget_id,
1280 gtk_widget_show (information->widget_id);
1282 /* Setup alias label/entry */
1283 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1285 information->widget_alias = gtk_entry_new ();
1287 if (!(information->flags & EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS))
1288 g_signal_connect (information->widget_alias, "focus-out-event",
1289 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1292 /* Make return activate the window default (the Close button) */
1293 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1298 information->widget_alias = gtk_label_new (NULL);
1299 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1300 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1302 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1304 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1305 information->widget_alias,
1307 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1308 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1310 gtk_widget_show (information->widget_alias);
1314 contact_widget_destroy_cb (GtkWidget *widget,
1315 EmpathyContactWidget *information)
1317 contact_widget_remove_contact (information);
1319 if (information->widget_id_timeout != 0)
1321 g_source_remove (information->widget_id_timeout);
1323 if (information->manager)
1325 g_object_unref (information->manager);
1328 g_slice_free (EmpathyContactWidget, information);
1332 * empathy_contact_widget_new:
1333 * @contact: an #EmpathyContact
1334 * @flags: #EmpathyContactWidgetFlags for the new contact widget
1336 * Creates a new #EmpathyContactWidget.
1338 * Return value: a new #EmpathyContactWidget
1341 empathy_contact_widget_new (EmpathyContact *contact,
1342 EmpathyContactWidgetFlags flags)
1344 EmpathyContactWidget *information;
1348 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1350 information = g_slice_new0 (EmpathyContactWidget);
1351 information->flags = flags;
1353 filename = empathy_file_lookup ("empathy-contact-widget.ui",
1355 gui = empathy_builder_get_file (filename,
1356 "vbox_contact_widget", &information->vbox_contact_widget,
1357 "hbox_contact", &information->hbox_contact,
1358 "hbox_presence", &information->hbox_presence,
1359 "label_alias", &information->label_alias,
1360 "image_state", &information->image_state,
1361 "table_contact", &information->table_contact,
1362 "vbox_avatar", &information->vbox_avatar,
1363 "vbox_location", &information->vbox_location,
1364 "subvbox_location", &information->subvbox_location,
1365 "label_location", &information->label_location,
1366 #if HAVE_LIBCHAMPLAIN
1367 "viewport_map", &information->viewport_map,
1369 "vbox_groups", &information->vbox_groups,
1370 "entry_group", &information->entry_group,
1371 "button_group", &information->button_group,
1372 "treeview_groups", &information->treeview_groups,
1373 "vbox_details", &information->vbox_details,
1374 "table_details", &information->table_details,
1375 "hbox_details_requested", &information->hbox_details_requested,
1376 "vbox_client", &information->vbox_client,
1377 "table_client", &information->table_client,
1378 "hbox_client_requested", &information->hbox_client_requested,
1382 empathy_builder_connect (gui, information,
1383 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1384 "entry_group", "changed", contact_widget_entry_group_changed_cb,
1385 "entry_group", "activate", contact_widget_entry_group_activate_cb,
1386 "button_group", "clicked", contact_widget_button_group_clicked_cb,
1388 information->table_location = NULL;
1390 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1391 "EmpathyContactWidget",
1394 /* Create widgets */
1395 contact_widget_contact_setup (information);
1396 contact_widget_groups_setup (information);
1397 contact_widget_details_setup (information);
1398 contact_widget_client_setup (information);
1400 if (contact != NULL)
1401 contact_widget_set_contact (information, contact);
1402 else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1403 information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1404 contact_widget_change_contact (information);
1406 return empathy_builder_unref_and_keep_widget (gui,
1407 information->vbox_contact_widget);
1411 * empathy_contact_widget_get_contact:
1412 * @widget: an #EmpathyContactWidget
1414 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1416 * Returns: the #EmpathyContact associated with @widget
1419 empathy_contact_widget_get_contact (GtkWidget *widget)
1421 EmpathyContactWidget *information;
1423 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1425 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1429 return information->contact;
1433 empathy_contact_widget_get_alias (GtkWidget *widget)
1435 EmpathyContactWidget *information;
1437 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1439 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1443 return gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
1447 * empathy_contact_widget_set_contact:
1448 * @widget: an #EmpathyContactWidget
1449 * @contact: a different #EmpathyContact
1451 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1454 empathy_contact_widget_set_contact (GtkWidget *widget,
1455 EmpathyContact *contact)
1457 EmpathyContactWidget *information;
1459 g_return_if_fail (GTK_IS_WIDGET (widget));
1460 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1462 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1466 contact_widget_set_contact (information, contact);
1470 * empathy_contact_widget_set_account_filter:
1471 * @widget: an #EmpathyContactWidget
1472 * @filter: a #EmpathyAccountChooserFilterFunc
1473 * @user_data: user data to pass to @filter, or %NULL
1475 * Set a filter on the #EmpathyAccountChooser included in the
1476 * #EmpathyContactWidget.
1479 empathy_contact_widget_set_account_filter (
1481 EmpathyAccountChooserFilterFunc filter,
1484 EmpathyContactWidget *information;
1485 EmpathyAccountChooser *chooser;
1487 g_return_if_fail (GTK_IS_WIDGET (widget));
1489 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1493 chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
1495 empathy_account_chooser_set_filter (chooser, filter, user_data);