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-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;
91 GtkWidget *image_account;
92 GtkWidget *label_account;
94 GtkWidget *widget_alias;
95 GtkWidget *label_alias;
96 GtkWidget *entry_alias;
97 GtkWidget *hbox_presence;
98 GtkWidget *image_state;
99 GtkWidget *label_status;
100 GtkWidget *table_contact;
101 GtkWidget *vbox_avatar;
104 GtkWidget *vbox_location;
105 GtkWidget *subvbox_location;
106 GtkWidget *table_location;
107 GtkWidget *label_location;
108 #if HAVE_LIBCHAMPLAIN
109 GtkWidget *viewport_map;
110 GtkWidget *map_view_embed;
111 ChamplainView *map_view;
115 GtkWidget *vbox_groups;
116 GtkWidget *entry_group;
117 GtkWidget *button_group;
118 GtkWidget *treeview_groups;
121 GtkWidget *vbox_details;
122 GtkWidget *table_details;
123 GtkWidget *hbox_details_requested;
126 GtkWidget *vbox_client;
127 GtkWidget *table_client;
128 GtkWidget *hbox_client_requested;
129 } EmpathyContactWidget;
133 EmpathyContactWidget *information;
136 GtkTreeIter found_iter;
139 static void contact_widget_destroy_cb (GtkWidget *widget,
140 EmpathyContactWidget *information);
141 static void contact_widget_remove_contact (EmpathyContactWidget *information);
142 static void contact_widget_set_contact (EmpathyContactWidget *information,
143 EmpathyContact *contact);
144 static void contact_widget_contact_setup (EmpathyContactWidget *information);
145 static void contact_widget_contact_update (EmpathyContactWidget *information);
146 static void contact_widget_change_contact (EmpathyContactWidget *information);
147 static void contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
148 EmpathyContactWidget *information);
149 static gboolean contact_widget_id_focus_out_cb (GtkWidget *widget,
150 GdkEventFocus *event, EmpathyContactWidget *information);
151 static gboolean contact_widget_entry_alias_focus_event_cb (
152 GtkEditable *editable, GdkEventFocus *event,
153 EmpathyContactWidget *information);
154 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
155 static void contact_widget_presence_notify_cb (
156 EmpathyContactWidget *information);
157 static void contact_widget_avatar_notify_cb (
158 EmpathyContactWidget *information);
159 static void contact_widget_groups_setup (
160 EmpathyContactWidget *information);
161 static void contact_widget_groups_update (EmpathyContactWidget *information);
162 static void contact_widget_model_setup (EmpathyContactWidget *information);
163 static void contact_widget_model_populate_columns (
164 EmpathyContactWidget *information);
165 static void contact_widget_groups_populate_data (
166 EmpathyContactWidget *information);
167 static void contact_widget_groups_notify_cb (
168 EmpathyContactWidget *information);
169 static gboolean contact_widget_model_find_name (
170 EmpathyContactWidget *information,const gchar *name, GtkTreeIter *iter);
171 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
172 GtkTreePath *path, GtkTreeIter *iter, FindName *data);
173 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
174 gchar *path_string, EmpathyContactWidget *information);
175 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
176 EmpathyContactWidget *information);
177 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
178 EmpathyContactWidget *information);
179 static void contact_widget_button_group_clicked_cb (GtkButton *button,
180 EmpathyContactWidget *information);
181 static void contact_widget_details_setup (EmpathyContactWidget *information);
182 static void contact_widget_details_update (EmpathyContactWidget *information);
183 static void contact_widget_client_setup (EmpathyContactWidget *information);
184 static void contact_widget_client_update (EmpathyContactWidget *information);
185 static void contact_widget_location_update (EmpathyContactWidget *information);
196 * empathy_contact_widget_new:
197 * @contact: an #EmpathyContact
198 * @flags: #EmpathyContactWidgetFlags for the new contact widget
200 * Creates a new #EmpathyContactWidget.
202 * Return value: a new #EmpathyContactWidget
205 empathy_contact_widget_new (EmpathyContact *contact,
206 EmpathyContactWidgetFlags flags)
208 EmpathyContactWidget *information;
212 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
214 information = g_slice_new0 (EmpathyContactWidget);
215 information->flags = flags;
217 filename = empathy_file_lookup ("empathy-contact-widget.ui",
219 gui = empathy_builder_get_file (filename,
220 "vbox_contact_widget", &information->vbox_contact_widget,
221 "vbox_contact", &information->vbox_contact,
222 "hbox_presence", &information->hbox_presence,
223 "label_alias", &information->label_alias,
224 "image_state", &information->image_state,
225 "table_contact", &information->table_contact,
226 "vbox_avatar", &information->vbox_avatar,
227 "vbox_location", &information->vbox_location,
228 "subvbox_location", &information->subvbox_location,
229 "label_location", &information->label_location,
230 #if HAVE_LIBCHAMPLAIN
231 "viewport_map", &information->viewport_map,
233 "vbox_groups", &information->vbox_groups,
234 "entry_group", &information->entry_group,
235 "button_group", &information->button_group,
236 "treeview_groups", &information->treeview_groups,
237 "vbox_details", &information->vbox_details,
238 "table_details", &information->table_details,
239 "hbox_details_requested", &information->hbox_details_requested,
240 "vbox_client", &information->vbox_client,
241 "table_client", &information->table_client,
242 "hbox_client_requested", &information->hbox_client_requested,
246 empathy_builder_connect (gui, information,
247 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
248 "entry_group", "changed", contact_widget_entry_group_changed_cb,
249 "entry_group", "activate", contact_widget_entry_group_activate_cb,
250 "button_group", "clicked", contact_widget_button_group_clicked_cb,
252 information->table_location = NULL;
254 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
255 "EmpathyContactWidget",
259 contact_widget_contact_setup (information);
260 contact_widget_groups_setup (information);
261 contact_widget_details_setup (information);
262 contact_widget_client_setup (information);
265 contact_widget_set_contact (information, contact);
267 else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
268 information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
269 contact_widget_change_contact (information);
271 return empathy_builder_unref_and_keep_widget (gui,
272 information->vbox_contact_widget);
276 * empathy_contact_widget_get_contact:
277 * @widget: an #EmpathyContactWidget
279 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
281 * Returns: the #EmpathyContact associated with @widget
284 empathy_contact_widget_get_contact (GtkWidget *widget)
286 EmpathyContactWidget *information;
288 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
290 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
294 return information->contact;
298 * empathy_contact_widget_set_contact:
299 * @widget: an #EmpathyContactWidget
300 * @contact: a different #EmpathyContact
302 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
305 empathy_contact_widget_set_contact (GtkWidget *widget,
306 EmpathyContact *contact)
308 EmpathyContactWidget *information;
310 g_return_if_fail (GTK_IS_WIDGET (widget));
311 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
313 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
317 contact_widget_set_contact (information, contact);
321 * empathy_contact_widget_set_account_filter:
322 * @widget: an #EmpathyContactWidget
323 * @filter: a #EmpathyAccountChooserFilterFunc
324 * @user_data: user data to pass to @filter, or %NULL
326 * Set a filter on the #EmpathyAccountChooser included in the
327 * #EmpathyContactWidget.
330 empathy_contact_widget_set_account_filter (
332 EmpathyAccountChooserFilterFunc filter,
335 EmpathyContactWidget *information;
336 EmpathyAccountChooser *chooser;
338 g_return_if_fail (GTK_IS_WIDGET (widget));
340 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
344 chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
346 empathy_account_chooser_set_filter (chooser, filter, user_data);
350 contact_widget_destroy_cb (GtkWidget *widget,
351 EmpathyContactWidget *information)
353 contact_widget_remove_contact (information);
355 if (information->widget_id_timeout != 0)
357 g_source_remove (information->widget_id_timeout);
359 if (information->manager)
361 g_object_unref (information->manager);
364 g_slice_free (EmpathyContactWidget, information);
368 contact_widget_remove_contact (EmpathyContactWidget *information)
370 if (information->contact)
372 g_signal_handlers_disconnect_by_func (information->contact,
373 contact_widget_name_notify_cb, information);
374 g_signal_handlers_disconnect_by_func (information->contact,
375 contact_widget_presence_notify_cb, information);
376 g_signal_handlers_disconnect_by_func (information->contact,
377 contact_widget_avatar_notify_cb, information);
378 g_signal_handlers_disconnect_by_func (information->contact,
379 contact_widget_groups_notify_cb, information);
381 g_object_unref (information->contact);
382 g_object_unref (information->factory);
383 information->contact = NULL;
384 information->factory = NULL;
389 contact_widget_set_contact (EmpathyContactWidget *information,
390 EmpathyContact *contact)
392 if (contact == information->contact)
395 contact_widget_remove_contact (information);
398 TpConnection *connection;
400 connection = empathy_contact_get_connection (contact);
401 information->contact = g_object_ref (contact);
402 information->factory = empathy_tp_contact_factory_dup_singleton (connection);
405 /* set the selected account to be the account this contact came from */
406 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
407 empathy_account_chooser_set_account (
408 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
409 empathy_contact_get_account (contact));
412 /* Update information for widgets */
413 contact_widget_contact_update (information);
414 contact_widget_groups_update (information);
415 contact_widget_details_update (information);
416 contact_widget_client_update (information);
417 contact_widget_location_update (information);
421 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
423 contact_widget_change_contact (self);
428 contact_widget_id_changed_cb (GtkEntry *entry,
429 EmpathyContactWidget *self)
431 if (self->widget_id_timeout != 0)
433 g_source_remove (self->widget_id_timeout);
436 self->widget_id_timeout =
437 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
438 (GSourceFunc) contact_widget_id_activate_timeout, self);
442 save_avatar_menu_activate_cb (GtkWidget *widget,
443 EmpathyContactWidget *information)
446 EmpathyAvatar *avatar;
447 gchar *ext = NULL, *filename;
449 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
451 GTK_FILE_CHOOSER_ACTION_SAVE,
452 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
453 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
456 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
459 /* look for the avatar extension */
460 avatar = empathy_contact_get_avatar (information->contact);
461 if (avatar->format != NULL)
465 splitted = g_strsplit (avatar->format, "/", 2);
466 if (splitted[0] != NULL && splitted[1] != NULL)
467 ext = g_strdup (splitted[1]);
469 g_strfreev (splitted);
473 /* Avatar was loaded from the cache so was converted to PNG */
474 ext = g_strdup ("png");
481 id = tp_escape_as_identifier (empathy_contact_get_id (
482 information->contact));
484 filename = g_strdup_printf ("%s.%s", id, ext);
485 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
492 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
494 GError *error = NULL;
496 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
498 if (!empathy_avatar_save_to_file (avatar, filename, &error))
501 GtkWidget *error_dialog;
503 error_dialog = gtk_message_dialog_new (NULL, 0,
504 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
505 _("Unable to save avatar"));
507 gtk_message_dialog_format_secondary_text (
508 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
510 g_signal_connect (error_dialog, "response",
511 G_CALLBACK (gtk_widget_destroy), NULL);
513 gtk_window_present (GTK_WINDOW (error_dialog));
515 g_clear_error (&error);
521 gtk_widget_destroy (dialog);
525 popup_avatar_menu (EmpathyContactWidget *information,
527 GdkEventButton *event)
529 GtkWidget *menu, *item;
530 gint button, event_time;
532 if (information->contact == NULL ||
533 empathy_contact_get_avatar (information->contact) == NULL)
536 menu = gtk_menu_new ();
538 /* Add "Save as..." entry */
539 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
540 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
541 gtk_widget_show (item);
543 g_signal_connect (item, "activate",
544 G_CALLBACK (save_avatar_menu_activate_cb), information);
548 button = event->button;
549 event_time = event->time;
554 event_time = gtk_get_current_event_time ();
557 gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
558 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
563 widget_avatar_popup_menu_cb (GtkWidget *widget,
564 EmpathyContactWidget *information)
566 popup_avatar_menu (information, widget, NULL);
572 widget_avatar_button_press_event_cb (GtkWidget *widget,
573 GdkEventButton *event,
574 EmpathyContactWidget *information)
576 /* Ignore double-clicks and triple-clicks */
577 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
579 popup_avatar_menu (information, widget, event);
587 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
588 EmpathyAvatarChooser *avatar_chooser)
590 TpConnection *connection;
592 connection = empathy_account_chooser_get_connection (account_chooser);
593 g_object_set (avatar_chooser, "connection", connection, NULL);
597 contact_widget_contact_setup (EmpathyContactWidget *information)
599 /* Setup label_status as a KludgeLabel */
600 information->label_status = empathy_kludge_label_new ("");
601 gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
602 PANGO_WRAP_WORD_CHAR);
603 gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
606 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
607 gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
609 gtk_box_pack_start (GTK_BOX (information->hbox_presence),
610 information->label_status, TRUE, TRUE, 0);
611 gtk_widget_show (information->label_status);
613 /* Setup account label/chooser */
614 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
616 information->widget_account = empathy_account_chooser_new ();
618 g_signal_connect_swapped (information->widget_account, "changed",
619 G_CALLBACK (contact_widget_change_contact),
624 /* Pack the protocol icon with the account name in an hbox */
625 information->widget_account = gtk_hbox_new (FALSE, 6);
627 information->label_account = gtk_label_new (NULL);
628 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
629 gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
631 gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
632 gtk_widget_show (information->label_account);
634 information->image_account = gtk_image_new ();
635 gtk_widget_show (information->image_account);
637 gtk_box_pack_start (GTK_BOX (information->widget_account),
638 information->image_account, FALSE, FALSE, 0);
639 gtk_box_pack_start (GTK_BOX (information->widget_account),
640 information->label_account, FALSE, TRUE, 0);
642 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
643 information->widget_account,
645 gtk_widget_show (information->widget_account);
647 /* Set up avatar chooser/display */
648 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
650 information->widget_avatar = empathy_avatar_chooser_new ();
651 g_signal_connect (information->widget_avatar, "changed",
652 G_CALLBACK (contact_widget_avatar_changed_cb),
654 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
656 g_signal_connect (information->widget_account, "changed",
657 G_CALLBACK (update_avatar_chooser_account_cb),
658 information->widget_avatar);
659 update_avatar_chooser_account_cb (
660 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
661 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
666 information->widget_avatar = empathy_avatar_image_new ();
668 g_signal_connect (information->widget_avatar, "popup-menu",
669 G_CALLBACK (widget_avatar_popup_menu_cb), information);
670 g_signal_connect (information->widget_avatar, "button-press-event",
671 G_CALLBACK (widget_avatar_button_press_event_cb), information);
674 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
675 information->widget_avatar,
678 gtk_widget_show (information->widget_avatar);
680 /* Setup id label/entry */
681 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
683 information->widget_id = gtk_entry_new ();
684 g_signal_connect (information->widget_id, "focus-out-event",
685 G_CALLBACK (contact_widget_id_focus_out_cb),
687 g_signal_connect (information->widget_id, "changed",
688 G_CALLBACK (contact_widget_id_changed_cb),
693 information->widget_id = gtk_label_new (NULL);
694 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
695 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
697 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
699 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
700 information->widget_id,
702 gtk_widget_show (information->widget_id);
704 /* Setup alias label/entry */
705 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
707 information->widget_alias = gtk_entry_new ();
708 g_signal_connect (information->widget_alias, "focus-out-event",
709 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
711 /* Make return activate the window default (the Close button) */
712 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
717 information->widget_alias = gtk_label_new (NULL);
718 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
719 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
721 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
723 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
724 information->widget_alias,
726 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
727 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
729 gtk_widget_show (information->widget_alias);
733 contact_widget_contact_update (EmpathyContactWidget *information)
735 TpAccount *account = NULL;
736 const gchar *id = NULL;
738 /* Connect and get info from new contact */
739 if (information->contact)
741 g_signal_connect_swapped (information->contact, "notify::name",
742 G_CALLBACK (contact_widget_name_notify_cb), information);
743 g_signal_connect_swapped (information->contact, "notify::presence",
744 G_CALLBACK (contact_widget_presence_notify_cb), information);
745 g_signal_connect_swapped (information->contact,
746 "notify::presence-message",
747 G_CALLBACK (contact_widget_presence_notify_cb), information);
748 g_signal_connect_swapped (information->contact, "notify::avatar",
749 G_CALLBACK (contact_widget_avatar_notify_cb), information);
751 account = empathy_contact_get_account (information->contact);
752 id = empathy_contact_get_id (information->contact);
755 /* Update account widget */
756 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
760 g_signal_handlers_block_by_func (information->widget_account,
761 contact_widget_change_contact,
763 empathy_account_chooser_set_account (
764 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
765 g_signal_handlers_unblock_by_func (information->widget_account,
766 contact_widget_change_contact, information);
775 name = tp_account_get_display_name (account);
776 gtk_label_set_label (GTK_LABEL (information->label_account), name);
778 name = tp_account_get_icon_name (account);
779 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
780 name, GTK_ICON_SIZE_MENU);
784 /* Update id widget */
785 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
786 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
788 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
790 /* Update other widgets */
791 if (information->contact)
793 contact_widget_name_notify_cb (information);
794 contact_widget_presence_notify_cb (information);
795 contact_widget_avatar_notify_cb (information);
797 gtk_widget_show (information->label_alias);
798 gtk_widget_show (information->widget_alias);
799 gtk_widget_show (information->hbox_presence);
800 gtk_widget_show (information->widget_avatar);
804 gtk_widget_hide (information->label_alias);
805 gtk_widget_hide (information->widget_alias);
806 gtk_widget_hide (information->hbox_presence);
807 gtk_widget_hide (information->widget_avatar);
812 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
813 EmpathyContact *contact,
816 GObject *weak_object)
818 EmpathyContactWidget *information = user_data;
822 DEBUG ("Error: %s", error->message);
826 contact_widget_set_contact (information, contact);
830 contact_widget_change_contact (EmpathyContactWidget *information)
832 EmpathyTpContactFactory *factory;
833 TpConnection *connection;
835 connection = empathy_account_chooser_get_connection (
836 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
840 factory = empathy_tp_contact_factory_dup_singleton (connection);
841 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
845 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
846 if (!EMP_STR_EMPTY (id))
848 empathy_tp_contact_factory_get_from_id (factory, id,
849 contact_widget_got_contact_cb, information, NULL,
850 G_OBJECT (information->vbox_contact_widget));
855 empathy_tp_contact_factory_get_from_handle (factory,
856 tp_connection_get_self_handle (connection),
857 contact_widget_got_contact_cb, information, NULL,
858 G_OBJECT (information->vbox_contact_widget));
861 g_object_unref (factory);
865 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
866 EmpathyContactWidget *information)
870 const gchar *mime_type;
872 empathy_avatar_chooser_get_image_data (
873 EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
874 &data, &size, &mime_type);
875 empathy_tp_contact_factory_set_avatar (information->factory,
876 data, size, mime_type);
880 contact_widget_id_focus_out_cb (GtkWidget *widget,
881 GdkEventFocus *event,
882 EmpathyContactWidget *information)
884 contact_widget_change_contact (information);
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 contact_widget_name_notify_cb (EmpathyContactWidget *information)
908 if (GTK_IS_ENTRY (information->widget_alias))
909 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
910 empathy_contact_get_name (information->contact));
912 gtk_label_set_label (GTK_LABEL (information->widget_alias),
913 empathy_contact_get_name (information->contact));
917 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
919 gtk_label_set_text (GTK_LABEL (information->label_status),
920 empathy_contact_get_status (information->contact));
921 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
922 empathy_icon_name_for_contact (information->contact),
923 GTK_ICON_SIZE_BUTTON);
924 gtk_widget_show (information->image_state);
928 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
930 EmpathyAvatar *avatar = NULL;
932 if (information->contact)
933 avatar = empathy_contact_get_avatar (information->contact);
935 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
937 g_signal_handlers_block_by_func (information->widget_avatar,
938 contact_widget_avatar_changed_cb,
940 empathy_avatar_chooser_set (
941 EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
942 g_signal_handlers_unblock_by_func (information->widget_avatar,
943 contact_widget_avatar_changed_cb, information);
946 empathy_avatar_image_set (
947 EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
951 contact_widget_groups_setup (EmpathyContactWidget *information)
953 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
955 information->manager = empathy_contact_manager_dup_singleton ();
956 contact_widget_model_setup (information);
961 contact_widget_groups_update (EmpathyContactWidget *information)
963 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
964 information->contact)
966 g_signal_connect_swapped (information->contact, "notify::groups",
967 G_CALLBACK (contact_widget_groups_notify_cb), information);
968 contact_widget_groups_populate_data (information);
970 gtk_widget_show (information->vbox_groups);
973 gtk_widget_hide (information->vbox_groups);
977 contact_widget_model_setup (EmpathyContactWidget *information)
981 GtkTreeSelection *selection;
983 view = GTK_TREE_VIEW (information->treeview_groups);
985 store = gtk_list_store_new (COL_COUNT,
986 G_TYPE_STRING, /* name */
987 G_TYPE_BOOLEAN, /* enabled */
988 G_TYPE_BOOLEAN); /* editable */
990 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
992 selection = gtk_tree_view_get_selection (view);
993 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
995 contact_widget_model_populate_columns (information);
997 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
998 COL_NAME, GTK_SORT_ASCENDING);
1000 g_object_unref (store);
1004 contact_widget_model_populate_columns (EmpathyContactWidget *information)
1007 GtkTreeModel *model;
1008 GtkTreeViewColumn *column;
1009 GtkCellRenderer *renderer;
1012 view = GTK_TREE_VIEW (information->treeview_groups);
1013 model = gtk_tree_view_get_model (view);
1015 renderer = gtk_cell_renderer_toggle_new ();
1016 g_signal_connect (renderer, "toggled",
1017 G_CALLBACK (contact_widget_cell_toggled), information);
1019 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
1020 "active", COL_ENABLED, NULL);
1022 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
1023 gtk_tree_view_column_set_fixed_width (column, 50);
1024 gtk_tree_view_append_column (view, column);
1026 renderer = gtk_cell_renderer_text_new ();
1027 col_offset = gtk_tree_view_insert_column_with_attributes (view,
1031 /* "editable", COL_EDITABLE, */
1034 g_object_set_data (G_OBJECT (renderer),
1035 "column", GINT_TO_POINTER (COL_NAME));
1037 column = gtk_tree_view_get_column (view, col_offset - 1);
1038 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
1039 gtk_tree_view_column_set_resizable (column,FALSE);
1040 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
1044 contact_widget_groups_populate_data (EmpathyContactWidget *information)
1047 GtkListStore *store;
1049 GList *my_groups, *l;
1052 view = GTK_TREE_VIEW (information->treeview_groups);
1053 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1054 gtk_list_store_clear (store);
1056 all_groups = empathy_contact_list_get_all_groups (
1057 EMPATHY_CONTACT_LIST (information->manager));
1058 my_groups = empathy_contact_list_get_groups (
1059 EMPATHY_CONTACT_LIST (information->manager),
1060 information->contact);
1062 for (l = all_groups; l; l = l->next)
1064 const gchar *group_str;
1067 group_str = l->data;
1069 enabled = g_list_find_custom (my_groups,
1070 group_str, (GCompareFunc) strcmp) != NULL;
1072 gtk_list_store_append (store, &iter);
1073 gtk_list_store_set (store, &iter,
1074 COL_NAME, group_str,
1076 COL_ENABLED, enabled,
1080 g_list_foreach (all_groups, (GFunc) g_free, NULL);
1081 g_list_foreach (my_groups, (GFunc) g_free, NULL);
1082 g_list_free (all_groups);
1083 g_list_free (my_groups);
1087 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
1089 /* FIXME: not implemented */
1093 contact_widget_model_find_name (EmpathyContactWidget *information,
1098 GtkTreeModel *model;
1101 if (EMP_STR_EMPTY (name))
1104 data.information = information;
1108 view = GTK_TREE_VIEW (information->treeview_groups);
1109 model = gtk_tree_view_get_model (view);
1111 gtk_tree_model_foreach (model,
1112 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
1115 if (data.found == TRUE)
1117 *iter = data.found_iter;
1125 contact_widget_model_find_name_foreach (GtkTreeModel *model,
1132 gtk_tree_model_get (model, iter,
1139 if (data->name && strcmp (data->name, name) == 0)
1142 data->found_iter = *iter;
1155 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
1157 EmpathyContactWidget *information)
1160 GtkTreeModel *model;
1161 GtkListStore *store;
1167 view = GTK_TREE_VIEW (information->treeview_groups);
1168 model = gtk_tree_view_get_model (view);
1169 store = GTK_LIST_STORE (model);
1171 path = gtk_tree_path_new_from_string (path_string);
1173 gtk_tree_model_get_iter (model, &iter, path);
1174 gtk_tree_model_get (model, &iter,
1175 COL_ENABLED, &enabled,
1179 gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
1180 gtk_tree_path_free (path);
1186 empathy_contact_list_remove_from_group (
1187 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1192 empathy_contact_list_add_to_group (
1193 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1201 contact_widget_entry_group_changed_cb (GtkEditable *editable,
1202 EmpathyContactWidget *information)
1207 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1209 if (contact_widget_model_find_name (information, group, &iter))
1210 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
1212 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
1213 !EMP_STR_EMPTY (group));
1217 contact_widget_entry_group_activate_cb (GtkEntry *entry,
1218 EmpathyContactWidget *information)
1220 gtk_widget_activate (GTK_WIDGET (information->button_group));
1224 contact_widget_button_group_clicked_cb (GtkButton *button,
1225 EmpathyContactWidget *information)
1228 GtkListStore *store;
1232 view = GTK_TREE_VIEW (information->treeview_groups);
1233 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1235 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1237 gtk_list_store_append (store, &iter);
1238 gtk_list_store_set (store, &iter,
1243 empathy_contact_list_add_to_group (
1244 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1249 contact_widget_details_setup (EmpathyContactWidget *information)
1251 /* FIXME: Needs new telepathy spec */
1252 gtk_widget_hide (information->vbox_details);
1256 contact_widget_details_update (EmpathyContactWidget *information)
1258 /* FIXME: Needs new telepathy spec */
1262 contact_widget_client_setup (EmpathyContactWidget *information)
1264 /* FIXME: Needs new telepathy spec */
1265 gtk_widget_hide (information->vbox_client);
1269 contact_widget_client_update (EmpathyContactWidget *information)
1271 /* FIXME: Needs new telepathy spec */
1274 /* Converts the Location's GHashTable's key to a user readable string */
1275 static const gchar *
1276 location_key_to_label (const gchar *key)
1278 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
1279 return _("Country ISO Code:");
1280 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
1281 return _("Country:");
1282 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
1284 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
1286 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
1288 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
1289 return _("Postal Code:");
1290 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
1291 return _("Street:");
1292 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
1293 return _("Building:");
1294 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
1296 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
1298 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
1300 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
1301 return _("Description:");
1302 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
1304 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
1305 return _("Accuracy Level:");
1306 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
1308 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
1309 return _("Vertical Error (meters):");
1310 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
1311 return _("Horizontal Error (meters):");
1312 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
1314 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
1315 return _("Bearing:");
1316 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
1317 return _("Climb Speed:");
1318 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
1319 return _("Last Updated on:");
1320 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
1321 return _("Longitude:");
1322 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
1323 return _("Latitude:");
1324 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
1325 return _("Altitude:");
1328 DEBUG ("Unexpected Location key: %s", key);
1334 contact_widget_location_update (EmpathyContactWidget *information)
1336 GHashTable *location;
1338 gdouble lat = 0.0, lon = 0.0;
1339 gboolean has_position = TRUE;
1342 static const gchar* ordered_geolocation_keys[] = {
1343 EMPATHY_LOCATION_TEXT,
1344 EMPATHY_LOCATION_URI,
1345 EMPATHY_LOCATION_DESCRIPTION,
1346 EMPATHY_LOCATION_BUILDING,
1347 EMPATHY_LOCATION_FLOOR,
1348 EMPATHY_LOCATION_ROOM,
1349 EMPATHY_LOCATION_STREET,
1350 EMPATHY_LOCATION_AREA,
1351 EMPATHY_LOCATION_LOCALITY,
1352 EMPATHY_LOCATION_REGION,
1353 EMPATHY_LOCATION_COUNTRY,
1359 if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
1361 gtk_widget_hide (information->vbox_location);
1365 location = empathy_contact_get_location (information->contact);
1366 if (location == NULL || g_hash_table_size (location) == 0)
1368 gtk_widget_hide (information->vbox_location);
1372 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
1374 has_position = FALSE;
1376 lat = g_value_get_double (value);
1378 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
1380 has_position = FALSE;
1382 lon = g_value_get_double (value);
1384 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
1387 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
1388 gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
1398 stamp = g_value_get_int64 (value);
1401 user_date = empathy_time_to_string_relative (time_);
1403 text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
1404 gtk_label_set_markup (GTK_LABEL (information->label_location), text);
1409 /* Prepare the location information table */
1410 if (information->table_location != NULL)
1412 gtk_widget_destroy (information->table_location);
1415 information->table_location = gtk_table_new (1, 2, FALSE);
1416 gtk_box_pack_start (GTK_BOX (information->subvbox_location),
1417 information->table_location, FALSE, FALSE, 5);
1420 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
1422 const gchar* user_label;
1424 char *svalue = NULL;
1426 gvalue = g_hash_table_lookup (location, (gpointer) skey);
1430 user_label = location_key_to_label (skey);
1432 label = gtk_label_new (user_label);
1433 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1434 gtk_table_attach (GTK_TABLE (information->table_location),
1435 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
1436 gtk_widget_show (label);
1438 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
1441 dvalue = g_value_get_double (gvalue);
1442 svalue = g_strdup_printf ("%f", dvalue);
1444 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
1446 svalue = g_value_dup_string (gvalue);
1448 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
1452 time_ = g_value_get_int64 (value);
1453 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
1458 label = gtk_label_new (svalue);
1459 gtk_table_attach_defaults (GTK_TABLE (information->table_location),
1460 label, 1, 2, row, row + 1);
1461 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1462 gtk_widget_show (label);
1464 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1465 gtk_label_set_selectable (GTK_LABEL (label), TRUE);
1474 gtk_widget_hide (information->vbox_location);
1478 gtk_widget_show (information->table_location);
1480 #if HAVE_LIBCHAMPLAIN
1481 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
1485 !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1487 ClutterActor *marker;
1488 ChamplainLayer *layer;
1490 information->map_view_embed = gtk_champlain_embed_new ();
1491 information->map_view = gtk_champlain_embed_get_view (
1492 GTK_CHAMPLAIN_EMBED (information->map_view_embed));
1494 gtk_container_add (GTK_CONTAINER (information->viewport_map),
1495 information->map_view_embed);
1496 g_object_set (G_OBJECT (information->map_view), "show-license", FALSE,
1497 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
1500 layer = champlain_layer_new ();
1501 champlain_view_add_layer (information->map_view, layer);
1503 marker = champlain_marker_new_with_text (
1504 empathy_contact_get_name (information->contact), NULL, NULL, NULL);
1505 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
1506 clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
1508 champlain_view_center_on (information->map_view, lat, lon);
1509 gtk_widget_show_all (information->viewport_map);
1513 gtk_widget_show (information->vbox_location);