2 * Copyright © 2010 Novell, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Vincent Untz <vuntz@gnome.org>
24 #include "gsettings-mapping.h"
27 g_settings_set_mapping_int (const GValue *value,
28 const GVariantType *expected_type)
30 GVariant *variant = NULL;
33 if (G_VALUE_HOLDS_INT (value))
34 l = g_value_get_int (value);
35 else if (G_VALUE_HOLDS_INT64 (value))
36 l = g_value_get_int64 (value);
40 if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT16))
42 if (G_MININT16 <= l && l <= G_MAXINT16)
43 variant = g_variant_new_int16 ((gint16) l);
45 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT16))
47 if (0 <= l && l <= G_MAXUINT16)
48 variant = g_variant_new_uint16 ((guint16) l);
50 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT32))
52 if (G_MININT32 <= l && l <= G_MAXINT32)
53 variant = g_variant_new_int32 ((gint) l);
55 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT32))
57 if (0 <= l && l <= G_MAXUINT32)
58 variant = g_variant_new_uint32 ((guint) l);
60 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT64))
62 if (G_MININT64 <= l && l <= G_MAXINT64)
63 variant = g_variant_new_int64 ((gint64) l);
65 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT64))
67 if (0 <= l && (guint64) l <= G_MAXUINT64)
68 variant = g_variant_new_uint64 ((guint64) l);
70 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_HANDLE))
72 if (0 <= l && l <= G_MAXUINT32)
73 variant = g_variant_new_handle ((guint) l);
75 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_DOUBLE))
76 variant = g_variant_new_double ((gdouble) l);
82 g_settings_set_mapping_float (const GValue *value,
83 const GVariantType *expected_type)
85 GVariant *variant = NULL;
89 if (G_VALUE_HOLDS_DOUBLE (value))
90 d = g_value_get_double (value);
95 if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT16))
97 if (G_MININT16 <= l && l <= G_MAXINT16)
98 variant = g_variant_new_int16 ((gint16) l);
100 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT16))
102 if (0 <= l && l <= G_MAXUINT16)
103 variant = g_variant_new_uint16 ((guint16) l);
105 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT32))
107 if (G_MININT32 <= l && l <= G_MAXINT32)
108 variant = g_variant_new_int32 ((gint) l);
110 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT32))
112 if (0 <= l && l <= G_MAXUINT32)
113 variant = g_variant_new_uint32 ((guint) l);
115 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT64))
117 if (G_MININT64 <= l && l <= G_MAXINT64)
118 variant = g_variant_new_int64 ((gint64) l);
120 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT64))
122 if (0 <= l && (guint64) l <= G_MAXUINT64)
123 variant = g_variant_new_uint64 ((guint64) l);
125 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_HANDLE))
127 if (0 <= l && l <= G_MAXUINT32)
128 variant = g_variant_new_handle ((guint) l);
130 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_DOUBLE))
131 variant = g_variant_new_double ((gdouble) d);
136 g_settings_set_mapping_unsigned_int (const GValue *value,
137 const GVariantType *expected_type)
139 GVariant *variant = NULL;
142 if (G_VALUE_HOLDS_UINT (value))
143 u = g_value_get_uint (value);
144 else if (G_VALUE_HOLDS_UINT64 (value))
145 u = g_value_get_uint64 (value);
149 if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT16))
152 variant = g_variant_new_int16 ((gint16) u);
154 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT16))
156 if (u <= G_MAXUINT16)
157 variant = g_variant_new_uint16 ((guint16) u);
159 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT32))
162 variant = g_variant_new_int32 ((gint) u);
164 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT32))
166 if (u <= G_MAXUINT32)
167 variant = g_variant_new_uint32 ((guint) u);
169 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT64))
172 variant = g_variant_new_int64 ((gint64) u);
174 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT64))
176 if (u <= G_MAXUINT64)
177 variant = g_variant_new_uint64 ((guint64) u);
179 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_HANDLE))
181 if (u <= G_MAXUINT32)
182 variant = g_variant_new_handle ((guint) u);
184 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_DOUBLE))
185 variant = g_variant_new_double ((gdouble) u);
191 g_settings_get_mapping_int (GValue *value,
194 const GVariantType *type;
197 type = g_variant_get_type (variant);
199 if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
200 l = g_variant_get_int16 (variant);
201 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
202 l = g_variant_get_int32 (variant);
203 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
204 l = g_variant_get_int64 (variant);
205 else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
206 l = g_variant_get_handle (variant);
210 if (G_VALUE_HOLDS_INT (value))
212 g_value_set_int (value, l);
213 return (G_MININT32 <= l && l <= G_MAXINT32);
215 else if (G_VALUE_HOLDS_UINT (value))
217 g_value_set_uint (value, l);
218 return (0 <= l && l <= G_MAXUINT32);
220 else if (G_VALUE_HOLDS_INT64 (value))
222 g_value_set_int64 (value, l);
223 return (G_MININT64 <= l && l <= G_MAXINT64);
225 else if (G_VALUE_HOLDS_UINT64 (value))
227 g_value_set_uint64 (value, l);
228 return (0 <= l && (guint64) l <= G_MAXUINT64);
230 else if (G_VALUE_HOLDS_DOUBLE (value))
232 g_value_set_double (value, l);
240 g_settings_get_mapping_float (GValue *value,
243 const GVariantType *type;
247 type = g_variant_get_type (variant);
249 if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
250 d = g_variant_get_double (variant);
255 if (G_VALUE_HOLDS_INT (value))
257 g_value_set_int (value, l);
258 return (G_MININT32 <= l && l <= G_MAXINT32);
260 else if (G_VALUE_HOLDS_UINT (value))
262 g_value_set_uint (value, l);
263 return (0 <= l && l <= G_MAXUINT32);
265 else if (G_VALUE_HOLDS_INT64 (value))
267 g_value_set_int64 (value, l);
268 return (G_MININT64 <= l && l <= G_MAXINT64);
270 else if (G_VALUE_HOLDS_UINT64 (value))
272 g_value_set_uint64 (value, l);
273 return (0 <= l && (guint64) l <= G_MAXUINT64);
275 else if (G_VALUE_HOLDS_DOUBLE (value))
277 g_value_set_double (value, d);
284 g_settings_get_mapping_unsigned_int (GValue *value,
287 const GVariantType *type;
290 type = g_variant_get_type (variant);
292 if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
293 u = g_variant_get_uint16 (variant);
294 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
295 u = g_variant_get_uint32 (variant);
296 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
297 u = g_variant_get_uint64 (variant);
301 if (G_VALUE_HOLDS_INT (value))
303 g_value_set_int (value, u);
304 return (u <= G_MAXINT32);
306 else if (G_VALUE_HOLDS_UINT (value))
308 g_value_set_uint (value, u);
309 return (u <= G_MAXUINT32);
311 else if (G_VALUE_HOLDS_INT64 (value))
313 g_value_set_int64 (value, u);
314 return (u <= G_MAXINT64);
316 else if (G_VALUE_HOLDS_UINT64 (value))
318 g_value_set_uint64 (value, u);
319 return (u <= G_MAXUINT64);
321 else if (G_VALUE_HOLDS_DOUBLE (value))
323 g_value_set_double (value, u);
331 g_settings_set_mapping (const GValue *value,
332 const GVariantType *expected_type,
337 if (G_VALUE_HOLDS_BOOLEAN (value))
339 if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_BOOLEAN))
340 return g_variant_new_boolean (g_value_get_boolean (value));
343 else if (G_VALUE_HOLDS_CHAR (value) ||
344 G_VALUE_HOLDS_UCHAR (value))
346 if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_BYTE))
348 if (G_VALUE_HOLDS_CHAR (value))
349 return g_variant_new_byte (g_value_get_schar (value));
351 return g_variant_new_byte (g_value_get_uchar (value));
355 else if (G_VALUE_HOLDS_INT (value) ||
356 G_VALUE_HOLDS_INT64 (value))
357 return g_settings_set_mapping_int (value, expected_type);
359 else if (G_VALUE_HOLDS_DOUBLE (value))
360 return g_settings_set_mapping_float (value, expected_type);
362 else if (G_VALUE_HOLDS_UINT (value) ||
363 G_VALUE_HOLDS_UINT64 (value))
364 return g_settings_set_mapping_unsigned_int (value, expected_type);
366 else if (G_VALUE_HOLDS_STRING (value))
368 if (g_value_get_string (value) == NULL)
370 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_STRING))
371 return g_variant_new_string (g_value_get_string (value));
372 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_BYTESTRING))
373 return g_variant_new_bytestring (g_value_get_string (value));
374 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_OBJECT_PATH))
375 return g_variant_new_object_path (g_value_get_string (value));
376 else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_SIGNATURE))
377 return g_variant_new_signature (g_value_get_string (value));
380 else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
382 if (g_value_get_boxed (value) == NULL)
384 return g_variant_new_strv ((const gchar **) g_value_get_boxed (value),
388 else if (G_VALUE_HOLDS_ENUM (value))
393 /* GParamSpecEnum holds a ref on the class so we just peek... */
394 eclass = g_type_class_peek (G_VALUE_TYPE (value));
395 enumval = g_enum_get_value (eclass, g_value_get_enum (value));
398 return g_variant_new_string (enumval->value_nick);
403 else if (G_VALUE_HOLDS_FLAGS (value))
405 GVariantBuilder builder;
406 GFlagsValue *flagsval;
410 fclass = g_type_class_peek (G_VALUE_TYPE (value));
411 flags = g_value_get_flags (value);
413 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
416 flagsval = g_flags_get_first_value (fclass, flags);
418 if (flagsval == NULL)
420 g_variant_builder_clear (&builder);
424 g_variant_builder_add (&builder, "s", flagsval->value_nick);
425 flags &= ~flagsval->value;
428 return g_variant_builder_end (&builder);
431 type_string = g_variant_type_dup_string (expected_type);
432 g_critical ("No GSettings bind handler for type \"%s\".", type_string);
433 g_free (type_string);
439 g_settings_get_mapping (GValue *value,
443 if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BOOLEAN))
445 if (!G_VALUE_HOLDS_BOOLEAN (value))
447 g_value_set_boolean (value, g_variant_get_boolean (variant));
451 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BYTE))
453 if (G_VALUE_HOLDS_UCHAR (value))
454 g_value_set_uchar (value, g_variant_get_byte (variant));
455 else if (G_VALUE_HOLDS_CHAR (value))
456 g_value_set_schar (value, (gint8)g_variant_get_byte (variant));
462 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT16) ||
463 g_variant_is_of_type (variant, G_VARIANT_TYPE_INT32) ||
464 g_variant_is_of_type (variant, G_VARIANT_TYPE_INT64) ||
465 g_variant_is_of_type (variant, G_VARIANT_TYPE_HANDLE))
466 return g_settings_get_mapping_int (value, variant);
468 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_DOUBLE))
469 return g_settings_get_mapping_float (value, variant);
471 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT16) ||
472 g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT32) ||
473 g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT64))
474 return g_settings_get_mapping_unsigned_int (value, variant);
476 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING) ||
477 g_variant_is_of_type (variant, G_VARIANT_TYPE_OBJECT_PATH) ||
478 g_variant_is_of_type (variant, G_VARIANT_TYPE_SIGNATURE))
480 if (G_VALUE_HOLDS_STRING (value))
482 g_value_set_string (value, g_variant_get_string (variant, NULL));
486 else if (G_VALUE_HOLDS_ENUM (value))
492 /* GParamSpecEnum holds a ref on the class so we just peek... */
493 eclass = g_type_class_peek (G_VALUE_TYPE (value));
494 nick = g_variant_get_string (variant, NULL);
495 evalue = g_enum_get_value_by_nick (eclass, nick);
499 g_value_set_enum (value, evalue->value);
503 g_warning ("Unable to look up enum nick ‘%s’ via GType", nick);
507 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE ("as")))
509 if (G_VALUE_HOLDS (value, G_TYPE_STRV))
511 g_value_take_boxed (value, g_variant_dup_strv (variant, NULL));
515 else if (G_VALUE_HOLDS_FLAGS (value))
523 fclass = g_type_class_peek (G_VALUE_TYPE (value));
525 g_variant_iter_init (&iter, variant);
526 while (g_variant_iter_next (&iter, "&s", &nick))
528 fvalue = g_flags_get_value_by_nick (fclass, nick);
531 flags |= fvalue->value;
535 g_warning ("Unable to lookup flags nick '%s' via GType",
541 g_value_set_flags (value, flags);
545 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BYTESTRING))
547 g_value_set_string (value, g_variant_get_bytestring (variant));
551 g_critical ("No GSettings bind handler for type \"%s\".",
552 g_variant_get_type_string (variant));
558 g_settings_mapping_is_compatible (GType gvalue_type,
559 const GVariantType *variant_type)
563 if (gvalue_type == G_TYPE_BOOLEAN)
564 ok = g_variant_type_equal (variant_type, G_VARIANT_TYPE_BOOLEAN);
565 else if (gvalue_type == G_TYPE_CHAR ||
566 gvalue_type == G_TYPE_UCHAR)
567 ok = g_variant_type_equal (variant_type, G_VARIANT_TYPE_BYTE);
568 else if (gvalue_type == G_TYPE_INT ||
569 gvalue_type == G_TYPE_UINT ||
570 gvalue_type == G_TYPE_INT64 ||
571 gvalue_type == G_TYPE_UINT64 ||
572 gvalue_type == G_TYPE_DOUBLE)
573 ok = (g_variant_type_equal (variant_type, G_VARIANT_TYPE_INT16) ||
574 g_variant_type_equal (variant_type, G_VARIANT_TYPE_UINT16) ||
575 g_variant_type_equal (variant_type, G_VARIANT_TYPE_INT32) ||
576 g_variant_type_equal (variant_type, G_VARIANT_TYPE_UINT32) ||
577 g_variant_type_equal (variant_type, G_VARIANT_TYPE_INT64) ||
578 g_variant_type_equal (variant_type, G_VARIANT_TYPE_UINT64) ||
579 g_variant_type_equal (variant_type, G_VARIANT_TYPE_HANDLE) ||
580 g_variant_type_equal (variant_type, G_VARIANT_TYPE_DOUBLE));
581 else if (gvalue_type == G_TYPE_STRING)
582 ok = (g_variant_type_equal (variant_type, G_VARIANT_TYPE_STRING) ||
583 g_variant_type_equal (variant_type, G_VARIANT_TYPE ("ay")) ||
584 g_variant_type_equal (variant_type, G_VARIANT_TYPE_OBJECT_PATH) ||
585 g_variant_type_equal (variant_type, G_VARIANT_TYPE_SIGNATURE));
586 else if (gvalue_type == G_TYPE_STRV)
587 ok = g_variant_type_equal (variant_type, G_VARIANT_TYPE ("as"));
588 else if (G_TYPE_IS_ENUM (gvalue_type))
589 ok = g_variant_type_equal (variant_type, G_VARIANT_TYPE_STRING);
590 else if (G_TYPE_IS_FLAGS (gvalue_type))
591 ok = g_variant_type_equal (variant_type, G_VARIANT_TYPE ("as"));