]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
Do not save when closing the dialog. Update information in real-time and
[empathy.git] / libempathy-gtk / empathy-contact-widget.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include <gtk/gtk.h>
29 #include <glade/glade.h>
30 #include <glib/gi18n.h>
31
32 #include <libempathy/empathy-contact-manager.h>
33
34 #include "empathy-contact-widget.h"
35 #include "gossip-ui-utils.h"
36
37 typedef struct {
38         GossipContact   *contact;
39         gboolean         is_user;
40         gboolean         editable;
41         GtkCellRenderer *renderer;
42
43         GtkWidget       *vbox_contact_widget;
44
45         GtkWidget       *vbox_contact;
46         GtkWidget       *widget_avatar;
47         GtkWidget       *label_id;
48         GtkWidget       *entry_alias;
49         GtkWidget       *widget_alias;
50         GtkWidget       *image_state;
51         GtkWidget       *label_status;
52         GtkWidget       *table_contact;
53         GtkWidget       *hbox_contact;
54
55         GtkWidget       *vbox_groups;
56         GtkWidget       *entry_group;
57         GtkWidget       *button_group;
58         GtkWidget       *treeview_groups;
59
60         GtkWidget       *vbox_details;
61         GtkWidget       *table_details;
62         GtkWidget       *hbox_details_requested;
63
64         GtkWidget       *vbox_client;
65         GtkWidget       *table_client;
66         GtkWidget       *hbow_client_requested;
67 } EmpathyContactWidget;
68
69 typedef struct {
70         EmpathyContactWidget *information;
71         const gchar          *name;
72         gboolean              found;
73         GtkTreeIter           found_iter;
74 } FindName;
75
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,
88                                                             const gchar           *name,
89                                                             GtkTreeIter           *iter);
90 static gboolean contact_widget_model_find_name_foreach     (GtkTreeModel          *model,
91                                                             GtkTreePath           *path,
92                                                             GtkTreeIter           *iter,
93                                                             FindName              *data);
94 static void     contact_widget_cell_toggled                (GtkCellRendererToggle *cell,
95                                                             gchar                 *path_string,
96                                                             EmpathyContactWidget  *information);
97 static void     contact_widget_entry_alias_focus_event_cb  (GtkEditable           *editable,
98                                                             GdkEventFocus         *event,
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);
108
109 enum {
110         COL_NAME,
111         COL_ENABLED,
112         COL_EDITABLE,
113         COL_COUNT
114 };
115
116 GtkWidget *
117 empathy_contact_widget_new (GossipContact *contact,
118                             gboolean       editable)
119 {
120         EmpathyContactWidget *information;
121         GladeXML             *glade;
122         GossipContact        *user_contact;
123
124         g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
125
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;
131
132         glade = gossip_glade_get_file ("empathy-contact-widget.glade",
133                                        "vbox_contact_widget",
134                                        NULL,
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,
152                                        NULL);
153
154         gossip_glade_connect (glade,
155                               information,
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,
160                               NULL);
161
162         g_object_unref (glade);
163
164         g_object_set_data (G_OBJECT (information->vbox_contact_widget),
165                            "EmpathyContactWidget",
166                            information);
167
168         contact_widget_contact_setup (information);
169         contact_widget_groups_setup (information);
170         contact_widget_details_setup (information);
171         contact_widget_client_setup (information);
172
173         gtk_widget_show (information->vbox_contact_widget);
174
175         return information->vbox_contact_widget;
176 }
177
178 GossipContact *
179 empathy_contact_widget_get_contact (GtkWidget *widget)
180 {
181         EmpathyContactWidget *information;
182
183         g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
184
185         information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
186         if (!information) {
187                 return NULL;
188         }
189
190         return information->contact;
191 }
192         
193 static void
194 contact_widget_destroy_cb (GtkWidget            *widget,
195                            EmpathyContactWidget *information)
196 {
197         g_signal_handlers_disconnect_by_func (information->contact,
198                                               contact_widget_name_notify_cb,
199                                               information);
200         g_signal_handlers_disconnect_by_func (information->contact,
201                                               contact_widget_presence_notify_cb,
202                                               information);
203         g_signal_handlers_disconnect_by_func (information->contact,
204                                               contact_widget_avatar_notify_cb,
205                                               information);
206         g_signal_handlers_disconnect_by_func (information->contact,
207                                               contact_widget_groups_notify_cb,
208                                               information);
209
210         g_object_unref (information->contact);
211         g_slice_free (EmpathyContactWidget, information);
212 }
213
214 static void
215 contact_widget_contact_setup (EmpathyContactWidget *information)
216 {
217         g_signal_connect_swapped (information->contact, "notify::name",
218                                   G_CALLBACK (contact_widget_name_notify_cb),
219                                   information);
220         g_signal_connect_swapped (information->contact, "notify::presence",
221                                   G_CALLBACK (contact_widget_presence_notify_cb),
222                                   information);
223         g_signal_connect_swapped (information->contact, "notify::avatar",
224                                   G_CALLBACK (contact_widget_avatar_notify_cb),
225                                   information);
226
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,
231                           FALSE, FALSE,
232                           6);
233
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),
239                                   information);
240         } else {
241                 information->widget_alias = gtk_label_new (NULL);
242                 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
243         }
244         gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
245                                    information->widget_alias,
246                                    1, 2, 1, 2);
247         gtk_widget_show (information->widget_alias);
248
249         /* Setup id label */
250         gtk_label_set_text (GTK_LABEL (information->label_id),
251                             gossip_contact_get_id (information->contact));
252
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);
257 }
258
259 static void
260 contact_widget_name_notify_cb (EmpathyContactWidget *information)
261 {
262         if (information->editable) {
263                 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
264                                     gossip_contact_get_name (information->contact));
265         } else {
266                 gtk_label_set_label (GTK_LABEL (information->widget_alias),
267                                      gossip_contact_get_name (information->contact));
268         }
269 }
270
271 static void
272 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
273 {
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);
279
280 }
281
282 static void
283 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
284 {
285         GdkPixbuf *avatar_pixbuf;
286
287         avatar_pixbuf = gossip_pixbuf_avatar_from_contact_scaled (information->contact,
288                                                                   48, 48);
289
290         if (avatar_pixbuf) {
291                 gtk_image_set_from_pixbuf (GTK_IMAGE (information->widget_avatar),
292                                            avatar_pixbuf);
293                 gtk_widget_show  (information->widget_avatar);
294                 g_object_unref (avatar_pixbuf);
295         } else {
296                 gtk_widget_hide  (information->widget_avatar);
297         }
298 }
299
300 static void
301 contact_widget_groups_setup (EmpathyContactWidget *information)
302 {
303         if (information->editable) {
304                 contact_widget_model_setup (information);
305
306                 g_signal_connect_swapped (information->contact, "notify::groups",
307                                           G_CALLBACK (contact_widget_groups_notify_cb),
308                                           information);
309                 contact_widget_groups_populate_data (information);
310
311                 gtk_widget_show (information->vbox_groups);
312         }
313 }
314
315 static void
316 contact_widget_model_setup (EmpathyContactWidget *information)
317 {
318         GtkTreeView      *view;
319         GtkListStore     *store;
320         GtkTreeSelection *selection;
321
322         view = GTK_TREE_VIEW (information->treeview_groups);
323
324         store = gtk_list_store_new (COL_COUNT,
325                                     G_TYPE_STRING,   /* name */
326                                     G_TYPE_BOOLEAN,  /* enabled */
327                                     G_TYPE_BOOLEAN); /* editable */
328
329         gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
330
331         selection = gtk_tree_view_get_selection (view);
332         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
333
334         contact_widget_model_populate_columns (information);
335
336         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
337                                               COL_NAME, GTK_SORT_ASCENDING);
338
339         g_object_unref (store);
340 }
341
342 static void
343 contact_widget_model_populate_columns (EmpathyContactWidget *information)
344 {
345         GtkTreeView       *view;
346         GtkTreeModel      *model;
347         GtkTreeViewColumn *column;
348         GtkCellRenderer   *renderer;
349         guint              col_offset;
350
351         view = GTK_TREE_VIEW (information->treeview_groups);
352         model = gtk_tree_view_get_model (view);
353
354         renderer = gtk_cell_renderer_toggle_new ();
355         g_signal_connect (renderer, "toggled",
356                           G_CALLBACK (contact_widget_cell_toggled),
357                           information);
358
359         column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
360                                                            "active", COL_ENABLED,
361                                                            NULL);
362
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);
366
367         renderer = gtk_cell_renderer_text_new ();
368         col_offset = gtk_tree_view_insert_column_with_attributes (view,
369                                                                   -1, _("Group"),
370                                                                   renderer,
371                                                                   "text", COL_NAME,
372                                                                   /* "editable", COL_EDITABLE, */
373                                                                   NULL);
374
375         g_object_set_data (G_OBJECT (renderer),
376                            "column", GINT_TO_POINTER (COL_NAME));
377
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);
382
383         if (information->renderer) {
384                 g_object_unref (information->renderer);
385         }
386
387         information->renderer = g_object_ref (renderer);
388 }
389
390 static void
391 contact_widget_groups_populate_data (EmpathyContactWidget *information)
392 {
393         EmpathyContactManager *manager;
394         GtkTreeView           *view;
395         GtkListStore          *store;
396         GtkTreeIter            iter;
397         GList                 *my_groups, *l;
398         GList                 *all_groups;
399
400         view = GTK_TREE_VIEW (information->treeview_groups);
401         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
402
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);
407
408         for (l = all_groups; l; l = l->next) {
409                 const gchar *group_str;
410                 gboolean     enabled;
411
412                 group_str = l->data;
413
414                 enabled = g_list_find_custom (my_groups,
415                                               group_str,
416                                               (GCompareFunc) strcmp) != NULL;
417
418                 gtk_list_store_append (store, &iter);
419                 gtk_list_store_set (store, &iter,
420                                     COL_NAME, group_str,
421                                     COL_EDITABLE, TRUE,
422                                     COL_ENABLED, enabled,
423                                     -1);
424         }
425
426         g_list_free (all_groups);
427 }
428
429 static void
430 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
431 {
432         /* FIXME: not implemented */
433 }
434
435 static gboolean
436 contact_widget_model_find_name (EmpathyContactWidget *information,
437                                 const gchar          *name,
438                                 GtkTreeIter          *iter)
439 {
440         GtkTreeView  *view;
441         GtkTreeModel *model;
442         FindName      data;
443
444         if (G_STR_EMPTY (name)) {
445                 return FALSE;
446         }
447
448         data.information = information;
449         data.name = name;
450         data.found = FALSE;
451
452         view = GTK_TREE_VIEW (information->treeview_groups);
453         model = gtk_tree_view_get_model (view);
454
455         gtk_tree_model_foreach (model,
456                                 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
457                                 &data);
458
459         if (data.found == TRUE) {
460                 *iter = data.found_iter;
461                 return TRUE;
462         }
463
464         return FALSE;
465 }
466
467 static gboolean
468 contact_widget_model_find_name_foreach (GtkTreeModel *model,
469                                         GtkTreePath  *path,
470                                         GtkTreeIter  *iter,
471                                         FindName     *data)
472 {
473         gchar *name;
474
475         gtk_tree_model_get (model, iter,
476                             COL_NAME, &name,
477                             -1);
478
479         if (!name) {
480                 return FALSE;
481         }
482
483         if (data->name && strcmp (data->name, name) == 0) {
484                 data->found = TRUE;
485                 data->found_iter = *iter;
486
487                 g_free (name);
488
489                 return TRUE;
490         }
491
492         g_free (name);
493
494         return FALSE;
495 }
496
497 static void
498 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
499                              gchar                 *path_string,
500                              EmpathyContactWidget  *information)
501 {
502         GtkTreeView  *view;
503         GtkTreeModel *model;
504         GtkListStore *store;
505         GtkTreePath  *path;
506         GtkTreeIter   iter;
507         gboolean      enabled;
508         gchar        *group;
509
510         view = GTK_TREE_VIEW (information->treeview_groups);
511         model = gtk_tree_view_get_model (view);
512         store = GTK_LIST_STORE (model);
513
514         path = gtk_tree_path_new_from_string (path_string);
515
516         gtk_tree_model_get_iter (model, &iter, path);
517         gtk_tree_model_get (model, &iter,
518                             COL_ENABLED, &enabled,
519                             COL_NAME, &group,
520                             -1);
521
522         gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
523         gtk_tree_path_free (path);
524
525         if (group) {
526                 if (enabled) {
527                         gossip_contact_remove_group (information->contact, group);
528                 } else {
529                         gossip_contact_add_group (information->contact, group); 
530                 }
531
532                 g_free (group);
533         }
534 }
535
536 static void
537 contact_widget_entry_alias_focus_event_cb (GtkEditable          *editable,
538                                            GdkEventFocus        *event,
539                                            EmpathyContactWidget *information)
540 {
541         const gchar *name;
542
543         name = gtk_entry_get_text (GTK_ENTRY (editable));
544         gossip_contact_set_name (information->contact, name);
545 }
546
547 static void
548 contact_widget_entry_group_changed_cb (GtkEditable           *editable,
549                                        EmpathyContactWidget  *information)
550 {
551         GtkTreeIter  iter;
552         const gchar *group;
553
554         group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
555
556         if (contact_widget_model_find_name (information, group, &iter)) {
557                 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
558
559         } else {
560                 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
561                                           !G_STR_EMPTY (group));
562         }
563 }
564
565 static void
566 contact_widget_entry_group_activate_cb (GtkEntry              *entry,
567                                         EmpathyContactWidget  *information)
568 {
569         gtk_widget_activate (GTK_WIDGET (information->button_group));
570 }
571
572 static void
573 contact_widget_button_group_clicked_cb (GtkButton             *button,
574                                         EmpathyContactWidget  *information)
575 {
576         GtkTreeView  *view;
577         GtkListStore *store;
578         GtkTreeIter   iter;
579         const gchar  *group;
580
581         view = GTK_TREE_VIEW (information->treeview_groups);
582         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
583
584         group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
585
586         gtk_list_store_append (store, &iter);
587         gtk_list_store_set (store, &iter,
588                             COL_NAME, group,
589                             COL_ENABLED, TRUE,
590                             -1);
591
592         gossip_contact_add_group (information->contact, group);
593 }
594
595 static void
596 contact_widget_details_setup (EmpathyContactWidget *information)
597 {
598         /* FIXME: Needs new telepathy spec */
599 }
600
601 static void
602 contact_widget_client_setup (EmpathyContactWidget *information)
603 {
604         /* FIXME: Needs new telepathy spec */
605 }
606