1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
29 #include <glade/glade.h>
30 #include <glib/gi18n.h>
32 #include <libmissioncontrol/mc-account.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-contact-widget.h"
38 #include "empathy-account-chooser.h"
39 #include "empathy-ui-utils.h"
41 /* Delay before updating the widget when the id entry changed (ms) */
42 #define ID_CHANGED_TIMEOUT 500
45 EmpathyContact *contact;
47 EmpathyContactWidgetType type;
48 GtkCellRenderer *renderer;
49 guint widget_id_timeout;
51 GtkWidget *vbox_contact_widget;
54 GtkWidget *vbox_contact;
55 GtkWidget *widget_avatar;
56 GtkWidget *widget_account;
58 GtkWidget *widget_alias;
59 GtkWidget *label_alias;
60 GtkWidget *entry_alias;
61 GtkWidget *hbox_presence;
62 GtkWidget *image_state;
63 GtkWidget *label_status;
64 GtkWidget *table_contact;
65 GtkWidget *hbox_contact;
68 GtkWidget *vbox_groups;
69 GtkWidget *entry_group;
70 GtkWidget *button_group;
71 GtkWidget *treeview_groups;
74 GtkWidget *vbox_details;
75 GtkWidget *table_details;
76 GtkWidget *hbox_details_requested;
79 GtkWidget *vbox_client;
80 GtkWidget *table_client;
81 GtkWidget *hbow_client_requested;
83 } EmpathyContactWidget;
86 EmpathyContactWidget *information;
89 GtkTreeIter found_iter;
92 static void contact_widget_destroy_cb (GtkWidget *widget,
93 EmpathyContactWidget *information);
94 static void contact_widget_remove_contact (EmpathyContactWidget *information);
95 static void contact_widget_set_contact (EmpathyContactWidget *information,
96 EmpathyContact *contact);
97 static void contact_widget_contact_setup (EmpathyContactWidget *information);
98 static void contact_widget_contact_update (EmpathyContactWidget *information);
99 static gboolean contact_widget_update_contact (EmpathyContactWidget *information);
100 static void contact_widget_account_changed_cb (GtkComboBox *widget,
101 EmpathyContactWidget *information);
102 static gboolean contact_widget_id_focus_out_cb (GtkWidget *widget,
103 GdkEventFocus *event,
104 EmpathyContactWidget *information);
105 static gboolean contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
106 GdkEventFocus *event,
107 EmpathyContactWidget *information);
108 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
109 static void contact_widget_presence_notify_cb (EmpathyContactWidget *information);
110 static void contact_widget_avatar_notify_cb (EmpathyContactWidget *information);
111 static void contact_widget_groups_setup (EmpathyContactWidget *information);
112 static void contact_widget_groups_update (EmpathyContactWidget *information);
113 static void contact_widget_model_setup (EmpathyContactWidget *information);
114 static void contact_widget_model_populate_columns (EmpathyContactWidget *information);
115 static void contact_widget_groups_populate_data (EmpathyContactWidget *information);
116 static void contact_widget_groups_notify_cb (EmpathyContactWidget *information);
117 static gboolean contact_widget_model_find_name (EmpathyContactWidget *information,
120 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
124 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
126 EmpathyContactWidget *information);
127 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
128 EmpathyContactWidget *information);
129 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
130 EmpathyContactWidget *information);
131 static void contact_widget_button_group_clicked_cb (GtkButton *button,
132 EmpathyContactWidget *information);
133 static void contact_widget_details_setup (EmpathyContactWidget *information);
134 static void contact_widget_details_update (EmpathyContactWidget *information);
135 static void contact_widget_client_setup (EmpathyContactWidget *information);
136 static void contact_widget_client_update (EmpathyContactWidget *information);
146 empathy_contact_widget_new (EmpathyContact *contact,
147 EmpathyContactWidgetType type)
149 EmpathyContactWidget *information;
152 information = g_slice_new0 (EmpathyContactWidget);
153 information->type = type;
155 information->is_user = empathy_contact_is_user (contact);
157 information->is_user = FALSE;
160 glade = empathy_glade_get_file ("empathy-contact-widget.glade",
161 "vbox_contact_widget",
163 "vbox_contact_widget", &information->vbox_contact_widget,
164 "vbox_contact", &information->vbox_contact,
165 "hbox_presence", &information->hbox_presence,
166 "label_alias", &information->label_alias,
167 "image_state", &information->image_state,
168 "label_status", &information->label_status,
169 "table_contact", &information->table_contact,
170 "hbox_contact", &information->hbox_contact,
171 "vbox_groups", &information->vbox_groups,
172 "entry_group", &information->entry_group,
173 "button_group", &information->button_group,
174 "treeview_groups", &information->treeview_groups,
175 "vbox_details", &information->vbox_details,
176 "table_details", &information->table_details,
177 "hbox_details_requested", &information->hbox_details_requested,
178 "vbox_client", &information->vbox_client,
179 "table_client", &information->table_client,
180 "hbox_client_requested", &information->hbow_client_requested,
183 empathy_glade_connect (glade,
185 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
186 "entry_group", "changed", contact_widget_entry_group_changed_cb,
187 "entry_group", "activate", contact_widget_entry_group_activate_cb,
188 "button_group", "clicked", contact_widget_button_group_clicked_cb,
191 g_object_unref (glade);
193 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
194 "EmpathyContactWidget",
198 contact_widget_contact_setup (information);
199 contact_widget_groups_setup (information);
200 contact_widget_details_setup (information);
201 contact_widget_client_setup (information);
203 contact_widget_set_contact (information, contact);
205 gtk_widget_show (information->vbox_contact_widget);
207 return information->vbox_contact_widget;
211 empathy_contact_widget_get_contact (GtkWidget *widget)
213 EmpathyContactWidget *information;
215 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
217 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
222 return information->contact;
226 contact_widget_destroy_cb (GtkWidget *widget,
227 EmpathyContactWidget *information)
229 contact_widget_remove_contact (information);
231 if (information->widget_id_timeout != 0) {
232 g_source_remove (information->widget_id_timeout);
235 g_slice_free (EmpathyContactWidget, information);
239 contact_widget_remove_contact (EmpathyContactWidget *information)
241 if (information->contact) {
242 g_signal_handlers_disconnect_by_func (information->contact,
243 contact_widget_name_notify_cb,
245 g_signal_handlers_disconnect_by_func (information->contact,
246 contact_widget_presence_notify_cb,
248 g_signal_handlers_disconnect_by_func (information->contact,
249 contact_widget_avatar_notify_cb,
251 g_signal_handlers_disconnect_by_func (information->contact,
252 contact_widget_groups_notify_cb,
255 g_object_unref (information->contact);
256 information->contact = NULL;
261 contact_widget_set_contact (EmpathyContactWidget *information,
262 EmpathyContact *contact)
264 contact_widget_remove_contact (information);
266 information->contact = g_object_ref (contact);
269 /* Update information for widgets */
270 contact_widget_contact_update (information);
271 contact_widget_groups_update (information);
272 contact_widget_details_update (information);
273 contact_widget_client_update (information);
277 contact_widget_can_add_contact_to_account (McAccount *account,
283 const gchar *protocol_name;
285 mc = empathy_mission_control_new ();
286 tp_conn = mission_control_get_connection (mc, account, NULL);
288 if (tp_conn == NULL) {
289 /* Account is disconnected */
292 g_object_unref (tp_conn);
294 profile = mc_account_get_profile (account);
295 protocol_name = mc_profile_get_protocol_name (profile);
296 if (strcmp (protocol_name, "local-xmpp") == 0) {
297 /* We can't add accounts to a XMPP LL connection
298 * FIXME: We should inspect the flags of the contact list group interface
300 g_object_unref (profile);
304 g_object_unref (profile);
309 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
311 contact_widget_update_contact (self);
316 contact_widget_id_changed_cb (GtkEntry *entry,
317 EmpathyContactWidget *self)
319 if (self->widget_id_timeout != 0) {
320 g_source_remove (self->widget_id_timeout);
323 self->widget_id_timeout =
324 g_timeout_add (ID_CHANGED_TIMEOUT,
325 (GSourceFunc) contact_widget_id_activate_timeout,
330 contact_widget_contact_setup (EmpathyContactWidget *information)
332 /* FIXME: Use EmpathyAvatarImage if (editable && is_user) */
333 information->widget_avatar = gtk_image_new ();
334 gtk_box_pack_end (GTK_BOX (information->hbox_contact),
335 information->widget_avatar,
339 /* Setup account label/chooser */
340 if (information->type == CONTACT_WIDGET_TYPE_ADD) {
341 information->widget_account = empathy_account_chooser_new ();
342 empathy_account_chooser_set_filter (
343 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
344 contact_widget_can_add_contact_to_account,
347 g_signal_connect (information->widget_account, "changed",
348 G_CALLBACK (contact_widget_account_changed_cb),
351 information->widget_account = gtk_label_new (NULL);
352 gtk_label_set_selectable (GTK_LABEL (information->widget_account), TRUE);
353 gtk_misc_set_alignment (GTK_MISC (information->widget_account), 0, 0.5);
355 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
356 information->widget_account,
358 gtk_widget_show (information->widget_account);
360 /* Setup id label/entry */
361 if (information->type == CONTACT_WIDGET_TYPE_ADD) {
362 information->widget_id = gtk_entry_new ();
363 g_signal_connect (information->widget_id, "focus-out-event",
364 G_CALLBACK (contact_widget_id_focus_out_cb),
366 g_signal_connect (information->widget_id, "changed",
367 G_CALLBACK (contact_widget_id_changed_cb),
370 information->widget_id = gtk_label_new (NULL);
371 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
372 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
374 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
375 information->widget_id,
377 gtk_widget_show (information->widget_id);
379 /* Setup alias label/entry */
380 if (information->type > CONTACT_WIDGET_TYPE_SHOW) {
381 information->widget_alias = gtk_entry_new ();
382 g_signal_connect (information->widget_alias, "focus-out-event",
383 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
386 information->widget_alias = gtk_label_new (NULL);
387 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
388 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
390 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
391 information->widget_alias,
393 gtk_widget_show (information->widget_alias);
397 contact_widget_contact_update (EmpathyContactWidget *information)
399 McAccount *account = NULL;
400 const gchar *id = NULL;
402 /* Connect and get info from new contact */
403 if (information->contact) {
404 g_signal_connect_swapped (information->contact, "notify::name",
405 G_CALLBACK (contact_widget_name_notify_cb),
407 g_signal_connect_swapped (information->contact, "notify::presence",
408 G_CALLBACK (contact_widget_presence_notify_cb),
410 g_signal_connect_swapped (information->contact, "notify::avatar",
411 G_CALLBACK (contact_widget_avatar_notify_cb),
414 account = empathy_contact_get_account (information->contact);
415 id = empathy_contact_get_id (information->contact);
418 /* Update account widget */
419 if (information->type == CONTACT_WIDGET_TYPE_ADD) {
421 g_signal_handlers_block_by_func (information->widget_account,
422 contact_widget_account_changed_cb,
424 empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
426 g_signal_handlers_unblock_by_func (information->widget_account,
427 contact_widget_account_changed_cb,
430 if (!G_STR_EMPTY (id)) {
431 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id);
437 name = mc_account_get_display_name (account);
438 gtk_label_set_label (GTK_LABEL (information->widget_account), name);
440 gtk_label_set_label (GTK_LABEL (information->widget_id), id);
443 /* Update other widgets */
444 if (information->contact) {
445 contact_widget_name_notify_cb (information);
446 contact_widget_presence_notify_cb (information);
447 contact_widget_avatar_notify_cb (information);
449 gtk_widget_show (information->label_alias);
450 gtk_widget_show (information->widget_alias);
451 gtk_widget_show (information->hbox_presence);
453 gtk_widget_hide (information->label_alias);
454 gtk_widget_hide (information->widget_alias);
455 gtk_widget_hide (information->hbox_presence);
456 gtk_widget_hide (information->widget_avatar);
461 contact_widget_update_contact (EmpathyContactWidget *information)
466 account = empathy_account_chooser_get_account (EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
467 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
469 if (account && !G_STR_EMPTY (id)) {
470 EmpathyContactManager *manager;
471 EmpathyContact *contact;
473 manager = empathy_contact_manager_new ();
474 contact = empathy_contact_manager_create (manager, account, id);
475 contact_widget_set_contact (information, contact);
478 g_object_unref (contact);
480 g_object_unref (manager);
487 contact_widget_account_changed_cb (GtkComboBox *widget,
488 EmpathyContactWidget *information)
490 contact_widget_update_contact (information);
494 contact_widget_id_focus_out_cb (GtkWidget *widget,
495 GdkEventFocus *event,
496 EmpathyContactWidget *information)
498 contact_widget_update_contact (information);
503 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
504 GdkEventFocus *event,
505 EmpathyContactWidget *information)
507 if (information->contact) {
510 name = gtk_entry_get_text (GTK_ENTRY (editable));
511 empathy_contact_set_name (information->contact, name);
518 contact_widget_name_notify_cb (EmpathyContactWidget *information)
520 if (GTK_IS_ENTRY (information->widget_alias)) {
521 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
522 empathy_contact_get_name (information->contact));
524 gtk_label_set_label (GTK_LABEL (information->widget_alias),
525 empathy_contact_get_name (information->contact));
530 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
532 gtk_label_set_text (GTK_LABEL (information->label_status),
533 empathy_contact_get_status (information->contact));
534 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
535 empathy_icon_name_for_contact (information->contact),
536 GTK_ICON_SIZE_BUTTON);
541 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
543 GdkPixbuf *avatar_pixbuf;
545 avatar_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (information->contact,
549 gtk_image_set_from_pixbuf (GTK_IMAGE (information->widget_avatar),
551 gtk_widget_show (information->widget_avatar);
552 g_object_unref (avatar_pixbuf);
554 gtk_widget_hide (information->widget_avatar);
559 contact_widget_groups_setup (EmpathyContactWidget *information)
561 if (information->type > CONTACT_WIDGET_TYPE_SHOW) {
562 contact_widget_model_setup (information);
567 contact_widget_groups_update (EmpathyContactWidget *information)
569 if (information->type > CONTACT_WIDGET_TYPE_SHOW &&
570 information->contact) {
571 g_signal_connect_swapped (information->contact, "notify::groups",
572 G_CALLBACK (contact_widget_groups_notify_cb),
574 contact_widget_groups_populate_data (information);
576 gtk_widget_show (information->vbox_groups);
578 gtk_widget_hide (information->vbox_groups);
583 contact_widget_model_setup (EmpathyContactWidget *information)
587 GtkTreeSelection *selection;
589 view = GTK_TREE_VIEW (information->treeview_groups);
591 store = gtk_list_store_new (COL_COUNT,
592 G_TYPE_STRING, /* name */
593 G_TYPE_BOOLEAN, /* enabled */
594 G_TYPE_BOOLEAN); /* editable */
596 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
598 selection = gtk_tree_view_get_selection (view);
599 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
601 contact_widget_model_populate_columns (information);
603 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
604 COL_NAME, GTK_SORT_ASCENDING);
606 g_object_unref (store);
610 contact_widget_model_populate_columns (EmpathyContactWidget *information)
614 GtkTreeViewColumn *column;
615 GtkCellRenderer *renderer;
618 view = GTK_TREE_VIEW (information->treeview_groups);
619 model = gtk_tree_view_get_model (view);
621 renderer = gtk_cell_renderer_toggle_new ();
622 g_signal_connect (renderer, "toggled",
623 G_CALLBACK (contact_widget_cell_toggled),
626 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
627 "active", COL_ENABLED,
630 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
631 gtk_tree_view_column_set_fixed_width (column, 50);
632 gtk_tree_view_append_column (view, column);
634 renderer = gtk_cell_renderer_text_new ();
635 col_offset = gtk_tree_view_insert_column_with_attributes (view,
639 /* "editable", COL_EDITABLE, */
642 g_object_set_data (G_OBJECT (renderer),
643 "column", GINT_TO_POINTER (COL_NAME));
645 column = gtk_tree_view_get_column (view, col_offset - 1);
646 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
647 gtk_tree_view_column_set_resizable (column,FALSE);
648 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
650 if (information->renderer) {
651 g_object_unref (information->renderer);
654 information->renderer = g_object_ref (renderer);
658 contact_widget_groups_populate_data (EmpathyContactWidget *information)
660 EmpathyContactManager *manager;
664 GList *my_groups, *l;
667 view = GTK_TREE_VIEW (information->treeview_groups);
668 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
669 gtk_list_store_clear (store);
671 manager = empathy_contact_manager_new ();
672 all_groups = empathy_contact_manager_get_groups (manager);
673 my_groups = empathy_contact_get_groups (information->contact);
674 g_object_unref (manager);
676 for (l = all_groups; l; l = l->next) {
677 const gchar *group_str;
682 enabled = g_list_find_custom (my_groups,
684 (GCompareFunc) strcmp) != NULL;
686 gtk_list_store_append (store, &iter);
687 gtk_list_store_set (store, &iter,
690 COL_ENABLED, enabled,
694 g_list_free (all_groups);
698 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
700 /* FIXME: not implemented */
704 contact_widget_model_find_name (EmpathyContactWidget *information,
712 if (G_STR_EMPTY (name)) {
716 data.information = information;
720 view = GTK_TREE_VIEW (information->treeview_groups);
721 model = gtk_tree_view_get_model (view);
723 gtk_tree_model_foreach (model,
724 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
727 if (data.found == TRUE) {
728 *iter = data.found_iter;
736 contact_widget_model_find_name_foreach (GtkTreeModel *model,
743 gtk_tree_model_get (model, iter,
751 if (data->name && strcmp (data->name, name) == 0) {
753 data->found_iter = *iter;
766 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
768 EmpathyContactWidget *information)
778 view = GTK_TREE_VIEW (information->treeview_groups);
779 model = gtk_tree_view_get_model (view);
780 store = GTK_LIST_STORE (model);
782 path = gtk_tree_path_new_from_string (path_string);
784 gtk_tree_model_get_iter (model, &iter, path);
785 gtk_tree_model_get (model, &iter,
786 COL_ENABLED, &enabled,
790 gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
791 gtk_tree_path_free (path);
795 empathy_contact_remove_group (information->contact, group);
797 empathy_contact_add_group (information->contact, group);
805 contact_widget_entry_group_changed_cb (GtkEditable *editable,
806 EmpathyContactWidget *information)
811 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
813 if (contact_widget_model_find_name (information, group, &iter)) {
814 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
817 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
818 !G_STR_EMPTY (group));
823 contact_widget_entry_group_activate_cb (GtkEntry *entry,
824 EmpathyContactWidget *information)
826 gtk_widget_activate (GTK_WIDGET (information->button_group));
830 contact_widget_button_group_clicked_cb (GtkButton *button,
831 EmpathyContactWidget *information)
838 view = GTK_TREE_VIEW (information->treeview_groups);
839 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
841 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
843 gtk_list_store_append (store, &iter);
844 gtk_list_store_set (store, &iter,
849 empathy_contact_add_group (information->contact, group);
853 contact_widget_details_setup (EmpathyContactWidget *information)
855 /* FIXME: Needs new telepathy spec */
856 gtk_widget_hide (information->vbox_details);
860 contact_widget_details_update (EmpathyContactWidget *information)
862 /* FIXME: Needs new telepathy spec */
866 contact_widget_client_setup (EmpathyContactWidget *information)
868 /* FIXME: Needs new telepathy spec */
869 gtk_widget_hide (information->vbox_client);
873 contact_widget_client_update (EmpathyContactWidget *information)
875 /* FIXME: Needs new telepathy spec */