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/util.h>
37 #include <libempathy/empathy-tp-contact-factory.h>
38 #include <libempathy/empathy-contact-manager.h>
39 #include <libempathy/empathy-contact-list.h>
40 #include <libempathy/empathy-location.h>
41 #include <libempathy/empathy-time.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy/empathy-account.h>
45 #include "empathy-contact-widget.h"
46 #include "empathy-account-chooser.h"
47 #include "empathy-avatar-chooser.h"
48 #include "empathy-avatar-image.h"
49 #include "empathy-ui-utils.h"
50 #include "empathy-kludge-label.h"
52 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
53 #include <libempathy/empathy-debug.h>
56 * SECTION:empathy-contact-widget
57 * @title:EmpathyContactWidget
58 * @short_description: A widget used to display and edit details about a contact
59 * @include: libempathy-empathy-contact-widget.h
61 * #EmpathyContactWidget is a widget which displays appropriate widgets
62 * with details about a contact, also allowing changing these details,
67 * EmpathyContactWidget:
68 * @parent: parent object
70 * Widget which displays appropriate widgets with details about a contact,
71 * also allowing changing these details, if desired.
74 /* Delay before updating the widget when the id entry changed (seconds) */
75 #define ID_CHANGED_TIMEOUT 1
79 EmpathyTpContactFactory *factory;
80 EmpathyContactManager *manager;
81 EmpathyContact *contact;
82 EmpathyContactWidgetFlags flags;
83 guint widget_id_timeout;
85 GtkWidget *vbox_contact_widget;
88 GtkWidget *vbox_contact;
89 GtkWidget *widget_avatar;
90 GtkWidget *widget_account;
92 GtkWidget *widget_alias;
93 GtkWidget *label_alias;
94 GtkWidget *entry_alias;
95 GtkWidget *hbox_presence;
96 GtkWidget *image_state;
97 GtkWidget *label_status;
98 GtkWidget *table_contact;
99 GtkWidget *vbox_avatar;
102 GtkWidget *vbox_location;
103 GtkWidget *subvbox_location;
104 GtkWidget *table_location;
105 GtkWidget *label_location;
106 #if HAVE_LIBCHAMPLAIN
107 GtkWidget *viewport_map;
108 GtkWidget *map_view_embed;
109 ChamplainView *map_view;
113 GtkWidget *vbox_groups;
114 GtkWidget *entry_group;
115 GtkWidget *button_group;
116 GtkWidget *treeview_groups;
119 GtkWidget *vbox_details;
120 GtkWidget *table_details;
121 GtkWidget *hbox_details_requested;
124 GtkWidget *vbox_client;
125 GtkWidget *table_client;
126 GtkWidget *hbox_client_requested;
127 } EmpathyContactWidget;
131 EmpathyContactWidget *information;
134 GtkTreeIter found_iter;
137 static void contact_widget_destroy_cb (GtkWidget *widget,
138 EmpathyContactWidget *information);
139 static void contact_widget_remove_contact (EmpathyContactWidget *information);
140 static void contact_widget_set_contact (EmpathyContactWidget *information,
141 EmpathyContact *contact);
142 static void contact_widget_contact_setup (EmpathyContactWidget *information);
143 static void contact_widget_contact_update (EmpathyContactWidget *information);
144 static void contact_widget_change_contact (EmpathyContactWidget *information);
145 static void contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
146 EmpathyContactWidget *information);
147 static gboolean contact_widget_id_focus_out_cb (GtkWidget *widget,
148 GdkEventFocus *event, EmpathyContactWidget *information);
149 static gboolean contact_widget_entry_alias_focus_event_cb (
150 GtkEditable *editable, GdkEventFocus *event,
151 EmpathyContactWidget *information);
152 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
153 static void contact_widget_presence_notify_cb (
154 EmpathyContactWidget *information);
155 static void contact_widget_avatar_notify_cb (
156 EmpathyContactWidget *information);
157 static void contact_widget_groups_setup (
158 EmpathyContactWidget *information);
159 static void contact_widget_groups_update (EmpathyContactWidget *information);
160 static void contact_widget_model_setup (EmpathyContactWidget *information);
161 static void contact_widget_model_populate_columns (
162 EmpathyContactWidget *information);
163 static void contact_widget_groups_populate_data (
164 EmpathyContactWidget *information);
165 static void contact_widget_groups_notify_cb (
166 EmpathyContactWidget *information);
167 static gboolean contact_widget_model_find_name (
168 EmpathyContactWidget *information,const gchar *name, GtkTreeIter *iter);
169 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
170 GtkTreePath *path, GtkTreeIter *iter, FindName *data);
171 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
172 gchar *path_string, EmpathyContactWidget *information);
173 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
174 EmpathyContactWidget *information);
175 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
176 EmpathyContactWidget *information);
177 static void contact_widget_button_group_clicked_cb (GtkButton *button,
178 EmpathyContactWidget *information);
179 static void contact_widget_details_setup (EmpathyContactWidget *information);
180 static void contact_widget_details_update (EmpathyContactWidget *information);
181 static void contact_widget_client_setup (EmpathyContactWidget *information);
182 static void contact_widget_client_update (EmpathyContactWidget *information);
183 static void contact_widget_location_update (EmpathyContactWidget *information);
194 * empathy_contact_widget_new:
195 * @contact: an #EmpathyContact
196 * @flags: #EmpathyContactWidgetFlags for the new contact widget
198 * Creates a new #EmpathyContactWidget.
200 * Return value: a new #EmpathyContactWidget
203 empathy_contact_widget_new (EmpathyContact *contact,
204 EmpathyContactWidgetFlags flags)
206 EmpathyContactWidget *information;
210 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
212 information = g_slice_new0 (EmpathyContactWidget);
213 information->flags = flags;
215 filename = empathy_file_lookup ("empathy-contact-widget.ui",
217 gui = empathy_builder_get_file (filename,
218 "vbox_contact_widget", &information->vbox_contact_widget,
219 "vbox_contact", &information->vbox_contact,
220 "hbox_presence", &information->hbox_presence,
221 "label_alias", &information->label_alias,
222 "image_state", &information->image_state,
223 "table_contact", &information->table_contact,
224 "vbox_avatar", &information->vbox_avatar,
225 "vbox_location", &information->vbox_location,
226 "subvbox_location", &information->subvbox_location,
227 "label_location", &information->label_location,
228 #if HAVE_LIBCHAMPLAIN
229 "viewport_map", &information->viewport_map,
231 "vbox_groups", &information->vbox_groups,
232 "entry_group", &information->entry_group,
233 "button_group", &information->button_group,
234 "treeview_groups", &information->treeview_groups,
235 "vbox_details", &information->vbox_details,
236 "table_details", &information->table_details,
237 "hbox_details_requested", &information->hbox_details_requested,
238 "vbox_client", &information->vbox_client,
239 "table_client", &information->table_client,
240 "hbox_client_requested", &information->hbox_client_requested,
244 empathy_builder_connect (gui, information,
245 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
246 "entry_group", "changed", contact_widget_entry_group_changed_cb,
247 "entry_group", "activate", contact_widget_entry_group_activate_cb,
248 "button_group", "clicked", contact_widget_button_group_clicked_cb,
250 information->table_location = NULL;
252 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
253 "EmpathyContactWidget",
257 contact_widget_contact_setup (information);
258 contact_widget_groups_setup (information);
259 contact_widget_details_setup (information);
260 contact_widget_client_setup (information);
263 contact_widget_set_contact (information, contact);
265 else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
266 information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
267 contact_widget_change_contact (information);
269 return empathy_builder_unref_and_keep_widget (gui,
270 information->vbox_contact_widget);
274 * empathy_contact_widget_get_contact:
275 * @widget: an #EmpathyContactWidget
277 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
279 * Returns: the #EmpathyContact associated with @widget
282 empathy_contact_widget_get_contact (GtkWidget *widget)
284 EmpathyContactWidget *information;
286 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
288 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
292 return information->contact;
296 * empathy_contact_widget_set_contact:
297 * @widget: an #EmpathyContactWidget
298 * @contact: a different #EmpathyContact
300 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
303 empathy_contact_widget_set_contact (GtkWidget *widget,
304 EmpathyContact *contact)
306 EmpathyContactWidget *information;
308 g_return_if_fail (GTK_IS_WIDGET (widget));
309 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
311 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
315 contact_widget_set_contact (information, contact);
319 * empathy_contact_widget_set_account_filter:
320 * @widget: an #EmpathyContactWidget
321 * @filter: a #EmpathyAccountChooserFilterFunc
322 * @user_data: user data to pass to @filter, or %NULL
324 * Set a filter on the #EmpathyAccountChooser included in the
325 * #EmpathyContactWidget.
328 empathy_contact_widget_set_account_filter (
330 EmpathyAccountChooserFilterFunc filter,
333 EmpathyContactWidget *information;
334 EmpathyAccountChooser *chooser;
336 g_return_if_fail (GTK_IS_WIDGET (widget));
338 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
342 chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
344 empathy_account_chooser_set_filter (chooser, filter, user_data);
348 contact_widget_destroy_cb (GtkWidget *widget,
349 EmpathyContactWidget *information)
351 contact_widget_remove_contact (information);
353 if (information->widget_id_timeout != 0)
355 g_source_remove (information->widget_id_timeout);
357 if (information->manager)
359 g_object_unref (information->manager);
362 g_slice_free (EmpathyContactWidget, information);
366 contact_widget_remove_contact (EmpathyContactWidget *information)
368 if (information->contact)
370 g_signal_handlers_disconnect_by_func (information->contact,
371 contact_widget_name_notify_cb, information);
372 g_signal_handlers_disconnect_by_func (information->contact,
373 contact_widget_presence_notify_cb, information);
374 g_signal_handlers_disconnect_by_func (information->contact,
375 contact_widget_avatar_notify_cb, information);
376 g_signal_handlers_disconnect_by_func (information->contact,
377 contact_widget_groups_notify_cb, information);
379 g_object_unref (information->contact);
380 g_object_unref (information->factory);
381 information->contact = NULL;
382 information->factory = NULL;
387 contact_widget_set_contact (EmpathyContactWidget *information,
388 EmpathyContact *contact)
390 if (contact == information->contact)
393 contact_widget_remove_contact (information);
396 TpConnection *connection;
398 connection = empathy_contact_get_connection (contact);
399 information->contact = g_object_ref (contact);
400 information->factory = empathy_tp_contact_factory_dup_singleton (connection);
403 /* set the selected account to be the account this contact came from */
404 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
405 empathy_account_chooser_set_account (
406 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
407 empathy_contact_get_account (contact));
410 /* Update information for widgets */
411 contact_widget_contact_update (information);
412 contact_widget_groups_update (information);
413 contact_widget_details_update (information);
414 contact_widget_client_update (information);
415 contact_widget_location_update (information);
419 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
421 contact_widget_change_contact (self);
426 contact_widget_id_changed_cb (GtkEntry *entry,
427 EmpathyContactWidget *self)
429 if (self->widget_id_timeout != 0)
431 g_source_remove (self->widget_id_timeout);
434 self->widget_id_timeout =
435 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
436 (GSourceFunc) contact_widget_id_activate_timeout, self);
440 save_avatar_menu_activate_cb (GtkWidget *widget,
441 EmpathyContactWidget *information)
444 EmpathyAvatar *avatar;
445 gchar *ext = NULL, *filename;
447 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
449 GTK_FILE_CHOOSER_ACTION_SAVE,
450 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
451 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
454 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
457 /* look for the avatar extension */
458 avatar = empathy_contact_get_avatar (information->contact);
459 if (avatar->format != NULL)
463 splitted = g_strsplit (avatar->format, "/", 2);
464 if (splitted[0] != NULL && splitted[1] != NULL)
465 ext = g_strdup (splitted[1]);
467 g_strfreev (splitted);
471 /* Avatar was loaded from the cache so was converted to PNG */
472 ext = g_strdup ("png");
479 id = tp_escape_as_identifier (empathy_contact_get_id (
480 information->contact));
482 filename = g_strdup_printf ("%s.%s", id, ext);
483 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
490 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
492 GError *error = NULL;
494 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
496 if (!empathy_avatar_save_to_file (avatar, filename, &error))
501 dialog = gtk_message_dialog_new (NULL, 0,
502 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
503 _("Unable to save avatar"));
505 gtk_message_dialog_format_secondary_text (
506 GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
508 g_signal_connect (dialog, "response",
509 G_CALLBACK (gtk_widget_destroy), NULL);
511 gtk_window_present (GTK_WINDOW (dialog));
513 g_clear_error (&error);
519 gtk_widget_destroy (dialog);
523 popup_avatar_menu (EmpathyContactWidget *information,
525 GdkEventButton *event)
527 GtkWidget *menu, *item;
528 gint button, event_time;
530 if (information->contact == NULL ||
531 empathy_contact_get_avatar (information->contact) == NULL)
534 menu = gtk_menu_new ();
536 /* Add "Save as..." entry */
537 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
538 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
539 gtk_widget_show (item);
541 g_signal_connect (item, "activate",
542 G_CALLBACK (save_avatar_menu_activate_cb), information);
546 button = event->button;
547 event_time = event->time;
552 event_time = gtk_get_current_event_time ();
555 gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
556 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
561 widget_avatar_popup_menu_cb (GtkWidget *widget,
562 EmpathyContactWidget *information)
564 popup_avatar_menu (information, widget, NULL);
570 widget_avatar_button_press_event_cb (GtkWidget *widget,
571 GdkEventButton *event,
572 EmpathyContactWidget *information)
574 /* Ignore double-clicks and triple-clicks */
575 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
577 popup_avatar_menu (information, widget, event);
585 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
586 EmpathyAvatarChooser *avatar_chooser)
588 TpConnection *connection;
590 connection = empathy_account_chooser_get_connection (account_chooser);
591 g_object_set (avatar_chooser, "connection", connection, NULL);
595 contact_widget_contact_setup (EmpathyContactWidget *information)
597 /* Setup label_status as a KludgeLabel */
598 information->label_status = empathy_kludge_label_new ("");
599 gtk_box_pack_start (GTK_BOX (information->hbox_presence),
600 information->label_status, TRUE, TRUE, 0);
601 gtk_widget_show (information->label_status);
603 /* Setup account label/chooser */
604 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
606 information->widget_account = empathy_account_chooser_new ();
608 g_signal_connect_swapped (information->widget_account, "changed",
609 G_CALLBACK (contact_widget_change_contact),
614 information->widget_account = gtk_label_new (NULL);
615 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
616 gtk_label_set_selectable (GTK_LABEL (information->widget_account), TRUE);
618 gtk_misc_set_alignment (GTK_MISC (information->widget_account), 0, 0.5);
620 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
621 information->widget_account,
623 gtk_widget_show (information->widget_account);
625 /* Set up avatar chooser/display */
626 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
628 information->widget_avatar = empathy_avatar_chooser_new ();
629 g_signal_connect (information->widget_avatar, "changed",
630 G_CALLBACK (contact_widget_avatar_changed_cb),
632 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
634 g_signal_connect (information->widget_account, "changed",
635 G_CALLBACK (update_avatar_chooser_account_cb),
636 information->widget_avatar);
637 update_avatar_chooser_account_cb (
638 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
639 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
644 information->widget_avatar = empathy_avatar_image_new ();
646 g_signal_connect (information->widget_avatar, "popup-menu",
647 G_CALLBACK (widget_avatar_popup_menu_cb), information);
648 g_signal_connect (information->widget_avatar, "button-press-event",
649 G_CALLBACK (widget_avatar_button_press_event_cb), information);
652 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
653 information->widget_avatar,
656 gtk_widget_show (information->widget_avatar);
658 /* Setup id label/entry */
659 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
661 information->widget_id = gtk_entry_new ();
662 g_signal_connect (information->widget_id, "focus-out-event",
663 G_CALLBACK (contact_widget_id_focus_out_cb),
665 g_signal_connect (information->widget_id, "changed",
666 G_CALLBACK (contact_widget_id_changed_cb),
671 information->widget_id = gtk_label_new (NULL);
672 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
673 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
675 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
677 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
678 information->widget_id,
680 gtk_widget_show (information->widget_id);
682 /* Setup alias label/entry */
683 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
685 information->widget_alias = gtk_entry_new ();
686 g_signal_connect (information->widget_alias, "focus-out-event",
687 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
689 /* Make return activate the window default (the Close button) */
690 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
695 information->widget_alias = gtk_label_new (NULL);
696 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
697 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
699 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
701 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
702 information->widget_alias,
704 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
705 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
707 gtk_widget_show (information->widget_alias);
711 contact_widget_contact_update (EmpathyContactWidget *information)
713 EmpathyAccount *account = NULL;
714 const gchar *id = NULL;
716 /* Connect and get info from new contact */
717 if (information->contact)
719 g_signal_connect_swapped (information->contact, "notify::name",
720 G_CALLBACK (contact_widget_name_notify_cb), information);
721 g_signal_connect_swapped (information->contact, "notify::presence",
722 G_CALLBACK (contact_widget_presence_notify_cb), information);
723 g_signal_connect_swapped (information->contact,
724 "notify::presence-message",
725 G_CALLBACK (contact_widget_presence_notify_cb), information);
726 g_signal_connect_swapped (information->contact, "notify::avatar",
727 G_CALLBACK (contact_widget_avatar_notify_cb), information);
729 account = empathy_contact_get_account (information->contact);
730 id = empathy_contact_get_id (information->contact);
733 /* Update account widget */
734 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
738 g_signal_handlers_block_by_func (information->widget_account,
739 contact_widget_change_contact,
741 empathy_account_chooser_set_account (
742 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
743 g_signal_handlers_unblock_by_func (information->widget_account,
744 contact_widget_change_contact, information);
753 name = empathy_account_get_display_name (account);
754 gtk_label_set_label (GTK_LABEL (information->widget_account), name);
758 /* Update id widget */
759 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
760 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
762 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
764 /* Update other widgets */
765 if (information->contact)
767 contact_widget_name_notify_cb (information);
768 contact_widget_presence_notify_cb (information);
769 contact_widget_avatar_notify_cb (information);
771 gtk_widget_show (information->label_alias);
772 gtk_widget_show (information->widget_alias);
773 gtk_widget_show (information->hbox_presence);
774 gtk_widget_show (information->widget_avatar);
778 gtk_widget_hide (information->label_alias);
779 gtk_widget_hide (information->widget_alias);
780 gtk_widget_hide (information->hbox_presence);
781 gtk_widget_hide (information->widget_avatar);
786 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
787 EmpathyContact *contact,
790 GObject *weak_object)
792 EmpathyContactWidget *information = user_data;
796 DEBUG ("Error: %s", error->message);
800 contact_widget_set_contact (information, contact);
804 contact_widget_change_contact (EmpathyContactWidget *information)
806 EmpathyTpContactFactory *factory;
807 TpConnection *connection;
809 connection = empathy_account_chooser_get_connection (
810 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
814 factory = empathy_tp_contact_factory_dup_singleton (connection);
815 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
819 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
820 if (!EMP_STR_EMPTY (id))
822 empathy_tp_contact_factory_get_from_id (factory, id,
823 contact_widget_got_contact_cb, information, NULL,
824 G_OBJECT (information->vbox_contact_widget));
829 empathy_tp_contact_factory_get_from_handle (factory,
830 tp_connection_get_self_handle (connection),
831 contact_widget_got_contact_cb, information, NULL,
832 G_OBJECT (information->vbox_contact_widget));
835 g_object_unref (factory);
839 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
840 EmpathyContactWidget *information)
844 const gchar *mime_type;
846 empathy_avatar_chooser_get_image_data (
847 EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
848 &data, &size, &mime_type);
849 empathy_tp_contact_factory_set_avatar (information->factory,
850 data, size, mime_type);
854 contact_widget_id_focus_out_cb (GtkWidget *widget,
855 GdkEventFocus *event,
856 EmpathyContactWidget *information)
858 contact_widget_change_contact (information);
863 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
864 GdkEventFocus *event,
865 EmpathyContactWidget *information)
867 if (information->contact)
871 alias = gtk_entry_get_text (GTK_ENTRY (editable));
872 empathy_tp_contact_factory_set_alias (information->factory,
873 information->contact, alias);
880 contact_widget_name_notify_cb (EmpathyContactWidget *information)
882 if (GTK_IS_ENTRY (information->widget_alias))
883 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
884 empathy_contact_get_name (information->contact));
886 gtk_label_set_label (GTK_LABEL (information->widget_alias),
887 empathy_contact_get_name (information->contact));
891 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
893 gtk_label_set_text (GTK_LABEL (information->label_status),
894 empathy_contact_get_status (information->contact));
895 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
896 empathy_icon_name_for_contact (information->contact),
897 GTK_ICON_SIZE_BUTTON);
901 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
903 EmpathyAvatar *avatar = NULL;
905 if (information->contact)
906 avatar = empathy_contact_get_avatar (information->contact);
908 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
910 g_signal_handlers_block_by_func (information->widget_avatar,
911 contact_widget_avatar_changed_cb,
913 empathy_avatar_chooser_set (
914 EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
915 g_signal_handlers_unblock_by_func (information->widget_avatar,
916 contact_widget_avatar_changed_cb, information);
919 empathy_avatar_image_set (
920 EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
924 contact_widget_groups_setup (EmpathyContactWidget *information)
926 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
928 information->manager = empathy_contact_manager_dup_singleton ();
929 contact_widget_model_setup (information);
934 contact_widget_groups_update (EmpathyContactWidget *information)
936 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
937 information->contact)
939 g_signal_connect_swapped (information->contact, "notify::groups",
940 G_CALLBACK (contact_widget_groups_notify_cb), information);
941 contact_widget_groups_populate_data (information);
943 gtk_widget_show (information->vbox_groups);
946 gtk_widget_hide (information->vbox_groups);
950 contact_widget_model_setup (EmpathyContactWidget *information)
954 GtkTreeSelection *selection;
956 view = GTK_TREE_VIEW (information->treeview_groups);
958 store = gtk_list_store_new (COL_COUNT,
959 G_TYPE_STRING, /* name */
960 G_TYPE_BOOLEAN, /* enabled */
961 G_TYPE_BOOLEAN); /* editable */
963 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
965 selection = gtk_tree_view_get_selection (view);
966 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
968 contact_widget_model_populate_columns (information);
970 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
971 COL_NAME, GTK_SORT_ASCENDING);
973 g_object_unref (store);
977 contact_widget_model_populate_columns (EmpathyContactWidget *information)
981 GtkTreeViewColumn *column;
982 GtkCellRenderer *renderer;
985 view = GTK_TREE_VIEW (information->treeview_groups);
986 model = gtk_tree_view_get_model (view);
988 renderer = gtk_cell_renderer_toggle_new ();
989 g_signal_connect (renderer, "toggled",
990 G_CALLBACK (contact_widget_cell_toggled), information);
992 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
993 "active", COL_ENABLED, NULL);
995 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
996 gtk_tree_view_column_set_fixed_width (column, 50);
997 gtk_tree_view_append_column (view, column);
999 renderer = gtk_cell_renderer_text_new ();
1000 col_offset = gtk_tree_view_insert_column_with_attributes (view,
1004 /* "editable", COL_EDITABLE, */
1007 g_object_set_data (G_OBJECT (renderer),
1008 "column", GINT_TO_POINTER (COL_NAME));
1010 column = gtk_tree_view_get_column (view, col_offset - 1);
1011 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
1012 gtk_tree_view_column_set_resizable (column,FALSE);
1013 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
1017 contact_widget_groups_populate_data (EmpathyContactWidget *information)
1020 GtkListStore *store;
1022 GList *my_groups, *l;
1025 view = GTK_TREE_VIEW (information->treeview_groups);
1026 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1027 gtk_list_store_clear (store);
1029 all_groups = empathy_contact_list_get_all_groups (
1030 EMPATHY_CONTACT_LIST (information->manager));
1031 my_groups = empathy_contact_list_get_groups (
1032 EMPATHY_CONTACT_LIST (information->manager),
1033 information->contact);
1035 for (l = all_groups; l; l = l->next)
1037 const gchar *group_str;
1040 group_str = l->data;
1042 enabled = g_list_find_custom (my_groups,
1043 group_str, (GCompareFunc) strcmp) != NULL;
1045 gtk_list_store_append (store, &iter);
1046 gtk_list_store_set (store, &iter,
1047 COL_NAME, group_str,
1049 COL_ENABLED, enabled,
1053 g_list_foreach (all_groups, (GFunc) g_free, NULL);
1054 g_list_foreach (my_groups, (GFunc) g_free, NULL);
1055 g_list_free (all_groups);
1056 g_list_free (my_groups);
1060 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
1062 /* FIXME: not implemented */
1066 contact_widget_model_find_name (EmpathyContactWidget *information,
1071 GtkTreeModel *model;
1074 if (EMP_STR_EMPTY (name))
1077 data.information = information;
1081 view = GTK_TREE_VIEW (information->treeview_groups);
1082 model = gtk_tree_view_get_model (view);
1084 gtk_tree_model_foreach (model,
1085 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
1088 if (data.found == TRUE)
1090 *iter = data.found_iter;
1098 contact_widget_model_find_name_foreach (GtkTreeModel *model,
1105 gtk_tree_model_get (model, iter,
1112 if (data->name && strcmp (data->name, name) == 0)
1115 data->found_iter = *iter;
1128 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
1130 EmpathyContactWidget *information)
1133 GtkTreeModel *model;
1134 GtkListStore *store;
1140 view = GTK_TREE_VIEW (information->treeview_groups);
1141 model = gtk_tree_view_get_model (view);
1142 store = GTK_LIST_STORE (model);
1144 path = gtk_tree_path_new_from_string (path_string);
1146 gtk_tree_model_get_iter (model, &iter, path);
1147 gtk_tree_model_get (model, &iter,
1148 COL_ENABLED, &enabled,
1152 gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
1153 gtk_tree_path_free (path);
1159 empathy_contact_list_remove_from_group (
1160 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1165 empathy_contact_list_add_to_group (
1166 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1174 contact_widget_entry_group_changed_cb (GtkEditable *editable,
1175 EmpathyContactWidget *information)
1180 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1182 if (contact_widget_model_find_name (information, group, &iter))
1183 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
1185 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
1186 !EMP_STR_EMPTY (group));
1190 contact_widget_entry_group_activate_cb (GtkEntry *entry,
1191 EmpathyContactWidget *information)
1193 gtk_widget_activate (GTK_WIDGET (information->button_group));
1197 contact_widget_button_group_clicked_cb (GtkButton *button,
1198 EmpathyContactWidget *information)
1201 GtkListStore *store;
1205 view = GTK_TREE_VIEW (information->treeview_groups);
1206 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1208 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1210 gtk_list_store_append (store, &iter);
1211 gtk_list_store_set (store, &iter,
1216 empathy_contact_list_add_to_group (
1217 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1222 contact_widget_details_setup (EmpathyContactWidget *information)
1224 /* FIXME: Needs new telepathy spec */
1225 gtk_widget_hide (information->vbox_details);
1229 contact_widget_details_update (EmpathyContactWidget *information)
1231 /* FIXME: Needs new telepathy spec */
1235 contact_widget_client_setup (EmpathyContactWidget *information)
1237 /* FIXME: Needs new telepathy spec */
1238 gtk_widget_hide (information->vbox_client);
1242 contact_widget_client_update (EmpathyContactWidget *information)
1244 /* FIXME: Needs new telepathy spec */
1247 /* Converts the Location's GHashTable's key to a user readable string */
1248 static const gchar *
1249 location_key_to_label (const gchar *key)
1251 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
1252 return _("Country ISO Code:");
1253 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
1254 return _("Country:");
1255 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
1257 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
1259 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
1261 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
1262 return _("Postal Code:");
1263 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
1264 return _("Street:");
1265 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
1266 return _("Building:");
1267 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
1269 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
1271 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
1273 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
1274 return _("Description:");
1275 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
1277 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
1278 return _("Accuracy Level:");
1279 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
1281 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
1282 return _("Vertical Error (meters):");
1283 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
1284 return _("Horizontal Error (meters):");
1285 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
1287 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
1288 return _("Bearing:");
1289 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
1290 return _("Climb Speed:");
1291 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
1292 return _("Last Updated on:");
1293 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
1294 return _("Longitude:");
1295 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
1296 return _("Latitude:");
1297 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
1298 return _("Altitude:");
1301 DEBUG ("Unexpected Location key: %s", key);
1307 contact_widget_location_update (EmpathyContactWidget *information)
1309 GHashTable *location;
1311 gdouble lat = 0.0, lon = 0.0;
1312 gboolean has_position = TRUE;
1315 GHashTableIter iter;
1316 gpointer key, pvalue;
1318 if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
1320 gtk_widget_hide (information->vbox_location);
1324 location = empathy_contact_get_location (information->contact);
1325 if (location == NULL || g_hash_table_size (location) == 0)
1327 gtk_widget_hide (information->vbox_location);
1331 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
1333 has_position = FALSE;
1335 lat = g_value_get_double (value);
1337 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
1339 has_position = FALSE;
1341 lon = g_value_get_double (value);
1343 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
1345 gtk_label_set_markup (GTK_LABEL (information->label_location), _("<b>Location</b>"));
1353 stamp = g_value_get_int64 (value);
1356 user_date = empathy_time_to_string_relative (time);
1358 text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
1359 gtk_label_set_markup (GTK_LABEL (information->label_location), text);
1364 /* Prepare the location information table */
1365 if (information->table_location != NULL)
1367 gtk_widget_destroy (information->table_location);
1370 information->table_location = gtk_table_new (1, 2, FALSE);
1371 gtk_box_pack_start (GTK_BOX (information->subvbox_location),
1372 information->table_location, FALSE, FALSE, 5);
1374 g_hash_table_iter_init (&iter, location);
1375 while (g_hash_table_iter_next (&iter, &key, &pvalue))
1378 const gchar* user_label;
1380 char *svalue = NULL;
1382 skey = (const gchar *) key;
1384 user_label = location_key_to_label (skey);
1385 gvalue = (GValue *) pvalue;
1387 label = gtk_label_new (user_label);
1388 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1389 gtk_table_attach (GTK_TABLE (information->table_location),
1390 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
1391 gtk_widget_show (label);
1393 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
1396 dvalue = g_value_get_double (gvalue);
1397 svalue = g_strdup_printf ("%f", dvalue);
1399 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
1401 svalue = g_value_dup_string (gvalue);
1403 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
1407 time = g_value_get_int64 (value);
1408 svalue = empathy_time_to_string_utc (time, _("%B %e, %Y at %R UTC"));
1413 label = gtk_label_new (svalue);
1414 gtk_table_attach_defaults (GTK_TABLE (information->table_location),
1415 label, 1, 2, row, row + 1);
1416 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1417 gtk_widget_show (label);
1424 gtk_widget_show (information->table_location);
1426 #if HAVE_LIBCHAMPLAIN
1427 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
1431 !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1433 ClutterActor *marker;
1434 ChamplainLayer *layer;
1436 information->map_view_embed = gtk_champlain_embed_new ();
1437 information->map_view = gtk_champlain_embed_get_view (
1438 GTK_CHAMPLAIN_EMBED (information->map_view_embed));
1440 gtk_container_add (GTK_CONTAINER (information->viewport_map),
1441 information->map_view_embed);
1442 g_object_set (G_OBJECT (information->map_view), "show-license", FALSE,
1443 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
1446 layer = champlain_layer_new ();
1447 champlain_view_add_layer (information->map_view, layer);
1449 marker = champlain_marker_new_with_text (
1450 empathy_contact_get_name (information->contact), NULL, NULL, NULL);
1451 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
1452 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
1454 champlain_view_center_on (information->map_view, lat, lon);
1455 gtk_widget_show_all (information->viewport_map);
1459 gtk_widget_show (information->vbox_location);