]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-groups-widget.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-groups-widget.c
1 /*
2  * Copyright (C) 2007-2010 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Philip Withnall <philip.withnall@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
29
30 #include <telepathy-glib/util.h>
31
32 #include <folks/folks.h>
33
34 #include <libempathy/empathy-utils.h>
35 #include <libempathy/empathy-contact-manager.h>
36
37 #include "empathy-groups-widget.h"
38 #include "empathy-ui-utils.h"
39
40 /**
41  * SECTION:empathy-groups-widget
42  * @title:EmpathyGroupsWidget
43  * @short_description: A widget used to edit the groups of a #FolksGroupable
44  * @include: libempathy-gtk/empathy-groups-widget.h
45  *
46  * #EmpathyGroupsWidget is a widget which lists the groups of a #FolksGroupable
47  * (i.e. a #FolksPersona or a #FolksIndividual) and allows them to be added and
48  * removed.
49  */
50
51 /**
52  * EmpathyGroupsWidget:
53  * @parent: parent object
54  *
55  * Widget which displays and allows editing of the groups of a #FolksGroupable
56  * (i.e. a #FolksPersona or #FolksIndividual).
57  */
58
59 /* Delay before updating the widget when the id entry changed (seconds) */
60 #define ID_CHANGED_TIMEOUT 1
61
62 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyGroupsWidget)
63
64 typedef struct
65 {
66   /* The object we're actually changing the groups of */
67   FolksGroupable *groupable; /* owned */
68   GtkListStore *group_store; /* owned */
69
70   GtkWidget *add_group_entry; /* child widget */
71   GtkWidget *add_group_button; /* child widget */
72 } EmpathyGroupsWidgetPriv;
73
74 enum {
75   PROP_GROUPABLE = 1,
76 };
77
78 enum {
79   COL_NAME,
80   COL_ENABLED,
81   COL_EDITABLE
82 };
83 #define NUM_COLUMNS COL_EDITABLE + 1
84
85 G_DEFINE_TYPE (EmpathyGroupsWidget, empathy_groups_widget, GTK_TYPE_BOX);
86
87 typedef struct
88 {
89   EmpathyGroupsWidget *widget;
90   const gchar *name;
91   gboolean found;
92   GtkTreeIter found_iter;
93 } FindNameData;
94
95 static gboolean
96 model_find_name_foreach (GtkTreeModel *model,
97     GtkTreePath *path,
98     GtkTreeIter *iter,
99     FindNameData *data)
100 {
101   gchar *name;
102
103   gtk_tree_model_get (model, iter,
104       COL_NAME, &name,
105       -1);
106
107   if (name != NULL && strcmp (data->name, name) == 0)
108     {
109       data->found = TRUE;
110       data->found_iter = *iter;
111
112       g_free (name);
113
114       return TRUE;
115     }
116
117   g_free (name);
118
119   return FALSE;
120 }
121
122 static gboolean
123 model_find_name (EmpathyGroupsWidget *self,
124     const gchar *name,
125     GtkTreeIter *iter)
126 {
127   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
128   FindNameData data;
129
130   if (EMP_STR_EMPTY (name))
131     return FALSE;
132
133   data.widget = self;
134   data.name = name;
135   data.found = FALSE;
136
137   gtk_tree_model_foreach (GTK_TREE_MODEL (priv->group_store),
138       (GtkTreeModelForeachFunc) model_find_name_foreach, &data);
139
140   if (data.found == TRUE)
141     {
142       *iter = data.found_iter;
143       return TRUE;
144     }
145
146   return FALSE;
147 }
148
149 static void
150 populate_data (EmpathyGroupsWidget *self)
151 {
152   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
153   EmpathyContactManager *manager;
154   GtkTreeIter iter;
155   GHashTable *my_groups;
156   GList *all_groups, *l;
157
158   /* Remove the old groups */
159   gtk_list_store_clear (priv->group_store);
160
161   /* FIXME: We have to get the whole group list from EmpathyContactManager, as
162    * libfolks hasn't grown API to get the whole group list yet. (bgo#627398) */
163   manager = empathy_contact_manager_dup_singleton ();
164   all_groups = empathy_contact_list_get_all_groups (
165       EMPATHY_CONTACT_LIST (manager));
166   g_object_unref (manager);
167
168   /* Get the list of groups that this #FolksGroupable is currently in */
169   my_groups = folks_groupable_get_groups (priv->groupable);
170
171   for (l = all_groups; l != NULL; l = l->next)
172     {
173       const gchar *group_str = l->data;
174       gboolean enabled;
175
176       enabled = GPOINTER_TO_UINT (g_hash_table_lookup (my_groups, group_str));
177
178       gtk_list_store_append (priv->group_store, &iter);
179       gtk_list_store_set (priv->group_store, &iter,
180           COL_NAME, group_str,
181           COL_EDITABLE, TRUE,
182           COL_ENABLED, enabled,
183           -1);
184
185       g_free (l->data);
186     }
187
188   g_list_free (all_groups);
189 }
190
191 static void
192 add_group_entry_changed_cb (GtkEditable *editable,
193     EmpathyGroupsWidget *self)
194 {
195   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
196   GtkTreeIter iter;
197   const gchar *group;
198
199   group = gtk_entry_get_text (GTK_ENTRY (priv->add_group_entry));
200
201   if (model_find_name (self, group, &iter))
202     {
203       gtk_widget_set_sensitive (GTK_WIDGET (priv->add_group_button), FALSE);
204     }
205   else
206     {
207       gtk_widget_set_sensitive (GTK_WIDGET (priv->add_group_button),
208           !EMP_STR_EMPTY (group));
209     }
210 }
211
212 static void
213 add_group_entry_activate_cb (GtkEntry *entry,
214     EmpathyGroupsWidget  *self)
215 {
216   gtk_widget_activate (GTK_WIDGET (GET_PRIV (self)->add_group_button));
217 }
218
219 static void
220 change_group_cb (FolksGroupable *groupable,
221     GAsyncResult *async_result,
222     EmpathyGroupsWidget *self)
223 {
224   GError *error = NULL;
225
226   folks_groupable_change_group_finish (groupable, async_result, &error);
227
228   if (error != NULL)
229     {
230       g_warning ("Failed to change group: %s", error->message);
231       g_clear_error (&error);
232     }
233 }
234
235 static void
236 add_group_button_clicked_cb (GtkButton *button,
237    EmpathyGroupsWidget *self)
238 {
239   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
240   GtkTreeIter iter;
241   const gchar *group;
242
243   group = gtk_entry_get_text (GTK_ENTRY (priv->add_group_entry));
244
245   gtk_list_store_append (priv->group_store, &iter);
246   gtk_list_store_set (priv->group_store, &iter,
247       COL_NAME, group,
248       COL_ENABLED, TRUE,
249       -1);
250
251   folks_groupable_change_group (priv->groupable, group, TRUE,
252       (GAsyncReadyCallback) change_group_cb, self);
253 }
254
255 static void
256 cell_toggled_cb (GtkCellRendererToggle *cell,
257     const gchar *path_string,
258     EmpathyGroupsWidget *self)
259 {
260   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
261   GtkTreePath *path;
262   GtkTreeIter iter;
263   gboolean was_enabled;
264   gchar *group;
265
266   path = gtk_tree_path_new_from_string (path_string);
267
268   gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->group_store), &iter,
269       path);
270   gtk_tree_model_get (GTK_TREE_MODEL (priv->group_store), &iter,
271       COL_ENABLED, &was_enabled,
272       COL_NAME, &group,
273       -1);
274
275   gtk_list_store_set (priv->group_store, &iter,
276       COL_ENABLED, !was_enabled,
277       -1);
278
279   gtk_tree_path_free (path);
280
281   if (group != NULL)
282     {
283       folks_groupable_change_group (priv->groupable, group, !was_enabled,
284           (GAsyncReadyCallback) change_group_cb, self);
285       g_free (group);
286     }
287 }
288
289
290 static void
291 groupable_group_changed_cb (FolksGroupable *groups,
292     const gchar *group,
293     gboolean is_member,
294     EmpathyGroupsWidget *self)
295 {
296   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
297   GtkTreeIter iter;
298
299   if (model_find_name (self, group, &iter) == TRUE)
300     {
301       gtk_list_store_set (priv->group_store, &iter,
302           COL_ENABLED, is_member,
303           -1);
304     }
305 }
306
307 static void
308 set_up (EmpathyGroupsWidget *self)
309 {
310   EmpathyGroupsWidgetPriv *priv;
311   GtkWidget *label, *alignment;
312   GtkBox *vbox, *hbox;
313   GtkTreeView *tree_view;
314   GtkTreeSelection *selection;
315   GtkTreeViewColumn *column;
316   GtkCellRenderer *renderer;
317   guint col_offset;
318   GtkScrolledWindow *scrolled_window;
319   gchar *markup;
320
321   priv = GET_PRIV (self);
322
323   /* Set up ourself */
324   gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
325       GTK_ORIENTATION_VERTICAL);
326   gtk_box_set_spacing (GTK_BOX (self), 6);
327
328   /* Create our child widgets */
329   label = gtk_label_new (NULL);
330   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
331
332   markup = g_strdup_printf ("<b>%s</b>", _("Groups"));
333   gtk_label_set_markup (GTK_LABEL (label), markup);
334   g_free (markup);
335
336   gtk_box_pack_start (GTK_BOX (self), label, FALSE, FALSE, 0);
337   gtk_widget_show (label);
338
339   alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
340   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
341
342   vbox = GTK_BOX (gtk_vbox_new (FALSE, 6));
343
344   label = gtk_label_new (_("Select the groups you want this contact to appear "
345       "in.  Note that you can select more than one group or no groups."));
346   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
347   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
348
349   gtk_box_pack_start (vbox, label, FALSE, FALSE, 0);
350   gtk_widget_show (label);
351
352   hbox = GTK_BOX (gtk_hbox_new (FALSE, 12));
353
354   priv->add_group_entry = gtk_entry_new ();
355   g_signal_connect (priv->add_group_entry, "changed",
356       (GCallback) add_group_entry_changed_cb, self);
357   g_signal_connect (priv->add_group_entry, "activate",
358       (GCallback) add_group_entry_activate_cb, self);
359
360   gtk_box_pack_start (hbox, priv->add_group_entry, TRUE, TRUE, 0);
361   gtk_widget_show (priv->add_group_entry);
362
363   priv->add_group_button = gtk_button_new_with_mnemonic (_("_Add Group"));
364   gtk_widget_set_sensitive (priv->add_group_button, FALSE);
365   gtk_widget_set_receives_default (priv->add_group_button, TRUE);
366   g_signal_connect (priv->add_group_button, "clicked",
367       (GCallback) add_group_button_clicked_cb, self);
368
369   gtk_box_pack_start (hbox, priv->add_group_button, FALSE, FALSE, 0);
370   gtk_widget_show (priv->add_group_button);
371
372   gtk_box_pack_start (vbox, GTK_WIDGET (hbox), FALSE, FALSE, 0);
373   gtk_widget_show (GTK_WIDGET (hbox));
374
375   scrolled_window = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
376   gtk_scrolled_window_set_policy (scrolled_window, GTK_POLICY_NEVER,
377       GTK_POLICY_AUTOMATIC);
378   gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
379   gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
380
381   priv->group_store = gtk_list_store_new (NUM_COLUMNS,
382       G_TYPE_STRING,   /* name */
383       G_TYPE_BOOLEAN,  /* enabled */
384       G_TYPE_BOOLEAN); /* editable */
385
386   tree_view = GTK_TREE_VIEW (gtk_tree_view_new_with_model (
387       GTK_TREE_MODEL (priv->group_store)));
388   gtk_tree_view_set_headers_visible (tree_view, FALSE);
389   gtk_tree_view_set_enable_search (tree_view, FALSE);
390
391   selection = gtk_tree_view_get_selection (tree_view);
392   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
393
394   renderer = gtk_cell_renderer_toggle_new ();
395   g_signal_connect (renderer, "toggled", (GCallback) cell_toggled_cb, self);
396
397   column = gtk_tree_view_column_new_with_attributes (
398       C_("verb in a column header displaying group names", "Select"), renderer,
399       "active", COL_ENABLED,
400       NULL);
401
402   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
403   gtk_tree_view_column_set_fixed_width (column, 50);
404   gtk_tree_view_append_column (tree_view, column);
405
406   renderer = gtk_cell_renderer_text_new ();
407   col_offset = gtk_tree_view_insert_column_with_attributes (tree_view,
408       -1, _("Group"),
409       renderer,
410       "text", COL_NAME,
411       /* "editable", COL_EDITABLE, */
412       NULL);
413
414   column = gtk_tree_view_get_column (tree_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   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->group_store),
420       COL_NAME, GTK_SORT_ASCENDING);
421
422   gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (tree_view));
423   gtk_widget_show (GTK_WIDGET (tree_view));
424
425   gtk_box_pack_start (vbox, GTK_WIDGET (scrolled_window), TRUE, TRUE, 0);
426   gtk_widget_show (GTK_WIDGET (scrolled_window));
427
428   gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (vbox));
429   gtk_widget_show (GTK_WIDGET (vbox));
430
431   gtk_box_pack_start (GTK_BOX (self), alignment, TRUE, TRUE, 0);
432   gtk_widget_show (alignment);
433 }
434
435 static void
436 empathy_groups_widget_init (EmpathyGroupsWidget *self)
437 {
438   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
439       EMPATHY_TYPE_GROUPS_WIDGET, EmpathyGroupsWidgetPriv);
440
441   set_up (self);
442 }
443
444 static void
445 get_property (GObject *object,
446     guint param_id,
447     GValue *value,
448     GParamSpec *pspec)
449 {
450   EmpathyGroupsWidgetPriv *priv;
451
452   priv = GET_PRIV (object);
453
454   switch (param_id)
455     {
456       case PROP_GROUPABLE:
457         g_value_set_object (value, priv->groupable);
458         break;
459       default:
460         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
461         break;
462     }
463 }
464
465 static void
466 set_property (GObject *object,
467     guint param_id,
468     const GValue *value,
469     GParamSpec *pspec)
470 {
471   EmpathyGroupsWidgetPriv *priv;
472
473   priv = GET_PRIV (object);
474
475   switch (param_id)
476     {
477       case PROP_GROUPABLE:
478         empathy_groups_widget_set_groupable (EMPATHY_GROUPS_WIDGET (object),
479             g_value_get_object (value));
480         break;
481       default:
482         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
483         break;
484     }
485 }
486
487 static void
488 dispose (GObject *object)
489 {
490   EmpathyGroupsWidgetPriv *priv = GET_PRIV (object);
491
492   empathy_groups_widget_set_groupable (EMPATHY_GROUPS_WIDGET (object), NULL);
493   tp_clear_object (&priv->group_store);
494
495   G_OBJECT_CLASS (empathy_groups_widget_parent_class)->dispose (object);
496 }
497
498 static void
499 empathy_groups_widget_class_init (EmpathyGroupsWidgetClass *klass)
500 {
501   GObjectClass *object_class = G_OBJECT_CLASS (klass);
502
503   object_class->get_property = get_property;
504   object_class->set_property = set_property;
505   object_class->dispose = dispose;
506
507   /**
508    * EmpathyGroupsWidget:groupable:
509    *
510    * The #FolksGroupable whose group membership is to be edited by the
511    * #EmpathyGroupsWidget.
512    */
513   g_object_class_install_property (object_class, PROP_GROUPABLE,
514       g_param_spec_object ("groupable",
515           "Groupable",
516           "The #FolksGroupable whose groups are being edited.",
517           FOLKS_TYPE_GROUPABLE,
518           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
519
520   g_type_class_add_private (object_class, sizeof (EmpathyGroupsWidgetPriv));
521 }
522
523 /**
524  * empathy_groups_widget_new:
525  * @groupable: a #FolksGroupable, or %NULL
526  *
527  * Creates a new #EmpathyGroupsWidget to edit the groups of the given
528  * @groupable.
529  *
530  * Return value: a new #EmpathyGroupsWidget
531  */
532 GtkWidget *
533 empathy_groups_widget_new (FolksGroupable *groupable)
534 {
535   g_return_val_if_fail (groupable == NULL || FOLKS_IS_GROUPABLE (groupable),
536       NULL);
537
538   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_GROUPS_WIDGET,
539       "groupable", groupable,
540       NULL));
541 }
542
543 /**
544  * empathy_groups_widget_get_groupable:
545  * @self: an #EmpathyGroupsWidget
546  *
547  * Get the #FolksGroupable whose group membership is being edited by the
548  * #EmpathyGroupsWidget.
549  *
550  * Returns: the #FolksGroupable associated with @widget, or %NULL
551  */
552 FolksGroupable *
553 empathy_groups_widget_get_groupable (EmpathyGroupsWidget *self)
554 {
555   g_return_val_if_fail (EMPATHY_IS_GROUPS_WIDGET (self), NULL);
556
557   return GET_PRIV (self)->groupable;
558 }
559
560 /**
561  * empathy_groups_widget_set_groupable:
562  * @self: an #EmpathyGroupsWidget
563  * @groupable: the #FolksGroupable whose membership is to be edited, or %NULL
564  *
565  * Change the #FolksGroupable whose group membership is to be edited by the
566  * #EmpathyGroupsWidget.
567  */
568 void
569 empathy_groups_widget_set_groupable (EmpathyGroupsWidget *self,
570     FolksGroupable *groupable)
571 {
572   EmpathyGroupsWidgetPriv *priv;
573
574   g_return_if_fail (EMPATHY_IS_GROUPS_WIDGET (self));
575   g_return_if_fail (groupable == NULL || FOLKS_IS_GROUPABLE (groupable));
576
577   priv = GET_PRIV (self);
578
579   if (groupable == priv->groupable)
580     return;
581
582   if (priv->groupable != NULL)
583     {
584       g_signal_handlers_disconnect_by_func (priv->groupable,
585           groupable_group_changed_cb, self);
586     }
587
588   tp_clear_object (&priv->groupable);
589
590   if (groupable != NULL)
591     {
592       priv->groupable = g_object_ref (groupable);
593
594       g_signal_connect (priv->groupable, "group-changed",
595           (GCallback) groupable_group_changed_cb, self);
596
597       populate_data (self);
598     }
599
600   g_object_notify (G_OBJECT (self), "groupable");
601 }