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