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-string-parser.h"
51 #include "empathy-kludge-label.h"
53 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
54 #include <libempathy/empathy-debug.h>
57 * SECTION:empathy-contact-widget
58 * @title:EmpathyContactWidget
59 * @short_description: A widget used to display and edit details about a contact
60 * @include: libempathy-empathy-contact-widget.h
62 * #EmpathyContactWidget is a widget which displays appropriate widgets
63 * with details about a contact, also allowing changing these details,
68 * EmpathyContactWidget:
69 * @parent: parent object
71 * Widget which displays appropriate widgets with details about a contact,
72 * also allowing changing these details, if desired.
75 /* Delay before updating the widget when the id entry changed (seconds) */
76 #define ID_CHANGED_TIMEOUT 1
80 EmpathyTpContactFactory *factory;
81 EmpathyContactManager *manager;
82 EmpathyContact *contact;
83 EmpathyContactWidgetFlags flags;
84 guint widget_id_timeout;
87 GtkWidget *vbox_contact_widget;
90 GtkWidget *hbox_contact;
91 GtkWidget *widget_avatar;
92 GtkWidget *widget_account;
93 GtkWidget *image_account;
94 GtkWidget *label_account;
96 GtkWidget *widget_alias;
97 GtkWidget *label_alias;
98 GtkWidget *entry_alias;
99 GtkWidget *hbox_presence;
100 GtkWidget *image_state;
101 GtkWidget *label_status;
102 GtkWidget *table_contact;
103 GtkWidget *vbox_avatar;
104 GtkWidget *favourite_checkbox;
107 GtkWidget *vbox_location;
108 GtkWidget *subvbox_location;
109 GtkWidget *table_location;
110 GtkWidget *label_location;
111 #if HAVE_LIBCHAMPLAIN
112 GtkWidget *viewport_map;
113 GtkWidget *map_view_embed;
114 ChamplainView *map_view;
118 GtkWidget *vbox_groups;
119 GtkWidget *entry_group;
120 GtkWidget *button_group;
121 GtkWidget *treeview_groups;
124 GtkWidget *vbox_details;
125 GtkWidget *table_details;
126 GtkWidget *hbox_details_requested;
129 GtkWidget *vbox_client;
130 GtkWidget *table_client;
131 GtkWidget *hbox_client_requested;
132 } EmpathyContactWidget;
136 EmpathyContactWidget *information;
139 GtkTreeIter found_iter;
151 contact_widget_details_setup (EmpathyContactWidget *information)
153 /* FIXME: Needs new telepathy spec */
154 gtk_widget_hide (information->vbox_details);
158 contact_widget_details_update (EmpathyContactWidget *information)
160 /* FIXME: Needs new telepathy spec */
164 contact_widget_client_update (EmpathyContactWidget *information)
166 /* FIXME: Needs new telepathy spec */
170 contact_widget_client_setup (EmpathyContactWidget *information)
172 /* FIXME: Needs new telepathy spec */
173 gtk_widget_hide (information->vbox_client);
177 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
179 EmpathyContactWidget *information)
189 view = GTK_TREE_VIEW (information->treeview_groups);
190 model = gtk_tree_view_get_model (view);
191 store = GTK_LIST_STORE (model);
193 path = gtk_tree_path_new_from_string (path_string);
195 gtk_tree_model_get_iter (model, &iter, path);
196 gtk_tree_model_get (model, &iter,
197 COL_ENABLED, &enabled,
201 gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
202 gtk_tree_path_free (path);
208 empathy_contact_list_remove_from_group (
209 EMPATHY_CONTACT_LIST (information->manager), information->contact,
214 empathy_contact_list_add_to_group (
215 EMPATHY_CONTACT_LIST (information->manager), information->contact,
223 contact_widget_model_populate_columns (EmpathyContactWidget *information)
227 GtkTreeViewColumn *column;
228 GtkCellRenderer *renderer;
231 view = GTK_TREE_VIEW (information->treeview_groups);
232 model = gtk_tree_view_get_model (view);
234 renderer = gtk_cell_renderer_toggle_new ();
235 g_signal_connect (renderer, "toggled",
236 G_CALLBACK (contact_widget_cell_toggled), information);
238 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
239 "active", COL_ENABLED, NULL);
241 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
242 gtk_tree_view_column_set_fixed_width (column, 50);
243 gtk_tree_view_append_column (view, column);
245 renderer = gtk_cell_renderer_text_new ();
246 col_offset = gtk_tree_view_insert_column_with_attributes (view,
250 /* "editable", COL_EDITABLE, */
253 g_object_set_data (G_OBJECT (renderer),
254 "column", GINT_TO_POINTER (COL_NAME));
256 column = gtk_tree_view_get_column (view, col_offset - 1);
257 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
258 gtk_tree_view_column_set_resizable (column,FALSE);
259 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
263 contact_widget_model_setup (EmpathyContactWidget *information)
267 GtkTreeSelection *selection;
269 view = GTK_TREE_VIEW (information->treeview_groups);
271 store = gtk_list_store_new (COL_COUNT,
272 G_TYPE_STRING, /* name */
273 G_TYPE_BOOLEAN, /* enabled */
274 G_TYPE_BOOLEAN); /* editable */
276 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
278 selection = gtk_tree_view_get_selection (view);
279 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
281 contact_widget_model_populate_columns (information);
283 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
284 COL_NAME, GTK_SORT_ASCENDING);
286 g_object_unref (store);
290 contact_widget_groups_populate_data (EmpathyContactWidget *information)
295 GList *my_groups, *l;
298 view = GTK_TREE_VIEW (information->treeview_groups);
299 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
300 gtk_list_store_clear (store);
302 all_groups = empathy_contact_list_get_all_groups (
303 EMPATHY_CONTACT_LIST (information->manager));
304 my_groups = empathy_contact_list_get_groups (
305 EMPATHY_CONTACT_LIST (information->manager),
306 information->contact);
308 for (l = all_groups; l; l = l->next)
310 const gchar *group_str;
315 enabled = g_list_find_custom (my_groups,
316 group_str, (GCompareFunc) strcmp) != NULL;
318 gtk_list_store_append (store, &iter);
319 gtk_list_store_set (store, &iter,
322 COL_ENABLED, enabled,
326 g_list_foreach (all_groups, (GFunc) g_free, NULL);
327 g_list_foreach (my_groups, (GFunc) g_free, NULL);
328 g_list_free (all_groups);
329 g_list_free (my_groups);
333 contact_widget_model_find_name_foreach (GtkTreeModel *model,
340 gtk_tree_model_get (model, iter,
347 if (data->name && strcmp (data->name, name) == 0)
350 data->found_iter = *iter;
363 contact_widget_model_find_name (EmpathyContactWidget *information,
371 if (EMP_STR_EMPTY (name))
374 data.information = information;
378 view = GTK_TREE_VIEW (information->treeview_groups);
379 model = gtk_tree_view_get_model (view);
381 gtk_tree_model_foreach (model,
382 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
385 if (data.found == TRUE)
387 *iter = data.found_iter;
395 contact_widget_entry_group_changed_cb (GtkEditable *editable,
396 EmpathyContactWidget *information)
401 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
403 if (contact_widget_model_find_name (information, group, &iter))
404 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
406 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
407 !EMP_STR_EMPTY (group));
411 contact_widget_entry_group_activate_cb (GtkEntry *entry,
412 EmpathyContactWidget *information)
414 gtk_widget_activate (GTK_WIDGET (information->button_group));
418 contact_widget_button_group_clicked_cb (GtkButton *button,
419 EmpathyContactWidget *information)
426 view = GTK_TREE_VIEW (information->treeview_groups);
427 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
429 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
431 gtk_list_store_append (store, &iter);
432 gtk_list_store_set (store, &iter,
437 empathy_contact_list_add_to_group (
438 EMPATHY_CONTACT_LIST (information->manager), information->contact,
443 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
445 /* FIXME: not implemented */
449 contact_widget_groups_setup (EmpathyContactWidget *information)
451 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
453 contact_widget_model_setup (information);
458 contact_widget_groups_update (EmpathyContactWidget *information)
460 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
461 information->contact)
463 g_signal_connect_swapped (information->contact, "notify::groups",
464 G_CALLBACK (contact_widget_groups_notify_cb), information);
465 contact_widget_groups_populate_data (information);
467 gtk_widget_show (information->vbox_groups);
470 gtk_widget_hide (information->vbox_groups);
473 /* Converts the Location's GHashTable's key to a user readable string */
475 location_key_to_label (const gchar *key)
477 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
478 return _("Country ISO Code:");
479 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
480 return _("Country:");
481 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
483 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
485 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
487 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
488 return _("Postal Code:");
489 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
491 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
492 return _("Building:");
493 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
495 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
497 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
499 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
500 return _("Description:");
501 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
503 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
504 return _("Accuracy Level:");
505 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
507 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
508 return _("Vertical Error (meters):");
509 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
510 return _("Horizontal Error (meters):");
511 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
513 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
514 return _("Bearing:");
515 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
516 return _("Climb Speed:");
517 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
518 return _("Last Updated on:");
519 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
520 return _("Longitude:");
521 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
522 return _("Latitude:");
523 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
524 return _("Altitude:");
527 DEBUG ("Unexpected Location key: %s", key);
533 contact_widget_location_update (EmpathyContactWidget *information)
535 GHashTable *location;
537 gdouble lat = 0.0, lon = 0.0;
538 gboolean has_position = TRUE;
541 static const gchar* ordered_geolocation_keys[] = {
542 EMPATHY_LOCATION_TEXT,
543 EMPATHY_LOCATION_URI,
544 EMPATHY_LOCATION_DESCRIPTION,
545 EMPATHY_LOCATION_BUILDING,
546 EMPATHY_LOCATION_FLOOR,
547 EMPATHY_LOCATION_ROOM,
548 EMPATHY_LOCATION_STREET,
549 EMPATHY_LOCATION_AREA,
550 EMPATHY_LOCATION_LOCALITY,
551 EMPATHY_LOCATION_REGION,
552 EMPATHY_LOCATION_COUNTRY,
557 gboolean display_map = FALSE;
559 if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
561 gtk_widget_hide (information->vbox_location);
565 location = empathy_contact_get_location (information->contact);
566 if (location == NULL || g_hash_table_size (location) == 0)
568 gtk_widget_hide (information->vbox_location);
572 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
574 has_position = FALSE;
576 lat = g_value_get_double (value);
578 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
580 has_position = FALSE;
582 lon = g_value_get_double (value);
584 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
587 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
588 gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
598 stamp = g_value_get_int64 (value);
601 user_date = empathy_time_to_string_relative (time_);
603 text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
604 gtk_label_set_markup (GTK_LABEL (information->label_location), text);
609 /* Prepare the location information table */
610 if (information->table_location != NULL)
612 gtk_widget_destroy (information->table_location);
615 information->table_location = gtk_table_new (1, 2, FALSE);
616 gtk_box_pack_start (GTK_BOX (information->subvbox_location),
617 information->table_location, FALSE, FALSE, 5);
620 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
622 const gchar* user_label;
626 gvalue = g_hash_table_lookup (location, (gpointer) skey);
630 user_label = location_key_to_label (skey);
632 label = gtk_label_new (user_label);
633 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
634 gtk_table_attach (GTK_TABLE (information->table_location),
635 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
636 gtk_widget_show (label);
638 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
641 dvalue = g_value_get_double (gvalue);
642 svalue = g_strdup_printf ("%f", dvalue);
644 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
646 svalue = g_value_dup_string (gvalue);
648 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
652 time_ = g_value_get_int64 (value);
653 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
658 label = gtk_label_new (svalue);
659 gtk_table_attach_defaults (GTK_TABLE (information->table_location),
660 label, 1, 2, row, row + 1);
661 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
662 gtk_widget_show (label);
664 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
665 gtk_label_set_selectable (GTK_LABEL (label), TRUE);
672 #if HAVE_LIBCHAMPLAIN
674 !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
676 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
684 /* We can display some fields */
685 gtk_widget_show (information->table_location);
687 else if (!display_map)
689 /* Can't display either fields or map */
690 gtk_widget_hide (information->vbox_location);
694 #if HAVE_LIBCHAMPLAIN
697 ClutterActor *marker;
698 ChamplainLayer *layer;
700 information->map_view_embed = gtk_champlain_embed_new ();
701 information->map_view = gtk_champlain_embed_get_view (
702 GTK_CHAMPLAIN_EMBED (information->map_view_embed));
704 gtk_container_add (GTK_CONTAINER (information->viewport_map),
705 information->map_view_embed);
706 g_object_set (G_OBJECT (information->map_view),
707 "show-license", TRUE,
708 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
712 layer = champlain_layer_new ();
713 champlain_view_add_layer (information->map_view, layer);
715 marker = champlain_marker_new_with_text (
716 empathy_contact_get_name (information->contact), NULL, NULL, NULL);
717 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
718 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
720 champlain_view_center_on (information->map_view, lat, lon);
721 gtk_widget_show_all (information->viewport_map);
725 gtk_widget_show (information->vbox_location);
729 save_avatar_menu_activate_cb (GtkWidget *widget,
730 EmpathyContactWidget *information)
733 EmpathyAvatar *avatar;
734 gchar *ext = NULL, *filename;
736 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
738 GTK_FILE_CHOOSER_ACTION_SAVE,
739 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
740 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
743 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
746 /* look for the avatar extension */
747 avatar = empathy_contact_get_avatar (information->contact);
748 if (avatar->format != NULL)
752 splitted = g_strsplit (avatar->format, "/", 2);
753 if (splitted[0] != NULL && splitted[1] != NULL)
754 ext = g_strdup (splitted[1]);
756 g_strfreev (splitted);
760 /* Avatar was loaded from the cache so was converted to PNG */
761 ext = g_strdup ("png");
768 id = tp_escape_as_identifier (empathy_contact_get_id (
769 information->contact));
771 filename = g_strdup_printf ("%s.%s", id, ext);
772 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
779 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
781 GError *error = NULL;
783 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
785 if (!empathy_avatar_save_to_file (avatar, filename, &error))
788 GtkWidget *error_dialog;
790 error_dialog = gtk_message_dialog_new (NULL, 0,
791 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
792 _("Unable to save avatar"));
794 gtk_message_dialog_format_secondary_text (
795 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
797 g_signal_connect (error_dialog, "response",
798 G_CALLBACK (gtk_widget_destroy), NULL);
800 gtk_window_present (GTK_WINDOW (error_dialog));
802 g_clear_error (&error);
808 gtk_widget_destroy (dialog);
812 popup_avatar_menu (EmpathyContactWidget *information,
814 GdkEventButton *event)
816 GtkWidget *menu, *item;
817 gint button, event_time;
819 if (information->contact == NULL ||
820 empathy_contact_get_avatar (information->contact) == NULL)
823 menu = gtk_menu_new ();
825 /* Add "Save as..." entry */
826 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
827 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
828 gtk_widget_show (item);
830 g_signal_connect (item, "activate",
831 G_CALLBACK (save_avatar_menu_activate_cb), information);
835 button = event->button;
836 event_time = event->time;
841 event_time = gtk_get_current_event_time ();
844 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
846 g_object_ref_sink (menu);
847 g_object_unref (menu);
851 widget_avatar_popup_menu_cb (GtkWidget *widget,
852 EmpathyContactWidget *information)
854 popup_avatar_menu (information, widget, NULL);
860 widget_avatar_button_press_event_cb (GtkWidget *widget,
861 GdkEventButton *event,
862 EmpathyContactWidget *information)
864 /* Ignore double-clicks and triple-clicks */
865 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
867 popup_avatar_menu (information, widget, event);
875 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
876 EmpathyContactWidget *information)
880 const gchar *mime_type;
882 empathy_avatar_chooser_get_image_data (
883 EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
884 &data, &size, &mime_type);
885 empathy_tp_contact_factory_set_avatar (information->factory,
886 data, size, mime_type);
890 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
891 GdkEventFocus *event,
892 EmpathyContactWidget *information)
894 if (information->contact)
898 alias = gtk_entry_get_text (GTK_ENTRY (editable));
899 empathy_tp_contact_factory_set_alias (information->factory,
900 information->contact, alias);
907 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
908 EmpathyAvatarChooser *avatar_chooser)
910 TpConnection *connection;
912 connection = empathy_account_chooser_get_connection (account_chooser);
913 g_object_set (avatar_chooser, "connection", connection, NULL);
917 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
919 EmpathyAvatar *avatar = NULL;
921 if (information->contact)
922 avatar = empathy_contact_get_avatar (information->contact);
924 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
926 g_signal_handlers_block_by_func (information->widget_avatar,
927 contact_widget_avatar_changed_cb,
929 empathy_avatar_chooser_set (
930 EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
931 g_signal_handlers_unblock_by_func (information->widget_avatar,
932 contact_widget_avatar_changed_cb, information);
935 empathy_avatar_image_set (
936 EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
940 contact_widget_name_notify_cb (EmpathyContactWidget *information)
942 if (GTK_IS_ENTRY (information->widget_alias))
943 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
944 empathy_contact_get_name (information->contact));
946 gtk_label_set_label (GTK_LABEL (information->widget_alias),
947 empathy_contact_get_name (information->contact));
951 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
954 gchar *markup_text = NULL;
956 status = empathy_contact_get_status (information->contact);
958 markup_text = empathy_add_link_markup (status);
959 gtk_label_set_markup (GTK_LABEL (information->label_status), markup_text);
960 g_free (markup_text);
962 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
963 empathy_icon_name_for_contact (information->contact),
964 GTK_ICON_SIZE_BUTTON);
965 gtk_widget_show (information->image_state);
968 #if HAVE_FAVOURITE_CONTACTS
970 contact_widget_favourites_changed_cb (EmpathyContactManager *manager,
971 EmpathyContact *contact,
972 gboolean is_favourite,
973 EmpathyContactWidget *information)
975 if (contact != information->contact)
978 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
979 information->favourite_checkbox), is_favourite);
984 contact_widget_remove_contact (EmpathyContactWidget *information)
986 if (information->contact)
988 g_signal_handlers_disconnect_by_func (information->contact,
989 contact_widget_name_notify_cb, information);
990 g_signal_handlers_disconnect_by_func (information->contact,
991 contact_widget_presence_notify_cb, information);
992 g_signal_handlers_disconnect_by_func (information->contact,
993 contact_widget_avatar_notify_cb, information);
994 g_signal_handlers_disconnect_by_func (information->contact,
995 contact_widget_groups_notify_cb, information);
997 g_object_unref (information->contact);
998 g_object_unref (information->factory);
999 information->contact = NULL;
1000 information->factory = NULL;
1004 static void contact_widget_change_contact (EmpathyContactWidget *information);
1007 contact_widget_contact_update (EmpathyContactWidget *information)
1009 TpAccount *account = NULL;
1010 const gchar *id = NULL;
1012 /* Connect and get info from new contact */
1013 if (information->contact)
1015 g_signal_connect_swapped (information->contact, "notify::name",
1016 G_CALLBACK (contact_widget_name_notify_cb), information);
1017 g_signal_connect_swapped (information->contact, "notify::presence",
1018 G_CALLBACK (contact_widget_presence_notify_cb), information);
1019 g_signal_connect_swapped (information->contact,
1020 "notify::presence-message",
1021 G_CALLBACK (contact_widget_presence_notify_cb), information);
1022 g_signal_connect_swapped (information->contact, "notify::avatar",
1023 G_CALLBACK (contact_widget_avatar_notify_cb), information);
1025 account = empathy_contact_get_account (information->contact);
1026 id = empathy_contact_get_id (information->contact);
1029 /* Update account widget */
1030 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1034 g_signal_handlers_block_by_func (information->widget_account,
1035 contact_widget_change_contact,
1037 empathy_account_chooser_set_account (
1038 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1039 g_signal_handlers_unblock_by_func (information->widget_account,
1040 contact_widget_change_contact, information);
1049 name = tp_account_get_display_name (account);
1050 gtk_label_set_label (GTK_LABEL (information->label_account), name);
1052 name = tp_account_get_icon_name (account);
1053 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1054 name, GTK_ICON_SIZE_MENU);
1058 /* Update id widget */
1059 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1060 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1062 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1064 /* Update other widgets */
1065 if (information->contact)
1067 contact_widget_name_notify_cb (information);
1068 contact_widget_presence_notify_cb (information);
1069 contact_widget_avatar_notify_cb (information);
1071 #if HAVE_FAVOURITE_CONTACTS
1072 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1074 gboolean is_favourite;
1076 is_favourite = empathy_contact_list_is_favourite (
1077 EMPATHY_CONTACT_LIST (information->manager),
1078 information->contact);
1080 contact_widget_favourites_changed_cb (information->manager,
1081 information->contact, is_favourite, information);
1085 gtk_widget_show (information->label_alias);
1086 gtk_widget_show (information->widget_alias);
1087 gtk_widget_show (information->hbox_presence);
1088 gtk_widget_show (information->widget_avatar);
1092 gtk_widget_hide (information->label_alias);
1093 gtk_widget_hide (information->widget_alias);
1094 gtk_widget_hide (information->hbox_presence);
1095 gtk_widget_hide (information->widget_avatar);
1100 contact_widget_set_contact (EmpathyContactWidget *information,
1101 EmpathyContact *contact)
1103 if (contact == information->contact)
1106 contact_widget_remove_contact (information);
1109 TpConnection *connection;
1111 connection = empathy_contact_get_connection (contact);
1112 information->contact = g_object_ref (contact);
1113 information->factory = empathy_tp_contact_factory_dup_singleton (connection);
1116 /* set the selected account to be the account this contact came from */
1117 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1118 empathy_account_chooser_set_account (
1119 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1120 empathy_contact_get_account (contact));
1123 /* Update information for widgets */
1124 contact_widget_contact_update (information);
1125 contact_widget_groups_update (information);
1126 contact_widget_details_update (information);
1127 contact_widget_client_update (information);
1128 contact_widget_location_update (information);
1132 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
1133 EmpathyContact *contact,
1134 const GError *error,
1136 GObject *weak_object)
1138 EmpathyContactWidget *information = user_data;
1142 DEBUG ("Error: %s", error->message);
1146 contact_widget_set_contact (information, contact);
1150 contact_widget_change_contact (EmpathyContactWidget *information)
1152 EmpathyTpContactFactory *factory;
1153 TpConnection *connection;
1155 connection = empathy_account_chooser_get_connection (
1156 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1160 factory = empathy_tp_contact_factory_dup_singleton (connection);
1161 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1165 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1166 if (!EMP_STR_EMPTY (id))
1168 empathy_tp_contact_factory_get_from_id (factory, id,
1169 contact_widget_got_contact_cb, information, NULL,
1170 G_OBJECT (information->vbox_contact_widget));
1175 empathy_tp_contact_factory_get_from_handle (factory,
1176 tp_connection_get_self_handle (connection),
1177 contact_widget_got_contact_cb, information, NULL,
1178 G_OBJECT (information->vbox_contact_widget));
1181 g_object_unref (factory);
1185 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1187 contact_widget_change_contact (self);
1192 contact_widget_id_changed_cb (GtkEntry *entry,
1193 EmpathyContactWidget *self)
1195 if (self->widget_id_timeout != 0)
1197 g_source_remove (self->widget_id_timeout);
1200 self->widget_id_timeout =
1201 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1202 (GSourceFunc) contact_widget_id_activate_timeout, self);
1206 contact_widget_id_focus_out_cb (GtkWidget *widget,
1207 GdkEventFocus *event,
1208 EmpathyContactWidget *information)
1210 contact_widget_change_contact (information);
1214 #if HAVE_FAVOURITE_CONTACTS
1216 favourite_toggled_cb (GtkToggleButton *button,
1217 EmpathyContactWidget *information)
1221 active = gtk_toggle_button_get_active (button);
1225 empathy_contact_list_add_to_favourites (
1226 EMPATHY_CONTACT_LIST (information->manager), information->contact);
1230 empathy_contact_list_remove_from_favourites (
1231 EMPATHY_CONTACT_LIST (information->manager), information->contact);
1237 contact_widget_contact_setup (EmpathyContactWidget *information)
1239 /* Setup label_status as a KludgeLabel */
1240 information->label_status = empathy_kludge_label_new ("");
1241 gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1242 PANGO_WRAP_WORD_CHAR);
1243 gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1246 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1247 gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1249 gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1250 information->label_status, TRUE, TRUE, 0);
1251 gtk_widget_show (information->label_status);
1253 /* Setup account label/chooser */
1254 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1256 information->widget_account = empathy_account_chooser_new ();
1258 g_signal_connect_swapped (information->widget_account, "changed",
1259 G_CALLBACK (contact_widget_change_contact),
1264 /* Pack the protocol icon with the account name in an hbox */
1265 information->widget_account = gtk_hbox_new (FALSE, 6);
1267 information->label_account = gtk_label_new (NULL);
1268 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1269 gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1271 gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1272 gtk_widget_show (information->label_account);
1274 information->image_account = gtk_image_new ();
1275 gtk_widget_show (information->image_account);
1277 gtk_box_pack_start (GTK_BOX (information->widget_account),
1278 information->image_account, FALSE, FALSE, 0);
1279 gtk_box_pack_start (GTK_BOX (information->widget_account),
1280 information->label_account, FALSE, TRUE, 0);
1282 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1283 information->widget_account,
1285 gtk_widget_show (information->widget_account);
1287 /* Set up avatar chooser/display */
1288 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1290 information->widget_avatar = empathy_avatar_chooser_new ();
1291 g_signal_connect (information->widget_avatar, "changed",
1292 G_CALLBACK (contact_widget_avatar_changed_cb),
1294 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1296 g_signal_connect (information->widget_account, "changed",
1297 G_CALLBACK (update_avatar_chooser_account_cb),
1298 information->widget_avatar);
1299 update_avatar_chooser_account_cb (
1300 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1301 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1306 information->widget_avatar = empathy_avatar_image_new ();
1308 g_signal_connect (information->widget_avatar, "popup-menu",
1309 G_CALLBACK (widget_avatar_popup_menu_cb), information);
1310 g_signal_connect (information->widget_avatar, "button-press-event",
1311 G_CALLBACK (widget_avatar_button_press_event_cb), information);
1314 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1315 information->widget_avatar,
1318 gtk_widget_show (information->widget_avatar);
1320 /* Setup id label/entry */
1321 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1323 information->widget_id = gtk_entry_new ();
1324 g_signal_connect (information->widget_id, "focus-out-event",
1325 G_CALLBACK (contact_widget_id_focus_out_cb),
1327 g_signal_connect (information->widget_id, "changed",
1328 G_CALLBACK (contact_widget_id_changed_cb),
1333 information->widget_id = gtk_label_new (NULL);
1334 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1335 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1337 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1339 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1340 information->widget_id,
1342 gtk_widget_show (information->widget_id);
1344 /* Setup alias label/entry */
1345 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1347 information->widget_alias = gtk_entry_new ();
1349 if (!(information->flags & EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS))
1350 g_signal_connect (information->widget_alias, "focus-out-event",
1351 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1354 /* Make return activate the window default (the Close button) */
1355 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1360 information->widget_alias = gtk_label_new (NULL);
1361 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1362 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1364 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1366 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1367 information->widget_alias,
1369 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1370 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1372 gtk_widget_show (information->widget_alias);
1374 #if HAVE_FAVOURITE_CONTACTS
1376 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1378 information->favourite_checkbox = gtk_check_button_new_with_label (
1381 g_signal_connect (information->favourite_checkbox, "toggled",
1382 G_CALLBACK (favourite_toggled_cb), information);
1384 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1385 information->favourite_checkbox, 0, 2, 3, 4);
1387 information->fav_sig_id = g_signal_connect (information->manager,
1388 "favourites-changed",
1389 G_CALLBACK (contact_widget_favourites_changed_cb), information);
1391 gtk_widget_show (information->favourite_checkbox);
1397 contact_widget_destroy_cb (GtkWidget *widget,
1398 EmpathyContactWidget *information)
1400 contact_widget_remove_contact (information);
1402 if (information->widget_id_timeout != 0)
1404 g_source_remove (information->widget_id_timeout);
1407 if (information->fav_sig_id != 0)
1408 g_signal_handler_disconnect (information->manager, information->fav_sig_id);
1410 g_object_unref (information->manager);
1412 g_slice_free (EmpathyContactWidget, information);
1416 * empathy_contact_widget_new:
1417 * @contact: an #EmpathyContact
1418 * @flags: #EmpathyContactWidgetFlags for the new contact widget
1420 * Creates a new #EmpathyContactWidget.
1422 * Return value: a new #EmpathyContactWidget
1425 empathy_contact_widget_new (EmpathyContact *contact,
1426 EmpathyContactWidgetFlags flags)
1428 EmpathyContactWidget *information;
1432 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1434 information = g_slice_new0 (EmpathyContactWidget);
1435 information->flags = flags;
1437 filename = empathy_file_lookup ("empathy-contact-widget.ui",
1439 gui = empathy_builder_get_file (filename,
1440 "vbox_contact_widget", &information->vbox_contact_widget,
1441 "hbox_contact", &information->hbox_contact,
1442 "hbox_presence", &information->hbox_presence,
1443 "label_alias", &information->label_alias,
1444 "image_state", &information->image_state,
1445 "table_contact", &information->table_contact,
1446 "vbox_avatar", &information->vbox_avatar,
1447 "vbox_location", &information->vbox_location,
1448 "subvbox_location", &information->subvbox_location,
1449 "label_location", &information->label_location,
1450 #if HAVE_LIBCHAMPLAIN
1451 "viewport_map", &information->viewport_map,
1453 "vbox_groups", &information->vbox_groups,
1454 "entry_group", &information->entry_group,
1455 "button_group", &information->button_group,
1456 "treeview_groups", &information->treeview_groups,
1457 "vbox_details", &information->vbox_details,
1458 "table_details", &information->table_details,
1459 "hbox_details_requested", &information->hbox_details_requested,
1460 "vbox_client", &information->vbox_client,
1461 "table_client", &information->table_client,
1462 "hbox_client_requested", &information->hbox_client_requested,
1466 empathy_builder_connect (gui, information,
1467 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1468 "entry_group", "changed", contact_widget_entry_group_changed_cb,
1469 "entry_group", "activate", contact_widget_entry_group_activate_cb,
1470 "button_group", "clicked", contact_widget_button_group_clicked_cb,
1472 information->table_location = NULL;
1474 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1475 "EmpathyContactWidget",
1478 information->manager = empathy_contact_manager_dup_singleton ();
1480 /* Create widgets */
1481 contact_widget_contact_setup (information);
1482 contact_widget_groups_setup (information);
1483 contact_widget_details_setup (information);
1484 contact_widget_client_setup (information);
1486 if (contact != NULL)
1487 contact_widget_set_contact (information, contact);
1488 else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1489 information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1490 contact_widget_change_contact (information);
1492 return empathy_builder_unref_and_keep_widget (gui,
1493 information->vbox_contact_widget);
1497 * empathy_contact_widget_get_contact:
1498 * @widget: an #EmpathyContactWidget
1500 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1502 * Returns: the #EmpathyContact associated with @widget
1505 empathy_contact_widget_get_contact (GtkWidget *widget)
1507 EmpathyContactWidget *information;
1509 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1511 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1515 return information->contact;
1519 empathy_contact_widget_get_alias (GtkWidget *widget)
1521 EmpathyContactWidget *information;
1523 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1525 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1529 return gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
1533 * empathy_contact_widget_set_contact:
1534 * @widget: an #EmpathyContactWidget
1535 * @contact: a different #EmpathyContact
1537 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1540 empathy_contact_widget_set_contact (GtkWidget *widget,
1541 EmpathyContact *contact)
1543 EmpathyContactWidget *information;
1545 g_return_if_fail (GTK_IS_WIDGET (widget));
1546 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1548 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1552 contact_widget_set_contact (information, contact);
1556 * empathy_contact_widget_set_account_filter:
1557 * @widget: an #EmpathyContactWidget
1558 * @filter: a #EmpathyAccountChooserFilterFunc
1559 * @user_data: user data to pass to @filter, or %NULL
1561 * Set a filter on the #EmpathyAccountChooser included in the
1562 * #EmpathyContactWidget.
1565 empathy_contact_widget_set_account_filter (
1567 EmpathyAccountChooserFilterFunc filter,
1570 EmpathyContactWidget *information;
1571 EmpathyAccountChooser *chooser;
1573 g_return_if_fail (GTK_IS_WIDGET (widget));
1575 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1579 chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
1581 empathy_account_chooser_set_filter (chooser, filter, user_data);