[gnome-builder] libide/core: add settings path resolver helper



commit f2ea8bfc1385010d2f38410da62fd0da0ce8511a
Author: Christian Hergert <chergert redhat com>
Date:   Wed Aug 24 01:56:17 2022 -0700

    libide/core: add settings path resolver helper
    
    This is to be used to help us in tweaks get a dynamic path for something
    that could be either pathless or pathful and possibly rerouted based on
    the project-id (or absense thereof).

 src/libide/core/ide-settings.c | 48 ++++++++++++++++++++++++++++++++++++++++++
 src/libide/core/ide-settings.h |  4 ++++
 2 files changed, 52 insertions(+)
---
diff --git a/src/libide/core/ide-settings.c b/src/libide/core/ide-settings.c
index 9e8cf836e..56323fc24 100644
--- a/src/libide/core/ide-settings.c
+++ b/src/libide/core/ide-settings.c
@@ -116,6 +116,54 @@ ide_settings_layered_settings_changed_cb (IdeSettings        *self,
   g_action_group_action_state_changed (G_ACTION_GROUP (self), key, value);
 }
 
+char *
+ide_settings_resolve_schema_path (const char *schema_id,
+                                  const char *project_id,
+                                  const char *path_suffix)
+{
+  g_autoptr(GSettingsSchema) schema = NULL;
+  GSettingsSchemaSource *source;
+  const char *schema_path;
+
+  g_return_val_if_fail (schema_id != NULL, NULL);
+  g_return_val_if_fail (!path_suffix || g_str_has_suffix (path_suffix, "/"), NULL);
+
+  source = g_settings_schema_source_get_default ();
+
+  if (!(schema = g_settings_schema_source_lookup (source, schema_id, TRUE)))
+    {
+      g_critical ("Failed to locate schema %s", schema_id);
+      return NULL;
+    }
+
+  if ((schema_path = g_settings_schema_get_path (schema)))
+    {
+      if (project_id != NULL)
+        g_critical ("Attempt to resolve non-relocatable schema %s with project-id %s",
+                    schema_id, project_id);
+      return g_strdup (schema_path);
+    }
+
+  if (!g_str_has_prefix (schema_id, "org.gnome.builder."))
+    {
+      g_critical ("Relocatable schemas must be prefixed with org.gnome.builder.");
+      return NULL;
+    }
+
+  if (project_id == NULL)
+    {
+      g_autofree char *escaped = g_strdelimit (g_strdup (schema_id), ".", '/');
+      return g_strconcat ("/", escaped, "/", NULL);
+    }
+  else
+    {
+      const char *suffix = schema_id + strlen ("org.gnome.builder.");
+      g_autofree char *escaped = g_strdelimit (g_strdup (suffix), ".", '/');
+
+      return g_strconcat ("/org/gnome/builder/projects/", project_id, "/", escaped, "/", path_suffix, NULL);
+    }
+}
+
 static void
 ide_settings_constructed (GObject *object)
 {
diff --git a/src/libide/core/ide-settings.h b/src/libide/core/ide-settings.h
index 1595e4534..b3c07748d 100644
--- a/src/libide/core/ide-settings.h
+++ b/src/libide/core/ide-settings.h
@@ -35,6 +35,10 @@ G_BEGIN_DECLS
 IDE_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (IdeSettings, ide_settings, IDE, SETTINGS, GObject)
 
+IDE_AVAILABLE_IN_ALL
+char        *ide_settings_resolve_schema_path         (const char              *schema_id,
+                                                       const char              *project_id,
+                                                       const char              *path_suffix);
 IDE_AVAILABLE_IN_ALL
 IdeSettings *ide_settings_new                         (const char              *project_id,
                                                        const char              *schema_id);


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