-
Notifications
You must be signed in to change notification settings - Fork 9
/
builtin-gconf.c
2817 lines (2486 loc) · 67.7 KB
/
builtin-gconf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
543
544
545
546
547
548
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
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
670
671
672
673
674
675
676
677
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file builtin-gconf.c
* GConf compatibility module - for dynamic mce settings
* <p>
* Copyright (c) 2012 - 2022 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/>.
*/
/* ------------------------------------------------------------------------- *
* 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
* ------------------------------------------------------------------------- */
#include "mce-log.h"
#include "mce-io.h"
#include "mce-dbus.h"
#include "mce-setting.h"
#include "powerkey.h"
#include "tklock.h"
#include "event-input.h"
#include "modules/memnotify.h"
#include "modules/display.h"
#include "modules/proximity.h"
#include "modules/powersavemode.h"
#include "modules/doubletap.h"
#include "modules/led.h"
#include "modules/inactivity.h"
#include "modules/charging.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glob.h>
/* ========================================================================= *
*
* 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
/** Path to persistent storage file */
#define VALUES_PATH G_STRINGIFY(MCE_VAR_DIR)"/builtin-gconf.values"
/* ========================================================================= *
*
* 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);
#if GCONF_ENABLE_DEBUG_LOGGING
static char *gconf_value_repr(const char *key, GConfValue *self);
#endif
static char *gconf_value_str(GConfValue *self);
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);
bool gconf_value_set_bool(GConfValue *self, gboolean val);
int gconf_value_get_int(const GConfValue *self);
bool gconf_value_set_int(GConfValue *self, gint val);
double gconf_value_get_float(const GConfValue *self);
bool gconf_value_set_float(GConfValue *self, double val);
const char *gconf_value_get_string(const GConfValue *self);
bool gconf_value_set_string(GConfValue *self, const char *val);
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);
bool gconf_value_set_list(GConfValue *self, GSList *list);
static GConfEntry *gconf_entry_init(const char *key, const char *type, const char *data);
static void gconf_entry_free(GConfEntry *self);
static void gconf_entry_free_cb(gpointer self);
const char *gconf_entry_get_key(const GConfEntry *entry);
GConfValue *gconf_entry_get_value(const GConfEntry *entry);
#if GCONF_ENABLE_DEBUG_LOGGING
static void gconf_client_debug(GConfClient *self);
#endif
GConfClient *gconf_client_get_default(void);
static void gconf_client_free_default(void);
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);
static void gconf_client_notify_free_cb(gpointer self);
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);
/* ========================================================================= *
*
* 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);
}
/* ========================================================================= *
*
* 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
# define gconf_log_error(FMT, ARG...) mce_log_file(LL_WARN, __FILE__, __FUNCTION__, FMT , ##ARG)
#else
# define gconf_log_error(FMT, ARG...) do{}while(0)
#endif
/** Debug logging function */
#if GCONF_ENABLE_DEBUG_LOGGING
# 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)
#else
# define gconf_log_debug_p() 0
# define gconf_log_debug(FMT, ARG...) do{}while(0)
#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);
/* Assume caller will report error in appropriate context,
* log from builtin-gconf only in debug verbosity level */
mce_log(LL_DEBUG, "%s", msg ?: "unknown");
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:
gconf_log_error("unknown type '%c'", chr);
break;
}
return res;
}
/* ========================================================================= *
*
* MISCELLANEOUS
*
* ========================================================================= */
#if GCONF_ENABLE_DEBUG_LOGGING
/** Boolean to text helper */
static
const char *
gconf_bool_repr(gboolean value)
{
return value ? *gconf_true_lut : *gconf_false_lut;
}
#endif
/** 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
*
* ========================================================================= */
#if GCONF_ENABLE_DEBUG_LOGGING
/** GConfValue to text helper
*
* The result is for human readable for debugging purposes
*/
static
char *
gconf_value_repr(const char *key, GConfValue *self)
{
char *data = 0;
size_t size = 0;
FILE *file = 0;
if( !(file = open_memstream(&data, &size)) )
{
goto cleanup;
}
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;
}
cleanup:
if( file ) fclose(file);
return data;
}
#endif
/** 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 ) {
fprintf(file, ",");
}
}
break;
case GCONF_VALUE_PAIR:
fprintf(file, "???");
break;
case GCONF_VALUE_INVALID:
default:
break;
}
cleanup:
if( file ) fclose(file);
return data;
}
/** 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 )
{
gconf_log_error("list has NULL value");
return FALSE;
}
if( val->type != type )
{
gconf_log_error("list has %s value, expected %s\n",
gconf_type_repr(val->type), gconf_type_repr(type));
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:
free(self->data.s);
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;
}
self->list_head = gconf_value_list_free(self->list_head);
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 */
bool
gconf_value_set_bool(GConfValue *self, gboolean val)
{
bool changed = false;
if( self->type != GCONF_VALUE_BOOL )
{
gconf_log_error("not a bool value");
}
else if( self->data.b != val )
{
self->data.b = val;
changed = true;
}
return changed;
}
/** 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 */
bool
gconf_value_set_int(GConfValue *self, gint val)
{
bool changed = false;
if( self->type != GCONF_VALUE_INT )
{
gconf_log_error("not an int value");
}
else if( self->data.i != val )
{
self->data.i = val;
changed = true;
}
return changed;
}
/** 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 */
bool
gconf_value_set_float(GConfValue *self, double val)
{
bool changed = false;
if( self->type != GCONF_VALUE_FLOAT )
{
gconf_log_error("not a float value");
}
else if( self->data.f != val )
{
self->data.f = val;
changed = true;
}
return changed;
}
/** 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 */
bool
gconf_value_set_string(GConfValue *self, const char *val)
{
bool changed = false;
if( self->type != GCONF_VALUE_STRING )
{
gconf_log_error("not a string value");
}
else if( !eq(self->data.s, val) )
{
free(self->data.s), self->data.s = val ? strdup(val) : 0;
changed = true;
}
return changed;
}
/** 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:
gconf_log_error("list type can't be %s", gconf_type_repr(list_type));
goto cleanup;
}
if( self->type != GCONF_VALUE_LIST )
{
gconf_log_error("not a list value");