1 /* GConf
2  * Copyright (C) 1999, 2000, 2002 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include <config.h>
21 #include "gconf-schema.h"
22 #include "gconf-internals.h"
23 
24 /* FIXME clean this up, obviously RealSchema isn't needed. */
25 struct _GConfSchema {
26   int dummy;
27 };
28 
29 typedef struct {
30   GConfValueType type; /* Type of the described entry */
31   GConfValueType list_type; /* List type of the described entry */
32   GConfValueType car_type; /* Pair car type of the described entry */
33   GConfValueType cdr_type; /* Pair cdr type of the described entry */
34   gchar* locale;       /* Schema locale */
35   gchar* owner;        /* Name of creating application */
36   gchar* short_desc;   /* 40 char or less description, no newlines */
37   gchar* long_desc;    /* could be a paragraph or so */
38   GConfValue* default_value; /* Default value of the key */
39 } GConfRealSchema;
40 
41 #define REAL_SCHEMA(schema) ((GConfRealSchema*)(schema))
42 
43 GConfSchema*
gconf_schema_new(void)44 gconf_schema_new (void)
45 {
46   GConfRealSchema *real;
47 
48   real = g_new0 (GConfRealSchema, 1);
49 
50   real->type = GCONF_VALUE_INVALID;
51   real->list_type = GCONF_VALUE_INVALID;
52   real->car_type = GCONF_VALUE_INVALID;
53   real->cdr_type = GCONF_VALUE_INVALID;
54 
55   return (GConfSchema*) real;
56 }
57 
58 void
gconf_schema_free(GConfSchema * sc)59 gconf_schema_free (GConfSchema* sc)
60 {
61   GConfRealSchema *real = REAL_SCHEMA (sc);
62 
63   g_free (real->locale);
64   g_free (real->short_desc);
65   g_free (real->long_desc);
66   g_free (real->owner);
67 
68   if (real->default_value)
69     gconf_value_free (real->default_value);
70 
71   g_free (sc);
72 }
73 
74 GConfSchema*
gconf_schema_copy(const GConfSchema * sc)75 gconf_schema_copy (const GConfSchema* sc)
76 {
77   GConfRealSchema *dest;
78   const GConfRealSchema *real;
79 
80   real = REAL_SCHEMA (sc);
81   dest = (GConfRealSchema*) gconf_schema_new ();
82 
83   dest->type = real->type;
84   dest->list_type = real->list_type;
85   dest->car_type = real->car_type;
86   dest->cdr_type = real->cdr_type;
87 
88   dest->locale = g_strdup (real->locale);
89 
90   dest->short_desc = g_strdup (real->short_desc);
91 
92   dest->long_desc = g_strdup (real->long_desc);
93 
94   dest->owner = g_strdup (real->owner);
95 
96   dest->default_value = real->default_value ? gconf_value_copy (real->default_value) : NULL;
97 
98   return (GConfSchema*) dest;
99 }
100 
101 void
gconf_schema_set_type(GConfSchema * sc,GConfValueType type)102 gconf_schema_set_type (GConfSchema* sc, GConfValueType type)
103 {
104   REAL_SCHEMA (sc)->type = type;
105 }
106 
107 void
gconf_schema_set_list_type(GConfSchema * sc,GConfValueType type)108 gconf_schema_set_list_type (GConfSchema* sc, GConfValueType type)
109 {
110   REAL_SCHEMA (sc)->list_type = type;
111 }
112 
113 void
gconf_schema_set_car_type(GConfSchema * sc,GConfValueType type)114 gconf_schema_set_car_type (GConfSchema* sc, GConfValueType type)
115 {
116   REAL_SCHEMA (sc)->car_type = type;
117 }
118 
119 void
gconf_schema_set_cdr_type(GConfSchema * sc,GConfValueType type)120 gconf_schema_set_cdr_type (GConfSchema* sc, GConfValueType type)
121 {
122   REAL_SCHEMA (sc)->cdr_type = type;
123 }
124 
125 void
gconf_schema_set_locale(GConfSchema * sc,const gchar * locale)126 gconf_schema_set_locale (GConfSchema* sc, const gchar* locale)
127 {
128   g_return_if_fail (locale == NULL || g_utf8_validate (locale, -1, NULL));
129 
130   if (REAL_SCHEMA (sc)->locale)
131     g_free (REAL_SCHEMA (sc)->locale);
132 
133   if (locale)
134     REAL_SCHEMA (sc)->locale = g_strdup (locale);
135   else
136     REAL_SCHEMA (sc)->locale = NULL;
137 }
138 
139 void
gconf_schema_set_short_desc(GConfSchema * sc,const gchar * desc)140 gconf_schema_set_short_desc (GConfSchema* sc, const gchar* desc)
141 {
142   g_return_if_fail (desc == NULL || g_utf8_validate (desc, -1, NULL));
143 
144   if (REAL_SCHEMA (sc)->short_desc)
145     g_free (REAL_SCHEMA (sc)->short_desc);
146 
147   if (desc)
148     REAL_SCHEMA (sc)->short_desc = g_strdup (desc);
149   else
150     REAL_SCHEMA (sc)->short_desc = NULL;
151 }
152 
153 void
gconf_schema_set_long_desc(GConfSchema * sc,const gchar * desc)154 gconf_schema_set_long_desc (GConfSchema* sc, const gchar* desc)
155 {
156   g_return_if_fail (desc == NULL || g_utf8_validate (desc, -1, NULL));
157 
158   if (REAL_SCHEMA (sc)->long_desc)
159     g_free (REAL_SCHEMA (sc)->long_desc);
160 
161   if (desc)
162     REAL_SCHEMA (sc)->long_desc = g_strdup (desc);
163   else
164     REAL_SCHEMA (sc)->long_desc = NULL;
165 }
166 
167 void
gconf_schema_set_owner(GConfSchema * sc,const gchar * owner)168 gconf_schema_set_owner (GConfSchema* sc, const gchar* owner)
169 {
170   g_return_if_fail (owner == NULL || g_utf8_validate (owner, -1, NULL));
171 
172   if (REAL_SCHEMA (sc)->owner)
173     g_free (REAL_SCHEMA (sc)->owner);
174 
175   if (owner)
176     REAL_SCHEMA (sc)->owner = g_strdup (owner);
177   else
178     REAL_SCHEMA (sc)->owner = NULL;
179 }
180 
181 void
gconf_schema_set_default_value(GConfSchema * sc,const GConfValue * val)182 gconf_schema_set_default_value (GConfSchema* sc, const GConfValue* val)
183 {
184   if (REAL_SCHEMA (sc)->default_value != NULL)
185     gconf_value_free (REAL_SCHEMA (sc)->default_value);
186 
187   REAL_SCHEMA (sc)->default_value = gconf_value_copy (val);
188 }
189 
190 void
gconf_schema_set_default_value_nocopy(GConfSchema * sc,GConfValue * val)191 gconf_schema_set_default_value_nocopy (GConfSchema* sc, GConfValue* val)
192 {
193   if (REAL_SCHEMA (sc)->default_value != NULL)
194     gconf_value_free (REAL_SCHEMA (sc)->default_value);
195 
196   REAL_SCHEMA (sc)->default_value = val;
197 }
198 
199 gboolean
gconf_schema_validate(const GConfSchema * sc,GError ** err)200 gconf_schema_validate (const GConfSchema *sc,
201                        GError           **err)
202 {
203   GConfRealSchema *real;
204 
205   real = REAL_SCHEMA (sc);
206 
207   if (real->locale && !g_utf8_validate (real->locale, -1, NULL))
208     {
209       g_set_error (err, GCONF_ERROR,
210                    GCONF_ERROR_FAILED,
211                    _("Schema contains invalid UTF-8"));
212       return FALSE;
213     }
214 
215   if (real->short_desc && !g_utf8_validate (real->short_desc, -1, NULL))
216     {
217       g_set_error (err, GCONF_ERROR,
218                    GCONF_ERROR_FAILED,
219                    _("Schema contains invalid UTF-8"));
220       return FALSE;
221     }
222 
223   if (real->long_desc && !g_utf8_validate (real->long_desc, -1, NULL))
224     {
225       g_set_error (err, GCONF_ERROR,
226                    GCONF_ERROR_FAILED,
227                    _("Schema contains invalid UTF-8"));
228       return FALSE;
229     }
230 
231   if (real->owner && !g_utf8_validate (real->owner, -1, NULL))
232     {
233       g_set_error (err, GCONF_ERROR,
234                    GCONF_ERROR_FAILED,
235                    _("Schema contains invalid UTF-8"));
236       return FALSE;
237     }
238 
239   if (real->type == GCONF_VALUE_LIST &&
240       real->list_type == GCONF_VALUE_INVALID)
241     {
242       g_set_error (err, GCONF_ERROR,
243                    GCONF_ERROR_FAILED,
244                    _("Schema specifies type list but doesn't specify the type of the list elements"));
245       return FALSE;
246     }
247 
248   if (real->type == GCONF_VALUE_PAIR &&
249       (real->car_type == GCONF_VALUE_INVALID ||
250        real->cdr_type == GCONF_VALUE_INVALID))
251     {
252       g_set_error (err, GCONF_ERROR,
253                    GCONF_ERROR_FAILED,
254                    _("Schema specifies type pair but doesn't specify the type of the car/cdr elements"));
255       return FALSE;
256     }
257 
258   return TRUE;
259 }
260 
261 GConfValueType
gconf_schema_get_type(const GConfSchema * schema)262 gconf_schema_get_type (const GConfSchema *schema)
263 {
264   g_return_val_if_fail (schema != NULL, GCONF_VALUE_INVALID);
265 
266   return REAL_SCHEMA (schema)->type;
267 }
268 
269 GConfValueType
gconf_schema_get_list_type(const GConfSchema * schema)270 gconf_schema_get_list_type (const GConfSchema *schema)
271 {
272   g_return_val_if_fail (schema != NULL, GCONF_VALUE_INVALID);
273 
274   return REAL_SCHEMA (schema)->list_type;
275 }
276 
277 GConfValueType
gconf_schema_get_car_type(const GConfSchema * schema)278 gconf_schema_get_car_type (const GConfSchema *schema)
279 {
280   g_return_val_if_fail (schema != NULL, GCONF_VALUE_INVALID);
281 
282   return REAL_SCHEMA (schema)->car_type;
283 }
284 
285 GConfValueType
gconf_schema_get_cdr_type(const GConfSchema * schema)286 gconf_schema_get_cdr_type (const GConfSchema *schema)
287 {
288   g_return_val_if_fail (schema != NULL, GCONF_VALUE_INVALID);
289 
290   return REAL_SCHEMA (schema)->cdr_type;
291 }
292 
293 const char*
gconf_schema_get_locale(const GConfSchema * schema)294 gconf_schema_get_locale (const GConfSchema *schema)
295 {
296   g_return_val_if_fail (schema != NULL, NULL);
297 
298   return REAL_SCHEMA (schema)->locale;
299 }
300 
301 const char*
gconf_schema_get_short_desc(const GConfSchema * schema)302 gconf_schema_get_short_desc (const GConfSchema *schema)
303 {
304   g_return_val_if_fail (schema != NULL, NULL);
305 
306   return REAL_SCHEMA (schema)->short_desc;
307 }
308 
309 const char*
gconf_schema_get_long_desc(const GConfSchema * schema)310 gconf_schema_get_long_desc (const GConfSchema *schema)
311 {
312   g_return_val_if_fail (schema != NULL, NULL);
313 
314   return REAL_SCHEMA (schema)->long_desc;
315 }
316 
317 const char*
gconf_schema_get_owner(const GConfSchema * schema)318 gconf_schema_get_owner (const GConfSchema *schema)
319 {
320   g_return_val_if_fail (schema != NULL, NULL);
321 
322   return REAL_SCHEMA (schema)->owner;
323 }
324 
325 GConfValue*
gconf_schema_get_default_value(const GConfSchema * schema)326 gconf_schema_get_default_value (const GConfSchema *schema)
327 {
328   g_return_val_if_fail (schema != NULL, NULL);
329 
330   return REAL_SCHEMA (schema)->default_value;
331 }
332 
333 GConfValue*
gconf_schema_steal_default_value(GConfSchema * schema)334 gconf_schema_steal_default_value (GConfSchema *schema)
335 {
336   GConfValue *val;
337 
338   g_return_val_if_fail (schema != NULL, NULL);
339 
340   val = REAL_SCHEMA (schema)->default_value;
341   REAL_SCHEMA (schema)->default_value = NULL;
342 
343   return val;
344 }
345