1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU Lesser General Public
7 * License as published by the Free Software Foundation.
9 * This program 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 * General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include <glib/gi18n-lib.h>
22 #include "libedataserver/e-categories.h"
23 #include "e-categories-selector.h"
24 #include "e-data-server-ui-marshal.h"
28 e_categories_selector,
31 struct _ECategoriesSelectorPrivate {
33 GHashTable *selected_categories;
35 gboolean ignore_category_changes;
56 static gint signals[LAST_SIGNAL] = {0};
59 categories_selector_build_model (ECategoriesSelector *selector)
64 store = gtk_list_store_new (
65 N_COLUMNS, G_TYPE_BOOLEAN, GDK_TYPE_PIXBUF, G_TYPE_STRING);
67 gtk_tree_sortable_set_sort_column_id (
68 GTK_TREE_SORTABLE (store),
69 COLUMN_CATEGORY, GTK_SORT_ASCENDING);
71 list = e_categories_get_list ();
72 for (iter = list; iter != NULL; iter = iter->next) {
73 const gchar *category_name = iter->data;
74 const gchar *filename;
75 GdkPixbuf *pixbuf = NULL;
79 /* Only add user-visible categories. */
80 if (!e_categories_is_searchable (category_name))
83 active = (g_hash_table_lookup (
84 selector->priv->selected_categories,
85 category_name) != NULL);
87 filename = e_categories_get_icon_file_for (category_name);
89 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
91 gtk_list_store_append (store, &iter);
95 COLUMN_ACTIVE, active,
97 COLUMN_CATEGORY, category_name,
101 g_object_unref (pixbuf);
104 gtk_tree_view_set_model (
105 GTK_TREE_VIEW (selector), GTK_TREE_MODEL (store));
107 /* This has to be reset everytime we install a new model */
108 gtk_tree_view_set_search_column (
109 GTK_TREE_VIEW (selector), COLUMN_CATEGORY);
112 g_object_unref (store);
116 category_toggled_cb (GtkCellRenderer *renderer,
118 ECategoriesSelector *selector)
121 GtkTreePath *tree_path;
124 model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector));
125 g_return_if_fail (model);
127 tree_path = gtk_tree_path_new_from_string (path);
128 g_return_if_fail (tree_path);
130 if (gtk_tree_model_get_iter (model, &iter, tree_path)) {
134 gtk_tree_model_get (model, &iter,
135 COLUMN_ACTIVE, &active,
136 COLUMN_CATEGORY, &category, -1);
139 GTK_LIST_STORE (model), &iter,
140 COLUMN_ACTIVE, !active, -1);
143 g_hash_table_remove (
144 selector->priv->selected_categories, category);
146 g_hash_table_insert (
147 selector->priv->selected_categories,
148 g_strdup (category), g_strdup (category));
151 selector, signals[CATEGORY_CHECKED], 0,
157 gtk_tree_path_free (tree_path);
161 categories_selector_listener_cb (gpointer useless_pointer,
162 ECategoriesSelector *selector)
164 if (!selector->priv->ignore_category_changes)
165 categories_selector_build_model (selector);
169 categories_selector_key_press_event (ECategoriesSelector *selector,
172 if (event->keyval == GDK_KEY_Delete) {
173 e_categories_selector_delete_selection (selector);
181 categories_selector_selection_changed (GtkTreeSelection *selection,
182 ECategoriesSelector *selector)
184 g_signal_emit (selector, signals[SELECTION_CHANGED], 0, selection);
188 categories_selector_get_property (GObject *object,
193 switch (property_id) {
194 case PROP_ITEMS_CHECKABLE:
195 g_value_set_boolean (
197 e_categories_selector_get_items_checkable (
198 E_CATEGORIES_SELECTOR (object)));
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
206 categories_selector_set_property (GObject *object,
211 switch (property_id) {
212 case PROP_ITEMS_CHECKABLE:
213 e_categories_selector_set_items_checkable (
214 E_CATEGORIES_SELECTOR (object),
215 g_value_get_boolean (value));
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
223 categories_selector_dispose (GObject *object)
225 ECategoriesSelectorPrivate *priv;
227 priv = E_CATEGORIES_SELECTOR (object)->priv;
229 if (priv->selected_categories != NULL) {
230 g_hash_table_destroy (priv->selected_categories);
231 priv->selected_categories = NULL;
234 /* Chain up to parent's dispose() method.*/
235 G_OBJECT_CLASS (e_categories_selector_parent_class)->dispose (object);
239 categories_selector_finalize (GObject *object)
241 e_categories_unregister_change_listener (
242 G_CALLBACK (categories_selector_listener_cb), object);
246 e_categories_selector_class_init (ECategoriesSelectorClass *class)
248 GObjectClass *object_class;
250 g_type_class_add_private (class, sizeof (ECategoriesSelectorPrivate));
252 object_class = G_OBJECT_CLASS (class);
253 object_class->set_property = categories_selector_set_property;
254 object_class->get_property = categories_selector_get_property;
255 object_class->dispose = categories_selector_dispose;
256 object_class->finalize = categories_selector_finalize;
258 g_object_class_install_property (
260 PROP_ITEMS_CHECKABLE,
261 g_param_spec_boolean (
268 signals[CATEGORY_CHECKED] = g_signal_new (
270 G_TYPE_FROM_CLASS (class),
272 G_STRUCT_OFFSET (ECategoriesSelectorClass, category_checked),
274 e_data_server_ui_marshal_VOID__STRING_BOOLEAN,
279 signals[SELECTION_CHANGED] = g_signal_new (
281 G_TYPE_FROM_CLASS (class),
283 G_STRUCT_OFFSET (ECategoriesSelectorClass, selection_changed),
285 g_cclosure_marshal_VOID__OBJECT,
287 GTK_TYPE_TREE_SELECTION);
291 e_categories_selector_init (ECategoriesSelector *selector)
293 GtkCellRenderer *renderer;
294 GtkTreeViewColumn *column;
295 GtkTreeSelection *selection;
297 selector->priv = G_TYPE_INSTANCE_GET_PRIVATE (
298 selector, E_TYPE_CATEGORIES_SELECTOR,
299 ECategoriesSelectorPrivate);
301 selector->priv->checkable = TRUE;
302 selector->priv->selected_categories = g_hash_table_new_full (
303 (GHashFunc) g_str_hash,
304 (GEqualFunc) g_str_equal,
305 (GDestroyNotify) g_free,
306 (GDestroyNotify) g_free);
307 selector->priv->ignore_category_changes = FALSE;
309 renderer = gtk_cell_renderer_toggle_new ();
310 column = gtk_tree_view_column_new_with_attributes (
311 "?", renderer, "active", COLUMN_ACTIVE, NULL);
312 gtk_tree_view_append_column (GTK_TREE_VIEW (selector), column);
316 G_CALLBACK (category_toggled_cb), selector);
318 renderer = gtk_cell_renderer_pixbuf_new ();
319 column = gtk_tree_view_column_new_with_attributes (
320 _("Icon"), renderer, "pixbuf", COLUMN_ICON, NULL);
321 gtk_tree_view_append_column (GTK_TREE_VIEW (selector), column);
323 renderer = gtk_cell_renderer_text_new ();
324 column = gtk_tree_view_column_new_with_attributes (
325 _("Category"), renderer, "text", COLUMN_CATEGORY, NULL);
326 gtk_tree_view_append_column (GTK_TREE_VIEW (selector), column);
328 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (selector));
330 selection, "changed",
331 G_CALLBACK (categories_selector_selection_changed), selector);
334 selector, "key-press-event",
335 G_CALLBACK (categories_selector_key_press_event), NULL);
337 e_categories_register_change_listener (
338 G_CALLBACK (categories_selector_listener_cb), selector);
340 categories_selector_build_model (selector);
344 * e_categories_selector_new:
349 e_categories_selector_new (void)
351 return g_object_new (
352 E_TYPE_CATEGORIES_SELECTOR,
353 "items-checkable", TRUE, NULL);
357 * e_categories_selector_get_items_checkable:
362 e_categories_selector_get_items_checkable (ECategoriesSelector *selector)
364 g_return_val_if_fail (E_IS_CATEGORIES_SELECTOR (selector), TRUE);
366 return selector->priv->checkable;
370 * e_categories_selector_set_items_checkable:
375 e_categories_selector_set_items_checkable (ECategoriesSelector *selector,
378 GtkTreeViewColumn *column;
380 g_return_if_fail (E_IS_CATEGORIES_SELECTOR (selector));
382 selector->priv->checkable = checkable;
384 column = gtk_tree_view_get_column (
385 GTK_TREE_VIEW (selector), COLUMN_ACTIVE);
386 gtk_tree_view_column_set_visible (column, checkable);
388 g_object_notify (G_OBJECT (selector), "items-checkable");
392 * e_categories_selector_get_checked:
394 * Free returned pointer with g_free().
399 e_categories_selector_get_checked (ECategoriesSelector *selector)
402 GList *list, *category;
404 g_return_val_if_fail (E_IS_CATEGORIES_SELECTOR (selector), NULL);
406 str = g_string_new ("");
407 list = g_hash_table_get_values (selector->priv->selected_categories);
409 /* to get them always in the same order */
410 list = g_list_sort (list, (GCompareFunc) g_utf8_collate);
412 for (category = list; category != NULL; category = category->next) {
414 g_string_append_printf (
415 str, ",%s", (gchar *) category->data);
417 g_string_append (str, (gchar *) category->data);
422 return g_string_free (str, FALSE);
426 * e_categories_selector_set_checked:
431 e_categories_selector_set_checked (ECategoriesSelector *selector,
432 const gchar *categories)
439 g_return_if_fail (E_IS_CATEGORIES_SELECTOR (selector));
441 /* Clean up table of selected categories. */
442 g_hash_table_remove_all (selector->priv->selected_categories);
444 arr = g_strsplit (categories, ",", 0);
446 for (i = 0; arr[i] != NULL; i++) {
448 g_hash_table_insert (
449 selector->priv->selected_categories,
450 g_strdup (arr[i]), g_strdup (arr[i]));
455 model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector));
456 if (gtk_tree_model_get_iter_first (model, &iter)) {
458 gchar *category_name;
461 gtk_tree_model_get (model, &iter,
462 COLUMN_CATEGORY, &category_name,
464 found = (g_hash_table_lookup (
465 selector->priv->selected_categories,
466 category_name) != NULL);
468 GTK_LIST_STORE (model), &iter,
469 COLUMN_ACTIVE, found, -1);
471 g_free (category_name);
472 } while (gtk_tree_model_iter_next (model, &iter));
477 * e_categories_selector_delete_selection:
482 e_categories_selector_delete_selection (ECategoriesSelector *selector)
485 GtkTreeSelection *selection;
486 GList *selected, *item;
488 g_return_if_fail (E_IS_CATEGORIES_SELECTOR (selector));
490 model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector));
491 g_return_if_fail (model != NULL);
493 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (selector));
494 selected = gtk_tree_selection_get_selected_rows (selection, &model);
496 /* Remove categories in reverse order to avoid invalidating
497 * tree paths as we iterate over the list. Note, the list is
498 * probably already sorted but we sort again just to be safe. */
499 selected = g_list_reverse (g_list_sort (
500 selected, (GCompareFunc) gtk_tree_path_compare));
502 /* Prevent the model from being rebuilt every time we
503 * remove a category, since we're already modifying it. */
504 selector->priv->ignore_category_changes = TRUE;
506 for (item = selected; item != NULL; item = item->next) {
507 GtkTreePath *path = item->data;
511 gtk_tree_model_get_iter (model, &iter, path);
512 gtk_tree_model_get (model, &iter,
513 COLUMN_CATEGORY, &category, -1);
514 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
515 e_categories_remove (category);
519 selector->priv->ignore_category_changes = FALSE;
521 /* If we only remove one category, try to select another */
522 if (g_list_length (selected) == 1) {
523 GtkTreePath *path = selected->data;
525 gtk_tree_selection_select_path (selection, path);
526 if (!gtk_tree_selection_path_is_selected (selection, path))
527 if (gtk_tree_path_prev (path))
528 gtk_tree_selection_select_path (selection, path);
531 g_list_foreach (selected, (GFunc) gtk_tree_path_free, NULL);
532 g_list_free (selected);
536 * e_categories_selector_get_selected:
538 * Free returned pointer with g_free().
543 e_categories_selector_get_selected (ECategoriesSelector *selector)
546 GtkTreeSelection *selection;
547 GList *selected, *item;
548 GString *str = g_string_new ("");
550 g_return_val_if_fail (E_IS_CATEGORIES_SELECTOR (selector), NULL);
552 model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector));
553 g_return_val_if_fail (model != NULL, NULL);
555 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (selector));
556 selected = gtk_tree_selection_get_selected_rows (selection, &model);
558 for (item = selected; item != NULL; item = item->next) {
559 GtkTreePath *path = item->data;
563 gtk_tree_model_get_iter (model, &iter, path);
564 gtk_tree_model_get (model, &iter,
565 COLUMN_CATEGORY, &category, -1);
567 g_string_assign (str, category);
569 g_string_append_printf (str, ",%s", category);
574 g_list_foreach (selected, (GFunc) gtk_tree_path_free, NULL);
575 g_list_free (selected);
577 return g_string_free (str, FALSE);