/
builtin-gconf.c
2796 lines (2465 loc) · 67.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @file builtin-gconf.c
* GConf compatibility module - for dynamic mce settings
* <p>
* Copyright (C) 2012-2019 Jolla Ltd.
* <p>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mce. If not, see <http://www.gnu.org/licenses/>.
*/
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* ------------------------------------------------------------------------- *
* NOTE: This module implements just enough of the GConf API to allow us
* to detach mce from D-Bus session bus. By no means is this even meant
* to be a complete GConf replacement.
*
* CAVEATS include *at least*:
* - the keys have no hierarchy
* - anything related to directories is simply ignored
* - gconf_client_get() returns the real thing, not deep copied duplicate
* - pair values are not supported
* - adding new values is not supported
* - value types can't be changed
* ------------------------------------------------------------------------- */
36
37
38
#include "mce-log.h"
#include "mce-io.h"
#include "mce-dbus.h"
39
#include "mce-setting.h"
40
41
#include "powerkey.h"
42
#include "tklock.h"
43
#include "event-input.h"
44
45
#include "modules/memnotify.h"
46
#include "modules/display.h"
47
#include "modules/proximity.h"
48
49
50
#include "modules/powersavemode.h"
#include "modules/doubletap.h"
#include "modules/led.h"
51
#include "modules/inactivity.h"
52
53
54
#include <stdlib.h>
#include <string.h>
55
#include <errno.h>
56
#include <glob.h>
57
58
59
60
61
62
63
64
65
66
67
68
69
/* ========================================================================= *
*
* CONFIGURATION
*
* ========================================================================= */
/** Enable error logging to stderr via gconf_log_error() */
#define GCONF_ENABLE_ERROR_LOGGING 01
/** Enable debug logging to stderr via gconf_log_debug() */
#define GCONF_ENABLE_DEBUG_LOGGING 01
70
71
72
/** Path to persistent storage file */
#define VALUES_PATH G_STRINGIFY(MCE_VAR_DIR)"/builtin-gconf.values"
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* ========================================================================= *
*
* MACROS
*
* ========================================================================= */
/** Tag unused parameters as such to avoid compilation warnings */
#define unused(variable) (void)&variable
/* ========================================================================= *
*
* FUNCTION PROTOTYPES
*
* ========================================================================= */
static GQuark gconf_error_quark(void);
static void gconf_set_error(GError **err, gint code, const gchar *fmt, ...);
static char *gconf_strip_string(char *str);
static int gconf_parse_int(const char *str);
static double gconf_parse_float(const char *str);
static gboolean gconf_parse_bool(const char *str);
static GConfValueType gconf_parse_type(int chr);
static const char *gconf_type_repr(GConfValueType type);
static gboolean gconf_require_type(const char *key, const GConfValue *value, GConfValueType type, GError **err);
static gboolean gconf_require_list_type(const char *key, const GConfValue *value, GConfValueType type, GError **err);
gchar *gconf_concat_dir_and_key(const gchar *dir, const gchar *key);
99
#if GCONF_ENABLE_DEBUG_LOGGING
100
static char *gconf_value_repr(const char *key, GConfValue *self);
101
#endif
102
static char *gconf_value_str(GConfValue *self);
103
104
105
106
107
108
109
110
111
112
static void gconf_value_unset(GConfValue *self);
static gboolean gconf_value_list_validata(GSList *src, GConfValueType type);
static GSList *gconf_value_list_copy(GSList *src);
static GSList *gconf_value_list_free(GSList *list);
static void gconf_value_set_from_string(GConfValue *self, const char *data);
static GConfValue *gconf_value_init(GConfValueType type, GConfValueType list_type, const char *data);
GConfValue *gconf_value_copy(const GConfValue *src);
GConfValue *gconf_value_new(GConfValueType type);
void gconf_value_free(GConfValue *self);
gboolean gconf_value_get_bool(const GConfValue *self);
113
bool gconf_value_set_bool(GConfValue *self, gboolean val);
114
int gconf_value_get_int(const GConfValue *self);
115
bool gconf_value_set_int(GConfValue *self, gint val);
116
double gconf_value_get_float(const GConfValue *self);
117
bool gconf_value_set_float(GConfValue *self, double val);
118
const char *gconf_value_get_string(const GConfValue *self);
119
bool gconf_value_set_string(GConfValue *self, const char *val);
120
121
122
GConfValueType gconf_value_get_list_type(const GConfValue *self);
void gconf_value_set_list_type(GConfValue *self, GConfValueType list_type);
GSList *gconf_value_get_list(const GConfValue *self);
123
bool gconf_value_set_list(GConfValue *self, GSList *list);
124
static GConfEntry *gconf_entry_init(const char *key, const char *type, const char *data);
125
126
static void gconf_entry_free(GConfEntry *self);
static void gconf_entry_free_cb(gpointer self);
127
128
const char *gconf_entry_get_key(const GConfEntry *entry);
GConfValue *gconf_entry_get_value(const GConfEntry *entry);
129
#if GCONF_ENABLE_DEBUG_LOGGING
130
static void gconf_client_debug(GConfClient *self);
131
#endif
132
GConfClient *gconf_client_get_default(void);
133
static void gconf_client_free_default(void);
134
135
136
137
138
void gconf_client_add_dir(GConfClient *client, const gchar *dir, GConfClientPreloadType preload, GError **err);
static GConfEntry *gconf_client_find_entry(GConfClient *self, const gchar *key, GError **err);
static GConfValue *gconf_client_find_value(GConfClient *self, const gchar *key, GError **err);
GConfValue *gconf_client_get(GConfClient *self, const gchar *key, GError **err);
static void gconf_client_notify_free(GConfClientNotify *self);
139
static void gconf_client_notify_free_cb(gpointer self);
140
141
142
143
144
145
146
147
148
149
150
static GConfClientNotify *gconf_client_notify_new(const gchar *namespace_section, GConfClientNotifyFunc func, gpointer user_data, GFreeFunc destroy_notify);
static void gconf_client_notify_change(GConfClient *client, const gchar *namespace_section);
guint gconf_client_notify_add(GConfClient *client, const gchar *namespace_section, GConfClientNotifyFunc func, gpointer user_data, GFreeFunc destroy_notify, GError **err);
void gconf_client_notify_remove(GConfClient *client, guint cnxn);
gboolean gconf_client_set_bool(GConfClient *client, const gchar *key, gboolean val, GError **err);
gboolean gconf_client_set_int(GConfClient *client, const gchar *key, gint val, GError **err);
gboolean gconf_client_set_float(GConfClient *client, const gchar *key, double val, GError **err);
gboolean gconf_client_set_string(GConfClient *client, const gchar *key, const gchar *val, GError **err);
gboolean gconf_client_set_list(GConfClient *client, const gchar *key, GConfValueType list_type, GSList *list, GError **err);
void gconf_client_suggest_sync(GConfClient *client, GError **err);
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* ========================================================================= *
*
* MISCELLANEOUS UTILITIES
*
* ========================================================================= */
/* Null tolerant string equality predicate
*
* @param s1 string
* @param s2 string
*
* @return true if both s1 and s2 are null or same string, false otherwise
*/
static inline bool eq(const char *s1, const char *s2)
{
return (s1 && s2) ? !strcmp(s1, s2) : (s1 == s2);
}
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* ========================================================================= *
*
* ERRORS & LOGGING
*
* ========================================================================= */
/** Our custom error quark */
static
GQuark
gconf_error_quark (void)
{
return g_quark_from_static_string ("gconf-setting-error-quark");
}
#define GCONF_ERROR gconf_error_quark()
/** Error logging function */
#if GCONF_ENABLE_ERROR_LOGGING
187
# define gconf_log_error(FMT, ARG...) mce_log_file(LL_WARN, __FILE__, __FUNCTION__, FMT , ##ARG)
188
#else
189
# define gconf_log_error(FMT, ARG...) do{}while(0)
190
191
192
193
#endif
/** Debug logging function */
#if GCONF_ENABLE_DEBUG_LOGGING
194
195
# define gconf_log_debug_p() mce_log_p(LL_DEBUG)
# define gconf_log_debug(FMT, ARG...) mce_log_file(LL_DEBUG, __FILE__, __FUNCTION__, FMT , ##ARG)
196
#else
197
198
# define gconf_log_debug_p() 0
# define gconf_log_debug(FMT, ARG...) do{}while(0)
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#endif
/** Set GError helper */
static
void
gconf_set_error(GError **err, gint code, const gchar *fmt, ...)
__attribute__((format(printf, 3, 4)));
static
void
gconf_set_error(GError **err, gint code, const gchar *fmt, ...)
{
char *msg = 0;
va_list va;
va_start(va, fmt);
if( vasprintf(&msg, fmt, va) < 0 )
{
msg = 0;
}
va_end(va);
221
222
223
224
/* Assume caller will report error in appropriate context,
* log from builtin-gconf only in debug verbosity level */
mce_log(LL_DEBUG, "%s", msg ?: "unknown");
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
g_set_error_literal(err, GCONF_ERROR, code, msg ?: "unknown");
free(msg);
}
/* ========================================================================= *
*
* STRING PARSING
*
* ========================================================================= */
/** ASCII white character predicate */
static inline gboolean gconf_white_p(int c)
{
return (c > 0) && (c <= 32);
}
/** ASCII non-white character predicate */
static inline gboolean gconf_black_p(int c)
{
return (c < 0) || (c > 32);
}
/** Strip excess whitespace from string */
static char *gconf_strip_string(char *str)
{
if( str )
{
char *s = str;
char *d = str;
while( gconf_white_p(*s) ) ++s;
for( ;; )
{
while( gconf_black_p(*s) ) *d++ = *s++;
while( gconf_white_p(*s) ) ++s;
if( !*s ) break;
*d++ = ' ';
}
*d = 0;
}
return str;
}
/** Array of strings we accept as: boolean true */
static const char * const gconf_true_lut[] = { "true", "t", "yes", "y", 0 };
/** Array of strings we accept as: boolean false */
static const char * const gconf_false_lut[] = { "false", "f", "no", "n", 0 };
/** String to int helper */
static int gconf_parse_int(const char *str)
{
char *end = 0;
int res = strtol(str, &end, 0);
if( (end <= str) || *end )
{
gconf_log_error("'%s': is not fully qualified integer", str);
}
return res;
}
/** String to double helper */
static double gconf_parse_float(const char *str)
{
char *end = 0;
double res = strtod(str, &end);
if( (end <= str) || *end )
{
gconf_log_error("'%s': is not fully qualified float", str);
}
return res;
}
/** String to bool helper */
static gboolean gconf_parse_bool(const char *str)
{
for( size_t i = 0; gconf_true_lut[i]; ++i )
{
if( !strcmp(gconf_true_lut[i], str) ) return TRUE;
}
for( size_t i = 0; gconf_false_lut[i]; ++i )
{
if( !strcmp(gconf_false_lut[i], str) ) return FALSE;
}
int res = gconf_parse_int(str);
if( res < 0 || res > 1 )
{
gconf_log_error("'%s': is not fully qualified bool", str);
}
return res;
}
/** Char to GConfValueType helper */
static GConfValueType gconf_parse_type(int chr)
{
GConfValueType res = GCONF_VALUE_INVALID;
switch( chr )
{
case 'b': res = GCONF_VALUE_BOOL; break;
case 'i': res = GCONF_VALUE_INT; break;
case 'f': res = GCONF_VALUE_FLOAT; break;
case 's': res = GCONF_VALUE_STRING; break;
case 'a': res = GCONF_VALUE_LIST; break;
default:
339
gconf_log_error("unknown type '%c'", chr);
340
341
342
343
344
345
346
347
348
349
350
break;
}
return res;
}
/* ========================================================================= *
*
* MISCELLANEOUS
*
* ========================================================================= */
351
#if GCONF_ENABLE_DEBUG_LOGGING
352
353
354
355
356
357
358
/** Boolean to text helper */
static
const char *
gconf_bool_repr(gboolean value)
{
return value ? *gconf_true_lut : *gconf_false_lut;
}
359
#endif
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/** GConfValueType to text helper */
static
const char *
gconf_type_repr(GConfValueType type)
{
const char *res = "unknown";
switch( type )
{
case GCONF_VALUE_INVALID: res = "invalid"; break;
case GCONF_VALUE_STRING: res = "string"; break;
case GCONF_VALUE_INT: res = "int"; break;
case GCONF_VALUE_FLOAT: res = "float"; break;
case GCONF_VALUE_BOOL: res = "bool"; break;
case GCONF_VALUE_SCHEMA: res = "schema"; break;
case GCONF_VALUE_LIST: res = "list"; break;
case GCONF_VALUE_PAIR: res = "pair"; break;
default: break;
}
return res;
}
/** Type checking helper */
static
gboolean
gconf_require_type(const char *key, const GConfValue *value,
GConfValueType type, GError **err)
{
if( value->type == type )
{
return TRUE;
}
gconf_set_error(err, GCONF_ERROR_TYPE_MISMATCH, "%s: is %s, not %s",
key, gconf_type_repr(value->type), gconf_type_repr(type));
return FALSE;
}
/** List type checking helper */
static
gboolean
gconf_require_list_type(const char *key, const GConfValue *value,
GConfValueType type, GError **err)
{
if( !gconf_require_type(key, value, GCONF_VALUE_LIST, err) )
{
return FALSE;
}
if( value->list_type == type )
{
return TRUE;
}
gconf_set_error(err, GCONF_ERROR_TYPE_MISMATCH,
"%s: is %s list, not %s list", key,
gconf_type_repr(value->list_type), gconf_type_repr(type));
return FALSE;
}
/** See GConf API documentation */
gchar *
gconf_concat_dir_and_key(const gchar *dir,
const gchar *key)
{
gchar *res = g_strconcat(dir ?: "", "/", key ?: "", NULL);
if( res != 0 )
{
gchar *s = res;
gchar *d = res;
while( *s )
{
/* copy other than '/' verbatim */
if( *s != '/' )
{
*d++ = *s++;
continue;
}
/* compress '///' into '/' */
while( *++s == '/' ) {}
*d++ = '/';
}
/* terminate */
*d = 0;
}
return res;
}
/* ========================================================================= *
*
* GConfValue
*
* ========================================================================= */
460
#if GCONF_ENABLE_DEBUG_LOGGING
461
462
463
464
/** GConfValue to text helper
*
* The result is for human readable for debugging purposes
*/
465
466
467
468
469
470
static
char *
gconf_value_repr(const char *key, GConfValue *self)
{
char *data = 0;
size_t size = 0;
471
472
473
474
475
476
FILE *file = 0;
if( !(file = open_memstream(&data, &size)) )
{
goto cleanup;
}
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
fprintf(file, "'%s' %s", key, gconf_type_repr(self->type));
switch( self->type )
{
case GCONF_VALUE_STRING:
fprintf(file, " '%s'", self->data.s);
break;
case GCONF_VALUE_INT:
fprintf(file, " %d", self->data.i);
break;
case GCONF_VALUE_FLOAT:
fprintf(file, " %g", self->data.f);
break;
case GCONF_VALUE_BOOL:
fprintf(file, " %s", gconf_bool_repr(self->data.b));
break;
case GCONF_VALUE_SCHEMA:
fprintf(file, " %g", self->data.f);
break;
case GCONF_VALUE_LIST:
fprintf(file, " of %s [", gconf_type_repr(self->list_type));
for( GSList *v_iter = self->list_head; v_iter; v_iter = v_iter->next )
{
switch( (self = v_iter->data)->type )
{
case GCONF_VALUE_STRING:
fprintf(file, " '%s'", self->data.s);
break;
case GCONF_VALUE_INT:
fprintf(file, " %d", self->data.i);
break;
case GCONF_VALUE_FLOAT:
fprintf(file, " %g", self->data.f);
break;
case GCONF_VALUE_BOOL:
fprintf(file, " %s", gconf_bool_repr(self->data.b));
break;
default:
fprintf(file, " ???");
break;
}
}
fprintf(file, " ]");
break;
case GCONF_VALUE_PAIR:
fprintf(file, "pair (");
fprintf(file, " )");
break;
case GCONF_VALUE_INVALID:
default:
break;
}
543
544
cleanup:
if( file ) fclose(file);
545
546
547
return data;
}
548
#endif
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/** GConfValue to text helper
*
* The result is compatible with gconf_value_set_from_string()
*/
static
char *
gconf_value_str(GConfValue *self)
{
char *data = 0;
size_t size = 0;
FILE *file = 0;
if( !(file = open_memstream(&data, &size)) )
{
goto cleanup;
}
switch( self->type )
{
case GCONF_VALUE_STRING:
fprintf(file, "%s", self->data.s);
break;
case GCONF_VALUE_INT:
fprintf(file, "%d", self->data.i);
break;
case GCONF_VALUE_FLOAT:
fprintf(file, "%g", self->data.f);
break;
case GCONF_VALUE_BOOL:
fprintf(file, "%s", gconf_bool_repr(self->data.b));
break;
case GCONF_VALUE_SCHEMA:
fprintf(file, "%g", self->data.f);
break;
case GCONF_VALUE_LIST:
for( GSList *v_iter = self->list_head; v_iter; v_iter = v_iter->next )
{
switch( (self = v_iter->data)->type )
{
case GCONF_VALUE_STRING:
fprintf(file, "%s", self->data.s);
break;
case GCONF_VALUE_INT:
fprintf(file, "%d", self->data.i);
break;
case GCONF_VALUE_FLOAT:
fprintf(file, "%g", self->data.f);
break;
case GCONF_VALUE_BOOL:
fprintf(file, "%s", gconf_bool_repr(self->data.b));
break;
default:
fprintf(file, "???");
break;
}
if( v_iter->next ) {
615
fprintf(file, ",");
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
}
}
break;
case GCONF_VALUE_PAIR:
fprintf(file, "???");
break;
case GCONF_VALUE_INVALID:
default:
break;
}
cleanup:
if( file ) fclose(file);
return data;
}
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/** Release dynamic resources of value, but not the value itself */
static
void
gconf_value_unset(GConfValue *self)
{
// clear the list of values
for( GSList *elem = self->list_head; elem; elem = elem->next )
{
gconf_value_free(elem->data);
}
g_slist_free(self->list_head), self->list_head = 0;
// free any dynamically allocated data
switch( self->type )
{
case GCONF_VALUE_STRING:
free(self->data.s), self->data.s = 0;
break;
default:
break;
}
}
/** Helper for type checking a list of values */
static
gboolean
gconf_value_list_validata(GSList *src, GConfValueType type)
{
for( ; src; src = src->next )
{
GConfValue *val = src->data;
if( !val )
{
670
gconf_log_error("list has NULL value");
671
672
673
674
675
return FALSE;
}
if( val->type != type )
{
676
gconf_log_error("list has %s value, expected %s\n",
677
gconf_type_repr(val->type), gconf_type_repr(type));
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
return FALSE;
}
}
return TRUE;
}
/** Helper for deep copying list of values */
static
GSList *
gconf_value_list_copy(GSList *src)
{
GSList *res = 0;
for( ; src; src = src->next )
{
res = g_slist_prepend(res, gconf_value_copy(src->data));
}
res = g_slist_reverse(res);
return res;
}
/** Helper for recursively releasing a list of values */
static
GSList *
gconf_value_list_free(GSList *list)
{
g_slist_free_full(list, (GDestroyNotify)gconf_value_free);
return 0;
}
/** Parse value content from text */
static void gconf_value_set_from_string(GConfValue *self, const char *data)
{
char *now, *zen, *tmp = 0;
switch( self->type )
{
case GCONF_VALUE_BOOL:
self->data.b = gconf_parse_bool(data);
break;
case GCONF_VALUE_INT:
self->data.i = gconf_parse_int(data);
break;
case GCONF_VALUE_FLOAT:
self->data.f = gconf_parse_float(data);
break;
case GCONF_VALUE_STRING:
729
free(self->data.s);
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
self->data.s = strdup(data);
break;
case GCONF_VALUE_LIST:
switch( self->list_type )
{
case GCONF_VALUE_BOOL:
case GCONF_VALUE_INT:
case GCONF_VALUE_FLOAT:
case GCONF_VALUE_STRING:
break;
default:
self->list_type = GCONF_VALUE_INVALID;
self->type = GCONF_VALUE_INVALID;
goto cleanup;
}
748
749
self->list_head = gconf_value_list_free(self->list_head);
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
for( now = tmp = strdup(data); now; now = zen )
{
if( (zen = strchr(now, ',')) )
{
*zen++ = 0;
}
GConfValue *elem = gconf_value_init(self->list_type,
GCONF_VALUE_INVALID,
gconf_strip_string(now));
self->list_head = g_slist_prepend(self->list_head, elem);
}
self->list_head = g_slist_reverse(self->list_head);
break;
default:
self->list_type = GCONF_VALUE_INVALID;
self->type = GCONF_VALUE_INVALID;
break;
}
cleanup:
free(tmp);
}
/** Create and initialize a GConfValue */
static
GConfValue *
gconf_value_init(GConfValueType type, GConfValueType list_type,
const char *data)
{
GConfValue *self = calloc(1, sizeof *self);
self->refcount = 1;
self->list_type = GCONF_VALUE_INVALID;
self->type = type;
if( self->type == GCONF_VALUE_LIST )
{
self->list_type = list_type;
}
if( data != 0 )
{
gconf_value_set_from_string(self, data);
}
return self;
}
/** See GConf API documentation */
GConfValue *
gconf_value_copy(const GConfValue *src)
{
GConfValue *self = gconf_value_init(src->type, src->list_type, 0);
if( self->type != src->type || self->list_type != src->list_type )
{
goto cleanup;
}
switch( self->type )
{
case GCONF_VALUE_BOOL:
self->data.b = src->data.b;
break;
case GCONF_VALUE_INT:
self->data.i = src->data.i;
break;
case GCONF_VALUE_FLOAT:
self->data.f = src->data.f;
break;
case GCONF_VALUE_STRING:
self->data.s = src->data.s ? strdup(src->data.s) : 0;
break;
case GCONF_VALUE_LIST:
self->list_head = gconf_value_list_copy(src->list_head);
break;
default:
self->list_type = GCONF_VALUE_INVALID;
self->type = GCONF_VALUE_INVALID;
break;
}
cleanup:
return self;
}
/** See GConf API documentation */
GConfValue *
gconf_value_new(GConfValueType type)
{
return gconf_value_init(type, GCONF_VALUE_INVALID, 0);
}
/** See GConf API documentation */
void
gconf_value_free(GConfValue *self)
{
if( self != 0 && --self->refcount == 0 )
{
/* get rid of dynamically allocated resources */
gconf_value_unset(self);
/* mark the value as invalid to ease stale pointer debugging */
self->type = GCONF_VALUE_INVALID;
self->list_type = GCONF_VALUE_INVALID;
/* free the value itself too */
free(self);
}
}
/** See GConf API documentation */
gboolean
gconf_value_get_bool(const GConfValue *self)
{
return (self->type == GCONF_VALUE_BOOL) ? self->data.b : 0;
}
/** See GConf API documentation */
877
bool
878
879
gconf_value_set_bool(GConfValue *self, gboolean val)
{
880
881
882
bool changed = false;
if( self->type != GCONF_VALUE_BOOL )
883
{
884
gconf_log_error("not a bool value");
885
}
886
else if( self->data.b != val )
887
{
888
889
self->data.b = val;
changed = true;
890
}
891
892
return changed;
893
894
895
896
897
898
899
900
901
902
}
/** See GConf API documentation */
int
gconf_value_get_int(const GConfValue *self)
{
return (self->type == GCONF_VALUE_INT) ? self->data.i : 0;
}
/** See GConf API documentation */
903
bool
904
905
gconf_value_set_int(GConfValue *self, gint val)
{
906
907
908
bool changed = false;
if( self->type != GCONF_VALUE_INT )
909
{
910
gconf_log_error("not an int value");
911
}
912
else if( self->data.i != val )
913
{
914
915
self->data.i = val;
changed = true;
916
}
917
918
return changed;
919
920
921
922
923
924
925
926
927
928
}
/** See GConf API documentation */
double
gconf_value_get_float(const GConfValue *self)
{
return (self->type == GCONF_VALUE_FLOAT) ? self->data.f : 0;
}
/** See GConf API documentation */
929
bool
930
931
gconf_value_set_float(GConfValue *self, double val)
{
932
933
934
bool changed = false;
if( self->type != GCONF_VALUE_FLOAT )
935
{
936
gconf_log_error("not a float value");
937
}
938
else if( self->data.f != val )
939
{
940
941
self->data.f = val;
changed = true;
942
}
943
944
return changed;
945
946
947
948
949
950
951
952
953
954
}
/** See GConf API documentation */
const char *
gconf_value_get_string(const GConfValue *self)
{
return (self->type == GCONF_VALUE_STRING) ? self->data.s : 0;
}
/** See GConf API documentation */
955
bool
956
957
gconf_value_set_string(GConfValue *self, const char *val)
{
958
959
960
bool changed = false;
if( self->type != GCONF_VALUE_STRING )
961
{
962
gconf_log_error("not a string value");
963
}
964
else if( !eq(self->data.s, val) )
965
{
966
967
free(self->data.s), self->data.s = val ? strdup(val) : 0;
changed = true;
968
}
969
970
return changed;
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
}
/** See GConf API documentation */
GConfValueType
gconf_value_get_list_type(const GConfValue *self)
{
return self->list_type;
}
/** See GConf API documentation */
void
gconf_value_set_list_type(GConfValue *self, GConfValueType list_type)
{
switch( list_type )
{
case GCONF_VALUE_STRING:
case GCONF_VALUE_INT:
case GCONF_VALUE_FLOAT:
case GCONF_VALUE_BOOL:
break;
default:
993
gconf_log_error("list type can't be %s", gconf_type_repr(list_type));
994
995
996
997
998
goto cleanup;
}
if( self->type != GCONF_VALUE_LIST )
{
999
gconf_log_error("not a list value");
1000
goto cleanup;