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 <libempathy/empathy-contact-manager.h>
34 #include "empathy-contact-widget.h"
35 #include "gossip-ui-utils.h"
38 GossipContact *contact;
41 gboolean changes_made;
42 GtkCellRenderer *renderer;
44 GtkWidget *vbox_contact_widget;
46 GtkWidget *vbox_contact;
47 GtkWidget *widget_avatar;
49 GtkWidget *entry_alias;
50 GtkWidget *widget_alias;
51 GtkWidget *image_state;
52 GtkWidget *label_status;
53 GtkWidget *table_contact;
54 GtkWidget *hbox_contact;
56 GtkWidget *vbox_groups;
57 GtkWidget *entry_group;
58 GtkWidget *button_group;
59 GtkWidget *treeview_groups;
61 GtkWidget *vbox_details;
62 GtkWidget *table_details;
63 GtkWidget *hbox_details_requested;
65 GtkWidget *vbox_client;
66 GtkWidget *table_client;
67 GtkWidget *hbow_client_requested;
68 } EmpathyContactWidget;
71 EmpathyContactWidget *information;
74 GtkTreeIter found_iter;
78 EmpathyContactWidget *information;
82 static void contact_widget_destroy_cb (GtkWidget *widget,
83 EmpathyContactWidget *information);
84 static void contact_widget_contact_setup (EmpathyContactWidget *information);
85 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
86 static void contact_widget_presence_notify_cb (EmpathyContactWidget *information);
87 static void contact_widget_avatar_notify_cb (EmpathyContactWidget *information);
88 static void contact_widget_groups_setup (EmpathyContactWidget *information);
89 static void contact_widget_model_setup (EmpathyContactWidget *information);
90 static void contact_widget_model_populate_columns (EmpathyContactWidget *information);
91 static void contact_widget_groups_populate_data (EmpathyContactWidget *information);
92 static void contact_widget_groups_notify_cb (EmpathyContactWidget *information);
93 static gboolean contact_widget_model_find_name (EmpathyContactWidget *information,
96 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
100 static GList * contact_widget_model_find_selected (EmpathyContactWidget *information);
101 static gboolean contact_widget_model_find_selected_foreach (GtkTreeModel *model,
105 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
107 EmpathyContactWidget *information);
108 static void contact_widget_entry_alias_changed_cb (GtkEditable *editable,
109 EmpathyContactWidget *information);
110 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
111 EmpathyContactWidget *information);
112 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
113 EmpathyContactWidget *information);
114 static void contact_widget_button_group_clicked_cb (GtkButton *button,
115 EmpathyContactWidget *information);
116 static void contact_widget_details_setup (EmpathyContactWidget *information);
117 static void contact_widget_client_setup (EmpathyContactWidget *information);
127 empathy_contact_widget_new (GossipContact *contact,
130 EmpathyContactWidget *information;
132 GossipContact *user_contact;
134 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
136 information = g_slice_new0 (EmpathyContactWidget);
137 information->contact = g_object_ref (contact);
138 user_contact = gossip_contact_get_user (contact);
139 information->is_user = gossip_contact_equal (contact, user_contact);
140 information->editable = editable;
142 glade = gossip_glade_get_file ("empathy-contact-widget.glade",
143 "vbox_contact_widget",
145 "vbox_contact_widget", &information->vbox_contact_widget,
146 "vbox_contact", &information->vbox_contact,
147 "label_id", &information->label_id,
148 "image_state", &information->image_state,
149 "label_status", &information->label_status,
150 "table_contact", &information->table_contact,
151 "hbox_contact", &information->hbox_contact,
152 "vbox_groups", &information->vbox_groups,
153 "entry_group", &information->entry_group,
154 "button_group", &information->button_group,
155 "treeview_groups", &information->treeview_groups,
156 "vbox_details", &information->vbox_details,
157 "table_details", &information->table_details,
158 "hbox_details_requested", &information->hbox_details_requested,
159 "vbox_client", &information->vbox_client,
160 "table_client", &information->table_client,
161 "hbox_client_requested", &information->hbow_client_requested,
164 gossip_glade_connect (glade,
166 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
167 "entry_group", "changed", contact_widget_entry_group_changed_cb,
168 "entry_group", "activate", contact_widget_entry_group_activate_cb,
169 "button_group", "clicked", contact_widget_button_group_clicked_cb,
172 g_object_unref (glade);
174 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
175 "EmpathyContactWidget",
178 contact_widget_contact_setup (information);
179 contact_widget_groups_setup (information);
180 contact_widget_details_setup (information);
181 contact_widget_client_setup (information);
183 gtk_widget_show (information->vbox_contact_widget);
185 return information->vbox_contact_widget;
189 empathy_contact_widget_save (GtkWidget *widget)
191 EmpathyContactWidget *information;
195 g_return_if_fail (GTK_IS_WIDGET (widget));
197 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
199 !information->editable ||
200 !information->changes_made) {
204 name = gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
205 groups = contact_widget_model_find_selected (information);
207 gossip_contact_set_name (information->contact, name);
208 gossip_contact_set_groups (information->contact, groups);
210 g_list_foreach (groups, (GFunc) g_free, NULL);
211 g_list_free (groups);
215 empathy_contact_widget_get_contact (GtkWidget *widget)
217 EmpathyContactWidget *information;
219 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
221 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
226 return information->contact;
230 contact_widget_destroy_cb (GtkWidget *widget,
231 EmpathyContactWidget *information)
233 g_signal_handlers_disconnect_by_func (information->contact,
234 contact_widget_name_notify_cb,
236 g_signal_handlers_disconnect_by_func (information->contact,
237 contact_widget_presence_notify_cb,
239 g_signal_handlers_disconnect_by_func (information->contact,
240 contact_widget_avatar_notify_cb,
242 g_signal_handlers_disconnect_by_func (information->contact,
243 contact_widget_groups_notify_cb,
246 g_object_unref (information->contact);
247 g_slice_free (EmpathyContactWidget, information);
251 contact_widget_contact_setup (EmpathyContactWidget *information)
253 g_signal_connect_swapped (information->contact, "notify::name",
254 G_CALLBACK (contact_widget_name_notify_cb),
256 g_signal_connect_swapped (information->contact, "notify::presence",
257 G_CALLBACK (contact_widget_presence_notify_cb),
259 g_signal_connect_swapped (information->contact, "notify::avatar",
260 G_CALLBACK (contact_widget_avatar_notify_cb),
263 /* FIXME: Use GossipAvatarImage if (editable && is_user) */
264 information->widget_avatar = gtk_image_new ();
265 gtk_box_pack_end (GTK_BOX (information->hbox_contact),
266 information->widget_avatar,
270 /* Setup alias entry or label */
271 if (information->editable) {
272 information->widget_alias = gtk_entry_new ();
273 g_signal_connect (information->widget_alias, "changed",
274 G_CALLBACK (contact_widget_entry_alias_changed_cb),
277 information->widget_alias = gtk_label_new (NULL);
278 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
280 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
281 information->widget_alias,
283 gtk_widget_show (information->widget_alias);
286 gtk_label_set_text (GTK_LABEL (information->label_id),
287 gossip_contact_get_id (information->contact));
289 /* Update all widgets */
290 contact_widget_name_notify_cb (information);
291 contact_widget_presence_notify_cb (information);
292 contact_widget_avatar_notify_cb (information);
296 contact_widget_name_notify_cb (EmpathyContactWidget *information)
298 if (information->editable) {
299 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
300 gossip_contact_get_name (information->contact));
302 gtk_label_set_label (GTK_LABEL (information->widget_alias),
303 gossip_contact_get_name (information->contact));
308 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
310 gtk_label_set_text (GTK_LABEL (information->label_status),
311 gossip_contact_get_status (information->contact));
312 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
313 gossip_icon_name_for_contact (information->contact),
314 GTK_ICON_SIZE_BUTTON);
319 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
321 GdkPixbuf *avatar_pixbuf;
323 avatar_pixbuf = gossip_pixbuf_avatar_from_contact_scaled (information->contact,
327 gtk_image_set_from_pixbuf (GTK_IMAGE (information->widget_avatar),
329 gtk_widget_show (information->widget_avatar);
330 g_object_unref (avatar_pixbuf);
332 gtk_widget_hide (information->widget_avatar);
337 contact_widget_groups_setup (EmpathyContactWidget *information)
339 if (information->editable) {
340 contact_widget_model_setup (information);
342 g_signal_connect_swapped (information->contact, "notify::groups",
343 G_CALLBACK (contact_widget_groups_notify_cb),
345 contact_widget_groups_populate_data (information);
347 gtk_widget_show (information->vbox_groups);
352 contact_widget_model_setup (EmpathyContactWidget *information)
356 GtkTreeSelection *selection;
358 view = GTK_TREE_VIEW (information->treeview_groups);
360 store = gtk_list_store_new (COL_COUNT,
361 G_TYPE_STRING, /* name */
362 G_TYPE_BOOLEAN, /* enabled */
363 G_TYPE_BOOLEAN); /* editable */
365 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
367 selection = gtk_tree_view_get_selection (view);
368 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
370 contact_widget_model_populate_columns (information);
372 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
373 COL_NAME, GTK_SORT_ASCENDING);
375 g_object_unref (store);
379 contact_widget_model_populate_columns (EmpathyContactWidget *information)
383 GtkTreeViewColumn *column;
384 GtkCellRenderer *renderer;
387 view = GTK_TREE_VIEW (information->treeview_groups);
388 model = gtk_tree_view_get_model (view);
390 renderer = gtk_cell_renderer_toggle_new ();
391 g_signal_connect (renderer, "toggled",
392 G_CALLBACK (contact_widget_cell_toggled),
395 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
396 "active", COL_ENABLED,
399 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
400 gtk_tree_view_column_set_fixed_width (column, 50);
401 gtk_tree_view_append_column (view, column);
403 renderer = gtk_cell_renderer_text_new ();
404 col_offset = gtk_tree_view_insert_column_with_attributes (view,
408 /* "editable", COL_EDITABLE, */
411 g_object_set_data (G_OBJECT (renderer),
412 "column", GINT_TO_POINTER (COL_NAME));
414 column = gtk_tree_view_get_column (view, col_offset - 1);
415 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
416 gtk_tree_view_column_set_resizable (column,FALSE);
417 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
419 if (information->renderer) {
420 g_object_unref (information->renderer);
423 information->renderer = g_object_ref (renderer);
427 contact_widget_groups_populate_data (EmpathyContactWidget *information)
429 EmpathyContactManager *manager;
434 GList *my_groups = NULL;
437 view = GTK_TREE_VIEW (information->treeview_groups);
438 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
440 manager = empathy_contact_manager_new ();
441 all_groups = empathy_contact_manager_get_groups (manager);
442 groups = gossip_contact_get_groups (information->contact);
443 g_object_unref (manager);
445 for (l = groups; l; l = l->next) {
446 const gchar *group_str;
449 if (strcmp (group_str, _("Unsorted")) == 0) {
453 my_groups = g_list_append (my_groups, g_strdup (group_str));
456 for (l = all_groups; l; l = l->next) {
457 const gchar *group_str;
461 if (strcmp (group_str, _("Unsorted")) == 0) {
465 enabled = g_list_find_custom (my_groups,
467 (GCompareFunc) strcmp) != NULL;
469 gtk_list_store_append (store, &iter);
470 gtk_list_store_set (store, &iter,
473 COL_ENABLED, enabled,
477 g_list_foreach (my_groups, (GFunc) g_free, NULL);
478 g_list_free (my_groups);
480 g_list_free (all_groups);
484 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
486 /* FIXME: not implemented */
490 contact_widget_model_find_name (EmpathyContactWidget *information,
498 if (G_STR_EMPTY (name)) {
502 data.information = information;
506 view = GTK_TREE_VIEW (information->treeview_groups);
507 model = gtk_tree_view_get_model (view);
509 gtk_tree_model_foreach (model,
510 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
513 if (data.found == TRUE) {
514 *iter = data.found_iter;
522 contact_widget_model_find_name_foreach (GtkTreeModel *model,
529 gtk_tree_model_get (model, iter,
537 if (data->name && strcmp (data->name, name) == 0) {
539 data->found_iter = *iter;
552 contact_widget_model_find_selected (EmpathyContactWidget *information)
558 data.information = information;
561 view = GTK_TREE_VIEW (information->treeview_groups);
562 model = gtk_tree_view_get_model (view);
564 gtk_tree_model_foreach (model,
565 (GtkTreeModelForeachFunc) contact_widget_model_find_selected_foreach,
572 contact_widget_model_find_selected_foreach (GtkTreeModel *model,
580 gtk_tree_model_get (model, iter,
582 COL_ENABLED, &selected,
590 data->list = g_list_append (data->list, name);
600 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
602 EmpathyContactWidget *information)
611 view = GTK_TREE_VIEW (information->treeview_groups);
612 model = gtk_tree_view_get_model (view);
613 store = GTK_LIST_STORE (model);
615 path = gtk_tree_path_new_from_string (path_string);
617 gtk_tree_model_get_iter (model, &iter, path);
618 gtk_tree_model_get (model, &iter, COL_ENABLED, &enabled, -1);
622 gtk_list_store_set (store, &iter, COL_ENABLED, enabled, -1);
623 gtk_tree_path_free (path);
625 information->changes_made = TRUE;
629 contact_widget_entry_alias_changed_cb (GtkEditable *editable,
630 EmpathyContactWidget *information)
632 information->changes_made = TRUE;
636 contact_widget_entry_group_changed_cb (GtkEditable *editable,
637 EmpathyContactWidget *information)
642 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
644 if (contact_widget_model_find_name (information, group, &iter)) {
645 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
648 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
649 !G_STR_EMPTY (group));
654 contact_widget_entry_group_activate_cb (GtkEntry *entry,
655 EmpathyContactWidget *information)
657 gtk_widget_activate (GTK_WIDGET (information->button_group));
661 contact_widget_button_group_clicked_cb (GtkButton *button,
662 EmpathyContactWidget *information)
669 view = GTK_TREE_VIEW (information->treeview_groups);
670 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
672 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
674 gtk_list_store_append (store, &iter);
675 gtk_list_store_set (store, &iter,
680 information->changes_made = TRUE;
684 contact_widget_details_setup (EmpathyContactWidget *information)
686 /* FIXME: Needs new telepathy spec */
690 contact_widget_client_setup (EmpathyContactWidget *information)
692 /* FIXME: Needs new telepathy spec */