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 GtkCellRenderer *renderer;
43 GtkWidget *vbox_contact_widget;
45 GtkWidget *vbox_contact;
46 GtkWidget *widget_avatar;
48 GtkWidget *entry_alias;
49 GtkWidget *widget_alias;
50 GtkWidget *image_state;
51 GtkWidget *label_status;
52 GtkWidget *table_contact;
53 GtkWidget *hbox_contact;
55 GtkWidget *vbox_groups;
56 GtkWidget *entry_group;
57 GtkWidget *button_group;
58 GtkWidget *treeview_groups;
60 GtkWidget *vbox_details;
61 GtkWidget *table_details;
62 GtkWidget *hbox_details_requested;
64 GtkWidget *vbox_client;
65 GtkWidget *table_client;
66 GtkWidget *hbow_client_requested;
67 } EmpathyContactWidget;
70 EmpathyContactWidget *information;
73 GtkTreeIter found_iter;
76 static void contact_widget_destroy_cb (GtkWidget *widget,
77 EmpathyContactWidget *information);
78 static void contact_widget_contact_setup (EmpathyContactWidget *information);
79 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
80 static void contact_widget_presence_notify_cb (EmpathyContactWidget *information);
81 static void contact_widget_avatar_notify_cb (EmpathyContactWidget *information);
82 static void contact_widget_groups_setup (EmpathyContactWidget *information);
83 static void contact_widget_model_setup (EmpathyContactWidget *information);
84 static void contact_widget_model_populate_columns (EmpathyContactWidget *information);
85 static void contact_widget_groups_populate_data (EmpathyContactWidget *information);
86 static void contact_widget_groups_notify_cb (EmpathyContactWidget *information);
87 static gboolean contact_widget_model_find_name (EmpathyContactWidget *information,
90 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
94 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
96 EmpathyContactWidget *information);
97 static void contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
99 EmpathyContactWidget *information);
100 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
101 EmpathyContactWidget *information);
102 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
103 EmpathyContactWidget *information);
104 static void contact_widget_button_group_clicked_cb (GtkButton *button,
105 EmpathyContactWidget *information);
106 static void contact_widget_details_setup (EmpathyContactWidget *information);
107 static void contact_widget_client_setup (EmpathyContactWidget *information);
117 empathy_contact_widget_new (GossipContact *contact,
120 EmpathyContactWidget *information;
122 GossipContact *user_contact;
124 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
126 information = g_slice_new0 (EmpathyContactWidget);
127 information->contact = g_object_ref (contact);
128 user_contact = gossip_contact_get_user (contact);
129 information->is_user = gossip_contact_equal (contact, user_contact);
130 information->editable = editable;
132 glade = gossip_glade_get_file ("empathy-contact-widget.glade",
133 "vbox_contact_widget",
135 "vbox_contact_widget", &information->vbox_contact_widget,
136 "vbox_contact", &information->vbox_contact,
137 "label_id", &information->label_id,
138 "image_state", &information->image_state,
139 "label_status", &information->label_status,
140 "table_contact", &information->table_contact,
141 "hbox_contact", &information->hbox_contact,
142 "vbox_groups", &information->vbox_groups,
143 "entry_group", &information->entry_group,
144 "button_group", &information->button_group,
145 "treeview_groups", &information->treeview_groups,
146 "vbox_details", &information->vbox_details,
147 "table_details", &information->table_details,
148 "hbox_details_requested", &information->hbox_details_requested,
149 "vbox_client", &information->vbox_client,
150 "table_client", &information->table_client,
151 "hbox_client_requested", &information->hbow_client_requested,
154 gossip_glade_connect (glade,
156 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
157 "entry_group", "changed", contact_widget_entry_group_changed_cb,
158 "entry_group", "activate", contact_widget_entry_group_activate_cb,
159 "button_group", "clicked", contact_widget_button_group_clicked_cb,
162 g_object_unref (glade);
164 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
165 "EmpathyContactWidget",
168 contact_widget_contact_setup (information);
169 contact_widget_groups_setup (information);
170 contact_widget_details_setup (information);
171 contact_widget_client_setup (information);
173 gtk_widget_show (information->vbox_contact_widget);
175 return information->vbox_contact_widget;
179 empathy_contact_widget_get_contact (GtkWidget *widget)
181 EmpathyContactWidget *information;
183 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
185 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
190 return information->contact;
194 contact_widget_destroy_cb (GtkWidget *widget,
195 EmpathyContactWidget *information)
197 g_signal_handlers_disconnect_by_func (information->contact,
198 contact_widget_name_notify_cb,
200 g_signal_handlers_disconnect_by_func (information->contact,
201 contact_widget_presence_notify_cb,
203 g_signal_handlers_disconnect_by_func (information->contact,
204 contact_widget_avatar_notify_cb,
206 g_signal_handlers_disconnect_by_func (information->contact,
207 contact_widget_groups_notify_cb,
210 g_object_unref (information->contact);
211 g_slice_free (EmpathyContactWidget, information);
215 contact_widget_contact_setup (EmpathyContactWidget *information)
217 g_signal_connect_swapped (information->contact, "notify::name",
218 G_CALLBACK (contact_widget_name_notify_cb),
220 g_signal_connect_swapped (information->contact, "notify::presence",
221 G_CALLBACK (contact_widget_presence_notify_cb),
223 g_signal_connect_swapped (information->contact, "notify::avatar",
224 G_CALLBACK (contact_widget_avatar_notify_cb),
227 /* FIXME: Use GossipAvatarImage if (editable && is_user) */
228 information->widget_avatar = gtk_image_new ();
229 gtk_box_pack_end (GTK_BOX (information->hbox_contact),
230 information->widget_avatar,
234 /* Setup alias entry or label */
235 if (information->editable) {
236 information->widget_alias = gtk_entry_new ();
237 g_signal_connect (information->widget_alias, "focus-out-event",
238 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
241 information->widget_alias = gtk_label_new (NULL);
242 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
244 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
245 information->widget_alias,
247 gtk_widget_show (information->widget_alias);
250 gtk_label_set_text (GTK_LABEL (information->label_id),
251 gossip_contact_get_id (information->contact));
253 /* Update all widgets */
254 contact_widget_name_notify_cb (information);
255 contact_widget_presence_notify_cb (information);
256 contact_widget_avatar_notify_cb (information);
260 contact_widget_name_notify_cb (EmpathyContactWidget *information)
262 if (information->editable) {
263 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
264 gossip_contact_get_name (information->contact));
266 gtk_label_set_label (GTK_LABEL (information->widget_alias),
267 gossip_contact_get_name (information->contact));
272 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
274 gtk_label_set_text (GTK_LABEL (information->label_status),
275 gossip_contact_get_status (information->contact));
276 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
277 gossip_icon_name_for_contact (information->contact),
278 GTK_ICON_SIZE_BUTTON);
283 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
285 GdkPixbuf *avatar_pixbuf;
287 avatar_pixbuf = gossip_pixbuf_avatar_from_contact_scaled (information->contact,
291 gtk_image_set_from_pixbuf (GTK_IMAGE (information->widget_avatar),
293 gtk_widget_show (information->widget_avatar);
294 g_object_unref (avatar_pixbuf);
296 gtk_widget_hide (information->widget_avatar);
301 contact_widget_groups_setup (EmpathyContactWidget *information)
303 if (information->editable) {
304 contact_widget_model_setup (information);
306 g_signal_connect_swapped (information->contact, "notify::groups",
307 G_CALLBACK (contact_widget_groups_notify_cb),
309 contact_widget_groups_populate_data (information);
311 gtk_widget_show (information->vbox_groups);
316 contact_widget_model_setup (EmpathyContactWidget *information)
320 GtkTreeSelection *selection;
322 view = GTK_TREE_VIEW (information->treeview_groups);
324 store = gtk_list_store_new (COL_COUNT,
325 G_TYPE_STRING, /* name */
326 G_TYPE_BOOLEAN, /* enabled */
327 G_TYPE_BOOLEAN); /* editable */
329 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
331 selection = gtk_tree_view_get_selection (view);
332 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
334 contact_widget_model_populate_columns (information);
336 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
337 COL_NAME, GTK_SORT_ASCENDING);
339 g_object_unref (store);
343 contact_widget_model_populate_columns (EmpathyContactWidget *information)
347 GtkTreeViewColumn *column;
348 GtkCellRenderer *renderer;
351 view = GTK_TREE_VIEW (information->treeview_groups);
352 model = gtk_tree_view_get_model (view);
354 renderer = gtk_cell_renderer_toggle_new ();
355 g_signal_connect (renderer, "toggled",
356 G_CALLBACK (contact_widget_cell_toggled),
359 column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
360 "active", COL_ENABLED,
363 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
364 gtk_tree_view_column_set_fixed_width (column, 50);
365 gtk_tree_view_append_column (view, column);
367 renderer = gtk_cell_renderer_text_new ();
368 col_offset = gtk_tree_view_insert_column_with_attributes (view,
372 /* "editable", COL_EDITABLE, */
375 g_object_set_data (G_OBJECT (renderer),
376 "column", GINT_TO_POINTER (COL_NAME));
378 column = gtk_tree_view_get_column (view, col_offset - 1);
379 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
380 gtk_tree_view_column_set_resizable (column,FALSE);
381 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
383 if (information->renderer) {
384 g_object_unref (information->renderer);
387 information->renderer = g_object_ref (renderer);
391 contact_widget_groups_populate_data (EmpathyContactWidget *information)
393 EmpathyContactManager *manager;
397 GList *my_groups, *l;
400 view = GTK_TREE_VIEW (information->treeview_groups);
401 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
403 manager = empathy_contact_manager_new ();
404 all_groups = empathy_contact_manager_get_groups (manager);
405 my_groups = gossip_contact_get_groups (information->contact);
406 g_object_unref (manager);
408 for (l = all_groups; l; l = l->next) {
409 const gchar *group_str;
414 enabled = g_list_find_custom (my_groups,
416 (GCompareFunc) strcmp) != NULL;
418 gtk_list_store_append (store, &iter);
419 gtk_list_store_set (store, &iter,
422 COL_ENABLED, enabled,
426 g_list_free (all_groups);
430 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
432 /* FIXME: not implemented */
436 contact_widget_model_find_name (EmpathyContactWidget *information,
444 if (G_STR_EMPTY (name)) {
448 data.information = information;
452 view = GTK_TREE_VIEW (information->treeview_groups);
453 model = gtk_tree_view_get_model (view);
455 gtk_tree_model_foreach (model,
456 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
459 if (data.found == TRUE) {
460 *iter = data.found_iter;
468 contact_widget_model_find_name_foreach (GtkTreeModel *model,
475 gtk_tree_model_get (model, iter,
483 if (data->name && strcmp (data->name, name) == 0) {
485 data->found_iter = *iter;
498 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
500 EmpathyContactWidget *information)
510 view = GTK_TREE_VIEW (information->treeview_groups);
511 model = gtk_tree_view_get_model (view);
512 store = GTK_LIST_STORE (model);
514 path = gtk_tree_path_new_from_string (path_string);
516 gtk_tree_model_get_iter (model, &iter, path);
517 gtk_tree_model_get (model, &iter,
518 COL_ENABLED, &enabled,
522 gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
523 gtk_tree_path_free (path);
527 gossip_contact_remove_group (information->contact, group);
529 gossip_contact_add_group (information->contact, group);
537 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
538 GdkEventFocus *event,
539 EmpathyContactWidget *information)
543 name = gtk_entry_get_text (GTK_ENTRY (editable));
544 gossip_contact_set_name (information->contact, name);
548 contact_widget_entry_group_changed_cb (GtkEditable *editable,
549 EmpathyContactWidget *information)
554 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
556 if (contact_widget_model_find_name (information, group, &iter)) {
557 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
560 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
561 !G_STR_EMPTY (group));
566 contact_widget_entry_group_activate_cb (GtkEntry *entry,
567 EmpathyContactWidget *information)
569 gtk_widget_activate (GTK_WIDGET (information->button_group));
573 contact_widget_button_group_clicked_cb (GtkButton *button,
574 EmpathyContactWidget *information)
581 view = GTK_TREE_VIEW (information->treeview_groups);
582 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
584 group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
586 gtk_list_store_append (store, &iter);
587 gtk_list_store_set (store, &iter,
592 gossip_contact_add_group (information->contact, group);
596 contact_widget_details_setup (EmpathyContactWidget *information)
598 /* FIXME: Needs new telepathy spec */
602 contact_widget_client_setup (EmpathyContactWidget *information)
604 /* FIXME: Needs new telepathy spec */