[gnome-builder] libide/tweaks: add variant type to/from gtype mapper



commit 9f8493f2f258f10addb9c6a5d3f3436d40252b41
Author: Christian Hergert <chergert redhat com>
Date:   Wed Aug 24 21:49:07 2022 -0700

    libide/tweaks: add variant type to/from gtype mapper
    
    We don't need to store everything, just enough types to be able to do our
    transforms from variant encoding in the Builder XML. What we really need
    is a way to specify type="" with <property/> so that we can control the
    type as it's decoded (even if it gets transformed to something else).

 src/libide/tweaks/ide-tweaks-setting.c | 29 +++---------
 src/libide/tweaks/ide-tweaks-variant.c | 81 ++++++++++++++++++++++++++++++++++
 src/libide/tweaks/ide-tweaks-variant.h | 30 +++++++++++++
 src/libide/tweaks/meson.build          |  2 +
 4 files changed, 119 insertions(+), 23 deletions(-)
---
diff --git a/src/libide/tweaks/ide-tweaks-setting.c b/src/libide/tweaks/ide-tweaks-setting.c
index 9e786014f..05b9334a1 100644
--- a/src/libide/tweaks/ide-tweaks-setting.c
+++ b/src/libide/tweaks/ide-tweaks-setting.c
@@ -24,6 +24,7 @@
 
 #include "ide-tweaks.h"
 #include "ide-tweaks-setting.h"
+#include "ide-tweaks-variant.h"
 
 #include "gsettings-mapping.h"
 
@@ -187,32 +188,14 @@ static GType
 ide_tweaks_setting_get_expected_type (IdeTweaksBinding *binding)
 {
   IdeTweaksSetting *self = IDE_TWEAKS_SETTING (binding);
+  g_autoptr(GSettings) settings = NULL;
+  const GVariantType *expected_type = NULL;
+  const char *key = NULL;
 
-  if (self->expected_type == NULL)
+  if (!(settings = ide_tweaks_setting_acquire (self, &key, &expected_type)))
     return G_TYPE_INVALID;
 
-#define MAP_VARIANT_TYPE_TO_GTYPE(variant_type, gtype)            \
-  G_STMT_START {                                                  \
-    if (g_variant_type_equal (self->expected_type, variant_type)) \
-      return gtype;                                               \
-  } G_STMT_END
-
-  /* Just the basics really for GSettings */
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_BYTE, G_TYPE_UCHAR);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_BYTESTRING, G_TYPE_STRING);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_DOUBLE, G_TYPE_DOUBLE);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_INT32, G_TYPE_INT);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_INT64, G_TYPE_INT64);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_STRING, G_TYPE_STRING);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_STRING_ARRAY, G_TYPE_STRV);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_UINT32, G_TYPE_UINT);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_UINT64, G_TYPE_UINT64);
-  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_VARIANT, G_TYPE_VARIANT);
-
-#undef MAP_VARIANT_TYPE_TO_GTYPE
-
-  return G_TYPE_INVALID;
+  return _ide_tweaks_variant_type_to_gtype (expected_type);
 }
 
 static void
diff --git a/src/libide/tweaks/ide-tweaks-variant.c b/src/libide/tweaks/ide-tweaks-variant.c
new file mode 100644
index 000000000..5b147a439
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-variant.c
@@ -0,0 +1,81 @@
+/* ide-tweaks-variant.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ide-tweaks-variant"
+
+#include "config.h"
+
+#include "ide-tweaks-variant.h"
+
+GType
+_ide_tweaks_variant_type_to_gtype (const GVariantType *variant_type)
+{
+  if (variant_type == NULL)
+    return G_TYPE_INVALID;
+
+#define MAP_VARIANT_TYPE_TO_GTYPE(vtype, gtype)     \
+  G_STMT_START {                                    \
+    if (g_variant_type_equal (variant_type, vtype)) \
+      return gtype;                                 \
+  } G_STMT_END
+
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_BYTE, G_TYPE_UCHAR);
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_DOUBLE, G_TYPE_DOUBLE);
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_INT32, G_TYPE_INT);
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_INT64, G_TYPE_INT64);
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_STRING, G_TYPE_STRING);
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_STRING_ARRAY, G_TYPE_STRV);
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_UINT32, G_TYPE_UINT);
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_UINT64, G_TYPE_UINT64);
+  MAP_VARIANT_TYPE_TO_GTYPE (G_VARIANT_TYPE_VARIANT, G_TYPE_VARIANT);
+
+#undef MAP_VARIANT_TYPE_TO_GTYPE
+
+  return G_TYPE_INVALID;
+}
+
+const GVariantType *
+_ide_tweaks_gtype_to_variant_type (GType type)
+{
+  if (type == G_TYPE_INVALID)
+    return NULL;
+
+#define MAP_GTYPE_TO_VARIANT_TYPE(vtype, gtype) \
+  G_STMT_START {                                \
+    if (g_type_is_a (type, gtype))              \
+      return vtype;                             \
+  } G_STMT_END
+
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_BYTE, G_TYPE_UCHAR);
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_DOUBLE, G_TYPE_DOUBLE);
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_INT32, G_TYPE_INT);
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_INT64, G_TYPE_INT64);
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_STRING, G_TYPE_STRING);
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_STRING_ARRAY, G_TYPE_STRV);
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_UINT32, G_TYPE_UINT);
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_UINT64, G_TYPE_UINT64);
+  MAP_GTYPE_TO_VARIANT_TYPE (G_VARIANT_TYPE_VARIANT, G_TYPE_VARIANT);
+
+#undef MAP_GTYPE_TO_VARIANT_TYPE
+
+  return NULL;
+}
diff --git a/src/libide/tweaks/ide-tweaks-variant.h b/src/libide/tweaks/ide-tweaks-variant.h
new file mode 100644
index 000000000..7b17ba00e
--- /dev/null
+++ b/src/libide/tweaks/ide-tweaks-variant.h
@@ -0,0 +1,30 @@
+/* ide-tweaks-variant.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+GType               _ide_tweaks_variant_type_to_gtype (const GVariantType *variant_type);
+const GVariantType *_ide_tweaks_gtype_to_variant_type (GType               type);
+
+G_END_DECLS
diff --git a/src/libide/tweaks/meson.build b/src/libide/tweaks/meson.build
index d6b3130c3..392199d78 100644
--- a/src/libide/tweaks/meson.build
+++ b/src/libide/tweaks/meson.build
@@ -29,6 +29,7 @@ libide_tweaks_public_headers = [
   'ide-tweaks-settings.h',
   'ide-tweaks-spin.h',
   'ide-tweaks-switch.h',
+  'ide-tweaks-variant.h',
   'ide-tweaks-widget.h',
   'ide-tweaks-window.h',
 ]
@@ -61,6 +62,7 @@ libide_tweaks_public_sources = [
   'ide-tweaks-settings.c',
   'ide-tweaks-spin.c',
   'ide-tweaks-switch.c',
+  'ide-tweaks-variant.c',
   'ide-tweaks-widget.c',
   'ide-tweaks-window.c',
 ]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]