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"
51 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
52 #include <libempathy/empathy-debug.h>
55 * SECTION:empathy-contact-widget
56 * @title:EmpathyContactWidget
57 * @short_description: A widget used to display and edit details about a contact
58 * @include: libempathy-empathy-contact-widget.h
60 * #EmpathyContactWidget is a widget which displays appropriate widgets
61 * with details about a contact, also allowing changing these details,
66 * EmpathyContactWidget:
67 * @parent: parent object
69 * Widget which displays appropriate widgets with details about a contact,
70 * also allowing changing these details, if desired.
73 /* Delay before updating the widget when the id entry changed (seconds) */
74 #define ID_CHANGED_TIMEOUT 1
78 EmpathyTpContactFactory *factory;
79 EmpathyContactManager *manager;
80 EmpathyContact *contact;
81 EmpathyContactWidgetFlags flags;
82 guint widget_id_timeout;
84 GtkWidget *vbox_contact_widget;
87 GtkWidget *vbox_contact;
88 GtkWidget *widget_avatar;
89 GtkWidget *widget_account;
91 GtkWidget *widget_alias;
92 GtkWidget *label_alias;
93 GtkWidget *entry_alias;
94 GtkWidget *hbox_presence;
95 GtkWidget *image_state;
96 GtkWidget *label_status;
97 GtkWidget *table_contact;
98 GtkWidget *vbox_avatar;
101 GtkWidget *vbox_location;
102 GtkWidget *subvbox_location;
103 GtkWidget *table_location;
104 GtkWidget *label_location;
105 #if HAVE_LIBCHAMPLAIN
106 GtkWidget *viewport_map;
107 GtkWidget *map_view_embed;
108 ChamplainView *map_view;
112 GtkWidget *vbox_groups;
113 GtkWidget *entry_group;
114 GtkWidget *button_group;
115 GtkWidget *treeview_groups;
118 GtkWidget *vbox_details;
119 GtkWidget *table_details;
120 GtkWidget *hbox_details_requested;
123 GtkWidget *vbox_client;
124 GtkWidget *table_client;
125 GtkWidget *hbox_client_requested;
126 } EmpathyContactWidget;
130 EmpathyContactWidget *information;
133 GtkTreeIter found_iter;
136 static void contact_widget_destroy_cb (GtkWidget *widget,
137 EmpathyContactWidget *information);
138 static void contact_widget_remove_contact (EmpathyContactWidget *information);
139 static void contact_widget_set_contact (EmpathyContactWidget *information,
140 EmpathyContact *contact);
141 static void contact_widget_contact_setup (EmpathyContactWidget *information);
142 static void contact_widget_contact_update (EmpathyContactWidget *information);
143 static void contact_widget_change_contact (EmpathyContactWidget *information);
144 static void contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
145 EmpathyContactWidget *information);
146 static gboolean contact_widget_id_focus_out_cb (GtkWidget *widget,
147 GdkEventFocus *event, EmpathyContactWidget *information);
148 static gboolean contact_widget_entry_alias_focus_event_cb (
149 GtkEditable *editable, GdkEventFocus *event,
150 EmpathyContactWidget *information);
151 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
152 static void contact_widget_presence_notify_cb (
153 EmpathyContactWidget *information);
154 static void contact_widget_avatar_notify_cb (
155 EmpathyContactWidget *information);
156 static void contact_widget_groups_setup (
157 EmpathyContactWidget *information);
158 static void contact_widget_groups_update (EmpathyContactWidget *information);
159 static void contact_widget_model_setup (EmpathyContactWidget *information);
160 static void contact_widget_model_populate_columns (
161 EmpathyContactWidget *information);
162 static void contact_widget_groups_populate_data (
163 EmpathyContactWidget *information);
164 static void contact_widget_groups_notify_cb (
165 EmpathyContactWidget *information);
166 static gboolean contact_widget_model_find_name (
167 EmpathyContactWidget *information,const gchar *name, GtkTreeIter *iter);
168 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
169 GtkTreePath *path, GtkTreeIter *iter, FindName *data);
170 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
171 gchar *path_string, EmpathyContactWidget *information);
172 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
173 EmpathyContactWidget *information);
174 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
175 EmpathyContactWidget *information);
176 static void contact_widget_button_group_clicked_cb (GtkButton *button,
177 EmpathyContactWidget *information);
178 static void contact_widget_details_setup (EmpathyContactWidget *information);
179 static void contact_widget_details_update (EmpathyContactWidget *information);
180 static void contact_widget_client_setup (EmpathyContactWidget *information);
181 static void contact_widget_client_update (EmpathyContactWidget *information);
182 static void contact_widget_location_update (EmpathyContactWidget *information);
193 * empathy_contact_widget_new:
194 * @contact: an #EmpathyContact
195 * @flags: #EmpathyContactWidgetFlags for the new contact widget
197 * Creates a new #EmpathyContactWidget.
199 * Return value: a new #EmpathyContactWidget
202 empathy_contact_widget_new (EmpathyContact *contact,
203 EmpathyContactWidgetFlags flags)
205 EmpathyContactWidget *information;
209 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
211 information = g_slice_new0 (EmpathyContactWidget);
212 information->flags = flags;
214 filename = empathy_file_lookup ("empathy-contact-widget.ui",
216 gui = empathy_builder_get_file (filename,
217 "vbox_contact_widget", &information->vbox_contact_widget,
218 "vbox_contact", &information->vbox_contact,
219 "hbox_presence", &information->hbox_presence,
220 "label_alias", &information->label_alias,
221 "image_state", &information->image_state,
222 "label_status", &information->label_status,
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 /* Update information for widgets */
404 contact_widget_contact_update (information);
405 contact_widget_groups_update (information);
406 contact_widget_details_update (information);
407 contact_widget_client_update (information);
408 contact_widget_location_update (information);
412 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
414 contact_widget_change_contact (self);
419 contact_widget_id_changed_cb (GtkEntry *entry,
420 EmpathyContactWidget *self)
422 if (self->widget_id_timeout != 0)
424 g_source_remove (self->widget_id_timeout);
427 self->widget_id_timeout =
428 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
429 (GSourceFunc) contact_widget_id_activate_timeout, self);
433 save_avatar_menu_activate_cb (GtkWidget *widget,
434 EmpathyContactWidget *information)
437 EmpathyAvatar *avatar;
438 gchar *ext = NULL, *filename;
440 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
442 GTK_FILE_CHOOSER_ACTION_SAVE,
443 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
444 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
447 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
450 /* look for the avatar extension */
451 avatar = empathy_contact_get_avatar (information->contact);
452 if (avatar->format != NULL)
456 splitted = g_strsplit (avatar->format, "/", 2);
457 if (splitted[0] != NULL && splitted[1] != NULL)
458 ext = g_strdup (splitted[1]);
460 g_strfreev (splitted);
464 /* Avatar was loaded from the cache so was converted to PNG */
465 ext = g_strdup ("png");
472 id = tp_escape_as_identifier (empathy_contact_get_id (
473 information->contact));
475 filename = g_strdup_printf ("%s.%s", id, ext);
476 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
483 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
485 GError *error = NULL;
487 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
489 if (!empathy_avatar_save_to_file (avatar, filename, &error))
494 dialog = gtk_message_dialog_new (NULL, 0,
495 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
496 _("Unable to save avatar"));
498 gtk_message_dialog_format_secondary_text (
499 GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
501 g_signal_connect (dialog, "response",
502 G_CALLBACK (gtk_widget_destroy), NULL);
504 gtk_window_present (GTK_WINDOW (dialog));
506 g_clear_error (&error);
512 gtk_widget_destroy (dialog);
516 popup_avatar_menu (EmpathyContactWidget *information,
518 GdkEventButton *event)
520 GtkWidget *menu, *item;
521 gint button, event_time;
523 if (information->contact == NULL ||
524 empathy_contact_get_avatar (information->contact) == NULL)
527 menu = gtk_menu_new ();
529 /* Add "Save as..." entry */
530 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
531 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
532 gtk_widget_show (item);
534 g_signal_connect (item, "activate",
535 G_CALLBACK (save_avatar_menu_activate_cb), information);
539 button = event->button;
540 event_time = event->time;
545 event_time = gtk_get_current_event_time ();
548 gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
549 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
554 widget_avatar_popup_menu_cb (GtkWidget *widget,
555 EmpathyContactWidget *information)
557 popup_avatar_menu (information, widget, NULL);
563 widget_avatar_button_press_event_cb (GtkWidget *widget,
564 GdkEventButton *event,
565 EmpathyContactWidget *information)
567 /* Ignore double-clicks and triple-clicks */
568 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
570 popup_avatar_menu (information, widget, event);
578 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
579 EmpathyAvatarChooser *avatar_chooser)
581 TpConnection *connection;
583 connection = empathy_account_chooser_get_connection (account_chooser);
584 g_object_set (avatar_chooser, "connection", connection, NULL);
588 contact_widget_contact_setup (EmpathyContactWidget *information)
590 /* Setup account label/chooser */
591 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
593 information->widget_account = empathy_account_chooser_new ();
595 g_signal_connect_swapped (information->widget_account, "changed",
596 G_CALLBACK (contact_widget_change_contact),
601 information->widget_account = gtk_label_new (NULL);
602 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
603 gtk_label_set_selectable (GTK_LABEL (information->widget_account), TRUE);
605 gtk_misc_set_alignment (GTK_MISC (information->widget_account), 0, 0.5);
607 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
608 information->widget_account,
610 gtk_widget_show (information->widget_account);
612 /* Set up avatar chooser/display */
613 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
615 information->widget_avatar = empathy_avatar_chooser_new ();
616 g_signal_connect (information->widget_avatar, "changed",
617 G_CALLBACK (contact_widget_avatar_changed_cb),
619 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
621 g_signal_connect (information->widget_account, "changed",
622 G_CALLBACK (update_avatar_chooser_account_cb),
623 information->widget_avatar);
624 update_avatar_chooser_account_cb (
625 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
626 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
631 information->widget_avatar = empathy_avatar_image_new ();
633 g_signal_connect (information->widget_avatar, "popup-menu",
634 G_CALLBACK (widget_avatar_popup_menu_cb), information);
635 g_signal_connect (information->widget_avatar, "button-press-event",
636 G_CALLBACK (widget_avatar_button_press_event_cb), information);
639 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
640 information->widget_avatar,
643 gtk_widget_show (information->widget_avatar);
645 /* Setup id label/entry */
646 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
648 information->widget_id = gtk_entry_new ();
649 g_signal_connect (information->widget_id, "focus-out-event",
650 G_CALLBACK (contact_widget_id_focus_out_cb),
652 g_signal_connect (information->widget_id, "changed",
653 G_CALLBACK (contact_widget_id_changed_cb),
658 information->widget_id = gtk_label_new (NULL);
659 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
660 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
662 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
664 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
665 information->widget_id,
667 gtk_widget_show (information->widget_id);
669 /* Setup alias label/entry */
670 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
672 information->widget_alias = gtk_entry_new ();
673 g_signal_connect (information->widget_alias, "focus-out-event",
674 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
676 /* Make return activate the window default (the Close button) */
677 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
682 information->widget_alias = gtk_label_new (NULL);
683 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
684 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
686 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
688 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
689 information->widget_alias,
691 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
692 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
694 gtk_widget_show (information->widget_alias);
698 contact_widget_contact_update (EmpathyContactWidget *information)
700 EmpathyAccount *account = NULL;
701 const gchar *id = NULL;
703 /* Connect and get info from new contact */
704 if (information->contact)
706 g_signal_connect_swapped (information->contact, "notify::name",
707 G_CALLBACK (contact_widget_name_notify_cb), information);
708 g_signal_connect_swapped (information->contact, "notify::presence",
709 G_CALLBACK (contact_widget_presence_notify_cb), information);
710 g_signal_connect_swapped (information->contact,
711 "notify::presence-message",
712 G_CALLBACK (contact_widget_presence_notify_cb), information);
713 g_signal_connect_swapped (information->contact, "notify::avatar",
714 G_CALLBACK (contact_widget_avatar_notify_cb), information);
716 account = empathy_contact_get_account (information->contact);
717 id = empathy_contact_get_id (information->contact);
720 /* Update account widget */
721 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
725 g_signal_handlers_block_by_func (information->widget_account,
726 contact_widget_change_contact,
728 empathy_account_chooser_set_account (
729 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
730 g_signal_handlers_unblock_by_func (information->widget_account,
731 contact_widget_change_contact, information);
740 name = empathy_account_get_display_name (account);
741 gtk_label_set_label (GTK_LABEL (information->widget_account), name);
745 /* Update id widget */
746 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
747 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
749 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
751 /* Update other widgets */
752 if (information->contact)
754 contact_widget_name_notify_cb (information);
755 contact_widget_presence_notify_cb (information);
756 contact_widget_avatar_notify_cb (information);
758 gtk_widget_show (information->label_alias);
759 gtk_widget_show (information->widget_alias);
760 gtk_widget_show (information->hbox_presence);
761 gtk_widget_show (information->widget_avatar);
765 gtk_widget_hide (information->label_alias);
766 gtk_widget_hide (information->widget_alias);
767 gtk_widget_hide (information->hbox_presence);
768 gtk_widget_hide (information->widget_avatar);
773 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
774 EmpathyContact *contact,
777 GObject *weak_object)
779 EmpathyContactWidget *information = user_data;
783 DEBUG ("Error: %s", error->message);
787 contact_widget_set_contact (information, contact);
791 contact_widget_change_contact (EmpathyContactWidget *information)
793 EmpathyTpContactFactory *factory;
794 TpConnection *connection;
796 connection = empathy_account_chooser_get_connection (
797 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
801 factory = empathy_tp_contact_factory_dup_singleton (connection);
802 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
806 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
807 if (!EMP_STR_EMPTY (id))
809 empathy_tp_contact_factory_get_from_id (factory, id,
810 contact_widget_got_contact_cb, information, NULL,
811 G_OBJECT (information->vbox_contact_widget));
816 empathy_tp_contact_factory_get_from_handle (factory,
817 tp_connection_get_self_handle (connection),
818 contact_widget_got_contact_cb, information, NULL,
819 G_OBJECT (information->vbox_contact_widget));
822 g_object_unref (factory);
826 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
827 EmpathyContactWidget *information)
831 const gchar *mime_type;
833 empathy_avatar_chooser_get_image_data (
834 EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
835 &data, &size, &mime_type);
836 empathy_tp_contact_factory_set_avatar (information->factory,
837 data, size, mime_type);
841 contact_widget_id_focus_out_cb (GtkWidget *widget,
842 GdkEventFocus *event,
843 EmpathyContactWidget *information)
845 contact_widget_change_contact (information);
850 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
851 GdkEventFocus *event,
852 EmpathyContactWidget *information)
854 if (information->contact)
858 alias = gtk_entry_get_text (GTK_ENTRY (editable));
859 empathy_tp_contact_factory_set_alias (information->factory,
860 information->contact, alias);
867 contact_widget_name_notify_cb (EmpathyContactWidget *information)
869 if (GTK_IS_ENTRY (information->widget_alias))
870 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
871 empathy_contact_get_name (information->contact));
873 gtk_label_set_label (GTK_LABEL (information->widget_alias),
874 empathy_contact_get_name (information->contact));
878 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
880 gtk_label_set_text (GTK_LABEL (information->label_status),
881 empathy_contact_get_status (information->contact));
882 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
883 empathy_icon_name_for_contact (information->contact),
884 GTK_ICON_SIZE_BUTTON);
888 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
890 EmpathyAvatar *avatar = NULL;
892 if (information->contact)
893 avatar = empathy_contact_get_avatar (information->contact);
895 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
897 g_signal_handlers_block_by_func (information->widget_avatar,
898 contact_widget_avatar_changed_cb,
900 empathy_avatar_chooser_set (
901 EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
902 g_signal_handlers_unblock_by_func (information->widget_avatar,
903 contact_widget_avatar_changed_cb, information);
906 empathy_avatar_image_set (
907 EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
911 contact_widget_groups_setup (EmpathyContactWidget *information)
913 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
915 information->manager = empathy_contact_manager_dup_singleton ();
916 contact_widget_model_setup (information);
921 contact_widget_groups_update (EmpathyContactWidget *information)
923 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
924 information->contact)
926 g_signal_connect_swapped (information->contact, "notify::groups",
927 G_CALLBACK (contact_widget_groups_notify_cb), information);
928 contact_widget_groups_populate_data (information);
930 gtk_widget_show (information->vbox_groups);
933 gtk_widget_hide (information->vbox_groups);
937 contact_widget_model_setup (EmpathyContactWidget *information)
941 GtkTreeSelection *selection;
943 view = GTK_TREE_VIEW (information->treeview_groups);
945 store = gtk_list_store_new (COL_COUNT,
946 G_TYPE_STRING, /* name */
947 G_TYPE_BOOLEAN, /* enabled */
948 G_TYPE_BOOLEAN); /* editable */
950 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
952 selection = gtk_tree_view_get_selection (view);
953 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
955 contact_widget_model_populate_columns (information);
957 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
958 COL_NAME, GTK_SORT_ASCENDING);
960 g_object_unref (store);
964 contact_widget_model_populate_columns (EmpathyContactWidget *information)
968 GtkTreeViewColumn *column;
969 GtkCellRenderer *renderer;
972 view = GTK_TREE_VIEW (information->treeview_groups);
973 model = gtk_tree_view_get_model (view);
975 renderer = gtk_cell_renderer_toggle_new ();
976 g_signal_connect (renderer, "toggled",
977 G_CALLBACK (contact_widget_cell_toggled), information);
979 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
980 "active", COL_ENABLED, NULL);
982 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
983 gtk_tree_view_column_set_fixed_width (column, 50);
984 gtk_tree_view_append_column (view, column);
986 renderer = gtk_cell_renderer_text_new ();
987 col_offset = gtk_tree_view_insert_column_with_attributes (view,
991 /* "editable", COL_EDITABLE, */
994 g_object_set_data (G_OBJECT (renderer),
995 "column", GINT_TO_POINTER (COL_NAME));
997 column = gtk_tree_view_get_column (view, col_offset - 1);
998 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
999 gtk_tree_view_column_set_resizable (column,FALSE);
1000 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
1004 contact_widget_groups_populate_data (EmpathyContactWidget *information)
1007 GtkListStore *store;
1009 GList *my_groups, *l;
1012 view = GTK_TREE_VIEW (information->treeview_groups);
1013 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1014 gtk_list_store_clear (store);
1016 all_groups = empathy_contact_list_get_all_groups (
1017 EMPATHY_CONTACT_LIST (information->manager));
1018 my_groups = empathy_contact_list_get_groups (
1019 EMPATHY_CONTACT_LIST (information->manager),
1020 information->contact);
1022 for (l = all_groups; l; l = l->next)
1024 const gchar *group_str;
1027 group_str = l->data;
1029 enabled = g_list_find_custom (my_groups,
1030 group_str, (GCompareFunc) strcmp) != NULL;
1032 gtk_list_store_append (store, &iter);
1033 gtk_list_store_set (store, &iter,
1034 COL_NAME, group_str,
1036 COL_ENABLED, enabled,
1040 g_list_foreach (all_groups, (GFunc) g_free, NULL);
1041 g_list_foreach (my_groups, (GFunc) g_free, NULL);
1042 g_list_free (all_groups);
1043 g_list_free (my_groups);
1047 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
1049 /* FIXME: not implemented */
1053 contact_widget_model_find_name (EmpathyContactWidget *information,
1058 GtkTreeModel *model;
1061 if (EMP_STR_EMPTY (name))
1064 data.information = information;
1068 view = GTK_TREE_VIEW (information->treeview_groups);
1069 model = gtk_tree_view_get_model (view);
1071 gtk_tree_model_foreach (model,
1072 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
1075 if (data.found == TRUE)
1077 *iter = data.found_iter;
1085 contact_widget_model_find_name_foreach (GtkTreeModel *model,
1092 gtk_tree_model_get (model, iter,
1099 if (data->name && strcmp (data->name, name) == 0)
1102 data->found_iter = *iter;
1115 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
1117 EmpathyContactWidget *information)
1120 GtkTreeModel *model;
1121 GtkListStore *store;
1127 view = GTK_TREE_VIEW (information->treeview_groups);
1128 model = gtk_tree_view_get_model (view);
1129 store = GTK_LIST_STORE (model);
1131 path = gtk_tree_path_new_from_string (path_string);
1133 gtk_tree_model_get_iter (model, &iter, path);
1134 gtk_tree_model_get (model, &iter,
1135 COL_ENABLED, &enabled,
1139 gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
1140 gtk_tree_path_free (path);
1146 empathy_contact_list_remove_from_group (
1147 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1152 empathy_contact_list_add_to_group (
1153 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1161 contact_widget_entry_group_changed_cb (GtkEditable *editable,
1162 EmpathyContactWidget *information)
1167 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1169 if (contact_widget_model_find_name (information, group, &iter))
1170 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
1172 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
1173 !EMP_STR_EMPTY (group));
1177 contact_widget_entry_group_activate_cb (GtkEntry *entry,
1178 EmpathyContactWidget *information)
1180 gtk_widget_activate (GTK_WIDGET (information->button_group));
1184 contact_widget_button_group_clicked_cb (GtkButton *button,
1185 EmpathyContactWidget *information)
1188 GtkListStore *store;
1192 view = GTK_TREE_VIEW (information->treeview_groups);
1193 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1195 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1197 gtk_list_store_append (store, &iter);
1198 gtk_list_store_set (store, &iter,
1203 empathy_contact_list_add_to_group (
1204 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1209 contact_widget_details_setup (EmpathyContactWidget *information)
1211 /* FIXME: Needs new telepathy spec */
1212 gtk_widget_hide (information->vbox_details);
1216 contact_widget_details_update (EmpathyContactWidget *information)
1218 /* FIXME: Needs new telepathy spec */
1222 contact_widget_client_setup (EmpathyContactWidget *information)
1224 /* FIXME: Needs new telepathy spec */
1225 gtk_widget_hide (information->vbox_client);
1229 contact_widget_client_update (EmpathyContactWidget *information)
1231 /* FIXME: Needs new telepathy spec */
1234 /* Converts the Location's GHashTable's key to a user readable string */
1235 static const gchar *
1236 location_key_to_label (const gchar *key)
1238 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
1239 return _("Country ISO Code:");
1240 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
1241 return _("Country:");
1242 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
1244 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
1246 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
1248 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
1249 return _("Postal Code:");
1250 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
1251 return _("Street:");
1252 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
1253 return _("Building:");
1254 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
1256 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
1258 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
1260 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
1261 return _("Description:");
1262 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
1264 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
1265 return _("Accuracy Level:");
1266 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
1268 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
1269 return _("Vertical Error (meters):");
1270 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
1271 return _("Horizontal Error (meters):");
1272 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
1274 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
1275 return _("Bearing:");
1276 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
1277 return _("Climb Speed:");
1278 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
1279 return _("Last Updated on:");
1280 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
1281 return _("Longitude:");
1282 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
1283 return _("Latitude:");
1284 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
1285 return _("Altitude:");
1288 DEBUG ("Unexpected Location key: %s", key);
1294 contact_widget_location_update (EmpathyContactWidget *information)
1296 GHashTable *location;
1298 gdouble lat = 0.0, lon = 0.0;
1299 gboolean has_position = TRUE;
1302 GHashTableIter iter;
1303 gpointer key, pvalue;
1305 if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
1307 gtk_widget_hide (information->vbox_location);
1311 location = empathy_contact_get_location (information->contact);
1312 if (location == NULL || g_hash_table_size (location) == 0)
1314 gtk_widget_hide (information->vbox_location);
1318 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
1320 has_position = FALSE;
1322 lat = g_value_get_double (value);
1324 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
1326 has_position = FALSE;
1328 lon = g_value_get_double (value);
1330 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
1332 gtk_label_set_markup (GTK_LABEL (information->label_location), _("<b>Location</b>"));
1340 stamp = g_value_get_int64 (value);
1343 user_date = empathy_time_to_string_relative (time);
1345 text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
1346 gtk_label_set_markup (GTK_LABEL (information->label_location), text);
1351 /* Prepare the location information table */
1352 if (information->table_location != NULL)
1354 gtk_widget_destroy (information->table_location);
1357 information->table_location = gtk_table_new (1, 2, FALSE);
1358 gtk_box_pack_start (GTK_BOX (information->subvbox_location),
1359 information->table_location, FALSE, FALSE, 5);
1361 g_hash_table_iter_init (&iter, location);
1362 while (g_hash_table_iter_next (&iter, &key, &pvalue))
1365 const gchar* user_label;
1367 char *svalue = NULL;
1369 skey = (const gchar *) key;
1371 user_label = location_key_to_label (skey);
1372 gvalue = (GValue *) pvalue;
1374 label = gtk_label_new (user_label);
1375 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1376 gtk_table_attach (GTK_TABLE (information->table_location),
1377 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
1378 gtk_widget_show (label);
1380 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
1383 dvalue = g_value_get_double (gvalue);
1384 svalue = g_strdup_printf ("%f", dvalue);
1386 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
1388 svalue = g_value_dup_string (gvalue);
1390 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
1394 time = g_value_get_int64 (value);
1395 svalue = empathy_time_to_string_utc (time, _("%B %e, %Y at %R UTC"));
1400 label = gtk_label_new (svalue);
1401 gtk_table_attach_defaults (GTK_TABLE (information->table_location),
1402 label, 1, 2, row, row + 1);
1403 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1404 gtk_widget_show (label);
1411 gtk_widget_show (information->table_location);
1413 #if HAVE_LIBCHAMPLAIN
1414 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
1418 !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1420 ClutterActor *marker;
1421 ChamplainLayer *layer;
1423 information->map_view_embed = gtk_champlain_embed_new ();
1424 information->map_view = gtk_champlain_embed_get_view (
1425 GTK_CHAMPLAIN_EMBED (information->map_view_embed));
1427 gtk_container_add (GTK_CONTAINER (information->viewport_map),
1428 information->map_view_embed);
1429 g_object_set (G_OBJECT (information->map_view), "show-license", FALSE,
1430 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
1433 layer = champlain_layer_new ();
1434 champlain_view_add_layer (information->map_view, layer);
1436 marker = champlain_marker_new_with_text (
1437 empathy_contact_get_name (information->contact), NULL, NULL, NULL);
1438 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
1439 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
1441 champlain_view_center_on (information->map_view, lat, lon);
1442 gtk_widget_show_all (information->viewport_map);
1446 gtk_widget_show (information->vbox_location);