Update copyright message
[nautilus-actions.git] / src / core / na-data-boxed.c
bloba570a8e6d4d8d3fb83f8ff46d1d8e4ae5e6f19d3
1 /*
2 * Nautilus Actions
3 * A Nautilus extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009, 2010, 2011 Pierre Wieser and others (see AUTHORS)
9 * This Program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This Program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this Library; see the file COPYING. If not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307, USA.
24 * Authors:
25 * Frederic Ruaudel <grumz@grumz.net>
26 * Rodrigo Moya <rodrigo@gnome-db.org>
27 * Pierre Wieser <pwieser@trychlos.org>
28 * ... and many others (see AUTHORS)
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <stdlib.h>
36 #include <string.h>
38 #include <api/na-core-utils.h>
39 #include <api/na-gconf-utils.h>
40 #include <api/na-data-def.h>
41 #include <api/na-data-types.h>
42 #include <api/na-data-boxed.h>
44 /* private class data
46 struct NADataBoxedClassPrivate {
47 void *empty; /* so that gcc -pedantic is happy */
50 /* private instance data
52 struct NADataBoxedPrivate {
53 gboolean dispose_has_run;
54 NADataDef *def ;
55 union {
56 gboolean boolean;
57 gchar *string;
58 GSList *slist;
59 void *pointer;
60 guint uint;
61 } u;
64 typedef struct {
65 guint type;
66 GParamSpec * ( *spec ) ( const NADataDef * );
67 void ( *free ) ( const NADataBoxed * );
68 void ( *dump ) ( const NADataBoxed * );
69 gboolean ( *are_equal ) ( const NADataBoxed *, const NADataBoxed * );
70 gboolean ( *is_default ) ( const NADataBoxed * );
71 gboolean ( *is_valid ) ( const NADataBoxed * );
72 gchar * ( *get_as_string ) ( const NADataBoxed * );
73 void * ( *get_as_void ) ( const NADataBoxed * );
74 void ( *get_as_value ) ( const NADataBoxed *, GValue *value );
75 void ( *set_from_boxed ) ( NADataBoxed *, const NADataBoxed * );
76 void ( *set_from_string )( NADataBoxed *, const gchar *string );
77 void ( *set_from_value ) ( NADataBoxed *, const GValue *value );
78 void ( *set_from_void ) ( NADataBoxed *, const void *value );
80 DataBoxedFn;
82 static GObjectClass *st_parent_class = NULL;
84 static GType register_type( void );
85 static void class_init( NADataBoxedClass *klass );
86 static void instance_init( GTypeInstance *instance, gpointer klass );
87 static void instance_dispose( GObject *object );
88 static void instance_finalize( GObject *object );
90 static DataBoxedFn *get_data_boxed_fn( guint type );
92 static GParamSpec *string_spec( const NADataDef *idtype );
93 static void string_free( const NADataBoxed *boxed );
94 static void string_dump( const NADataBoxed *boxed );
95 static gboolean string_are_equal( const NADataBoxed *a, const NADataBoxed *b );
96 static gboolean string_is_default( const NADataBoxed *boxed );
97 static gboolean string_is_valid( const NADataBoxed *boxed );
98 static gchar *string_get_as_string( const NADataBoxed *boxed );
99 static void *string_get_as_void( const NADataBoxed *boxed );
100 static void string_get_as_value( const NADataBoxed *boxed, GValue *value );
101 static void string_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source );
102 static void string_set_from_string( NADataBoxed *boxed, const gchar *string );
103 static void string_set_from_value( NADataBoxed *boxed, const GValue *value );
104 static void string_set_from_void( NADataBoxed *boxed, const void *value );
106 static gboolean locale_are_equal( const NADataBoxed *a, const NADataBoxed *b );
107 static gboolean locale_is_default( const NADataBoxed *boxed );
108 static gboolean locale_is_valid( const NADataBoxed *boxed );
110 static GParamSpec *slist_spec( const NADataDef *idtype );
111 static void slist_free( const NADataBoxed *boxed );
112 static void slist_dump( const NADataBoxed *boxed );
113 static gboolean slist_are_equal( const NADataBoxed *a, const NADataBoxed *b );
114 static gboolean slist_is_default( const NADataBoxed *boxed );
115 static gboolean slist_is_valid( const NADataBoxed *boxed );
116 static gchar *slist_get_as_string( const NADataBoxed *boxed );
117 static void *slist_get_as_void( const NADataBoxed *boxed );
118 static void slist_get_as_value( const NADataBoxed *boxed, GValue *value );
119 static void slist_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source );
120 static void slist_set_from_string( NADataBoxed *boxed, const gchar *string );
121 static void slist_set_from_value( NADataBoxed *boxed, const GValue *value );
122 static void slist_set_from_void( NADataBoxed *boxed, const void *value );
124 static GParamSpec *bool_spec( const NADataDef *idtype );
125 static void bool_free( const NADataBoxed *boxed );
126 static void bool_dump( const NADataBoxed *boxed );
127 static gboolean bool_are_equal( const NADataBoxed *a, const NADataBoxed *b );
128 static gboolean bool_is_default( const NADataBoxed *boxed );
129 static gboolean bool_is_valid( const NADataBoxed *boxed );
130 static gchar *bool_get_as_string( const NADataBoxed *boxed );
131 static void *bool_get_as_void( const NADataBoxed *boxed );
132 static void bool_get_as_value( const NADataBoxed *boxed, GValue *value );
133 static void bool_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source );
134 static void bool_set_from_string( NADataBoxed *boxed, const gchar *string );
135 static void bool_set_from_value( NADataBoxed *boxed, const GValue *value );
136 static void bool_set_from_void( NADataBoxed *boxed, const void *value );
138 static GParamSpec *pointer_spec( const NADataDef *idtype );
139 static void pointer_free( const NADataBoxed *boxed );
140 static void pointer_dump( const NADataBoxed *boxed );
141 static gboolean pointer_are_equal( const NADataBoxed *a, const NADataBoxed *b );
142 static gboolean pointer_is_default( const NADataBoxed *boxed );
143 static gboolean pointer_is_valid( const NADataBoxed *boxed );
144 static gchar *pointer_get_as_string( const NADataBoxed *boxed );
145 static void *pointer_get_as_void( const NADataBoxed *boxed );
146 static void pointer_get_as_value( const NADataBoxed *boxed, GValue *value );
147 static void pointer_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source );
148 static void pointer_set_from_string( NADataBoxed *boxed, const gchar *string );
149 static void pointer_set_from_value( NADataBoxed *boxed, const GValue *value );
150 static void pointer_set_from_void( NADataBoxed *boxed, const void *value );
152 static GParamSpec *uint_spec( const NADataDef *idtype );
153 static void uint_free( const NADataBoxed *boxed );
154 static void uint_dump( const NADataBoxed *boxed );
155 static gboolean uint_are_equal( const NADataBoxed *a, const NADataBoxed *b );
156 static gboolean uint_is_default( const NADataBoxed *boxed );
157 static gboolean uint_is_valid( const NADataBoxed *boxed );
158 static gchar *uint_get_as_string( const NADataBoxed *boxed );
159 static void *uint_get_as_void( const NADataBoxed *boxed );
160 static void uint_get_as_value( const NADataBoxed *boxed, GValue *value );
161 static void uint_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source );
162 static void uint_set_from_string( NADataBoxed *boxed, const gchar *string );
163 static void uint_set_from_value( NADataBoxed *boxed, const GValue *value );
164 static void uint_set_from_void( NADataBoxed *boxed, const void *value );
166 static DataBoxedFn st_data_boxed_fn[] = {
167 { NAFD_TYPE_STRING,
168 string_spec,
169 string_free,
170 string_dump,
171 string_are_equal,
172 string_is_default,
173 string_is_valid,
174 string_get_as_string,
175 string_get_as_void,
176 string_get_as_value,
177 string_set_from_boxed,
178 string_set_from_string,
179 string_set_from_value,
180 string_set_from_void
182 { NAFD_TYPE_LOCALE_STRING,
183 string_spec,
184 string_free,
185 string_dump,
186 locale_are_equal,
187 locale_is_default,
188 locale_is_valid,
189 string_get_as_string,
190 string_get_as_void,
191 string_get_as_value,
192 string_set_from_boxed,
193 string_set_from_string,
194 string_set_from_value,
195 string_set_from_void
197 { NAFD_TYPE_STRING_LIST,
198 slist_spec,
199 slist_free,
200 slist_dump,
201 slist_are_equal,
202 slist_is_default,
203 slist_is_valid,
204 slist_get_as_string,
205 slist_get_as_void,
206 slist_get_as_value,
207 slist_set_from_boxed,
208 slist_set_from_string,
209 slist_set_from_value,
210 slist_set_from_void
212 { NAFD_TYPE_BOOLEAN,
213 bool_spec,
214 bool_free,
215 bool_dump,
216 bool_are_equal,
217 bool_is_default,
218 bool_is_valid,
219 bool_get_as_string,
220 bool_get_as_void,
221 bool_get_as_value,
222 bool_set_from_boxed,
223 bool_set_from_string,
224 bool_set_from_value,
225 bool_set_from_void
227 { NAFD_TYPE_POINTER,
228 pointer_spec,
229 pointer_free,
230 pointer_dump,
231 pointer_are_equal,
232 pointer_is_default,
233 pointer_is_valid,
234 pointer_get_as_string,
235 pointer_get_as_void,
236 pointer_get_as_value,
237 pointer_set_from_boxed,
238 pointer_set_from_string,
239 pointer_set_from_value,
240 pointer_set_from_void
242 { NAFD_TYPE_UINT,
243 uint_spec,
244 uint_free,
245 uint_dump,
246 uint_are_equal,
247 uint_is_default,
248 uint_is_valid,
249 uint_get_as_string,
250 uint_get_as_void,
251 uint_get_as_value,
252 uint_set_from_boxed,
253 uint_set_from_string,
254 uint_set_from_value,
255 uint_set_from_void
257 { 0 }
260 GType
261 na_data_boxed_get_type( void )
263 static GType item_type = 0;
265 if( item_type == 0 ){
266 item_type = register_type();
269 return( item_type );
272 static GType
273 register_type( void )
275 static const gchar *thisfn = "na_data_boxed_register_type";
276 GType type;
278 static GTypeInfo info = {
279 sizeof( NADataBoxedClass ),
280 NULL,
281 NULL,
282 ( GClassInitFunc ) class_init,
283 NULL,
284 NULL,
285 sizeof( NADataBoxed ),
287 ( GInstanceInitFunc ) instance_init
290 g_debug( "%s", thisfn );
292 type = g_type_register_static( G_TYPE_OBJECT, "NADataBoxed", &info, 0 );
294 return( type );
297 static void
298 class_init( NADataBoxedClass *klass )
300 static const gchar *thisfn = "na_data_boxed_class_init";
301 GObjectClass *object_class;
303 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
305 st_parent_class = g_type_class_peek_parent( klass );
307 object_class = G_OBJECT_CLASS( klass );
308 object_class->dispose = instance_dispose;
309 object_class->finalize = instance_finalize;
311 klass->private = g_new0( NADataBoxedClassPrivate, 1 );
314 static void
315 instance_init( GTypeInstance *instance, gpointer klass )
317 static const gchar *thisfn = "na_data_boxed_instance_init";
318 NADataBoxed *self;
320 g_return_if_fail( NA_IS_DATA_BOXED( instance ));
322 g_debug( "%s: instance=%p (%s), klass=%p",
323 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
325 self = NA_DATA_BOXED( instance );
327 self->private = g_new0( NADataBoxedPrivate, 1 );
329 self->private->dispose_has_run = FALSE;
332 static void
333 instance_dispose( GObject *object )
335 static const gchar *thisfn = "na_data_boxed_instance_dispose";
336 NADataBoxed *self;
338 g_return_if_fail( NA_IS_DATA_BOXED( object ));
340 self = NA_DATA_BOXED( object );
342 if( !self->private->dispose_has_run ){
344 g_debug( "%s: object=%p (%s), name=%s",
345 thisfn,
346 ( void * ) object, G_OBJECT_TYPE_NAME( object ),
347 NA_DATA_BOXED( object )->private->def->name );
349 self->private->dispose_has_run = TRUE;
351 /* chain up to the parent class */
352 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
353 G_OBJECT_CLASS( st_parent_class )->dispose( object );
358 static void
359 instance_finalize( GObject *object )
361 NADataBoxed *self;
363 g_return_if_fail( NA_IS_DATA_BOXED( object ));
365 self = NA_DATA_BOXED( object );
367 DataBoxedFn *fn = get_data_boxed_fn( self->private->def->type );
368 if( fn->free ){
369 ( *fn->free )( self );
372 g_free( self->private );
374 /* chain call to parent class */
375 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
376 G_OBJECT_CLASS( st_parent_class )->finalize( object );
380 static DataBoxedFn *
381 get_data_boxed_fn( guint type )
383 static const gchar *thisfn = "na_data_boxed_get_data_boxed_fn";
384 int i;
385 DataBoxedFn *fn;
387 fn = NULL;
389 for( i = 0 ; st_data_boxed_fn[i].type && !fn ; ++i ){
390 if( st_data_boxed_fn[i].type == type ){
391 fn = st_data_boxed_fn+i;
395 if( !fn ){
396 g_warning( "%s: unmanaged type=%d", thisfn, type );
399 return( fn );
403 * na_data_boxed_get_param_spec:
404 * @def: a #NADataDef definition structure.
406 * Returns: a #GParamSpec structure.
408 * Since: 2.30
410 GParamSpec *
411 na_data_boxed_get_param_spec( const NADataDef *def )
413 GParamSpec *spec;
414 DataBoxedFn *fn;
416 g_return_val_if_fail( def != NULL, NULL );
418 spec = NULL;
419 fn = get_data_boxed_fn( def->type );
421 if( fn ){
422 if( fn->spec ){
423 spec = ( *fn->spec )( def );
427 return( spec );
431 * na_data_boxed_new:
432 * @def: the #NADataDef definition structure for this boxed.
434 * Returns: a newly allocated #NADataBoxed.
436 * Since: 2.30
438 NADataBoxed *
439 na_data_boxed_new( const NADataDef *def )
441 NADataBoxed *boxed;
443 g_return_val_if_fail( def != NULL, NULL );
445 boxed = g_object_new( NA_DATA_BOXED_TYPE, NULL );
447 boxed->private->def = ( NADataDef * ) def;
449 return( boxed );
453 * na_data_boxed_get_data_def:
454 * @boxed: this #NADataBoxed object.
456 * Returns: a pointer to the #NADataDef structure attached to the object.
457 * Should never be %NULL.
459 * Since: 2.30
461 NADataDef *
462 na_data_boxed_get_data_def( const NADataBoxed *boxed )
464 NADataDef *def;
466 g_return_val_if_fail( NA_IS_DATA_BOXED( boxed ), NULL );
468 def = NULL;
470 if( !boxed->private->dispose_has_run ){
472 def = boxed->private->def;
475 return( def );
479 * na_data_boxed_are_equal:
480 * @a: the first #NADataBoxed object.
481 * @b: the second #NADataBoxed object.
483 * Returns: %TRUE if the two boxeds are equal, %FALSE else.
485 * Since: 2.30
487 gboolean
488 na_data_boxed_are_equal( const NADataBoxed *a, const NADataBoxed *b )
490 DataBoxedFn *fn;
491 gboolean are_equal;
493 g_return_val_if_fail( NA_IS_DATA_BOXED( a ), FALSE );
494 g_return_val_if_fail( NA_IS_DATA_BOXED( b ), FALSE );
496 are_equal = FALSE;
498 if( !a->private->dispose_has_run &&
499 !b->private->dispose_has_run ){
501 if( a->private->def->type == b->private->def->type ){
503 fn = get_data_boxed_fn( a->private->def->type );
505 if( fn ){
506 if( fn->are_equal ){
507 are_equal = ( *fn->are_equal )( a, b );
513 return( are_equal );
517 * na_data_boxed_is_default:
518 * @boxed: this #NADataBoxed object.
520 * Returns: %TRUE if the #NADataBoxed holds its default value,
521 * %FALSE else.
523 * Since: 2.30
525 gboolean
526 na_data_boxed_is_default( const NADataBoxed *boxed )
528 gboolean is_default;
529 DataBoxedFn *fn;
531 g_return_val_if_fail( NA_IS_DATA_BOXED( boxed ), FALSE );
533 is_default = FALSE;
535 if( !boxed->private->dispose_has_run ){
537 fn = get_data_boxed_fn( boxed->private->def->type );
539 if( fn ){
540 if( fn->is_default ){
541 is_default = ( *fn->is_default )( boxed );
546 return( is_default );
550 * na_data_boxed_is_valid:
551 * @boxed: the #NADataBoxed object whose validity is to be checked.
553 * Returns: %TRUE if the boxed is valid, %FALSE else.
555 * Since: 2.30
557 gboolean
558 na_data_boxed_is_valid( const NADataBoxed *boxed )
560 DataBoxedFn *fn;
561 gboolean is_valid;
563 g_return_val_if_fail( NA_IS_DATA_BOXED( boxed ), FALSE );
565 is_valid = FALSE;
567 if( !boxed->private->dispose_has_run ){
569 fn = get_data_boxed_fn( boxed->private->def->type );
571 if( fn ){
572 if( fn->is_valid ){
573 is_valid = ( *fn->is_valid )( boxed );
578 return( is_valid );
582 * na_data_boxed_dump:
583 * @boxed: this #NADataBoxed object.
585 * Dump the content of @boxed.
587 * Since: 2.30
589 void
590 na_data_boxed_dump( const NADataBoxed *boxed )
592 DataBoxedFn *fn;
594 fn = get_data_boxed_fn( boxed->private->def->type );
596 if( fn ){
597 if( fn->dump ){
598 ( *fn->dump )( boxed );
604 * na_data_boxed_set_data_def:
605 * @boxed: this #NADataBoxed object.
606 * @def: the new #NADataDef to be set.
608 * Changes the #NADataDef a @boxed points to:
609 * -> the new type must be the same that the previous one.
610 * -> value is unchanged.
612 * Since: 2.30
614 void
615 na_data_boxed_set_data_def( NADataBoxed *boxed, const NADataDef *new_def )
617 g_return_if_fail( NA_IS_DATA_BOXED( boxed ));
618 g_return_if_fail( new_def != NULL );
619 g_return_if_fail( new_def->type == boxed->private->def->type );
621 if( !boxed->private->dispose_has_run ){
623 boxed->private->def = ( NADataDef * ) new_def;
628 * na_data_boxed_get_as_string:
629 * @boxed: the #NADataBoxed whose value is to be set.
631 * Returns: the value of the @boxed, as a newly allocated string which
632 * should be g_free() by the caller.
634 * Since: 2.30
636 gchar *
637 na_data_boxed_get_as_string( const NADataBoxed *boxed )
639 DataBoxedFn *fn;
640 gchar *value;
642 g_return_val_if_fail( NA_IS_DATA_BOXED( boxed ), NULL );
644 value = NULL;
646 if( !boxed->private->dispose_has_run ){
648 fn = get_data_boxed_fn( boxed->private->def->type );
650 if( fn ){
651 if( fn->get_as_string ){
652 value = ( *fn->get_as_string )( boxed );
657 return( value );
661 * na_data_boxed_get_as_void:
662 * @boxed: the #NADataBoxed whose value is to be set.
664 * Returns: the content of the @boxed.
666 * If of type NAFD_TYPE_STRING, NAFD_TYPE_LOCALE_STRING OR
667 * NAFD_TYPE_STRING_LIST, then the content is returned in a newly
668 * allocated value, which should be released by the caller.
670 * Since: 2.30
672 void *
673 na_data_boxed_get_as_void( const NADataBoxed *boxed )
675 DataBoxedFn *fn;
676 void *value;
678 g_return_val_if_fail( NA_IS_DATA_BOXED( boxed ), NULL );
680 value = NULL;
682 if( !boxed->private->dispose_has_run ){
684 fn = get_data_boxed_fn( boxed->private->def->type );
686 if( fn ){
687 if( fn->get_as_void ){
688 value = ( *fn->get_as_void )( boxed );
693 return( value );
697 * na_data_boxed_get_as_value:
698 * @boxed: the #NADataBoxed whose value is to be set.
699 * @value: the string to be set.
701 * Setup @value with the content of the @boxed.
703 * Since: 2.30
705 void
706 na_data_boxed_get_as_value( const NADataBoxed *boxed, GValue *value )
708 DataBoxedFn *fn;
710 g_return_if_fail( NA_IS_DATA_BOXED( boxed ));
712 if( !boxed->private->dispose_has_run ){
714 fn = get_data_boxed_fn( boxed->private->def->type );
716 if( fn ){
717 if( fn->get_as_value ){
718 ( *fn->get_as_value )( boxed, value );
725 * na_data_boxed_set_from_boxed:
726 * @boxed: the #NADataBoxed whose value is to be set.
727 * @value: the source #NADataBoxed.
729 * Copy value from @value to @boxed.
731 * Since: 2.30
733 void
734 na_data_boxed_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *value )
736 DataBoxedFn *fn;
738 g_return_if_fail( NA_IS_DATA_BOXED( boxed ));
739 g_return_if_fail( NA_IS_DATA_BOXED( value ));
740 g_return_if_fail( boxed->private->def->type == value->private->def->type );
742 if( !boxed->private->dispose_has_run ){
744 fn = get_data_boxed_fn( boxed->private->def->type );
746 if( fn ){
747 if( fn->free ){
748 ( *fn->free )( boxed );
750 if( fn->set_from_boxed ){
751 ( *fn->set_from_boxed )( boxed, value );
758 * na_data_boxed_set_from_string:
759 * @boxed: the #NADataBoxed whose value is to be set.
760 * @value: the string to be set.
762 * Evaluates the @value and set it to the @boxed.
764 * Since: 2.30
766 void
767 na_data_boxed_set_from_string( NADataBoxed *boxed, const gchar *value )
769 DataBoxedFn *fn;
771 g_return_if_fail( NA_IS_DATA_BOXED( boxed ));
773 if( !boxed->private->dispose_has_run ){
775 fn = get_data_boxed_fn( boxed->private->def->type );
777 if( fn ){
778 if( fn->free ){
779 ( *fn->free )( boxed );
781 if( fn->set_from_string ){
782 ( *fn->set_from_string )( boxed, value );
789 * na_data_boxed_set_from_value:
790 * @boxed: the #NADataBoxed whose value is to be set.
791 * @value: the value whose content is to be got.
793 * Evaluates the @value and set it to the @boxed.
795 * Since: 2.30
797 void
798 na_data_boxed_set_from_value( NADataBoxed *boxed, const GValue *value )
800 DataBoxedFn *fn;
802 g_return_if_fail( NA_IS_DATA_BOXED( boxed ));
804 if( !boxed->private->dispose_has_run ){
806 fn = get_data_boxed_fn( boxed->private->def->type );
808 if( fn ){
809 if( fn->free ){
810 ( *fn->free )( boxed );
812 if( fn->set_from_value ){
813 ( *fn->set_from_value )( boxed, value );
820 * na_data_boxed_set_from_void:
821 * @boxed: the #NADataBoxed whose value is to be set.
822 * @value: the value whose content is to be got.
824 * Evaluates the @value and set it to the @boxed.
826 * Since: 2.30
828 void
829 na_data_boxed_set_from_void( NADataBoxed *boxed, const void *value )
831 DataBoxedFn *fn;
833 g_return_if_fail( NA_IS_DATA_BOXED( boxed ));
835 if( !boxed->private->dispose_has_run ){
837 fn = get_data_boxed_fn( boxed->private->def->type );
839 if( fn ){
840 if( fn->free ){
841 ( *fn->free )( boxed );
843 if( fn->set_from_void ){
844 ( *fn->set_from_void )( boxed, value );
850 static GParamSpec *
851 string_spec( const NADataDef *def )
853 return( g_param_spec_string(
854 def->name,
855 def->short_label,
856 def->long_label,
857 def->default_value,
858 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
861 static void
862 string_free( const NADataBoxed *boxed )
864 g_free( boxed->private->u.string );
865 boxed->private->u.string = NULL;
868 static void
869 string_dump( const NADataBoxed *boxed )
871 g_debug( "na-data-boxed: %s=%s", boxed->private->def->name, boxed->private->u.string );
874 static gboolean
875 string_are_equal( const NADataBoxed *a, const NADataBoxed *b )
877 if( !a->private->u.string && !b->private->u.string ){
878 return( TRUE );
880 if( !a->private->u.string || !b->private->u.string ){
881 return( FALSE );
883 if( strcmp( a->private->u.string, b->private->u.string ) == 0 ){
884 return( TRUE );
886 return( FALSE );
889 static gboolean
890 string_is_default( const NADataBoxed *boxed )
892 gboolean is_default = FALSE;
894 if( boxed->private->def->default_value ){
895 if( boxed->private->u.string ){
896 /* default value is not null and string has something */
897 is_default = ( strcmp( boxed->private->u.string, boxed->private->def->default_value ) == 0 );
899 } else {
900 /* default value is not null, but string is null */
901 is_default = FALSE;
903 } else if( boxed->private->u.string ){
904 /* default value is null, but string has something */
905 is_default = FALSE;
907 } else {
908 /* default value and string are both null */
909 is_default = TRUE;
912 return( is_default );
915 static gboolean
916 string_is_valid( const NADataBoxed *boxed )
918 gboolean is_valid = TRUE;
920 if( boxed->private->def->mandatory ){
921 if( !boxed->private->u.string || !strlen( boxed->private->u.string )){
922 g_debug( "na_data_boxed_string_is_valid: invalid %s: mandatory but empty or null", boxed->private->def->name );
923 is_valid = FALSE;
927 return( is_valid );
930 static gchar *
931 string_get_as_string( const NADataBoxed *boxed )
933 return( boxed->private->u.string ? g_strdup( boxed->private->u.string ) : g_strdup( "" ));
936 static void *
937 string_get_as_void( const NADataBoxed *boxed )
939 void *value;
941 value = ( void * ) string_get_as_string( boxed );
943 return( value );
946 static void
947 string_get_as_value( const NADataBoxed *boxed, GValue *value )
949 gchar *str;
951 str = string_get_as_string( boxed );
952 g_value_set_string( value, str );
953 g_free( str );
956 static void
957 string_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source )
959 boxed->private->u.string = g_strdup( source->private->u.string );
962 static void
963 string_set_from_string( NADataBoxed *boxed, const gchar *string )
965 if( string ){
966 boxed->private->u.string = g_strdup( string );
970 static void
971 string_set_from_value( NADataBoxed *boxed, const GValue *value )
973 if( g_value_get_string( value )){
974 boxed->private->u.string = g_value_dup_string( value );
978 static void
979 string_set_from_void( NADataBoxed *boxed, const void *value )
981 if( value ){
982 boxed->private->u.string = g_strdup(( const gchar * ) value );
986 static gboolean
987 locale_are_equal( const NADataBoxed *a, const NADataBoxed *b )
989 if( !a->private->u.string && !b->private->u.string ){
990 return( TRUE );
992 if( !a->private->u.string || !b->private->u.string ){
993 return( FALSE );
995 return( na_core_utils_str_collate( a->private->u.string, b->private->u.string ) == 0 );
998 static gboolean
999 locale_is_default( const NADataBoxed *boxed )
1001 gboolean is_default = FALSE;
1003 if( boxed->private->def->default_value ){
1004 if( boxed->private->u.string ){
1005 /* default value is not null and string has something */
1006 is_default = ( na_core_utils_str_collate( boxed->private->u.string, boxed->private->def->default_value ) == 0 );
1008 } else {
1009 /* default value is not null, but string is null */
1010 is_default = FALSE;
1012 } else if( boxed->private->u.string ){
1013 /* default value is null, but string has something */
1014 is_default = FALSE;
1016 } else {
1017 /* default value and string are both null */
1018 is_default = TRUE;
1021 return( is_default );
1024 static gboolean
1025 locale_is_valid( const NADataBoxed *boxed )
1027 gboolean is_valid = TRUE;
1029 if( boxed->private->def->mandatory ){
1030 if( !boxed->private->u.string || !g_utf8_strlen( boxed->private->u.string, -1 )){
1031 g_debug( "na_data_boxed_string_is_valid: invalid %s: mandatory but empty or null", boxed->private->def->name );
1032 is_valid = FALSE;
1036 return( is_valid );
1039 static GParamSpec *
1040 slist_spec( const NADataDef *def )
1042 return( g_param_spec_pointer(
1043 def->name,
1044 def->short_label,
1045 def->long_label,
1046 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
1049 static void
1050 slist_free( const NADataBoxed *boxed )
1052 na_core_utils_slist_free( boxed->private->u.slist );
1053 boxed->private->u.slist = NULL;
1056 static void
1057 slist_dump( const NADataBoxed *boxed )
1059 static const gchar *thisfn = "na_data_boxed_slist_dump";
1061 g_debug( "%s: %s is", thisfn, boxed->private->def->name );
1062 na_core_utils_slist_dump( thisfn, boxed->private->u.slist );
1065 static gboolean
1066 slist_are_equal( const NADataBoxed *a, const NADataBoxed *b )
1068 if( !a->private->u.slist && !b->private->u.slist ){
1069 return( TRUE );
1071 if( !a->private->u.slist || !b->private->u.slist ){
1072 return( FALSE );
1074 return( na_core_utils_slist_are_equal( a->private->u.slist, b->private->u.slist ));
1077 static gboolean
1078 slist_is_default( const NADataBoxed *boxed )
1080 gboolean is_default = FALSE;
1081 GSList *default_value;
1083 if( boxed->private->def->default_value ){
1084 if( boxed->private->u.slist ){
1085 default_value = na_gconf_utils_slist_from_string( boxed->private->def->default_value );
1086 is_default = na_core_utils_slist_are_equal( default_value, boxed->private->u.slist );
1087 na_core_utils_slist_free( default_value );
1089 } else {
1090 is_default = FALSE;
1092 } else if( boxed->private->u.slist ){
1093 is_default = FALSE;
1095 } else {
1096 is_default = TRUE;
1099 return( is_default );
1102 static gboolean
1103 slist_is_valid( const NADataBoxed *boxed )
1105 gboolean is_valid = TRUE;
1107 if( boxed->private->def->mandatory ){
1108 if( !boxed->private->u.slist || !g_slist_length( boxed->private->u.slist )){
1109 g_debug( "na_data_boxed_string_is_valid: invalid %s: mandatory but empty or null", boxed->private->def->name );
1110 is_valid = FALSE;
1114 return( is_valid );
1117 static gchar *
1118 slist_get_as_string( const NADataBoxed *boxed )
1120 return( na_gconf_utils_slist_to_string( boxed->private->u.slist ));
1123 static void *
1124 slist_get_as_void( const NADataBoxed *boxed )
1126 void *value = NULL;
1128 if( boxed->private->u.slist ){
1129 value = na_core_utils_slist_duplicate( boxed->private->u.slist );
1132 return( value );
1135 static void
1136 slist_get_as_value( const NADataBoxed *boxed, GValue *value )
1138 g_value_set_pointer( value, na_core_utils_slist_duplicate( boxed->private->u.slist ));
1141 static void
1142 slist_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source )
1144 boxed->private->u.slist = na_core_utils_slist_duplicate( source->private->u.slist );
1147 static void
1148 slist_set_from_string( NADataBoxed *boxed, const gchar *string )
1150 GSList *slist;
1152 if( string ){
1154 /* if it is a string list which comes from GConf
1156 slist = na_gconf_utils_slist_from_string( string );
1158 if( slist ){
1159 boxed->private->u.slist = slist;
1161 } else {
1162 boxed->private->u.slist = g_slist_append( NULL, g_strdup( string ));
1167 static void
1168 slist_set_from_value( NADataBoxed *boxed, const GValue *value )
1170 if( g_value_get_pointer( value )){
1171 boxed->private->u.slist = na_core_utils_slist_duplicate( g_value_get_pointer( value ));
1175 static void
1176 slist_set_from_void( NADataBoxed *boxed, const void *value )
1178 if( value ){
1179 boxed->private->u.slist = na_core_utils_slist_duplicate(( GSList * ) value );
1183 static GParamSpec *
1184 bool_spec( const NADataDef *def )
1186 return( g_param_spec_boolean(
1187 def->name,
1188 def->short_label,
1189 def->long_label,
1190 na_core_utils_boolean_from_string( def->default_value ),
1191 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
1194 static void
1195 bool_free( const NADataBoxed *boxed )
1197 /* n/a */
1200 static void
1201 bool_dump( const NADataBoxed *boxed )
1203 g_debug( "na-data-boxed: %s=%s",
1204 boxed->private->def->name, boxed->private->u.boolean ? "True":"False" );
1207 static gboolean
1208 bool_are_equal( const NADataBoxed *a, const NADataBoxed *b )
1210 return( a->private->u.boolean == b->private->u.boolean );
1213 static gboolean
1214 bool_is_default( const NADataBoxed *boxed )
1216 gboolean is_default = FALSE;
1217 gboolean default_value;
1219 if( boxed->private->def->default_value && strlen( boxed->private->def->default_value )){
1220 default_value = na_core_utils_boolean_from_string( boxed->private->def->default_value );
1221 is_default = ( default_value == boxed->private->u.boolean );
1224 return( is_default );
1227 static gboolean
1228 bool_is_valid( const NADataBoxed *boxed )
1230 return( TRUE );
1233 static gchar *
1234 bool_get_as_string( const NADataBoxed *boxed )
1236 return( g_strdup_printf( "%s", boxed->private->u.boolean ? "True":"False" ));
1239 static void *
1240 bool_get_as_void( const NADataBoxed *boxed )
1242 return( GUINT_TO_POINTER( boxed->private->u.boolean ));
1245 static void
1246 bool_get_as_value( const NADataBoxed *boxed, GValue *value )
1248 g_value_set_boolean( value, boxed->private->u.boolean );
1251 static void
1252 bool_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source )
1254 boxed->private->u.boolean = source->private->u.boolean;
1257 static void
1258 bool_set_from_string( NADataBoxed *boxed, const gchar *string )
1260 boxed->private->u.boolean = na_core_utils_boolean_from_string( string );
1263 static void
1264 bool_set_from_value( NADataBoxed *boxed, const GValue *value )
1266 boxed->private->u.boolean = g_value_get_boolean( value );
1269 static void
1270 bool_set_from_void( NADataBoxed *boxed, const void *value )
1272 boxed->private->u.boolean = GPOINTER_TO_UINT( value );
1275 static GParamSpec *
1276 pointer_spec( const NADataDef *def )
1278 return( g_param_spec_pointer(
1279 def->name,
1280 def->short_label,
1281 def->long_label,
1282 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
1285 static void
1286 pointer_free( const NADataBoxed *boxed )
1288 boxed->private->u.pointer = NULL;
1291 static void
1292 pointer_dump( const NADataBoxed *boxed )
1294 g_debug( "na-data-boxed: %s=%p",
1295 boxed->private->def->name, ( void * ) boxed->private->u.pointer );
1298 static gboolean
1299 pointer_are_equal( const NADataBoxed *a, const NADataBoxed *b )
1301 return( a->private->u.pointer == b->private->u.pointer );
1305 * say that a pointer never has its default value
1306 * (essentially because there cannot be any relevant default value for a pointer)
1308 static gboolean
1309 pointer_is_default( const NADataBoxed *boxed )
1311 return( FALSE );
1314 static gboolean
1315 pointer_is_valid( const NADataBoxed *boxed )
1317 gboolean is_valid = TRUE;
1319 if( boxed->private->def->mandatory ){
1320 if( !boxed->private->u.pointer ){
1321 g_debug( "na_data_boxed_string_is_valid: invalid %s: mandatory but null", boxed->private->def->name );
1322 is_valid = FALSE;
1326 return( is_valid );
1329 static gchar *
1330 pointer_get_as_string( const NADataBoxed *boxed )
1332 return( g_strdup_printf( "%p", boxed->private->u.pointer ));
1335 static void *
1336 pointer_get_as_void( const NADataBoxed *boxed )
1338 return( boxed->private->u.pointer );
1341 static void
1342 pointer_get_as_value( const NADataBoxed *boxed, GValue *value )
1344 g_value_set_pointer( value, boxed->private->u.pointer );
1347 static void
1348 pointer_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source )
1350 boxed->private->u.pointer = source->private->u.pointer;
1353 static void
1354 pointer_set_from_string( NADataBoxed *boxed, const gchar *pointer )
1356 g_warning( "na_data_boxed_pointer_set_from_string: unrelevant function call" );
1359 static void
1360 pointer_set_from_value( NADataBoxed *boxed, const GValue *value )
1362 boxed->private->u.pointer = g_value_get_pointer( value );
1365 static void
1366 pointer_set_from_void( NADataBoxed *boxed, const void *value )
1368 boxed->private->u.pointer = ( void * ) value;
1371 static GParamSpec *
1372 uint_spec( const NADataDef *def )
1374 return( g_param_spec_uint(
1375 def->name,
1376 def->short_label,
1377 def->long_label,
1379 UINT_MAX,
1380 atoi( def->default_value ),
1381 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
1384 static void
1385 uint_free( const NADataBoxed *boxed )
1387 boxed->private->u.uint = 0;
1390 static void
1391 uint_dump( const NADataBoxed *boxed )
1393 g_debug( "na-data-boxed: %s=%d",
1394 boxed->private->def->name, boxed->private->u.uint );
1397 static gboolean
1398 uint_are_equal( const NADataBoxed *a, const NADataBoxed *b )
1400 return( a->private->u.uint == b->private->u.uint );
1403 static gboolean
1404 uint_is_default( const NADataBoxed *boxed )
1406 gboolean is_default = FALSE;
1407 guint default_value;
1409 if( boxed->private->def->default_value ){
1410 default_value = atoi( boxed->private->def->default_value );
1411 is_default = ( boxed->private->u.uint == default_value );
1414 return( is_default );
1417 static gboolean
1418 uint_is_valid( const NADataBoxed *boxed )
1420 return( TRUE );
1423 static gchar *
1424 uint_get_as_string( const NADataBoxed *boxed )
1426 return( g_strdup_printf( "%u", boxed->private->u.uint ));
1429 static void *
1430 uint_get_as_void( const NADataBoxed *boxed )
1432 return( GUINT_TO_POINTER( boxed->private->u.uint ));
1435 static void
1436 uint_get_as_value( const NADataBoxed *boxed, GValue *value )
1438 g_value_set_uint( value, boxed->private->u.uint );
1441 static void
1442 uint_set_from_boxed( NADataBoxed *boxed, const NADataBoxed *source )
1444 boxed->private->u.uint = source->private->u.uint;
1447 static void
1448 uint_set_from_string( NADataBoxed *boxed, const gchar *string )
1450 boxed->private->u.uint = string ? atoi( string ) : 0;
1453 static void
1454 uint_set_from_value( NADataBoxed *boxed, const GValue *value )
1456 boxed->private->u.uint = g_value_get_uint( value );
1459 static void
1460 uint_set_from_void( NADataBoxed *boxed, const void *value )
1462 boxed->private->u.uint = GPOINTER_TO_UINT( value );