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,
556 if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
558 gtk_widget_hide (information->vbox_location);
562 location = empathy_contact_get_location (information->contact);
563 if (location == NULL || g_hash_table_size (location) == 0)
565 gtk_widget_hide (information->vbox_location);
569 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
571 has_position = FALSE;
573 lat = g_value_get_double (value);
575 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
577 has_position = FALSE;
579 lon = g_value_get_double (value);
581 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
584 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
585 gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
595 stamp = g_value_get_int64 (value);
598 user_date = empathy_time_to_string_relative (time_);
600 text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
601 gtk_label_set_markup (GTK_LABEL (information->label_location), text);
606 /* Prepare the location information table */
607 if (information->table_location != NULL)
609 gtk_widget_destroy (information->table_location);
612 information->table_location = gtk_table_new (1, 2, FALSE);
613 gtk_box_pack_start (GTK_BOX (information->subvbox_location),
614 information->table_location, FALSE, FALSE, 5);
617 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
619 const gchar* user_label;
623 gvalue = g_hash_table_lookup (location, (gpointer) skey);
627 user_label = location_key_to_label (skey);
629 label = gtk_label_new (user_label);
630 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
631 gtk_table_attach (GTK_TABLE (information->table_location),
632 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
633 gtk_widget_show (label);
635 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
638 dvalue = g_value_get_double (gvalue);
639 svalue = g_strdup_printf ("%f", dvalue);
641 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
643 svalue = g_value_dup_string (gvalue);
645 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
649 time_ = g_value_get_int64 (value);
650 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
655 label = gtk_label_new (svalue);
656 gtk_table_attach_defaults (GTK_TABLE (information->table_location),
657 label, 1, 2, row, row + 1);
658 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
659 gtk_widget_show (label);
661 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
662 gtk_label_set_selectable (GTK_LABEL (label), TRUE);
671 gtk_widget_hide (information->vbox_location);
675 gtk_widget_show (information->table_location);
677 #if HAVE_LIBCHAMPLAIN
678 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
682 !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
684 ClutterActor *marker;
685 ChamplainLayer *layer;
687 information->map_view_embed = gtk_champlain_embed_new ();
688 information->map_view = gtk_champlain_embed_get_view (
689 GTK_CHAMPLAIN_EMBED (information->map_view_embed));
691 gtk_container_add (GTK_CONTAINER (information->viewport_map),
692 information->map_view_embed);
693 g_object_set (G_OBJECT (information->map_view), "show-license", FALSE,
694 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
697 layer = champlain_layer_new ();
698 champlain_view_add_layer (information->map_view, layer);
700 marker = champlain_marker_new_with_text (
701 empathy_contact_get_name (information->contact), NULL, NULL, NULL);
702 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
703 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
705 champlain_view_center_on (information->map_view, lat, lon);
706 gtk_widget_show_all (information->viewport_map);
710 gtk_widget_show (information->vbox_location);
714 save_avatar_menu_activate_cb (GtkWidget *widget,
715 EmpathyContactWidget *information)
718 EmpathyAvatar *avatar;
719 gchar *ext = NULL, *filename;
721 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
723 GTK_FILE_CHOOSER_ACTION_SAVE,
724 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
725 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
728 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
731 /* look for the avatar extension */
732 avatar = empathy_contact_get_avatar (information->contact);
733 if (avatar->format != NULL)
737 splitted = g_strsplit (avatar->format, "/", 2);
738 if (splitted[0] != NULL && splitted[1] != NULL)
739 ext = g_strdup (splitted[1]);
741 g_strfreev (splitted);
745 /* Avatar was loaded from the cache so was converted to PNG */
746 ext = g_strdup ("png");
753 id = tp_escape_as_identifier (empathy_contact_get_id (
754 information->contact));
756 filename = g_strdup_printf ("%s.%s", id, ext);
757 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
764 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
766 GError *error = NULL;
768 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
770 if (!empathy_avatar_save_to_file (avatar, filename, &error))
773 GtkWidget *error_dialog;
775 error_dialog = gtk_message_dialog_new (NULL, 0,
776 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
777 _("Unable to save avatar"));
779 gtk_message_dialog_format_secondary_text (
780 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
782 g_signal_connect (error_dialog, "response",
783 G_CALLBACK (gtk_widget_destroy), NULL);
785 gtk_window_present (GTK_WINDOW (error_dialog));
787 g_clear_error (&error);
793 gtk_widget_destroy (dialog);
797 popup_avatar_menu (EmpathyContactWidget *information,
799 GdkEventButton *event)
801 GtkWidget *menu, *item;
802 gint button, event_time;
804 if (information->contact == NULL ||
805 empathy_contact_get_avatar (information->contact) == NULL)
808 menu = gtk_menu_new ();
810 /* Add "Save as..." entry */
811 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
812 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
813 gtk_widget_show (item);
815 g_signal_connect (item, "activate",
816 G_CALLBACK (save_avatar_menu_activate_cb), information);
820 button = event->button;
821 event_time = event->time;
826 event_time = gtk_get_current_event_time ();
829 gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
830 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
835 widget_avatar_popup_menu_cb (GtkWidget *widget,
836 EmpathyContactWidget *information)
838 popup_avatar_menu (information, widget, NULL);
844 widget_avatar_button_press_event_cb (GtkWidget *widget,
845 GdkEventButton *event,
846 EmpathyContactWidget *information)
848 /* Ignore double-clicks and triple-clicks */
849 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
851 popup_avatar_menu (information, widget, event);
859 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
860 EmpathyContactWidget *information)
864 const gchar *mime_type;
866 empathy_avatar_chooser_get_image_data (
867 EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
868 &data, &size, &mime_type);
869 empathy_tp_contact_factory_set_avatar (information->factory,
870 data, size, mime_type);
874 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
875 GdkEventFocus *event,
876 EmpathyContactWidget *information)
878 if (information->contact)
882 alias = gtk_entry_get_text (GTK_ENTRY (editable));
883 empathy_tp_contact_factory_set_alias (information->factory,
884 information->contact, alias);
891 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
892 EmpathyAvatarChooser *avatar_chooser)
894 TpConnection *connection;
896 connection = empathy_account_chooser_get_connection (account_chooser);
897 g_object_set (avatar_chooser, "connection", connection, NULL);
901 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
903 EmpathyAvatar *avatar = NULL;
905 if (information->contact)
906 avatar = empathy_contact_get_avatar (information->contact);
908 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
910 g_signal_handlers_block_by_func (information->widget_avatar,
911 contact_widget_avatar_changed_cb,
913 empathy_avatar_chooser_set (
914 EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
915 g_signal_handlers_unblock_by_func (information->widget_avatar,
916 contact_widget_avatar_changed_cb, information);
919 empathy_avatar_image_set (
920 EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
924 contact_widget_name_notify_cb (EmpathyContactWidget *information)
926 if (GTK_IS_ENTRY (information->widget_alias))
927 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
928 empathy_contact_get_name (information->contact));
930 gtk_label_set_label (GTK_LABEL (information->widget_alias),
931 empathy_contact_get_name (information->contact));
935 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
937 gtk_label_set_text (GTK_LABEL (information->label_status),
938 empathy_contact_get_status (information->contact));
939 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
940 empathy_icon_name_for_contact (information->contact),
941 GTK_ICON_SIZE_BUTTON);
942 gtk_widget_show (information->image_state);
946 contact_widget_remove_contact (EmpathyContactWidget *information)
948 if (information->contact)
950 g_signal_handlers_disconnect_by_func (information->contact,
951 contact_widget_name_notify_cb, information);
952 g_signal_handlers_disconnect_by_func (information->contact,
953 contact_widget_presence_notify_cb, information);
954 g_signal_handlers_disconnect_by_func (information->contact,
955 contact_widget_avatar_notify_cb, information);
956 g_signal_handlers_disconnect_by_func (information->contact,
957 contact_widget_groups_notify_cb, information);
959 g_object_unref (information->contact);
960 g_object_unref (information->factory);
961 information->contact = NULL;
962 information->factory = NULL;
966 static void contact_widget_change_contact (EmpathyContactWidget *information);
969 contact_widget_contact_update (EmpathyContactWidget *information)
971 TpAccount *account = NULL;
972 const gchar *id = NULL;
974 /* Connect and get info from new contact */
975 if (information->contact)
977 g_signal_connect_swapped (information->contact, "notify::name",
978 G_CALLBACK (contact_widget_name_notify_cb), information);
979 g_signal_connect_swapped (information->contact, "notify::presence",
980 G_CALLBACK (contact_widget_presence_notify_cb), information);
981 g_signal_connect_swapped (information->contact,
982 "notify::presence-message",
983 G_CALLBACK (contact_widget_presence_notify_cb), information);
984 g_signal_connect_swapped (information->contact, "notify::avatar",
985 G_CALLBACK (contact_widget_avatar_notify_cb), information);
987 account = empathy_contact_get_account (information->contact);
988 id = empathy_contact_get_id (information->contact);
991 /* Update account widget */
992 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
996 g_signal_handlers_block_by_func (information->widget_account,
997 contact_widget_change_contact,
999 empathy_account_chooser_set_account (
1000 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1001 g_signal_handlers_unblock_by_func (information->widget_account,
1002 contact_widget_change_contact, information);
1011 name = tp_account_get_display_name (account);
1012 gtk_label_set_label (GTK_LABEL (information->label_account), name);
1014 name = tp_account_get_icon_name (account);
1015 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1016 name, GTK_ICON_SIZE_MENU);
1020 /* Update id widget */
1021 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1022 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1024 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1026 /* Update other widgets */
1027 if (information->contact)
1029 contact_widget_name_notify_cb (information);
1030 contact_widget_presence_notify_cb (information);
1031 contact_widget_avatar_notify_cb (information);
1033 gtk_widget_show (information->label_alias);
1034 gtk_widget_show (information->widget_alias);
1035 gtk_widget_show (information->hbox_presence);
1036 gtk_widget_show (information->widget_avatar);
1040 gtk_widget_hide (information->label_alias);
1041 gtk_widget_hide (information->widget_alias);
1042 gtk_widget_hide (information->hbox_presence);
1043 gtk_widget_hide (information->widget_avatar);
1048 contact_widget_set_contact (EmpathyContactWidget *information,
1049 EmpathyContact *contact)
1051 if (contact == information->contact)
1054 contact_widget_remove_contact (information);
1057 TpConnection *connection;
1059 connection = empathy_contact_get_connection (contact);
1060 information->contact = g_object_ref (contact);
1061 information->factory = empathy_tp_contact_factory_dup_singleton (connection);
1064 /* set the selected account to be the account this contact came from */
1065 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1066 empathy_account_chooser_set_account (
1067 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1068 empathy_contact_get_account (contact));
1071 /* Update information for widgets */
1072 contact_widget_contact_update (information);
1073 contact_widget_groups_update (information);
1074 contact_widget_details_update (information);
1075 contact_widget_client_update (information);
1076 contact_widget_location_update (information);
1080 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
1081 EmpathyContact *contact,
1082 const GError *error,
1084 GObject *weak_object)
1086 EmpathyContactWidget *information = user_data;
1090 DEBUG ("Error: %s", error->message);
1094 contact_widget_set_contact (information, contact);
1098 contact_widget_change_contact (EmpathyContactWidget *information)
1100 EmpathyTpContactFactory *factory;
1101 TpConnection *connection;
1103 connection = empathy_account_chooser_get_connection (
1104 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1108 factory = empathy_tp_contact_factory_dup_singleton (connection);
1109 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1113 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1114 if (!EMP_STR_EMPTY (id))
1116 empathy_tp_contact_factory_get_from_id (factory, id,
1117 contact_widget_got_contact_cb, information, NULL,
1118 G_OBJECT (information->vbox_contact_widget));
1123 empathy_tp_contact_factory_get_from_handle (factory,
1124 tp_connection_get_self_handle (connection),
1125 contact_widget_got_contact_cb, information, NULL,
1126 G_OBJECT (information->vbox_contact_widget));
1129 g_object_unref (factory);
1133 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1135 contact_widget_change_contact (self);
1140 contact_widget_id_changed_cb (GtkEntry *entry,
1141 EmpathyContactWidget *self)
1143 if (self->widget_id_timeout != 0)
1145 g_source_remove (self->widget_id_timeout);
1148 self->widget_id_timeout =
1149 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1150 (GSourceFunc) contact_widget_id_activate_timeout, self);
1154 contact_widget_id_focus_out_cb (GtkWidget *widget,
1155 GdkEventFocus *event,
1156 EmpathyContactWidget *information)
1158 contact_widget_change_contact (information);
1163 contact_widget_contact_setup (EmpathyContactWidget *information)
1165 /* Setup label_status as a KludgeLabel */
1166 information->label_status = empathy_kludge_label_new ("");
1167 gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1168 PANGO_WRAP_WORD_CHAR);
1169 gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1172 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1173 gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1175 gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1176 information->label_status, TRUE, TRUE, 0);
1177 gtk_widget_show (information->label_status);
1179 /* Setup account label/chooser */
1180 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1182 information->widget_account = empathy_account_chooser_new ();
1184 g_signal_connect_swapped (information->widget_account, "changed",
1185 G_CALLBACK (contact_widget_change_contact),
1190 /* Pack the protocol icon with the account name in an hbox */
1191 information->widget_account = gtk_hbox_new (FALSE, 6);
1193 information->label_account = gtk_label_new (NULL);
1194 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1195 gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1197 gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1198 gtk_widget_show (information->label_account);
1200 information->image_account = gtk_image_new ();
1201 gtk_widget_show (information->image_account);
1203 gtk_box_pack_start (GTK_BOX (information->widget_account),
1204 information->image_account, FALSE, FALSE, 0);
1205 gtk_box_pack_start (GTK_BOX (information->widget_account),
1206 information->label_account, FALSE, TRUE, 0);
1208 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1209 information->widget_account,
1211 gtk_widget_show (information->widget_account);
1213 /* Set up avatar chooser/display */
1214 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1216 information->widget_avatar = empathy_avatar_chooser_new ();
1217 g_signal_connect (information->widget_avatar, "changed",
1218 G_CALLBACK (contact_widget_avatar_changed_cb),
1220 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1222 g_signal_connect (information->widget_account, "changed",
1223 G_CALLBACK (update_avatar_chooser_account_cb),
1224 information->widget_avatar);
1225 update_avatar_chooser_account_cb (
1226 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1227 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1232 information->widget_avatar = empathy_avatar_image_new ();
1234 g_signal_connect (information->widget_avatar, "popup-menu",
1235 G_CALLBACK (widget_avatar_popup_menu_cb), information);
1236 g_signal_connect (information->widget_avatar, "button-press-event",
1237 G_CALLBACK (widget_avatar_button_press_event_cb), information);
1240 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1241 information->widget_avatar,
1244 gtk_widget_show (information->widget_avatar);
1246 /* Setup id label/entry */
1247 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1249 information->widget_id = gtk_entry_new ();
1250 g_signal_connect (information->widget_id, "focus-out-event",
1251 G_CALLBACK (contact_widget_id_focus_out_cb),
1253 g_signal_connect (information->widget_id, "changed",
1254 G_CALLBACK (contact_widget_id_changed_cb),
1259 information->widget_id = gtk_label_new (NULL);
1260 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1261 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1263 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1265 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1266 information->widget_id,
1268 gtk_widget_show (information->widget_id);
1270 /* Setup alias label/entry */
1271 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1273 information->widget_alias = gtk_entry_new ();
1274 g_signal_connect (information->widget_alias, "focus-out-event",
1275 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1277 /* Make return activate the window default (the Close button) */
1278 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1283 information->widget_alias = gtk_label_new (NULL);
1284 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1285 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1287 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1289 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1290 information->widget_alias,
1292 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1293 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1295 gtk_widget_show (information->widget_alias);
1299 contact_widget_destroy_cb (GtkWidget *widget,
1300 EmpathyContactWidget *information)
1302 contact_widget_remove_contact (information);
1304 if (information->widget_id_timeout != 0)
1306 g_source_remove (information->widget_id_timeout);
1308 if (information->manager)
1310 g_object_unref (information->manager);
1313 g_slice_free (EmpathyContactWidget, information);
1317 * empathy_contact_widget_new:
1318 * @contact: an #EmpathyContact
1319 * @flags: #EmpathyContactWidgetFlags for the new contact widget
1321 * Creates a new #EmpathyContactWidget.
1323 * Return value: a new #EmpathyContactWidget
1326 empathy_contact_widget_new (EmpathyContact *contact,
1327 EmpathyContactWidgetFlags flags)
1329 EmpathyContactWidget *information;
1333 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1335 information = g_slice_new0 (EmpathyContactWidget);
1336 information->flags = flags;
1338 filename = empathy_file_lookup ("empathy-contact-widget.ui",
1340 gui = empathy_builder_get_file (filename,
1341 "vbox_contact_widget", &information->vbox_contact_widget,
1342 "hbox_contact", &information->hbox_contact,
1343 "hbox_presence", &information->hbox_presence,
1344 "label_alias", &information->label_alias,
1345 "image_state", &information->image_state,
1346 "table_contact", &information->table_contact,
1347 "vbox_avatar", &information->vbox_avatar,
1348 "vbox_location", &information->vbox_location,
1349 "subvbox_location", &information->subvbox_location,
1350 "label_location", &information->label_location,
1351 #if HAVE_LIBCHAMPLAIN
1352 "viewport_map", &information->viewport_map,
1354 "vbox_groups", &information->vbox_groups,
1355 "entry_group", &information->entry_group,
1356 "button_group", &information->button_group,
1357 "treeview_groups", &information->treeview_groups,
1358 "vbox_details", &information->vbox_details,
1359 "table_details", &information->table_details,
1360 "hbox_details_requested", &information->hbox_details_requested,
1361 "vbox_client", &information->vbox_client,
1362 "table_client", &information->table_client,
1363 "hbox_client_requested", &information->hbox_client_requested,
1367 empathy_builder_connect (gui, information,
1368 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1369 "entry_group", "changed", contact_widget_entry_group_changed_cb,
1370 "entry_group", "activate", contact_widget_entry_group_activate_cb,
1371 "button_group", "clicked", contact_widget_button_group_clicked_cb,
1373 information->table_location = NULL;
1375 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1376 "EmpathyContactWidget",
1379 /* Create widgets */
1380 contact_widget_contact_setup (information);
1381 contact_widget_groups_setup (information);
1382 contact_widget_details_setup (information);
1383 contact_widget_client_setup (information);
1385 if (contact != NULL)
1386 contact_widget_set_contact (information, contact);
1387 else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1388 information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1389 contact_widget_change_contact (information);
1391 return empathy_builder_unref_and_keep_widget (gui,
1392 information->vbox_contact_widget);
1396 * empathy_contact_widget_get_contact:
1397 * @widget: an #EmpathyContactWidget
1399 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1401 * Returns: the #EmpathyContact associated with @widget
1404 empathy_contact_widget_get_contact (GtkWidget *widget)
1406 EmpathyContactWidget *information;
1408 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1410 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1414 return information->contact;
1418 * empathy_contact_widget_set_contact:
1419 * @widget: an #EmpathyContactWidget
1420 * @contact: a different #EmpathyContact
1422 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1425 empathy_contact_widget_set_contact (GtkWidget *widget,
1426 EmpathyContact *contact)
1428 EmpathyContactWidget *information;
1430 g_return_if_fail (GTK_IS_WIDGET (widget));
1431 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1433 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1437 contact_widget_set_contact (information, contact);
1441 * empathy_contact_widget_set_account_filter:
1442 * @widget: an #EmpathyContactWidget
1443 * @filter: a #EmpathyAccountChooserFilterFunc
1444 * @user_data: user data to pass to @filter, or %NULL
1446 * Set a filter on the #EmpathyAccountChooser included in the
1447 * #EmpathyContactWidget.
1450 empathy_contact_widget_set_account_filter (
1452 EmpathyAccountChooserFilterFunc filter,
1455 EmpathyContactWidget *information;
1456 EmpathyAccountChooser *chooser;
1458 g_return_if_fail (GTK_IS_WIDGET (widget));
1460 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1464 chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
1466 empathy_account_chooser_set_filter (chooser, filter, user_data);