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;
86 GtkWidget *vbox_contact_widget;
89 GtkWidget *hbox_contact;
90 GtkWidget *widget_avatar;
91 GtkWidget *widget_account;
92 GtkWidget *image_account;
93 GtkWidget *label_account;
95 GtkWidget *widget_alias;
96 GtkWidget *label_alias;
97 GtkWidget *entry_alias;
98 GtkWidget *hbox_presence;
99 GtkWidget *image_state;
100 GtkWidget *label_status;
101 GtkWidget *table_contact;
102 GtkWidget *vbox_avatar;
105 GtkWidget *vbox_location;
106 GtkWidget *subvbox_location;
107 GtkWidget *table_location;
108 GtkWidget *label_location;
109 #if HAVE_LIBCHAMPLAIN
110 GtkWidget *viewport_map;
111 GtkWidget *map_view_embed;
112 ChamplainView *map_view;
116 GtkWidget *vbox_groups;
117 GtkWidget *entry_group;
118 GtkWidget *button_group;
119 GtkWidget *treeview_groups;
122 GtkWidget *vbox_details;
123 GtkWidget *table_details;
124 GtkWidget *hbox_details_requested;
127 GtkWidget *vbox_client;
128 GtkWidget *table_client;
129 GtkWidget *hbox_client_requested;
130 } EmpathyContactWidget;
134 EmpathyContactWidget *information;
137 GtkTreeIter found_iter;
149 contact_widget_details_setup (EmpathyContactWidget *information)
151 /* FIXME: Needs new telepathy spec */
152 gtk_widget_hide (information->vbox_details);
156 contact_widget_details_update (EmpathyContactWidget *information)
158 /* FIXME: Needs new telepathy spec */
162 contact_widget_client_update (EmpathyContactWidget *information)
164 /* FIXME: Needs new telepathy spec */
168 contact_widget_client_setup (EmpathyContactWidget *information)
170 /* FIXME: Needs new telepathy spec */
171 gtk_widget_hide (information->vbox_client);
175 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
177 EmpathyContactWidget *information)
187 view = GTK_TREE_VIEW (information->treeview_groups);
188 model = gtk_tree_view_get_model (view);
189 store = GTK_LIST_STORE (model);
191 path = gtk_tree_path_new_from_string (path_string);
193 gtk_tree_model_get_iter (model, &iter, path);
194 gtk_tree_model_get (model, &iter,
195 COL_ENABLED, &enabled,
199 gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
200 gtk_tree_path_free (path);
206 empathy_contact_list_remove_from_group (
207 EMPATHY_CONTACT_LIST (information->manager), information->contact,
212 empathy_contact_list_add_to_group (
213 EMPATHY_CONTACT_LIST (information->manager), information->contact,
221 contact_widget_model_populate_columns (EmpathyContactWidget *information)
225 GtkTreeViewColumn *column;
226 GtkCellRenderer *renderer;
229 view = GTK_TREE_VIEW (information->treeview_groups);
230 model = gtk_tree_view_get_model (view);
232 renderer = gtk_cell_renderer_toggle_new ();
233 g_signal_connect (renderer, "toggled",
234 G_CALLBACK (contact_widget_cell_toggled), information);
236 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
237 "active", COL_ENABLED, NULL);
239 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
240 gtk_tree_view_column_set_fixed_width (column, 50);
241 gtk_tree_view_append_column (view, column);
243 renderer = gtk_cell_renderer_text_new ();
244 col_offset = gtk_tree_view_insert_column_with_attributes (view,
248 /* "editable", COL_EDITABLE, */
251 g_object_set_data (G_OBJECT (renderer),
252 "column", GINT_TO_POINTER (COL_NAME));
254 column = gtk_tree_view_get_column (view, col_offset - 1);
255 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
256 gtk_tree_view_column_set_resizable (column,FALSE);
257 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
261 contact_widget_model_setup (EmpathyContactWidget *information)
265 GtkTreeSelection *selection;
267 view = GTK_TREE_VIEW (information->treeview_groups);
269 store = gtk_list_store_new (COL_COUNT,
270 G_TYPE_STRING, /* name */
271 G_TYPE_BOOLEAN, /* enabled */
272 G_TYPE_BOOLEAN); /* editable */
274 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
276 selection = gtk_tree_view_get_selection (view);
277 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
279 contact_widget_model_populate_columns (information);
281 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
282 COL_NAME, GTK_SORT_ASCENDING);
284 g_object_unref (store);
288 contact_widget_groups_populate_data (EmpathyContactWidget *information)
293 GList *my_groups, *l;
296 view = GTK_TREE_VIEW (information->treeview_groups);
297 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
298 gtk_list_store_clear (store);
300 all_groups = empathy_contact_list_get_all_groups (
301 EMPATHY_CONTACT_LIST (information->manager));
302 my_groups = empathy_contact_list_get_groups (
303 EMPATHY_CONTACT_LIST (information->manager),
304 information->contact);
306 for (l = all_groups; l; l = l->next)
308 const gchar *group_str;
313 enabled = g_list_find_custom (my_groups,
314 group_str, (GCompareFunc) strcmp) != NULL;
316 gtk_list_store_append (store, &iter);
317 gtk_list_store_set (store, &iter,
320 COL_ENABLED, enabled,
324 g_list_foreach (all_groups, (GFunc) g_free, NULL);
325 g_list_foreach (my_groups, (GFunc) g_free, NULL);
326 g_list_free (all_groups);
327 g_list_free (my_groups);
331 contact_widget_model_find_name_foreach (GtkTreeModel *model,
338 gtk_tree_model_get (model, iter,
345 if (data->name && strcmp (data->name, name) == 0)
348 data->found_iter = *iter;
361 contact_widget_model_find_name (EmpathyContactWidget *information,
369 if (EMP_STR_EMPTY (name))
372 data.information = information;
376 view = GTK_TREE_VIEW (information->treeview_groups);
377 model = gtk_tree_view_get_model (view);
379 gtk_tree_model_foreach (model,
380 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
383 if (data.found == TRUE)
385 *iter = data.found_iter;
393 contact_widget_entry_group_changed_cb (GtkEditable *editable,
394 EmpathyContactWidget *information)
399 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
401 if (contact_widget_model_find_name (information, group, &iter))
402 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
404 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
405 !EMP_STR_EMPTY (group));
409 contact_widget_entry_group_activate_cb (GtkEntry *entry,
410 EmpathyContactWidget *information)
412 gtk_widget_activate (GTK_WIDGET (information->button_group));
416 contact_widget_button_group_clicked_cb (GtkButton *button,
417 EmpathyContactWidget *information)
424 view = GTK_TREE_VIEW (information->treeview_groups);
425 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
427 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
429 gtk_list_store_append (store, &iter);
430 gtk_list_store_set (store, &iter,
435 empathy_contact_list_add_to_group (
436 EMPATHY_CONTACT_LIST (information->manager), information->contact,
441 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
443 /* FIXME: not implemented */
447 contact_widget_groups_setup (EmpathyContactWidget *information)
449 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
451 information->manager = empathy_contact_manager_dup_singleton ();
452 contact_widget_model_setup (information);
457 contact_widget_groups_update (EmpathyContactWidget *information)
459 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
460 information->contact)
462 g_signal_connect_swapped (information->contact, "notify::groups",
463 G_CALLBACK (contact_widget_groups_notify_cb), information);
464 contact_widget_groups_populate_data (information);
466 gtk_widget_show (information->vbox_groups);
469 gtk_widget_hide (information->vbox_groups);
472 /* Converts the Location's GHashTable's key to a user readable string */
474 location_key_to_label (const gchar *key)
476 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
477 return _("Country ISO Code:");
478 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
479 return _("Country:");
480 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
482 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
484 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
486 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
487 return _("Postal Code:");
488 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
490 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
491 return _("Building:");
492 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
494 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
496 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
498 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
499 return _("Description:");
500 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
502 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
503 return _("Accuracy Level:");
504 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
506 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
507 return _("Vertical Error (meters):");
508 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
509 return _("Horizontal Error (meters):");
510 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
512 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
513 return _("Bearing:");
514 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
515 return _("Climb Speed:");
516 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
517 return _("Last Updated on:");
518 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
519 return _("Longitude:");
520 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
521 return _("Latitude:");
522 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
523 return _("Altitude:");
526 DEBUG ("Unexpected Location key: %s", key);
532 contact_widget_location_update (EmpathyContactWidget *information)
534 GHashTable *location;
536 gdouble lat = 0.0, lon = 0.0;
537 gboolean has_position = TRUE;
540 static const gchar* ordered_geolocation_keys[] = {
541 EMPATHY_LOCATION_TEXT,
542 EMPATHY_LOCATION_URI,
543 EMPATHY_LOCATION_DESCRIPTION,
544 EMPATHY_LOCATION_BUILDING,
545 EMPATHY_LOCATION_FLOOR,
546 EMPATHY_LOCATION_ROOM,
547 EMPATHY_LOCATION_STREET,
548 EMPATHY_LOCATION_AREA,
549 EMPATHY_LOCATION_LOCALITY,
550 EMPATHY_LOCATION_REGION,
551 EMPATHY_LOCATION_COUNTRY,
556 gboolean display_map = FALSE;
558 if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
560 gtk_widget_hide (information->vbox_location);
564 location = empathy_contact_get_location (information->contact);
565 if (location == NULL || g_hash_table_size (location) == 0)
567 gtk_widget_hide (information->vbox_location);
571 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
573 has_position = FALSE;
575 lat = g_value_get_double (value);
577 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
579 has_position = FALSE;
581 lon = g_value_get_double (value);
583 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
586 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
587 gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
597 stamp = g_value_get_int64 (value);
600 user_date = empathy_time_to_string_relative (time_);
602 text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
603 gtk_label_set_markup (GTK_LABEL (information->label_location), text);
608 /* Prepare the location information table */
609 if (information->table_location != NULL)
611 gtk_widget_destroy (information->table_location);
614 information->table_location = gtk_table_new (1, 2, FALSE);
615 gtk_box_pack_start (GTK_BOX (information->subvbox_location),
616 information->table_location, FALSE, FALSE, 5);
619 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
621 const gchar* user_label;
625 gvalue = g_hash_table_lookup (location, (gpointer) skey);
629 user_label = location_key_to_label (skey);
631 label = gtk_label_new (user_label);
632 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
633 gtk_table_attach (GTK_TABLE (information->table_location),
634 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
635 gtk_widget_show (label);
637 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
640 dvalue = g_value_get_double (gvalue);
641 svalue = g_strdup_printf ("%f", dvalue);
643 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
645 svalue = g_value_dup_string (gvalue);
647 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
651 time_ = g_value_get_int64 (value);
652 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
657 label = gtk_label_new (svalue);
658 gtk_table_attach_defaults (GTK_TABLE (information->table_location),
659 label, 1, 2, row, row + 1);
660 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
661 gtk_widget_show (label);
663 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
664 gtk_label_set_selectable (GTK_LABEL (label), TRUE);
671 #if HAVE_LIBCHAMPLAIN
673 !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
675 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
683 /* We can display some fields */
684 gtk_widget_show (information->table_location);
686 else if (!display_map)
688 /* Can't display either fields or map */
689 gtk_widget_hide (information->vbox_location);
693 #if HAVE_LIBCHAMPLAIN
696 ClutterActor *marker;
697 ChamplainLayer *layer;
699 information->map_view_embed = gtk_champlain_embed_new ();
700 information->map_view = gtk_champlain_embed_get_view (
701 GTK_CHAMPLAIN_EMBED (information->map_view_embed));
703 gtk_container_add (GTK_CONTAINER (information->viewport_map),
704 information->map_view_embed);
705 g_object_set (G_OBJECT (information->map_view),
706 "show-license", TRUE,
707 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
711 layer = champlain_layer_new ();
712 champlain_view_add_layer (information->map_view, layer);
714 marker = champlain_marker_new_with_text (
715 empathy_contact_get_name (information->contact), NULL, NULL, NULL);
716 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
717 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
719 champlain_view_center_on (information->map_view, lat, lon);
720 gtk_widget_show_all (information->viewport_map);
724 gtk_widget_show (information->vbox_location);
728 save_avatar_menu_activate_cb (GtkWidget *widget,
729 EmpathyContactWidget *information)
732 EmpathyAvatar *avatar;
733 gchar *ext = NULL, *filename;
735 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
737 GTK_FILE_CHOOSER_ACTION_SAVE,
738 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
739 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
742 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
745 /* look for the avatar extension */
746 avatar = empathy_contact_get_avatar (information->contact);
747 if (avatar->format != NULL)
751 splitted = g_strsplit (avatar->format, "/", 2);
752 if (splitted[0] != NULL && splitted[1] != NULL)
753 ext = g_strdup (splitted[1]);
755 g_strfreev (splitted);
759 /* Avatar was loaded from the cache so was converted to PNG */
760 ext = g_strdup ("png");
767 id = tp_escape_as_identifier (empathy_contact_get_id (
768 information->contact));
770 filename = g_strdup_printf ("%s.%s", id, ext);
771 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
778 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
780 GError *error = NULL;
782 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
784 if (!empathy_avatar_save_to_file (avatar, filename, &error))
787 GtkWidget *error_dialog;
789 error_dialog = gtk_message_dialog_new (NULL, 0,
790 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
791 _("Unable to save avatar"));
793 gtk_message_dialog_format_secondary_text (
794 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
796 g_signal_connect (error_dialog, "response",
797 G_CALLBACK (gtk_widget_destroy), NULL);
799 gtk_window_present (GTK_WINDOW (error_dialog));
801 g_clear_error (&error);
807 gtk_widget_destroy (dialog);
811 popup_avatar_menu (EmpathyContactWidget *information,
813 GdkEventButton *event)
815 GtkWidget *menu, *item;
816 gint button, event_time;
818 if (information->contact == NULL ||
819 empathy_contact_get_avatar (information->contact) == NULL)
822 menu = gtk_menu_new ();
824 /* Add "Save as..." entry */
825 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
826 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
827 gtk_widget_show (item);
829 g_signal_connect (item, "activate",
830 G_CALLBACK (save_avatar_menu_activate_cb), information);
834 button = event->button;
835 event_time = event->time;
840 event_time = gtk_get_current_event_time ();
843 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
845 g_object_ref_sink (menu);
846 g_object_unref (menu);
850 widget_avatar_popup_menu_cb (GtkWidget *widget,
851 EmpathyContactWidget *information)
853 popup_avatar_menu (information, widget, NULL);
859 widget_avatar_button_press_event_cb (GtkWidget *widget,
860 GdkEventButton *event,
861 EmpathyContactWidget *information)
863 /* Ignore double-clicks and triple-clicks */
864 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
866 popup_avatar_menu (information, widget, event);
874 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
875 EmpathyContactWidget *information)
879 const gchar *mime_type;
881 empathy_avatar_chooser_get_image_data (
882 EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
883 &data, &size, &mime_type);
884 empathy_tp_contact_factory_set_avatar (information->factory,
885 data, size, mime_type);
889 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
890 GdkEventFocus *event,
891 EmpathyContactWidget *information)
893 if (information->contact)
897 alias = gtk_entry_get_text (GTK_ENTRY (editable));
898 empathy_tp_contact_factory_set_alias (information->factory,
899 information->contact, alias);
906 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
907 EmpathyAvatarChooser *avatar_chooser)
909 TpConnection *connection;
911 connection = empathy_account_chooser_get_connection (account_chooser);
912 g_object_set (avatar_chooser, "connection", connection, NULL);
916 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
918 EmpathyAvatar *avatar = NULL;
920 if (information->contact)
921 avatar = empathy_contact_get_avatar (information->contact);
923 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
925 g_signal_handlers_block_by_func (information->widget_avatar,
926 contact_widget_avatar_changed_cb,
928 empathy_avatar_chooser_set (
929 EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
930 g_signal_handlers_unblock_by_func (information->widget_avatar,
931 contact_widget_avatar_changed_cb, information);
934 empathy_avatar_image_set (
935 EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
939 contact_widget_name_notify_cb (EmpathyContactWidget *information)
941 if (GTK_IS_ENTRY (information->widget_alias))
942 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
943 empathy_contact_get_name (information->contact));
945 gtk_label_set_label (GTK_LABEL (information->widget_alias),
946 empathy_contact_get_name (information->contact));
950 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
953 gchar *markup_text = NULL;
955 status = empathy_contact_get_status (information->contact);
957 markup_text = empathy_add_link_markup (status);
958 gtk_label_set_markup (GTK_LABEL (information->label_status), markup_text);
959 g_free (markup_text);
961 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
962 empathy_icon_name_for_contact (information->contact),
963 GTK_ICON_SIZE_BUTTON);
964 gtk_widget_show (information->image_state);
968 contact_widget_remove_contact (EmpathyContactWidget *information)
970 if (information->contact)
972 g_signal_handlers_disconnect_by_func (information->contact,
973 contact_widget_name_notify_cb, information);
974 g_signal_handlers_disconnect_by_func (information->contact,
975 contact_widget_presence_notify_cb, information);
976 g_signal_handlers_disconnect_by_func (information->contact,
977 contact_widget_avatar_notify_cb, information);
978 g_signal_handlers_disconnect_by_func (information->contact,
979 contact_widget_groups_notify_cb, information);
981 g_object_unref (information->contact);
982 g_object_unref (information->factory);
983 information->contact = NULL;
984 information->factory = NULL;
988 static void contact_widget_change_contact (EmpathyContactWidget *information);
991 contact_widget_contact_update (EmpathyContactWidget *information)
993 TpAccount *account = NULL;
994 const gchar *id = NULL;
996 /* Connect and get info from new contact */
997 if (information->contact)
999 g_signal_connect_swapped (information->contact, "notify::name",
1000 G_CALLBACK (contact_widget_name_notify_cb), information);
1001 g_signal_connect_swapped (information->contact, "notify::presence",
1002 G_CALLBACK (contact_widget_presence_notify_cb), information);
1003 g_signal_connect_swapped (information->contact,
1004 "notify::presence-message",
1005 G_CALLBACK (contact_widget_presence_notify_cb), information);
1006 g_signal_connect_swapped (information->contact, "notify::avatar",
1007 G_CALLBACK (contact_widget_avatar_notify_cb), information);
1009 account = empathy_contact_get_account (information->contact);
1010 id = empathy_contact_get_id (information->contact);
1013 /* Update account widget */
1014 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1018 g_signal_handlers_block_by_func (information->widget_account,
1019 contact_widget_change_contact,
1021 empathy_account_chooser_set_account (
1022 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1023 g_signal_handlers_unblock_by_func (information->widget_account,
1024 contact_widget_change_contact, information);
1033 name = tp_account_get_display_name (account);
1034 gtk_label_set_label (GTK_LABEL (information->label_account), name);
1036 name = tp_account_get_icon_name (account);
1037 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1038 name, GTK_ICON_SIZE_MENU);
1042 /* Update id widget */
1043 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1044 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1046 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1048 /* Update other widgets */
1049 if (information->contact)
1051 contact_widget_name_notify_cb (information);
1052 contact_widget_presence_notify_cb (information);
1053 contact_widget_avatar_notify_cb (information);
1055 gtk_widget_show (information->label_alias);
1056 gtk_widget_show (information->widget_alias);
1057 gtk_widget_show (information->hbox_presence);
1058 gtk_widget_show (information->widget_avatar);
1062 gtk_widget_hide (information->label_alias);
1063 gtk_widget_hide (information->widget_alias);
1064 gtk_widget_hide (information->hbox_presence);
1065 gtk_widget_hide (information->widget_avatar);
1070 contact_widget_set_contact (EmpathyContactWidget *information,
1071 EmpathyContact *contact)
1073 if (contact == information->contact)
1076 contact_widget_remove_contact (information);
1079 TpConnection *connection;
1081 connection = empathy_contact_get_connection (contact);
1082 information->contact = g_object_ref (contact);
1083 information->factory = empathy_tp_contact_factory_dup_singleton (connection);
1086 /* set the selected account to be the account this contact came from */
1087 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1088 empathy_account_chooser_set_account (
1089 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1090 empathy_contact_get_account (contact));
1093 /* Update information for widgets */
1094 contact_widget_contact_update (information);
1095 contact_widget_groups_update (information);
1096 contact_widget_details_update (information);
1097 contact_widget_client_update (information);
1098 contact_widget_location_update (information);
1102 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
1103 EmpathyContact *contact,
1104 const GError *error,
1106 GObject *weak_object)
1108 EmpathyContactWidget *information = user_data;
1112 DEBUG ("Error: %s", error->message);
1116 contact_widget_set_contact (information, contact);
1120 contact_widget_change_contact (EmpathyContactWidget *information)
1122 EmpathyTpContactFactory *factory;
1123 TpConnection *connection;
1125 connection = empathy_account_chooser_get_connection (
1126 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1130 factory = empathy_tp_contact_factory_dup_singleton (connection);
1131 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1135 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1136 if (!EMP_STR_EMPTY (id))
1138 empathy_tp_contact_factory_get_from_id (factory, id,
1139 contact_widget_got_contact_cb, information, NULL,
1140 G_OBJECT (information->vbox_contact_widget));
1145 empathy_tp_contact_factory_get_from_handle (factory,
1146 tp_connection_get_self_handle (connection),
1147 contact_widget_got_contact_cb, information, NULL,
1148 G_OBJECT (information->vbox_contact_widget));
1151 g_object_unref (factory);
1155 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1157 contact_widget_change_contact (self);
1162 contact_widget_id_changed_cb (GtkEntry *entry,
1163 EmpathyContactWidget *self)
1165 if (self->widget_id_timeout != 0)
1167 g_source_remove (self->widget_id_timeout);
1170 self->widget_id_timeout =
1171 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1172 (GSourceFunc) contact_widget_id_activate_timeout, self);
1176 contact_widget_id_focus_out_cb (GtkWidget *widget,
1177 GdkEventFocus *event,
1178 EmpathyContactWidget *information)
1180 contact_widget_change_contact (information);
1185 contact_widget_contact_setup (EmpathyContactWidget *information)
1187 /* Setup label_status as a KludgeLabel */
1188 information->label_status = empathy_kludge_label_new ("");
1189 gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1190 PANGO_WRAP_WORD_CHAR);
1191 gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1194 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1195 gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1197 gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1198 information->label_status, TRUE, TRUE, 0);
1199 gtk_widget_show (information->label_status);
1201 /* Setup account label/chooser */
1202 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1204 information->widget_account = empathy_account_chooser_new ();
1206 g_signal_connect_swapped (information->widget_account, "changed",
1207 G_CALLBACK (contact_widget_change_contact),
1212 /* Pack the protocol icon with the account name in an hbox */
1213 information->widget_account = gtk_hbox_new (FALSE, 6);
1215 information->label_account = gtk_label_new (NULL);
1216 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1217 gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1219 gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1220 gtk_widget_show (information->label_account);
1222 information->image_account = gtk_image_new ();
1223 gtk_widget_show (information->image_account);
1225 gtk_box_pack_start (GTK_BOX (information->widget_account),
1226 information->image_account, FALSE, FALSE, 0);
1227 gtk_box_pack_start (GTK_BOX (information->widget_account),
1228 information->label_account, FALSE, TRUE, 0);
1230 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1231 information->widget_account,
1233 gtk_widget_show (information->widget_account);
1235 /* Set up avatar chooser/display */
1236 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1238 information->widget_avatar = empathy_avatar_chooser_new ();
1239 g_signal_connect (information->widget_avatar, "changed",
1240 G_CALLBACK (contact_widget_avatar_changed_cb),
1242 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1244 g_signal_connect (information->widget_account, "changed",
1245 G_CALLBACK (update_avatar_chooser_account_cb),
1246 information->widget_avatar);
1247 update_avatar_chooser_account_cb (
1248 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1249 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1254 information->widget_avatar = empathy_avatar_image_new ();
1256 g_signal_connect (information->widget_avatar, "popup-menu",
1257 G_CALLBACK (widget_avatar_popup_menu_cb), information);
1258 g_signal_connect (information->widget_avatar, "button-press-event",
1259 G_CALLBACK (widget_avatar_button_press_event_cb), information);
1262 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1263 information->widget_avatar,
1266 gtk_widget_show (information->widget_avatar);
1268 /* Setup id label/entry */
1269 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1271 information->widget_id = gtk_entry_new ();
1272 g_signal_connect (information->widget_id, "focus-out-event",
1273 G_CALLBACK (contact_widget_id_focus_out_cb),
1275 g_signal_connect (information->widget_id, "changed",
1276 G_CALLBACK (contact_widget_id_changed_cb),
1281 information->widget_id = gtk_label_new (NULL);
1282 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1283 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1285 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1287 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1288 information->widget_id,
1290 gtk_widget_show (information->widget_id);
1292 /* Setup alias label/entry */
1293 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1295 information->widget_alias = gtk_entry_new ();
1297 if (!(information->flags & EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS))
1298 g_signal_connect (information->widget_alias, "focus-out-event",
1299 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1302 /* Make return activate the window default (the Close button) */
1303 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1308 information->widget_alias = gtk_label_new (NULL);
1309 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1310 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1312 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1314 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1315 information->widget_alias,
1317 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1318 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1320 gtk_widget_show (information->widget_alias);
1324 contact_widget_destroy_cb (GtkWidget *widget,
1325 EmpathyContactWidget *information)
1327 contact_widget_remove_contact (information);
1329 if (information->widget_id_timeout != 0)
1331 g_source_remove (information->widget_id_timeout);
1333 if (information->manager)
1335 g_object_unref (information->manager);
1338 g_slice_free (EmpathyContactWidget, information);
1342 * empathy_contact_widget_new:
1343 * @contact: an #EmpathyContact
1344 * @flags: #EmpathyContactWidgetFlags for the new contact widget
1346 * Creates a new #EmpathyContactWidget.
1348 * Return value: a new #EmpathyContactWidget
1351 empathy_contact_widget_new (EmpathyContact *contact,
1352 EmpathyContactWidgetFlags flags)
1354 EmpathyContactWidget *information;
1358 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1360 information = g_slice_new0 (EmpathyContactWidget);
1361 information->flags = flags;
1363 filename = empathy_file_lookup ("empathy-contact-widget.ui",
1365 gui = empathy_builder_get_file (filename,
1366 "vbox_contact_widget", &information->vbox_contact_widget,
1367 "hbox_contact", &information->hbox_contact,
1368 "hbox_presence", &information->hbox_presence,
1369 "label_alias", &information->label_alias,
1370 "image_state", &information->image_state,
1371 "table_contact", &information->table_contact,
1372 "vbox_avatar", &information->vbox_avatar,
1373 "vbox_location", &information->vbox_location,
1374 "subvbox_location", &information->subvbox_location,
1375 "label_location", &information->label_location,
1376 #if HAVE_LIBCHAMPLAIN
1377 "viewport_map", &information->viewport_map,
1379 "vbox_groups", &information->vbox_groups,
1380 "entry_group", &information->entry_group,
1381 "button_group", &information->button_group,
1382 "treeview_groups", &information->treeview_groups,
1383 "vbox_details", &information->vbox_details,
1384 "table_details", &information->table_details,
1385 "hbox_details_requested", &information->hbox_details_requested,
1386 "vbox_client", &information->vbox_client,
1387 "table_client", &information->table_client,
1388 "hbox_client_requested", &information->hbox_client_requested,
1392 empathy_builder_connect (gui, information,
1393 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1394 "entry_group", "changed", contact_widget_entry_group_changed_cb,
1395 "entry_group", "activate", contact_widget_entry_group_activate_cb,
1396 "button_group", "clicked", contact_widget_button_group_clicked_cb,
1398 information->table_location = NULL;
1400 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1401 "EmpathyContactWidget",
1404 /* Create widgets */
1405 contact_widget_contact_setup (information);
1406 contact_widget_groups_setup (information);
1407 contact_widget_details_setup (information);
1408 contact_widget_client_setup (information);
1410 if (contact != NULL)
1411 contact_widget_set_contact (information, contact);
1412 else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1413 information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1414 contact_widget_change_contact (information);
1416 return empathy_builder_unref_and_keep_widget (gui,
1417 information->vbox_contact_widget);
1421 * empathy_contact_widget_get_contact:
1422 * @widget: an #EmpathyContactWidget
1424 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1426 * Returns: the #EmpathyContact associated with @widget
1429 empathy_contact_widget_get_contact (GtkWidget *widget)
1431 EmpathyContactWidget *information;
1433 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1435 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1439 return information->contact;
1443 empathy_contact_widget_get_alias (GtkWidget *widget)
1445 EmpathyContactWidget *information;
1447 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1449 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1453 return gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
1457 * empathy_contact_widget_set_contact:
1458 * @widget: an #EmpathyContactWidget
1459 * @contact: a different #EmpathyContact
1461 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1464 empathy_contact_widget_set_contact (GtkWidget *widget,
1465 EmpathyContact *contact)
1467 EmpathyContactWidget *information;
1469 g_return_if_fail (GTK_IS_WIDGET (widget));
1470 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1472 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1476 contact_widget_set_contact (information, contact);
1480 * empathy_contact_widget_set_account_filter:
1481 * @widget: an #EmpathyContactWidget
1482 * @filter: a #EmpathyAccountChooserFilterFunc
1483 * @user_data: user data to pass to @filter, or %NULL
1485 * Set a filter on the #EmpathyAccountChooser included in the
1486 * #EmpathyContactWidget.
1489 empathy_contact_widget_set_account_filter (
1491 EmpathyAccountChooserFilterFunc filter,
1494 EmpathyContactWidget *information;
1495 EmpathyAccountChooser *chooser;
1497 g_return_if_fail (GTK_IS_WIDGET (widget));
1499 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1503 chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
1505 empathy_account_chooser_set_filter (chooser, filter, user_data);