]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
Fix response and action buttons.
[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         gboolean         changes_made;
42         GtkCellRenderer *renderer;
43
44         GtkWidget       *vbox_contact_widget;
45
46         GtkWidget       *vbox_contact;
47         GtkWidget       *widget_avatar;
48         GtkWidget       *label_id;
49         GtkWidget       *entry_alias;
50         GtkWidget       *widget_alias;
51         GtkWidget       *image_state;
52         GtkWidget       *label_status;
53         GtkWidget       *table_contact;
54         GtkWidget       *hbox_contact;
55
56         GtkWidget       *vbox_groups;
57         GtkWidget       *entry_group;
58         GtkWidget       *button_group;
59         GtkWidget       *treeview_groups;
60
61         GtkWidget       *vbox_details;
62         GtkWidget       *table_details;
63         GtkWidget       *hbox_details_requested;
64
65         GtkWidget       *vbox_client;
66         GtkWidget       *table_client;
67         GtkWidget       *hbow_client_requested;
68 } EmpathyContactWidget;
69
70 typedef struct {
71         EmpathyContactWidget *information;
72         const gchar          *name;
73         gboolean              found;
74         GtkTreeIter           found_iter;
75 } FindName;
76
77 typedef struct {
78         EmpathyContactWidget *information;
79         GList                *list;
80 } FindSelected;
81
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,
94                                                             const gchar           *name,
95                                                             GtkTreeIter           *iter);
96 static gboolean contact_widget_model_find_name_foreach     (GtkTreeModel          *model,
97                                                             GtkTreePath           *path,
98                                                             GtkTreeIter           *iter,
99                                                             FindName              *data);
100 static GList *  contact_widget_model_find_selected         (EmpathyContactWidget  *information);
101 static gboolean contact_widget_model_find_selected_foreach (GtkTreeModel          *model,
102                                                             GtkTreePath           *path,
103                                                             GtkTreeIter           *iter,
104                                                             FindSelected          *data);
105 static void     contact_widget_cell_toggled                (GtkCellRendererToggle *cell,
106                                                             gchar                 *path_string,
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);
118
119 enum {
120         COL_NAME,
121         COL_ENABLED,
122         COL_EDITABLE,
123         COL_COUNT
124 };
125
126 GtkWidget *
127 empathy_contact_widget_new (GossipContact *contact,
128                             gboolean       editable)
129 {
130         EmpathyContactWidget *information;
131         GladeXML             *glade;
132         GossipContact        *user_contact;
133
134         g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
135
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;
141
142         glade = gossip_glade_get_file ("empathy-contact-widget.glade",
143                                        "vbox_contact_widget",
144                                        NULL,
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,
162                                        NULL);
163
164         gossip_glade_connect (glade,
165                               information,
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,
170                               NULL);
171
172         g_object_unref (glade);
173
174         g_object_set_data (G_OBJECT (information->vbox_contact_widget),
175                            "EmpathyContactWidget",
176                            information);
177
178         contact_widget_contact_setup (information);
179         contact_widget_groups_setup (information);
180         contact_widget_details_setup (information);
181         contact_widget_client_setup (information);
182
183         gtk_widget_show (information->vbox_contact_widget);
184
185         return information->vbox_contact_widget;
186 }
187
188 void
189 empathy_contact_widget_save (GtkWidget *widget)
190 {
191         EmpathyContactWidget *information;
192         const gchar          *name;
193         GList                *groups;
194
195         g_return_if_fail (GTK_IS_WIDGET (widget));
196
197         information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
198         if (!information ||
199             !information->editable ||
200             !information->changes_made) {
201                 return;
202         }
203
204         name = gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
205         groups = contact_widget_model_find_selected (information);
206
207         gossip_contact_set_name (information->contact, name);
208         gossip_contact_set_groups (information->contact, groups);
209
210         g_list_foreach (groups, (GFunc) g_free, NULL);
211         g_list_free (groups);
212 }
213
214 GossipContact *
215 empathy_contact_widget_get_contact (GtkWidget *widget)
216 {
217         EmpathyContactWidget *information;
218
219         g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
220
221         information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
222         if (!information) {
223                 return NULL;
224         }
225
226         return information->contact;
227 }
228         
229 static void
230 contact_widget_destroy_cb (GtkWidget            *widget,
231                            EmpathyContactWidget *information)
232 {
233         g_signal_handlers_disconnect_by_func (information->contact,
234                                               contact_widget_name_notify_cb,
235                                               information);
236         g_signal_handlers_disconnect_by_func (information->contact,
237                                               contact_widget_presence_notify_cb,
238                                               information);
239         g_signal_handlers_disconnect_by_func (information->contact,
240                                               contact_widget_avatar_notify_cb,
241                                               information);
242         g_signal_handlers_disconnect_by_func (information->contact,
243                                               contact_widget_groups_notify_cb,
244                                               information);
245
246         g_object_unref (information->contact);
247         g_slice_free (EmpathyContactWidget, information);
248 }
249
250 static void
251 contact_widget_contact_setup (EmpathyContactWidget *information)
252 {
253         g_signal_connect_swapped (information->contact, "notify::name",
254                                   G_CALLBACK (contact_widget_name_notify_cb),
255                                   information);
256         g_signal_connect_swapped (information->contact, "notify::presence",
257                                   G_CALLBACK (contact_widget_presence_notify_cb),
258                                   information);
259         g_signal_connect_swapped (information->contact, "notify::avatar",
260                                   G_CALLBACK (contact_widget_avatar_notify_cb),
261                                   information);
262
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,
267                           FALSE, FALSE,
268                           6);
269
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),
275                                   information);
276         } else {
277                 information->widget_alias = gtk_label_new (NULL);
278                 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
279         }
280         gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
281                                    information->widget_alias,
282                                    1, 2, 1, 2);
283         gtk_widget_show (information->widget_alias);
284
285         /* Setup id label */
286         gtk_label_set_text (GTK_LABEL (information->label_id),
287                             gossip_contact_get_id (information->contact));
288
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);
293 }
294
295 static void
296 contact_widget_name_notify_cb (EmpathyContactWidget *information)
297 {
298         if (information->editable) {
299                 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
300                                     gossip_contact_get_name (information->contact));
301         } else {
302                 gtk_label_set_label (GTK_LABEL (information->widget_alias),
303                                      gossip_contact_get_name (information->contact));
304         }
305 }
306
307 static void
308 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
309 {
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);
315
316 }
317
318 static void
319 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
320 {
321         GdkPixbuf *avatar_pixbuf;
322
323         avatar_pixbuf = gossip_pixbuf_avatar_from_contact_scaled (information->contact,
324                                                                   48, 48);
325
326         if (avatar_pixbuf) {
327                 gtk_image_set_from_pixbuf (GTK_IMAGE (information->widget_avatar),
328                                            avatar_pixbuf);
329                 gtk_widget_show  (information->widget_avatar);
330                 g_object_unref (avatar_pixbuf);
331         } else {
332                 gtk_widget_hide  (information->widget_avatar);
333         }
334 }
335
336 static void
337 contact_widget_groups_setup (EmpathyContactWidget *information)
338 {
339         if (information->editable) {
340                 contact_widget_model_setup (information);
341
342                 g_signal_connect_swapped (information->contact, "notify::groups",
343                                           G_CALLBACK (contact_widget_groups_notify_cb),
344                                           information);
345                 contact_widget_groups_populate_data (information);
346
347                 gtk_widget_show (information->vbox_groups);
348         }
349 }
350
351 static void
352 contact_widget_model_setup (EmpathyContactWidget *information)
353 {
354         GtkTreeView      *view;
355         GtkListStore     *store;
356         GtkTreeSelection *selection;
357
358         view = GTK_TREE_VIEW (information->treeview_groups);
359
360         store = gtk_list_store_new (COL_COUNT,
361                                     G_TYPE_STRING,   /* name */
362                                     G_TYPE_BOOLEAN,  /* enabled */
363                                     G_TYPE_BOOLEAN); /* editable */
364
365         gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
366
367         selection = gtk_tree_view_get_selection (view);
368         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
369
370         contact_widget_model_populate_columns (information);
371
372         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
373                                               COL_NAME, GTK_SORT_ASCENDING);
374
375         g_object_unref (store);
376 }
377
378 static void
379 contact_widget_model_populate_columns (EmpathyContactWidget *information)
380 {
381         GtkTreeView       *view;
382         GtkTreeModel      *model;
383         GtkTreeViewColumn *column;
384         GtkCellRenderer   *renderer;
385         guint              col_offset;
386
387         view = GTK_TREE_VIEW (information->treeview_groups);
388         model = gtk_tree_view_get_model (view);
389
390         renderer = gtk_cell_renderer_toggle_new ();
391         g_signal_connect (renderer, "toggled",
392                           G_CALLBACK (contact_widget_cell_toggled),
393                           information);
394
395         column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
396                                                            "active", COL_ENABLED,
397                                                            NULL);
398
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);
402
403         renderer = gtk_cell_renderer_text_new ();
404         col_offset = gtk_tree_view_insert_column_with_attributes (view,
405                                                                   -1, _("Group"),
406                                                                   renderer,
407                                                                   "text", COL_NAME,
408                                                                   /* "editable", COL_EDITABLE, */
409                                                                   NULL);
410
411         g_object_set_data (G_OBJECT (renderer),
412                            "column", GINT_TO_POINTER (COL_NAME));
413
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);
418
419         if (information->renderer) {
420                 g_object_unref (information->renderer);
421         }
422
423         information->renderer = g_object_ref (renderer);
424 }
425
426 static void
427 contact_widget_groups_populate_data (EmpathyContactWidget *information)
428 {
429         EmpathyContactManager *manager;
430         GtkTreeView           *view;
431         GtkListStore          *store;
432         GtkTreeIter            iter;
433         GList                 *groups, *l;
434         GList                 *my_groups = NULL;
435         GList                 *all_groups;
436
437         view = GTK_TREE_VIEW (information->treeview_groups);
438         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
439
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);
444
445         for (l = groups; l; l = l->next) {
446                 const gchar *group_str;
447
448                 group_str = l->data;
449                 if (strcmp (group_str, _("Unsorted")) == 0) {
450                         continue;
451                 }
452
453                 my_groups = g_list_append (my_groups, g_strdup (group_str));
454         }
455
456         for (l = all_groups; l; l = l->next) {
457                 const gchar *group_str;
458                 gboolean     enabled;
459
460                 group_str = l->data;
461                 if (strcmp (group_str, _("Unsorted")) == 0) {
462                         continue;
463                 }
464
465                 enabled = g_list_find_custom (my_groups,
466                                               group_str,
467                                               (GCompareFunc) strcmp) != NULL;
468
469                 gtk_list_store_append (store, &iter);
470                 gtk_list_store_set (store, &iter,
471                                     COL_NAME, group_str,
472                                     COL_EDITABLE, TRUE,
473                                     COL_ENABLED, enabled,
474                                     -1);
475         }
476
477         g_list_foreach (my_groups, (GFunc) g_free, NULL);
478         g_list_free (my_groups);
479
480         g_list_free (all_groups);
481 }
482
483 static void
484 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
485 {
486         /* FIXME: not implemented */
487 }
488
489 static gboolean
490 contact_widget_model_find_name (EmpathyContactWidget *information,
491                                 const gchar          *name,
492                                 GtkTreeIter          *iter)
493 {
494         GtkTreeView  *view;
495         GtkTreeModel *model;
496         FindName      data;
497
498         if (G_STR_EMPTY (name)) {
499                 return FALSE;
500         }
501
502         data.information = information;
503         data.name = name;
504         data.found = FALSE;
505
506         view = GTK_TREE_VIEW (information->treeview_groups);
507         model = gtk_tree_view_get_model (view);
508
509         gtk_tree_model_foreach (model,
510                                 (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
511                                 &data);
512
513         if (data.found == TRUE) {
514                 *iter = data.found_iter;
515                 return TRUE;
516         }
517
518         return FALSE;
519 }
520
521 static gboolean
522 contact_widget_model_find_name_foreach (GtkTreeModel *model,
523                                         GtkTreePath  *path,
524                                         GtkTreeIter  *iter,
525                                         FindName     *data)
526 {
527         gchar *name;
528
529         gtk_tree_model_get (model, iter,
530                             COL_NAME, &name,
531                             -1);
532
533         if (!name) {
534                 return FALSE;
535         }
536
537         if (data->name && strcmp (data->name, name) == 0) {
538                 data->found = TRUE;
539                 data->found_iter = *iter;
540
541                 g_free (name);
542
543                 return TRUE;
544         }
545
546         g_free (name);
547
548         return FALSE;
549 }
550
551 static GList *
552 contact_widget_model_find_selected (EmpathyContactWidget *information)
553 {
554         GtkTreeView  *view;
555         GtkTreeModel *model;
556         FindSelected  data;
557
558         data.information = information;
559         data.list = NULL;
560
561         view = GTK_TREE_VIEW (information->treeview_groups);
562         model = gtk_tree_view_get_model (view);
563
564         gtk_tree_model_foreach (model,
565                                 (GtkTreeModelForeachFunc) contact_widget_model_find_selected_foreach,
566                                 &data);
567
568         return data.list;
569 }
570
571 static gboolean
572 contact_widget_model_find_selected_foreach (GtkTreeModel *model,
573                                             GtkTreePath  *path,
574                                             GtkTreeIter  *iter,
575                                             FindSelected *data)
576 {
577         gchar    *name;
578         gboolean  selected;
579
580         gtk_tree_model_get (model, iter,
581                             COL_NAME, &name,
582                             COL_ENABLED, &selected,
583                             -1);
584
585         if (!name) {
586                 return FALSE;
587         }
588
589         if (selected) {
590                 data->list = g_list_append (data->list, name);
591                 return FALSE;
592         }
593
594         g_free (name);
595
596         return FALSE;
597 }
598
599 static void
600 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
601                              gchar                 *path_string,
602                              EmpathyContactWidget  *information)
603 {
604         GtkTreeView  *view;
605         GtkTreeModel *model;
606         GtkListStore *store;
607         GtkTreePath  *path;
608         GtkTreeIter   iter;
609         gboolean      enabled;
610
611         view = GTK_TREE_VIEW (information->treeview_groups);
612         model = gtk_tree_view_get_model (view);
613         store = GTK_LIST_STORE (model);
614
615         path = gtk_tree_path_new_from_string (path_string);
616
617         gtk_tree_model_get_iter (model, &iter, path);
618         gtk_tree_model_get (model, &iter, COL_ENABLED, &enabled, -1);
619
620         enabled ^= 1;
621
622         gtk_list_store_set (store, &iter, COL_ENABLED, enabled, -1);
623         gtk_tree_path_free (path);
624
625         information->changes_made = TRUE;
626 }
627
628 static void
629 contact_widget_entry_alias_changed_cb (GtkEditable           *editable,
630                                        EmpathyContactWidget  *information)
631 {
632         information->changes_made = TRUE;
633 }
634
635 static void
636 contact_widget_entry_group_changed_cb (GtkEditable           *editable,
637                                        EmpathyContactWidget  *information)
638 {
639         GtkTreeIter  iter;
640         const gchar *group;
641
642         group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
643
644         if (contact_widget_model_find_name (information, group, &iter)) {
645                 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
646
647         } else {
648                 gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
649                                           !G_STR_EMPTY (group));
650         }
651 }
652
653 static void
654 contact_widget_entry_group_activate_cb (GtkEntry              *entry,
655                                         EmpathyContactWidget  *information)
656 {
657         gtk_widget_activate (GTK_WIDGET (information->button_group));
658 }
659
660 static void
661 contact_widget_button_group_clicked_cb (GtkButton             *button,
662                                         EmpathyContactWidget  *information)
663 {
664         GtkTreeView  *view;
665         GtkListStore *store;
666         GtkTreeIter   iter;
667         const gchar  *group;
668
669         view = GTK_TREE_VIEW (information->treeview_groups);
670         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
671
672         group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
673
674         gtk_list_store_append (store, &iter);
675         gtk_list_store_set (store, &iter,
676                             COL_NAME, group,
677                             COL_ENABLED, TRUE,
678                             -1);
679
680         information->changes_made = TRUE;
681 }
682
683 static void
684 contact_widget_details_setup (EmpathyContactWidget *information)
685 {
686         /* FIXME: Needs new telepathy spec */
687 }
688
689 static void
690 contact_widget_client_setup (EmpathyContactWidget *information)
691 {
692         /* FIXME: Needs new telepathy spec */
693 }
694