4
* Copyright (C) 2010 Stefan Walter
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 2.1 of
9
* the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24
#include "gcr-collection-model.h"
25
#include "gcr-internal.h"
26
#include "gcr-tree-selector.h"
28
#include <glib/gi18n-lib.h>
33
* SECTION:gcr-tree-selector
34
* @title: GcrTreeSelector
35
* @short_description: A selector widget to select certificates or keys.
37
* The #GcrTreeSelector can be used to select certificates or keys. It allows
38
* the user to select multiple objects from a tree.
43
* @parent: The parent object
45
* A tree selector widget.
49
* GcrTreeSelectorClass:
51
* The class for #GcrTreeSelector.
60
struct _GcrTreeSelectorPrivate {
61
GcrCollection *collection;
62
const GcrColumn *columns;
64
GcrCollectionModel *model;
67
G_DEFINE_TYPE (GcrTreeSelector, gcr_tree_selector, GTK_TYPE_TREE_VIEW);
69
/* -----------------------------------------------------------------------------
74
on_check_column_toggled (GtkCellRendererToggle *cell, gchar *path, GcrCollectionModel *model)
78
g_assert (path != NULL);
80
if (gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (model), &iter, path))
81
gcr_collection_model_toggle_selected (model, &iter);
84
typedef gint (*SortFunc) (GValue *, GValue *);
87
sort_string (GValue *val_a, GValue *val_b)
89
const gchar *str_a = g_value_get_string (val_a);
90
const gchar *str_b = g_value_get_string (val_b);
99
return g_utf8_collate (str_a, str_b);
103
sort_date (GValue *val_a, GValue *val_b)
105
GDate *date_a = g_value_get_boxed (val_a);
106
GDate *date_b = g_value_get_boxed (val_b);
108
if (date_a == date_b)
115
return g_date_compare (date_a, date_b);
118
static inline SortFunc
119
sort_implementation_for_type (GType type)
121
if (type == G_TYPE_STRING)
123
else if (type == G_TYPE_DATE)
130
on_sort_column (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
133
GcrColumn *column = user_data;
141
object_a = gcr_collection_model_object_for_iter (GCR_COLLECTION_MODEL (model), a);
142
g_return_val_if_fail (G_IS_OBJECT (object_a), 0);
143
object_b = gcr_collection_model_object_for_iter (GCR_COLLECTION_MODEL (model), b);
144
g_return_val_if_fail (G_IS_OBJECT (object_b), 0);
146
memset (&val_a, 0, sizeof (val_a));
147
memset (&val_b, 0, sizeof (val_b));
149
g_value_init (&val_a, column->property_type);
150
g_value_init (&val_b, column->property_type);
152
g_object_get_property (object_a, column->property_name, &val_a);
153
g_object_get_property (object_b, column->property_name, &val_b);
155
func = sort_implementation_for_type (column->property_type);
156
g_return_val_if_fail (func, 0);
158
ret = (func) (&val_a, &val_b);
160
g_value_unset (&val_a);
161
g_value_unset (&val_b);
167
add_string_column (GcrTreeSelector *self, const GcrColumn *column, gint column_id)
169
GtkCellRenderer *cell;
170
GtkTreeViewColumn *col;
173
g_assert (column->column_type == G_TYPE_STRING);
174
g_assert (!(column->flags & GCR_COLUMN_HIDDEN));
176
cell = gtk_cell_renderer_text_new ();
177
g_object_set (G_OBJECT (cell), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
178
label = column->label ? g_dpgettext2 (NULL, "column", column->label) : "";
179
col = gtk_tree_view_column_new_with_attributes (label, cell, "text", column_id, NULL);
180
gtk_tree_view_column_set_resizable (col, TRUE);
181
if (column->flags & GCR_COLUMN_SORTABLE)
182
gtk_tree_view_column_set_sort_column_id (col, column_id);
183
gtk_tree_view_append_column (GTK_TREE_VIEW (self), col);
187
add_icon_column (GcrTreeSelector *self, const GcrColumn *column, gint column_id)
189
GtkCellRenderer *cell;
190
GtkTreeViewColumn *col;
193
g_assert (column->column_type == G_TYPE_ICON);
194
g_assert (!(column->flags & GCR_COLUMN_HIDDEN));
196
cell = gtk_cell_renderer_pixbuf_new ();
197
g_object_set (cell, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
198
label = column->label ? g_dpgettext2 (NULL, "column", column->label) : "";
199
col = gtk_tree_view_column_new_with_attributes (label, cell, "gicon", column_id, NULL);
200
gtk_tree_view_column_set_resizable (col, TRUE);
201
if (column->flags & GCR_COLUMN_SORTABLE)
202
gtk_tree_view_column_set_sort_column_id (col, column_id);
203
gtk_tree_view_append_column (GTK_TREE_VIEW (self), col);
207
add_check_column (GcrTreeSelector *self, guint column_id)
209
GtkCellRenderer *cell;
210
GtkTreeViewColumn *col;
212
cell = gtk_cell_renderer_toggle_new ();
213
g_signal_connect (cell, "toggled", G_CALLBACK (on_check_column_toggled), self->pv->model);
215
col = gtk_tree_view_column_new_with_attributes ("", cell, "active", column_id, NULL);
216
gtk_tree_view_column_set_resizable (col, FALSE);
217
gtk_tree_view_append_column (GTK_TREE_VIEW (self), col);
220
/* -----------------------------------------------------------------------------
225
gcr_tree_selector_constructor (GType type, guint n_props, GObjectConstructParam *props)
227
GcrTreeSelector *self = GCR_TREE_SELECTOR (G_OBJECT_CLASS (gcr_tree_selector_parent_class)->constructor(type, n_props, props));
228
const GcrColumn *column;
229
GtkTreeSortable *sortable;
232
g_return_val_if_fail (self, NULL);
233
g_return_val_if_fail (self->pv->columns, NULL);
235
self->pv->model = gcr_collection_model_new_full (self->pv->collection,
238
self->pv->sort = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (self->pv->model));
239
sortable = GTK_TREE_SORTABLE (self->pv->sort);
241
gtk_tree_view_set_model (GTK_TREE_VIEW (self), GTK_TREE_MODEL (self->pv->sort));
243
/* First add the check mark column */
244
add_check_column (self, gcr_collection_model_column_for_selected (self->pv->model));
246
for (column = self->pv->columns, i = 0; column->property_name; ++column, ++i) {
247
if (column->flags & GCR_COLUMN_HIDDEN)
250
if (column->column_type == G_TYPE_STRING)
251
add_string_column (self, column, i);
252
else if (column->column_type == G_TYPE_ICON)
253
add_icon_column (self, column, i);
255
g_warning ("skipping unsupported column '%s' of type: %s",
256
column->property_name, g_type_name (column->column_type));
258
/* Setup the column itself */
259
if (column->flags & GCR_COLUMN_SORTABLE) {
260
if (sort_implementation_for_type (column->property_type))
261
gtk_tree_sortable_set_sort_func (sortable, i, on_sort_column,
262
(gpointer)column, NULL);
264
g_warning ("no sort implementation defined for type '%s' on column '%s'",
265
g_type_name (column->property_type), column->property_name);
269
return G_OBJECT (self);
273
gcr_tree_selector_init (GcrTreeSelector *self)
275
self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_TREE_SELECTOR, GcrTreeSelectorPrivate);
279
gcr_tree_selector_dispose (GObject *obj)
281
GcrTreeSelector *self = GCR_TREE_SELECTOR (obj);
284
g_object_unref (self->pv->model);
285
self->pv->model = NULL;
287
if (self->pv->collection)
288
g_object_unref (self->pv->collection);
289
self->pv->collection = NULL;
292
g_object_unref (self->pv->sort);
293
self->pv->sort = NULL;
295
G_OBJECT_CLASS (gcr_tree_selector_parent_class)->dispose (obj);
299
gcr_tree_selector_finalize (GObject *obj)
301
GcrTreeSelector *self = GCR_TREE_SELECTOR (obj);
303
g_assert (!self->pv->collection);
304
g_assert (!self->pv->model);
306
G_OBJECT_CLASS (gcr_tree_selector_parent_class)->finalize (obj);
310
gcr_tree_selector_set_property (GObject *obj, guint prop_id, const GValue *value,
313
GcrTreeSelector *self = GCR_TREE_SELECTOR (obj);
315
case PROP_COLLECTION:
316
g_return_if_fail (!self->pv->collection);
317
self->pv->collection = g_value_dup_object (value);
318
g_return_if_fail (self->pv->collection);
321
g_return_if_fail (!self->pv->columns);
322
self->pv->columns = g_value_get_pointer (value);
323
g_return_if_fail (self->pv->columns);
326
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
332
gcr_tree_selector_get_property (GObject *obj, guint prop_id, GValue *value,
335
GcrTreeSelector *self = GCR_TREE_SELECTOR (obj);
338
case PROP_COLLECTION:
339
g_value_set_object (value, gcr_tree_selector_get_collection (self));
342
g_value_set_pointer (value, (gpointer)gcr_tree_selector_get_columns (self));
345
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
351
gcr_tree_selector_class_init (GcrTreeSelectorClass *klass)
353
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
355
gobject_class->constructor = gcr_tree_selector_constructor;
356
gobject_class->dispose = gcr_tree_selector_dispose;
357
gobject_class->finalize = gcr_tree_selector_finalize;
358
gobject_class->set_property = gcr_tree_selector_set_property;
359
gobject_class->get_property = gcr_tree_selector_get_property;
361
g_type_class_add_private (gobject_class, sizeof (GcrTreeSelectorPrivate));
364
* GcrTreeSelector:collection:
366
* The collection which contains the objects to display in the selector.
368
g_object_class_install_property (gobject_class, PROP_COLLECTION,
369
g_param_spec_object ("collection", "Collection", "Collection to select from",
370
GCR_TYPE_COLLECTION, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
373
* GcrTreeSelector:columns:
375
* The columns to use to display the objects.
377
g_object_class_install_property (gobject_class, PROP_COLUMNS,
378
g_param_spec_pointer ("columns", "Columns", "Columns to display in selector",
379
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
384
/* -----------------------------------------------------------------------------
389
* gcr_tree_selector_new:
390
* @collection: The collection that contains the objects to display
391
* @columns: The columns to use to display the objects
393
* Create a new #GcrTreeSelector.
395
* Returns: A newly allocated selector, which should be released with
399
gcr_tree_selector_new (GcrCollection *collection, const GcrColumn *columns)
401
return g_object_new (GCR_TYPE_TREE_SELECTOR,
402
"collection", collection,
408
* gcr_tree_selector_get_collection:
409
* @self: The selector
411
* Get the collection that this selector is displaying objects from.
413
* Returns: The collection, owned by the selector.
416
gcr_tree_selector_get_collection (GcrTreeSelector *self)
418
g_return_val_if_fail (GCR_IS_TREE_SELECTOR (self), NULL);
419
return self->pv->collection;
423
* gcr_tree_selector_get_columns:
424
* @self: The selector
426
* Get the columns displayed in a selector in multiple mode.
428
* Returns: The columns, owned by the selector.
431
gcr_tree_selector_get_columns (GcrTreeSelector *self)
433
g_return_val_if_fail (GCR_IS_TREE_SELECTOR (self), NULL);
434
return self->pv->columns;
438
* gcr_tree_selector_get_selected:
439
* @self: The selector
441
* Get a list of selected objects.
443
* Returns: The list of selected objects, to be released with g_list_free().
446
gcr_tree_selector_get_selected (GcrTreeSelector *self)
448
g_return_val_if_fail (GCR_IS_TREE_SELECTOR (self), NULL);
449
return gcr_collection_model_get_selected_objects (self->pv->model);
453
* gcr_tree_selector_set_selected:
454
* @self: The selector
455
* @selected: The list of objects to select.
457
* Select certain objects in the selector.
460
gcr_tree_selector_set_selected (GcrTreeSelector *self, GList *selected)
462
g_return_if_fail (GCR_IS_TREE_SELECTOR (self));
463
gcr_collection_model_set_selected_objects (self->pv->model, selected);