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)
955 status = empathy_contact_get_status (information->contact);
956 markup_text = empathy_add_link_markup (status);
957 gtk_label_set_markup (GTK_LABEL (information->label_status), markup_text);
958 g_free (markup_text);
960 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
961 empathy_icon_name_for_contact (information->contact),
962 GTK_ICON_SIZE_BUTTON);
963 gtk_widget_show (information->image_state);
967 contact_widget_remove_contact (EmpathyContactWidget *information)
969 if (information->contact)
971 g_signal_handlers_disconnect_by_func (information->contact,
972 contact_widget_name_notify_cb, information);
973 g_signal_handlers_disconnect_by_func (information->contact,
974 contact_widget_presence_notify_cb, information);
975 g_signal_handlers_disconnect_by_func (information->contact,
976 contact_widget_avatar_notify_cb, information);
977 g_signal_handlers_disconnect_by_func (information->contact,
978 contact_widget_groups_notify_cb, information);
980 g_object_unref (information->contact);
981 g_object_unref (information->factory);
982 information->contact = NULL;
983 information->factory = NULL;
987 static void contact_widget_change_contact (EmpathyContactWidget *information);
990 contact_widget_contact_update (EmpathyContactWidget *information)
992 TpAccount *account = NULL;
993 const gchar *id = NULL;
995 /* Connect and get info from new contact */
996 if (information->contact)
998 g_signal_connect_swapped (information->contact, "notify::name",
999 G_CALLBACK (contact_widget_name_notify_cb), information);
1000 g_signal_connect_swapped (information->contact, "notify::presence",
1001 G_CALLBACK (contact_widget_presence_notify_cb), information);
1002 g_signal_connect_swapped (information->contact,
1003 "notify::presence-message",
1004 G_CALLBACK (contact_widget_presence_notify_cb), information);
1005 g_signal_connect_swapped (information->contact, "notify::avatar",
1006 G_CALLBACK (contact_widget_avatar_notify_cb), information);
1008 account = empathy_contact_get_account (information->contact);
1009 id = empathy_contact_get_id (information->contact);
1012 /* Update account widget */
1013 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1017 g_signal_handlers_block_by_func (information->widget_account,
1018 contact_widget_change_contact,
1020 empathy_account_chooser_set_account (
1021 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1022 g_signal_handlers_unblock_by_func (information->widget_account,
1023 contact_widget_change_contact, information);
1032 name = tp_account_get_display_name (account);
1033 gtk_label_set_label (GTK_LABEL (information->label_account), name);
1035 name = tp_account_get_icon_name (account);
1036 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1037 name, GTK_ICON_SIZE_MENU);
1041 /* Update id widget */
1042 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1043 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1045 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1047 /* Update other widgets */
1048 if (information->contact)
1050 contact_widget_name_notify_cb (information);
1051 contact_widget_presence_notify_cb (information);
1052 contact_widget_avatar_notify_cb (information);
1054 gtk_widget_show (information->label_alias);
1055 gtk_widget_show (information->widget_alias);
1056 gtk_widget_show (information->hbox_presence);
1057 gtk_widget_show (information->widget_avatar);
1061 gtk_widget_hide (information->label_alias);
1062 gtk_widget_hide (information->widget_alias);
1063 gtk_widget_hide (information->hbox_presence);
1064 gtk_widget_hide (information->widget_avatar);
1069 contact_widget_set_contact (EmpathyContactWidget *information,
1070 EmpathyContact *contact)
1072 if (contact == information->contact)
1075 contact_widget_remove_contact (information);
1078 TpConnection *connection;
1080 connection = empathy_contact_get_connection (contact);
1081 information->contact = g_object_ref (contact);
1082 information->factory = empathy_tp_contact_factory_dup_singleton (connection);
1085 /* set the selected account to be the account this contact came from */
1086 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1087 empathy_account_chooser_set_account (
1088 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1089 empathy_contact_get_account (contact));
1092 /* Update information for widgets */
1093 contact_widget_contact_update (information);
1094 contact_widget_groups_update (information);
1095 contact_widget_details_update (information);
1096 contact_widget_client_update (information);
1097 contact_widget_location_update (information);
1101 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
1102 EmpathyContact *contact,
1103 const GError *error,
1105 GObject *weak_object)
1107 EmpathyContactWidget *information = user_data;
1111 DEBUG ("Error: %s", error->message);
1115 contact_widget_set_contact (information, contact);
1119 contact_widget_change_contact (EmpathyContactWidget *information)
1121 EmpathyTpContactFactory *factory;
1122 TpConnection *connection;
1124 connection = empathy_account_chooser_get_connection (
1125 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1129 factory = empathy_tp_contact_factory_dup_singleton (connection);
1130 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1134 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1135 if (!EMP_STR_EMPTY (id))
1137 empathy_tp_contact_factory_get_from_id (factory, id,
1138 contact_widget_got_contact_cb, information, NULL,
1139 G_OBJECT (information->vbox_contact_widget));
1144 empathy_tp_contact_factory_get_from_handle (factory,
1145 tp_connection_get_self_handle (connection),
1146 contact_widget_got_contact_cb, information, NULL,
1147 G_OBJECT (information->vbox_contact_widget));
1150 g_object_unref (factory);
1154 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1156 contact_widget_change_contact (self);
1161 contact_widget_id_changed_cb (GtkEntry *entry,
1162 EmpathyContactWidget *self)
1164 if (self->widget_id_timeout != 0)
1166 g_source_remove (self->widget_id_timeout);
1169 self->widget_id_timeout =
1170 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1171 (GSourceFunc) contact_widget_id_activate_timeout, self);
1175 contact_widget_id_focus_out_cb (GtkWidget *widget,
1176 GdkEventFocus *event,
1177 EmpathyContactWidget *information)
1179 contact_widget_change_contact (information);
1184 contact_widget_contact_setup (EmpathyContactWidget *information)
1186 /* Setup label_status as a KludgeLabel */
1187 information->label_status = empathy_kludge_label_new ("");
1188 gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1189 PANGO_WRAP_WORD_CHAR);
1190 gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1193 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1194 gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1196 gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1197 information->label_status, TRUE, TRUE, 0);
1198 gtk_widget_show (information->label_status);
1200 /* Setup account label/chooser */
1201 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1203 information->widget_account = empathy_account_chooser_new ();
1205 g_signal_connect_swapped (information->widget_account, "changed",
1206 G_CALLBACK (contact_widget_change_contact),
1211 /* Pack the protocol icon with the account name in an hbox */
1212 information->widget_account = gtk_hbox_new (FALSE, 6);
1214 information->label_account = gtk_label_new (NULL);
1215 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1216 gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1218 gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1219 gtk_widget_show (information->label_account);
1221 information->image_account = gtk_image_new ();
1222 gtk_widget_show (information->image_account);
1224 gtk_box_pack_start (GTK_BOX (information->widget_account),
1225 information->image_account, FALSE, FALSE, 0);
1226 gtk_box_pack_start (GTK_BOX (information->widget_account),
1227 information->label_account, FALSE, TRUE, 0);
1229 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1230 information->widget_account,
1232 gtk_widget_show (information->widget_account);
1234 /* Set up avatar chooser/display */
1235 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1237 information->widget_avatar = empathy_avatar_chooser_new ();
1238 g_signal_connect (information->widget_avatar, "changed",
1239 G_CALLBACK (contact_widget_avatar_changed_cb),
1241 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1243 g_signal_connect (information->widget_account, "changed",
1244 G_CALLBACK (update_avatar_chooser_account_cb),
1245 information->widget_avatar);
1246 update_avatar_chooser_account_cb (
1247 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1248 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1253 information->widget_avatar = empathy_avatar_image_new ();
1255 g_signal_connect (information->widget_avatar, "popup-menu",
1256 G_CALLBACK (widget_avatar_popup_menu_cb), information);
1257 g_signal_connect (information->widget_avatar, "button-press-event",
1258 G_CALLBACK (widget_avatar_button_press_event_cb), information);
1261 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1262 information->widget_avatar,
1265 gtk_widget_show (information->widget_avatar);
1267 /* Setup id label/entry */
1268 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1270 information->widget_id = gtk_entry_new ();
1271 g_signal_connect (information->widget_id, "focus-out-event",
1272 G_CALLBACK (contact_widget_id_focus_out_cb),
1274 g_signal_connect (information->widget_id, "changed",
1275 G_CALLBACK (contact_widget_id_changed_cb),
1280 information->widget_id = gtk_label_new (NULL);
1281 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1282 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1284 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1286 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1287 information->widget_id,
1289 gtk_widget_show (information->widget_id);
1291 /* Setup alias label/entry */
1292 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1294 information->widget_alias = gtk_entry_new ();
1296 if (!(information->flags & EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS))
1297 g_signal_connect (information->widget_alias, "focus-out-event",
1298 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1301 /* Make return activate the window default (the Close button) */
1302 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1307 information->widget_alias = gtk_label_new (NULL);
1308 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1309 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1311 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1313 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1314 information->widget_alias,
1316 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1317 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1319 gtk_widget_show (information->widget_alias);
1323 contact_widget_destroy_cb (GtkWidget *widget,
1324 EmpathyContactWidget *information)
1326 contact_widget_remove_contact (information);
1328 if (information->widget_id_timeout != 0)
1330 g_source_remove (information->widget_id_timeout);
1332 if (information->manager)
1334 g_object_unref (information->manager);
1337 g_slice_free (EmpathyContactWidget, information);
1341 * empathy_contact_widget_new:
1342 * @contact: an #EmpathyContact
1343 * @flags: #EmpathyContactWidgetFlags for the new contact widget
1345 * Creates a new #EmpathyContactWidget.
1347 * Return value: a new #EmpathyContactWidget
1350 empathy_contact_widget_new (EmpathyContact *contact,
1351 EmpathyContactWidgetFlags flags)
1353 EmpathyContactWidget *information;
1357 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1359 information = g_slice_new0 (EmpathyContactWidget);
1360 information->flags = flags;
1362 filename = empathy_file_lookup ("empathy-contact-widget.ui",
1364 gui = empathy_builder_get_file (filename,
1365 "vbox_contact_widget", &information->vbox_contact_widget,
1366 "hbox_contact", &information->hbox_contact,
1367 "hbox_presence", &information->hbox_presence,
1368 "label_alias", &information->label_alias,
1369 "image_state", &information->image_state,
1370 "table_contact", &information->table_contact,
1371 "vbox_avatar", &information->vbox_avatar,
1372 "vbox_location", &information->vbox_location,
1373 "subvbox_location", &information->subvbox_location,
1374 "label_location", &information->label_location,
1375 #if HAVE_LIBCHAMPLAIN
1376 "viewport_map", &information->viewport_map,
1378 "vbox_groups", &information->vbox_groups,
1379 "entry_group", &information->entry_group,
1380 "button_group", &information->button_group,
1381 "treeview_groups", &information->treeview_groups,
1382 "vbox_details", &information->vbox_details,
1383 "table_details", &information->table_details,
1384 "hbox_details_requested", &information->hbox_details_requested,
1385 "vbox_client", &information->vbox_client,
1386 "table_client", &information->table_client,
1387 "hbox_client_requested", &information->hbox_client_requested,
1391 empathy_builder_connect (gui, information,
1392 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1393 "entry_group", "changed", contact_widget_entry_group_changed_cb,
1394 "entry_group", "activate", contact_widget_entry_group_activate_cb,
1395 "button_group", "clicked", contact_widget_button_group_clicked_cb,
1397 information->table_location = NULL;
1399 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1400 "EmpathyContactWidget",
1403 /* Create widgets */
1404 contact_widget_contact_setup (information);
1405 contact_widget_groups_setup (information);
1406 contact_widget_details_setup (information);
1407 contact_widget_client_setup (information);
1409 if (contact != NULL)
1410 contact_widget_set_contact (information, contact);
1411 else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1412 information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1413 contact_widget_change_contact (information);
1415 return empathy_builder_unref_and_keep_widget (gui,
1416 information->vbox_contact_widget);
1420 * empathy_contact_widget_get_contact:
1421 * @widget: an #EmpathyContactWidget
1423 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1425 * Returns: the #EmpathyContact associated with @widget
1428 empathy_contact_widget_get_contact (GtkWidget *widget)
1430 EmpathyContactWidget *information;
1432 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1434 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1438 return information->contact;
1442 empathy_contact_widget_get_alias (GtkWidget *widget)
1444 EmpathyContactWidget *information;
1446 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1448 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1452 return gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
1456 * empathy_contact_widget_set_contact:
1457 * @widget: an #EmpathyContactWidget
1458 * @contact: a different #EmpathyContact
1460 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1463 empathy_contact_widget_set_contact (GtkWidget *widget,
1464 EmpathyContact *contact)
1466 EmpathyContactWidget *information;
1468 g_return_if_fail (GTK_IS_WIDGET (widget));
1469 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1471 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1475 contact_widget_set_contact (information, contact);
1479 * empathy_contact_widget_set_account_filter:
1480 * @widget: an #EmpathyContactWidget
1481 * @filter: a #EmpathyAccountChooserFilterFunc
1482 * @user_data: user data to pass to @filter, or %NULL
1484 * Set a filter on the #EmpathyAccountChooser included in the
1485 * #EmpathyContactWidget.
1488 empathy_contact_widget_set_account_filter (
1490 EmpathyAccountChooserFilterFunc filter,
1493 EmpathyContactWidget *information;
1494 EmpathyAccountChooser *chooser;
1496 g_return_if_fail (GTK_IS_WIDGET (widget));
1498 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1502 chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
1504 empathy_account_chooser_set_filter (chooser, filter, user_data);