]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-groups-widget.c
Merge remote-tracking branch 'jonny/ft'
[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-connection-aggregator.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 #FolksGroupDetails
44  * @include: libempathy-gtk/empathy-groups-widget.h
45  *
46  * #EmpathyGroupsWidget is a widget which lists the groups of a
47  * #FolksGroupDetails (i.e. a #FolksPersona or a #FolksIndividual) and allows
48  * them to be added and removed.
49  */
50
51 /**
52  * EmpathyGroupsWidget:
53  * @parent: parent object
54  *
55  * Widget which displays and allows editing of the groups of a
56  * #FolksGroupDetails (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   FolksGroupDetails *group_details; /* 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_GROUP_DETAILS = 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   EmpathyConnectionAggregator *aggregator;
154   GtkTreeIter iter;
155   GeeSet *member_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
162    * EmpathyConnectionAggregator, as libfolks hasn't grown API to get the whole
163    * group list yet. (bgo#627398) */
164   aggregator = empathy_connection_aggregator_dup_singleton ();
165   all_groups = empathy_connection_aggregator_get_all_groups (aggregator);
166   g_object_unref (aggregator);
167
168   /* Get the list of groups that this #FolksGroupDetails is currently in */
169   member_groups = folks_group_details_get_groups (priv->group_details);
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 = gee_collection_contains (GEE_COLLECTION (member_groups),
177           group_str);
178
179       gtk_list_store_append (priv->group_store, &iter);
180       gtk_list_store_set (priv->group_store, &iter,
181           COL_NAME, group_str,
182           COL_EDITABLE, TRUE,
183           COL_ENABLED, enabled,
184           -1);
185
186       g_free (l->data);
187     }
188
189   g_list_free (all_groups);
190 }
191
192 static void
193 add_group_entry_changed_cb (GtkEditable *editable,
194     EmpathyGroupsWidget *self)
195 {
196   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
197   GtkTreeIter iter;
198   const gchar *group;
199
200   group = gtk_entry_get_text (GTK_ENTRY (priv->add_group_entry));
201
202   if (model_find_name (self, group, &iter))
203     {
204       gtk_widget_set_sensitive (GTK_WIDGET (priv->add_group_button), FALSE);
205     }
206   else
207     {
208       gtk_widget_set_sensitive (GTK_WIDGET (priv->add_group_button),
209           !EMP_STR_EMPTY (group));
210     }
211 }
212
213 static void
214 add_group_entry_activate_cb (GtkEntry *entry,
215     EmpathyGroupsWidget  *self)
216 {
217   gtk_widget_activate (GTK_WIDGET (GET_PRIV (self)->add_group_button));
218 }
219
220 static void
221 change_group_cb (FolksGroupDetails *group_details,
222     GAsyncResult *async_result,
223     EmpathyGroupsWidget *self)
224 {
225   GError *error = NULL;
226
227   folks_group_details_change_group_finish (group_details, async_result, &error);
228
229   if (error != NULL)
230     {
231       g_warning ("Failed to change group: %s", error->message);
232       g_clear_error (&error);
233     }
234 }
235
236 static void
237 add_group_button_clicked_cb (GtkButton *button,
238    EmpathyGroupsWidget *self)
239 {
240   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
241   GtkTreeIter iter;
242   const gchar *group;
243
244   group = gtk_entry_get_text (GTK_ENTRY (priv->add_group_entry));
245
246   gtk_list_store_append (priv->group_store, &iter);
247   gtk_list_store_set (priv->group_store, &iter,
248       COL_NAME, group,
249       COL_ENABLED, TRUE,
250       -1);
251
252   folks_group_details_change_group (priv->group_details, group, TRUE,
253       (GAsyncReadyCallback) change_group_cb, self);
254 }
255
256 static void
257 cell_toggled_cb (GtkCellRendererToggle *cell,
258     const gchar *path_string,
259     EmpathyGroupsWidget *self)
260 {
261   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
262   GtkTreePath *path;
263   GtkTreeIter iter;
264   gboolean was_enabled;
265   gchar *group;
266
267   path = gtk_tree_path_new_from_string (path_string);
268
269   gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->group_store), &iter,
270       path);
271   gtk_tree_model_get (GTK_TREE_MODEL (priv->group_store), &iter,
272       COL_ENABLED, &was_enabled,
273       COL_NAME, &group,
274       -1);
275
276   gtk_list_store_set (priv->group_store, &iter,
277       COL_ENABLED, !was_enabled,
278       -1);
279
280   gtk_tree_path_free (path);
281
282   if (group != NULL)
283     {
284       folks_group_details_change_group (priv->group_details, group,
285           !was_enabled, (GAsyncReadyCallback) change_group_cb, self);
286       g_free (group);
287     }
288 }
289
290
291 static void
292 group_details_group_changed_cb (FolksGroupDetails *groups,
293     const gchar *group,
294     gboolean is_member,
295     EmpathyGroupsWidget *self)
296 {
297   EmpathyGroupsWidgetPriv *priv = GET_PRIV (self);
298   GtkTreeIter iter;
299
300   if (model_find_name (self, group, &iter) == TRUE)
301     {
302       gtk_list_store_set (priv->group_store, &iter,
303           COL_ENABLED, is_member,
304           -1);
305     }
306 }
307
308 static void
309 set_up (EmpathyGroupsWidget *self)
310 {
311   EmpathyGroupsWidgetPriv *priv;
312   GtkWidget *label, *alignment;
313   GtkBox *vbox, *hbox;
314   GtkTreeView *tree_view;
315   GtkTreeSelection *selection;
316   GtkTreeViewColumn *column;
317   GtkCellRenderer *renderer;
318   guint col_offset;
319   GtkScrolledWindow *scrolled_window;
320   gchar *markup;
321
322   priv = GET_PRIV (self);
323
324   /* Set up ourself */
325   gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
326       GTK_ORIENTATION_VERTICAL);
327   gtk_box_set_spacing (GTK_BOX (self), 6);
328
329   /* Create our child widgets */
330   label = gtk_label_new (NULL);
331   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
332
333   markup = g_strdup_printf ("<b>%s</b>", _("Groups"));
334   gtk_label_set_markup (GTK_LABEL (label), markup);
335   g_free (markup);
336
337   gtk_box_pack_start (GTK_BOX (self), label, FALSE, FALSE, 0);
338   gtk_widget_show (label);
339
340   alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
341   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
342
343   vbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 6));
344
345   label = gtk_label_new (_("Select the groups you want this contact to appear "
346       "in.  Note that you can select more than one group or no groups."));
347   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
348   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
349
350   gtk_box_pack_start (vbox, label, FALSE, FALSE, 0);
351   gtk_widget_show (label);
352
353   hbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12));
354
355   priv->add_group_entry = gtk_entry_new ();
356   g_signal_connect (priv->add_group_entry, "changed",
357       (GCallback) add_group_entry_changed_cb, self);
358   g_signal_connect (priv->add_group_entry, "activate",
359       (GCallback) add_group_entry_activate_cb, self);
360
361   gtk_box_pack_start (hbox, priv->add_group_entry, TRUE, TRUE, 0);
362   gtk_widget_show (priv->add_group_entry);
363
364   priv->add_group_button = gtk_button_new_with_mnemonic (_("_Add Group"));
365   gtk_widget_set_sensitive (priv->add_group_button, FALSE);
366   gtk_widget_set_receives_default (priv->add_group_button, TRUE);
367   g_signal_connect (priv->add_group_button, "clicked",
368       (GCallback) add_group_button_clicked_cb, self);
369
370   gtk_box_pack_start (hbox, priv->add_group_button, FALSE, FALSE, 0);
371   gtk_widget_show (priv->add_group_button);
372
373   gtk_box_pack_start (vbox, GTK_WIDGET (hbox), FALSE, FALSE, 0);
374   gtk_widget_show (GTK_WIDGET (hbox));
375
376   scrolled_window = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
377   gtk_scrolled_window_set_policy (scrolled_window, GTK_POLICY_NEVER,
378       GTK_POLICY_AUTOMATIC);
379   gtk_scrolled_window_set_shadow_type (scrolled_window, GTK_SHADOW_IN);
380   gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100);
381
382   priv->group_store = gtk_list_store_new (NUM_COLUMNS,
383       G_TYPE_STRING,   /* name */
384       G_TYPE_BOOLEAN,  /* enabled */
385       G_TYPE_BOOLEAN); /* editable */
386
387   tree_view = GTK_TREE_VIEW (gtk_tree_view_new_with_model (
388       GTK_TREE_MODEL (priv->group_store)));
389   gtk_tree_view_set_headers_visible (tree_view, FALSE);
390   gtk_tree_view_set_enable_search (tree_view, FALSE);
391
392   selection = gtk_tree_view_get_selection (tree_view);
393   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
394
395   renderer = gtk_cell_renderer_toggle_new ();
396   g_signal_connect (renderer, "toggled", (GCallback) cell_toggled_cb, self);
397
398   column = gtk_tree_view_column_new_with_attributes (
399       C_("verb in a column header displaying group names", "Select"), renderer,
400       "active", COL_ENABLED,
401       NULL);
402
403   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
404   gtk_tree_view_column_set_fixed_width (column, 50);
405   gtk_tree_view_append_column (tree_view, column);
406
407   renderer = gtk_cell_renderer_text_new ();
408   col_offset = gtk_tree_view_insert_column_with_attributes (tree_view,
409       -1, _("Group"),
410       renderer,
411       "text", COL_NAME,
412       /* "editable", COL_EDITABLE, */
413       NULL);
414
415   column = gtk_tree_view_get_column (tree_view, col_offset - 1);
416   gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
417   gtk_tree_view_column_set_resizable (column, FALSE);
418   gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
419
420   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->group_store),
421       COL_NAME, GTK_SORT_ASCENDING);
422
423   gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (tree_view));
424   gtk_widget_show (GTK_WIDGET (tree_view));
425
426   gtk_box_pack_start (vbox, GTK_WIDGET (scrolled_window), TRUE, TRUE, 0);
427   gtk_widget_show (GTK_WIDGET (scrolled_window));
428
429   gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (vbox));
430   gtk_widget_show (GTK_WIDGET (vbox));
431
432   gtk_box_pack_start (GTK_BOX (self), alignment, TRUE, TRUE, 0);
433   gtk_widget_show (alignment);
434 }
435
436 static void
437 empathy_groups_widget_init (EmpathyGroupsWidget *self)
438 {
439   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
440       EMPATHY_TYPE_GROUPS_WIDGET, EmpathyGroupsWidgetPriv);
441
442   set_up (self);
443 }
444
445 static void
446 get_property (GObject *object,
447     guint param_id,
448     GValue *value,
449     GParamSpec *pspec)
450 {
451   EmpathyGroupsWidgetPriv *priv;
452
453   priv = GET_PRIV (object);
454
455   switch (param_id)
456     {
457       case PROP_GROUP_DETAILS:
458         g_value_set_object (value, priv->group_details);
459         break;
460       default:
461         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
462         break;
463     }
464 }
465
466 static void
467 set_property (GObject *object,
468     guint param_id,
469     const GValue *value,
470     GParamSpec *pspec)
471 {
472   switch (param_id)
473     {
474       case PROP_GROUP_DETAILS:
475         empathy_groups_widget_set_group_details (EMPATHY_GROUPS_WIDGET (object),
476             g_value_get_object (value));
477         break;
478       default:
479         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
480         break;
481     }
482 }
483
484 static void
485 dispose (GObject *object)
486 {
487   EmpathyGroupsWidgetPriv *priv = GET_PRIV (object);
488
489   empathy_groups_widget_set_group_details (EMPATHY_GROUPS_WIDGET (object),
490       NULL);
491   tp_clear_object (&priv->group_store);
492
493   G_OBJECT_CLASS (empathy_groups_widget_parent_class)->dispose (object);
494 }
495
496 static void
497 empathy_groups_widget_class_init (EmpathyGroupsWidgetClass *klass)
498 {
499   GObjectClass *object_class = G_OBJECT_CLASS (klass);
500
501   object_class->get_property = get_property;
502   object_class->set_property = set_property;
503   object_class->dispose = dispose;
504
505   /**
506    * EmpathyGroupsWidget:group_details:
507    *
508    * The #FolksGroupDetails whose group membership is to be edited by the
509    * #EmpathyGroupsWidget.
510    */
511   g_object_class_install_property (object_class, PROP_GROUP_DETAILS,
512       g_param_spec_object ("group-details",
513           "Group Details",
514           "The #FolksGroupDetails whose groups are being edited.",
515           FOLKS_TYPE_GROUP_DETAILS,
516           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
517
518   g_type_class_add_private (object_class, sizeof (EmpathyGroupsWidgetPriv));
519 }
520
521 /**
522  * empathy_groups_widget_new:
523  * @group_details: a #FolksGroupDetails, or %NULL
524  *
525  * Creates a new #EmpathyGroupsWidget to edit the groups of the given
526  * @group_details.
527  *
528  * Return value: a new #EmpathyGroupsWidget
529  */
530 GtkWidget *
531 empathy_groups_widget_new (FolksGroupDetails *group_details)
532 {
533   g_return_val_if_fail (
534       group_details == NULL || FOLKS_IS_GROUP_DETAILS (group_details),
535       NULL);
536
537   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_GROUPS_WIDGET,
538       "group-details", group_details,
539       NULL));
540 }
541
542 /**
543  * empathy_groups_widget_get_group_details:
544  * @self: an #EmpathyGroupsWidget
545  *
546  * Get the #FolksGroupDetails whose group membership is being edited by the
547  * #EmpathyGroupsWidget.
548  *
549  * Returns: the #FolksGroupDetails associated with @widget, or %NULL
550  */
551 FolksGroupDetails *
552 empathy_groups_widget_get_group_details (EmpathyGroupsWidget *self)
553 {
554   g_return_val_if_fail (EMPATHY_IS_GROUPS_WIDGET (self), NULL);
555
556   return GET_PRIV (self)->group_details;
557 }
558
559 /**
560  * empathy_groups_widget_set_group_details:
561  * @self: an #EmpathyGroupsWidget
562  * @group_details: the #FolksGroupDetails whose membership is to be edited, or
563  * %NULL
564  *
565  * Change the #FolksGroupDetails whose group membership is to be edited by the
566  * #EmpathyGroupsWidget.
567  */
568 void
569 empathy_groups_widget_set_group_details (EmpathyGroupsWidget *self,
570     FolksGroupDetails *group_details)
571 {
572   EmpathyGroupsWidgetPriv *priv;
573
574   g_return_if_fail (EMPATHY_IS_GROUPS_WIDGET (self));
575   g_return_if_fail (
576       group_details == NULL || FOLKS_IS_GROUP_DETAILS (group_details));
577
578   priv = GET_PRIV (self);
579
580   if (group_details == priv->group_details)
581     return;
582
583   if (priv->group_details != NULL)
584     {
585       g_signal_handlers_disconnect_by_func (priv->group_details,
586           group_details_group_changed_cb, self);
587     }
588
589   tp_clear_object (&priv->group_details);
590
591   if (group_details != NULL)
592     {
593       priv->group_details = g_object_ref (group_details);
594
595       g_signal_connect (priv->group_details, "group-changed",
596           (GCallback) group_details_group_changed_cb, self);
597
598       populate_data (self);
599     }
600
601   g_object_notify (G_OBJECT (self), "group-details");
602 }