1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 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>
30 #include <libmissioncontrol/mc-account.h>
31 #include <telepathy-glib/util.h>
33 #include <libempathy/empathy-contact-factory.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-contact-list.h>
36 #include <libempathy/empathy-utils.h>
38 #include "empathy-contact-widget.h"
39 #include "empathy-account-chooser.h"
40 #include "empathy-avatar-chooser.h"
41 #include "empathy-avatar-image.h"
42 #include "empathy-ui-utils.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
45 #include <libempathy/empathy-debug.h>
47 /* Delay before updating the widget when the id entry changed (seconds) */
48 #define ID_CHANGED_TIMEOUT 1
52 EmpathyContactFactory *factory;
53 EmpathyContactManager *manager;
54 EmpathyContact *contact;
55 EmpathyContactWidgetFlags flags;
56 GtkCellRenderer *renderer;
57 guint widget_id_timeout;
59 GtkWidget *vbox_contact_widget;
62 GtkWidget *vbox_contact;
63 GtkWidget *widget_avatar;
64 GtkWidget *widget_account;
66 GtkWidget *widget_alias;
67 GtkWidget *label_alias;
68 GtkWidget *entry_alias;
69 GtkWidget *hbox_presence;
70 GtkWidget *image_state;
71 GtkWidget *label_status;
72 GtkWidget *table_contact;
73 GtkWidget *vbox_avatar;
76 GtkWidget *vbox_groups;
77 GtkWidget *entry_group;
78 GtkWidget *button_group;
79 GtkWidget *treeview_groups;
82 GtkWidget *vbox_details;
83 GtkWidget *table_details;
84 GtkWidget *hbox_details_requested;
87 GtkWidget *vbox_client;
88 GtkWidget *table_client;
89 GtkWidget *hbox_client_requested;
90 } EmpathyContactWidget;
94 EmpathyContactWidget *information;
97 GtkTreeIter found_iter;
100 static void contact_widget_destroy_cb (GtkWidget *widget,
101 EmpathyContactWidget *information);
102 static void contact_widget_remove_contact (EmpathyContactWidget *information);
103 static void contact_widget_set_contact (EmpathyContactWidget *information,
104 EmpathyContact *contact);
105 static void contact_widget_contact_setup (EmpathyContactWidget *information);
106 static void contact_widget_contact_update (EmpathyContactWidget *information);
107 static void contact_widget_change_contact (EmpathyContactWidget *information);
108 static void contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
109 EmpathyContactWidget *information);
110 static void contact_widget_account_changed_cb (GtkComboBox *widget,
111 EmpathyContactWidget *information);
112 static gboolean contact_widget_id_focus_out_cb (GtkWidget *widget,
113 GdkEventFocus *event, EmpathyContactWidget *information);
114 static gboolean contact_widget_entry_alias_focus_event_cb (
115 GtkEditable *editable, GdkEventFocus *event,
116 EmpathyContactWidget *information);
117 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
118 static void contact_widget_presence_notify_cb (
119 EmpathyContactWidget *information);
120 static void contact_widget_avatar_notify_cb (
121 EmpathyContactWidget *information);
122 static void contact_widget_groups_setup (
123 EmpathyContactWidget *information);
124 static void contact_widget_groups_update (EmpathyContactWidget *information);
125 static void contact_widget_model_setup (EmpathyContactWidget *information);
126 static void contact_widget_model_populate_columns (
127 EmpathyContactWidget *information);
128 static void contact_widget_groups_populate_data (
129 EmpathyContactWidget *information);
130 static void contact_widget_groups_notify_cb (
131 EmpathyContactWidget *information);
132 static gboolean contact_widget_model_find_name (
133 EmpathyContactWidget *information,const gchar *name, GtkTreeIter *iter);
134 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
135 GtkTreePath *path, GtkTreeIter *iter, FindName *data);
136 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
137 gchar *path_string, EmpathyContactWidget *information);
138 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
139 EmpathyContactWidget *information);
140 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
141 EmpathyContactWidget *information);
142 static void contact_widget_button_group_clicked_cb (GtkButton *button,
143 EmpathyContactWidget *information);
144 static void contact_widget_details_setup (EmpathyContactWidget *information);
145 static void contact_widget_details_update (EmpathyContactWidget *information);
146 static void contact_widget_client_setup (EmpathyContactWidget *information);
147 static void contact_widget_client_update (EmpathyContactWidget *information);
158 empathy_contact_widget_new (EmpathyContact *contact,
159 EmpathyContactWidgetFlags flags)
161 EmpathyContactWidget *information;
165 information = g_slice_new0 (EmpathyContactWidget);
166 information->flags = flags;
167 information->factory = empathy_contact_factory_dup_singleton ();
169 filename = empathy_file_lookup ("empathy-contact-widget.ui",
171 gui = empathy_builder_get_file (filename,
172 "vbox_contact_widget", &information->vbox_contact_widget,
173 "vbox_contact", &information->vbox_contact,
174 "hbox_presence", &information->hbox_presence,
175 "label_alias", &information->label_alias,
176 "image_state", &information->image_state,
177 "label_status", &information->label_status,
178 "table_contact", &information->table_contact,
179 "vbox_avatar", &information->vbox_avatar,
180 "vbox_groups", &information->vbox_groups,
181 "entry_group", &information->entry_group,
182 "button_group", &information->button_group,
183 "treeview_groups", &information->treeview_groups,
184 "vbox_details", &information->vbox_details,
185 "table_details", &information->table_details,
186 "hbox_details_requested", &information->hbox_details_requested,
187 "vbox_client", &information->vbox_client,
188 "table_client", &information->table_client,
189 "hbox_client_requested", &information->hbox_client_requested,
193 empathy_builder_connect (gui, information,
194 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
195 "entry_group", "changed", contact_widget_entry_group_changed_cb,
196 "entry_group", "activate", contact_widget_entry_group_activate_cb,
197 "button_group", "clicked", contact_widget_button_group_clicked_cb,
200 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
201 "EmpathyContactWidget",
205 contact_widget_contact_setup (information);
206 contact_widget_groups_setup (information);
207 contact_widget_details_setup (information);
208 contact_widget_client_setup (information);
210 contact_widget_set_contact (information, contact);
212 g_object_ref (information->vbox_contact_widget);
213 g_object_force_floating (G_OBJECT (information->vbox_contact_widget));
214 g_object_unref (gui);
216 return information->vbox_contact_widget;
220 empathy_contact_widget_get_contact (GtkWidget *widget)
222 EmpathyContactWidget *information;
224 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
226 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
230 return information->contact;
234 empathy_contact_widget_set_contact (GtkWidget *widget,
235 EmpathyContact *contact)
237 EmpathyContactWidget *information;
239 g_return_if_fail (GTK_IS_WIDGET (widget));
240 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
242 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
246 contact_widget_set_contact (information, contact);
250 empathy_contact_widget_set_account_filter (
252 EmpathyAccountChooserFilterFunc filter,
255 EmpathyContactWidget *information;
256 EmpathyAccountChooser *chooser;
258 g_return_if_fail (GTK_IS_WIDGET (widget));
260 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
264 chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
266 empathy_account_chooser_set_filter (chooser, filter, user_data);
270 contact_widget_destroy_cb (GtkWidget *widget,
271 EmpathyContactWidget *information)
273 contact_widget_remove_contact (information);
275 if (information->widget_id_timeout != 0)
277 g_source_remove (information->widget_id_timeout);
279 if (information->factory)
281 g_object_unref (information->factory);
283 if (information->manager)
285 g_object_unref (information->manager);
288 g_slice_free (EmpathyContactWidget, information);
292 contact_widget_remove_contact (EmpathyContactWidget *information)
294 if (information->contact)
296 g_signal_handlers_disconnect_by_func (information->contact,
297 contact_widget_name_notify_cb, information);
298 g_signal_handlers_disconnect_by_func (information->contact,
299 contact_widget_presence_notify_cb, information);
300 g_signal_handlers_disconnect_by_func (information->contact,
301 contact_widget_avatar_notify_cb, information);
302 g_signal_handlers_disconnect_by_func (information->contact,
303 contact_widget_groups_notify_cb, information);
305 g_object_unref (information->contact);
306 information->contact = NULL;
311 contact_widget_set_contact (EmpathyContactWidget *information,
312 EmpathyContact *contact)
314 if (contact == information->contact)
317 contact_widget_remove_contact (information);
319 information->contact = g_object_ref (contact);
321 /* Update information for widgets */
322 contact_widget_contact_update (information);
323 contact_widget_groups_update (information);
324 contact_widget_details_update (information);
325 contact_widget_client_update (information);
329 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
331 contact_widget_change_contact (self);
336 contact_widget_id_changed_cb (GtkEntry *entry,
337 EmpathyContactWidget *self)
339 if (self->widget_id_timeout != 0)
341 g_source_remove (self->widget_id_timeout);
344 self->widget_id_timeout =
345 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
346 (GSourceFunc) contact_widget_id_activate_timeout, self);
350 save_avatar_menu_activate_cb (GtkWidget *widget,
351 EmpathyContactWidget *information)
354 EmpathyAvatar *avatar;
355 gchar *ext = NULL, *filename;
357 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
359 GTK_FILE_CHOOSER_ACTION_SAVE,
360 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
361 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
364 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
367 /* look for the avatar extension */
368 avatar = empathy_contact_get_avatar (information->contact);
369 if (avatar->format != NULL)
373 splitted = g_strsplit (avatar->format, "/", 2);
374 if (splitted[0] != NULL && splitted[1] != NULL)
375 ext = g_strdup (splitted[1]);
377 g_strfreev (splitted);
381 /* Avatar was loaded from the cache so was converted to PNG */
382 ext = g_strdup ("png");
389 id = tp_escape_as_identifier (empathy_contact_get_id (
390 information->contact));
392 filename = g_strdup_printf ("%s.%s", id, ext);
393 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
400 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
402 GError *error = NULL;
404 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
406 if (!empathy_avatar_save_to_file (avatar, filename, &error))
411 dialog = gtk_message_dialog_new (NULL, 0,
412 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
413 _("Unable to save avatar"));
415 gtk_message_dialog_format_secondary_text (
416 GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
418 g_signal_connect (dialog, "response",
419 G_CALLBACK (gtk_widget_destroy), NULL);
421 gtk_window_present (GTK_WINDOW (dialog));
423 g_clear_error (&error);
429 gtk_widget_destroy (dialog);
433 popup_avatar_menu (EmpathyContactWidget *information,
435 GdkEventButton *event)
437 GtkWidget *menu, *item;
438 gint button, event_time;
440 if (information->contact == NULL ||
441 empathy_contact_get_avatar (information->contact) == NULL)
444 menu = gtk_menu_new ();
446 /* Add "Save as..." entry */
447 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
448 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
449 gtk_widget_show (item);
451 g_signal_connect (item, "activate",
452 G_CALLBACK (save_avatar_menu_activate_cb), information);
456 button = event->button;
457 event_time = event->time;
462 event_time = gtk_get_current_event_time ();
465 gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
466 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
471 widget_avatar_popup_menu_cb (GtkWidget *widget,
472 EmpathyContactWidget *information)
474 popup_avatar_menu (information, widget, NULL);
480 widget_avatar_button_press_event_cb (GtkWidget *widget,
481 GdkEventButton *event,
482 EmpathyContactWidget *information)
484 /* Ignore double-clicks and triple-clicks */
485 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
487 popup_avatar_menu (information, widget, event);
495 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
496 EmpathyAvatarChooser *avatar_chooser)
500 account = empathy_account_chooser_get_account (account_chooser);
501 g_object_set (avatar_chooser, "account", account, NULL);
505 contact_widget_contact_setup (EmpathyContactWidget *information)
507 /* Setup account label/chooser */
508 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
510 information->widget_account = empathy_account_chooser_new ();
512 g_signal_connect (information->widget_account, "changed",
513 G_CALLBACK (contact_widget_account_changed_cb),
518 information->widget_account = gtk_label_new (NULL);
519 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
520 gtk_label_set_selectable (GTK_LABEL (information->widget_account), TRUE);
522 gtk_misc_set_alignment (GTK_MISC (information->widget_account), 0, 0.5);
524 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
525 information->widget_account,
527 gtk_widget_show (information->widget_account);
529 /* Set up avatar chooser/display */
530 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
532 information->widget_avatar = empathy_avatar_chooser_new ();
533 g_signal_connect (information->widget_avatar, "changed",
534 G_CALLBACK (contact_widget_avatar_changed_cb),
536 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
538 g_signal_connect (information->widget_account, "changed",
539 G_CALLBACK (update_avatar_chooser_account_cb),
540 information->widget_avatar);
541 update_avatar_chooser_account_cb (
542 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
543 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
548 information->widget_avatar = empathy_avatar_image_new ();
550 g_signal_connect (information->widget_avatar, "popup-menu",
551 G_CALLBACK (widget_avatar_popup_menu_cb), information);
552 g_signal_connect (information->widget_avatar, "button-press-event",
553 G_CALLBACK (widget_avatar_button_press_event_cb), information);
556 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
557 information->widget_avatar,
560 gtk_widget_show (information->widget_avatar);
562 /* Setup id label/entry */
563 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
565 information->widget_id = gtk_entry_new ();
566 g_signal_connect (information->widget_id, "focus-out-event",
567 G_CALLBACK (contact_widget_id_focus_out_cb),
569 g_signal_connect (information->widget_id, "changed",
570 G_CALLBACK (contact_widget_id_changed_cb),
575 information->widget_id = gtk_label_new (NULL);
576 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
577 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
579 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
581 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
582 information->widget_id,
584 gtk_widget_show (information->widget_id);
586 /* Setup alias label/entry */
587 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
589 information->widget_alias = gtk_entry_new ();
590 g_signal_connect (information->widget_alias, "focus-out-event",
591 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
593 /* Make return activate the window default (the Close button) */
594 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
599 information->widget_alias = gtk_label_new (NULL);
600 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
601 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
603 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
605 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
606 information->widget_alias,
608 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
609 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
611 gtk_widget_show (information->widget_alias);
615 contact_widget_contact_update (EmpathyContactWidget *information)
617 McAccount *account = NULL;
618 const gchar *id = NULL;
620 /* Connect and get info from new contact */
621 if (information->contact)
623 g_signal_connect_swapped (information->contact, "notify::name",
624 G_CALLBACK (contact_widget_name_notify_cb), information);
625 g_signal_connect_swapped (information->contact, "notify::presence",
626 G_CALLBACK (contact_widget_presence_notify_cb), information);
627 g_signal_connect_swapped (information->contact,
628 "notify::presence-message",
629 G_CALLBACK (contact_widget_presence_notify_cb), information);
630 g_signal_connect_swapped (information->contact, "notify::avatar",
631 G_CALLBACK (contact_widget_avatar_notify_cb), information);
633 account = empathy_contact_get_account (information->contact);
634 id = empathy_contact_get_id (information->contact);
637 /* Update account widget */
638 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
642 g_signal_handlers_block_by_func (information->widget_account,
643 contact_widget_account_changed_cb,
645 empathy_account_chooser_set_account (
646 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
647 g_signal_handlers_unblock_by_func (information->widget_account,
648 contact_widget_account_changed_cb, information);
657 name = mc_account_get_display_name (account);
658 gtk_label_set_label (GTK_LABEL (information->widget_account), name);
662 /* Update id widget */
663 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
664 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
666 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
668 /* Update other widgets */
669 if (information->contact)
671 contact_widget_name_notify_cb (information);
672 contact_widget_presence_notify_cb (information);
673 contact_widget_avatar_notify_cb (information);
675 gtk_widget_show (information->label_alias);
676 gtk_widget_show (information->widget_alias);
677 gtk_widget_show (information->hbox_presence);
678 gtk_widget_show (information->widget_avatar);
682 gtk_widget_hide (information->label_alias);
683 gtk_widget_hide (information->widget_alias);
684 gtk_widget_hide (information->hbox_presence);
685 gtk_widget_hide (information->widget_avatar);
690 contact_widget_change_contact_cb (EmpathyContact *contact,
692 gpointer information,
693 GObject *weak_object)
696 DEBUG ("Error: %s", error->message);
698 contact_widget_set_contact (information, contact);
699 g_object_unref (contact);
703 contact_widget_change_contact (EmpathyContactWidget *information)
705 EmpathyContact *contact;
708 account = empathy_account_chooser_get_account (
709 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
713 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
717 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
718 if (EMP_STR_EMPTY (id))
721 contact = empathy_contact_factory_get_from_id (information->factory,
726 contact = empathy_contact_factory_get_user (information->factory,
732 /* Give the contact ref to the callback */
733 empathy_contact_call_when_ready (contact,
734 EMPATHY_CONTACT_READY_HANDLE |
735 EMPATHY_CONTACT_READY_ID,
736 contact_widget_change_contact_cb,
738 G_OBJECT (information->vbox_contact_widget));
743 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
744 EmpathyContactWidget *information)
746 if (information->contact && empathy_contact_is_user (information->contact))
751 const gchar *mime_type;
753 account = empathy_contact_get_account (information->contact);
754 empathy_avatar_chooser_get_image_data (
755 EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
756 &data, &size, &mime_type);
757 empathy_contact_factory_set_avatar (information->factory, account,
758 data, size, mime_type);
763 contact_widget_account_changed_cb (GtkComboBox *widget,
764 EmpathyContactWidget *information)
766 contact_widget_change_contact (information);
770 contact_widget_id_focus_out_cb (GtkWidget *widget,
771 GdkEventFocus *event,
772 EmpathyContactWidget *information)
774 contact_widget_change_contact (information);
779 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
780 GdkEventFocus *event,
781 EmpathyContactWidget *information)
783 if (information->contact)
787 alias = gtk_entry_get_text (GTK_ENTRY (editable));
788 empathy_contact_factory_set_alias (information->factory,
789 information->contact, alias);
796 contact_widget_name_notify_cb (EmpathyContactWidget *information)
798 if (GTK_IS_ENTRY (information->widget_alias))
799 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
800 empathy_contact_get_name (information->contact));
802 gtk_label_set_label (GTK_LABEL (information->widget_alias),
803 empathy_contact_get_name (information->contact));
807 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
809 gtk_label_set_text (GTK_LABEL (information->label_status),
810 empathy_contact_get_status (information->contact));
811 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
812 empathy_icon_name_for_contact (information->contact),
813 GTK_ICON_SIZE_BUTTON);
817 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
819 EmpathyAvatar *avatar = NULL;
821 if (information->contact)
822 avatar = empathy_contact_get_avatar (information->contact);
824 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
826 g_signal_handlers_block_by_func (information->widget_avatar,
827 contact_widget_avatar_changed_cb,
829 empathy_avatar_chooser_set (
830 EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
831 g_signal_handlers_unblock_by_func (information->widget_avatar,
832 contact_widget_avatar_changed_cb, information);
835 empathy_avatar_image_set (
836 EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
840 contact_widget_groups_setup (EmpathyContactWidget *information)
842 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
844 information->manager = empathy_contact_manager_dup_singleton ();
845 contact_widget_model_setup (information);
850 contact_widget_groups_update (EmpathyContactWidget *information)
852 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
853 information->contact)
855 g_signal_connect_swapped (information->contact, "notify::groups",
856 G_CALLBACK (contact_widget_groups_notify_cb), information);
857 contact_widget_groups_populate_data (information);
859 gtk_widget_show (information->vbox_groups);
862 gtk_widget_hide (information->vbox_groups);
866 contact_widget_model_setup (EmpathyContactWidget *information)
870 GtkTreeSelection *selection;
872 view = GTK_TREE_VIEW (information->treeview_groups);
874 store = gtk_list_store_new (COL_COUNT,
875 G_TYPE_STRING, /* name */
876 G_TYPE_BOOLEAN, /* enabled */
877 G_TYPE_BOOLEAN); /* editable */
879 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
881 selection = gtk_tree_view_get_selection (view);
882 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
884 contact_widget_model_populate_columns (information);
886 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
887 COL_NAME, GTK_SORT_ASCENDING);
889 g_object_unref (store);
893 contact_widget_model_populate_columns (EmpathyContactWidget *information)
897 GtkTreeViewColumn *column;
898 GtkCellRenderer *renderer;
901 view = GTK_TREE_VIEW (information->treeview_groups);
902 model = gtk_tree_view_get_model (view);
904 renderer = gtk_cell_renderer_toggle_new ();
905 g_signal_connect (renderer, "toggled",
906 G_CALLBACK (contact_widget_cell_toggled), information);
908 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
909 "active", COL_ENABLED, NULL);
911 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
912 gtk_tree_view_column_set_fixed_width (column, 50);
913 gtk_tree_view_append_column (view, column);
915 renderer = gtk_cell_renderer_text_new ();
916 col_offset = gtk_tree_view_insert_column_with_attributes (view,
920 /* "editable", COL_EDITABLE, */
923 g_object_set_data (G_OBJECT (renderer),
924 "column", GINT_TO_POINTER (COL_NAME));
926 column = gtk_tree_view_get_column (view, col_offset - 1);
927 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
928 gtk_tree_view_column_set_resizable (column,FALSE);
929 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
931 if (information->renderer)
932 g_object_unref (information->renderer);
934 information->renderer = g_object_ref (renderer);
938 contact_widget_groups_populate_data (EmpathyContactWidget *information)
943 GList *my_groups, *l;
946 view = GTK_TREE_VIEW (information->treeview_groups);
947 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
948 gtk_list_store_clear (store);
950 all_groups = empathy_contact_list_get_all_groups (
951 EMPATHY_CONTACT_LIST (information->manager));
952 my_groups = empathy_contact_list_get_groups (
953 EMPATHY_CONTACT_LIST (information->manager),
954 information->contact);
956 for (l = all_groups; l; l = l->next)
958 const gchar *group_str;
963 enabled = g_list_find_custom (my_groups,
964 group_str, (GCompareFunc) strcmp) != NULL;
966 gtk_list_store_append (store, &iter);
967 gtk_list_store_set (store, &iter,
970 COL_ENABLED, enabled,
974 g_list_foreach (all_groups, (GFunc) g_free, NULL);
975 g_list_foreach (my_groups, (GFunc) g_free, NULL);
976 g_list_free (all_groups);
977 g_list_free (my_groups);
981 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
983 /* FIXME: not implemented */
987 contact_widget_model_find_name (EmpathyContactWidget *information,
995 if (EMP_STR_EMPTY (name))
998 data.information = information;
1002 view = GTK_TREE_VIEW (information->treeview_groups);
1003 model = gtk_tree_view_get_model (view);
1005 gtk_tree_model_foreach (model,
1006 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
1009 if (data.found == TRUE)
1011 *iter = data.found_iter;
1019 contact_widget_model_find_name_foreach (GtkTreeModel *model,
1026 gtk_tree_model_get (model, iter,
1033 if (data->name && strcmp (data->name, name) == 0)
1036 data->found_iter = *iter;
1049 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
1051 EmpathyContactWidget *information)
1054 GtkTreeModel *model;
1055 GtkListStore *store;
1061 view = GTK_TREE_VIEW (information->treeview_groups);
1062 model = gtk_tree_view_get_model (view);
1063 store = GTK_LIST_STORE (model);
1065 path = gtk_tree_path_new_from_string (path_string);
1067 gtk_tree_model_get_iter (model, &iter, path);
1068 gtk_tree_model_get (model, &iter,
1069 COL_ENABLED, &enabled,
1073 gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
1074 gtk_tree_path_free (path);
1080 empathy_contact_list_remove_from_group (
1081 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1086 empathy_contact_list_add_to_group (
1087 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1095 contact_widget_entry_group_changed_cb (GtkEditable *editable,
1096 EmpathyContactWidget *information)
1101 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1103 if (contact_widget_model_find_name (information, group, &iter))
1104 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
1106 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
1107 !EMP_STR_EMPTY (group));
1111 contact_widget_entry_group_activate_cb (GtkEntry *entry,
1112 EmpathyContactWidget *information)
1114 gtk_widget_activate (GTK_WIDGET (information->button_group));
1118 contact_widget_button_group_clicked_cb (GtkButton *button,
1119 EmpathyContactWidget *information)
1122 GtkListStore *store;
1126 view = GTK_TREE_VIEW (information->treeview_groups);
1127 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1129 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1131 gtk_list_store_append (store, &iter);
1132 gtk_list_store_set (store, &iter,
1137 empathy_contact_list_add_to_group (
1138 EMPATHY_CONTACT_LIST (information->manager), information->contact,
1143 contact_widget_details_setup (EmpathyContactWidget *information)
1145 /* FIXME: Needs new telepathy spec */
1146 gtk_widget_hide (information->vbox_details);
1150 contact_widget_details_update (EmpathyContactWidget *information)
1152 /* FIXME: Needs new telepathy spec */
1156 contact_widget_client_setup (EmpathyContactWidget *information)
1158 /* FIXME: Needs new telepathy spec */
1159 gtk_widget_hide (information->vbox_client);
1163 contact_widget_client_update (EmpathyContactWidget *information)
1165 /* FIXME: Needs new telepathy spec */