[gnome-builder] settings: install some reasonable defaults for common languages



commit 2df40563acc70e9a8953e842143cb5ef31c054de
Author: Christian Hergert <christian hergert me>
Date:   Wed Oct 15 14:09:33 2014 -0700

    settings: install some reasonable defaults for common languages
    
    If ~/.local/share/gnome-builder/.defaults-installed is not found, we
    will set some reasonable defaults for C, Python, Ruby, JavaScript, XML,
    etc.
    
    We will likely iterate on these defaults a bit. Especially with how they
    will tie into auto-indentation in the future.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=738565

 src/app/gb-application.c                  |   91 +++++++++++++++++++++++++++++
 src/resources/gnome-builder.gresource.xml |    2 +
 src/resources/language/defaults.ini       |   33 ++++++++++
 3 files changed, 126 insertions(+), 0 deletions(-)
---
diff --git a/src/app/gb-application.c b/src/app/gb-application.c
index eb04a56..cde5bed 100644
--- a/src/app/gb-application.c
+++ b/src/app/gb-application.c
@@ -34,9 +34,99 @@
 #include "gb-resources.h"
 #include "gb-workbench.h"
 
+#define LANGUAGE_SCHEMA "org.gnome.builder.editor.language"
+#define LANGUAGE_PATH "/org/gnome/builder/editor/language/"
+
 G_DEFINE_TYPE (GbApplication, gb_application, GTK_TYPE_APPLICATION)
 
 static void
+gb_application_install_language_defaults (GbApplication *self)
+{
+  gchar *defaults_installed_path;
+  gboolean exists;
+
+  g_return_if_fail (GB_IS_APPLICATION (self));
+
+  defaults_installed_path = g_build_filename (g_get_user_data_dir (),
+                                              "gnome-builder",
+                                              ".defaults-installed",
+                                              NULL);
+  exists = g_file_test (defaults_installed_path, G_FILE_TEST_EXISTS);
+
+  if (!exists)
+    {
+      GKeyFile *key_file;
+      GBytes *bytes;
+
+      key_file = g_key_file_new ();
+
+      bytes =
+        g_resources_lookup_data ("/org/gnome/builder/language/defaults.ini",
+                                 0, NULL);
+
+      if (bytes)
+        {
+          if (g_key_file_load_from_data (key_file,
+                                         g_bytes_get_data (bytes, NULL),
+                                         g_bytes_get_size (bytes),
+                                         0, NULL))
+            {
+              gchar **groups;
+              guint i;
+
+              groups = g_key_file_get_groups (key_file, NULL);
+
+              for (i = 0; groups [i]; i++)
+                {
+                  GSettings *settings;
+                  gchar *settings_path;
+                  gchar **keys;
+                  guint j;
+
+                  settings_path = g_strdup_printf (
+                      "/org/gnome/builder/editor/language/%s/", groups [i]);
+                  settings = g_settings_new_with_path (
+                      "org.gnome.builder.editor.language",
+                      settings_path);
+                  g_free (settings_path);
+
+                  keys = g_key_file_get_keys (key_file, groups [i], NULL, NULL);
+
+                  for (j = 0; keys [j]; j++)
+                    {
+                      GVariant *param;
+                      gchar *value;
+
+                      value = g_key_file_get_value (key_file, groups [i],
+                                                    keys [j], NULL);
+                      param = g_variant_parse (NULL, value, NULL, NULL, NULL);
+
+                      if (param)
+                        {
+                          g_settings_set_value (settings, keys [j], param);
+                          g_variant_unref (param);
+                        }
+
+                      g_free (value);
+                    }
+
+                  g_object_unref (settings);
+                }
+
+              g_strfreev (groups);
+            }
+
+          g_bytes_unref (bytes);
+        }
+
+      g_key_file_free (key_file);
+      g_file_set_contents (defaults_installed_path, "", 0, NULL);
+    }
+
+  g_free (defaults_installed_path);
+}
+
+static void
 gb_application_make_skeleton_dirs (GbApplication *self)
 {
   gchar *path;
@@ -416,6 +506,7 @@ gb_application_startup (GApplication *app)
   G_APPLICATION_CLASS (gb_application_parent_class)->startup (app);
 
   gb_application_make_skeleton_dirs (self);
+  gb_application_install_language_defaults (self);
   gb_application_register_actions (self);
   gb_application_register_keybindings (self);
   gb_application_register_theme_overrides (self);
diff --git a/src/resources/gnome-builder.gresource.xml b/src/resources/gnome-builder.gresource.xml
index d00fc49..4d8584d 100644
--- a/src/resources/gnome-builder.gresource.xml
+++ b/src/resources/gnome-builder.gresource.xml
@@ -13,6 +13,8 @@
 
     <file>keybindings/default.ini</file>
 
+    <file>language/defaults.ini</file>
+
     <file>snippets/c.snippets</file>
     <file>snippets/chdr.snippets</file>
 
diff --git a/src/resources/language/defaults.ini b/src/resources/language/defaults.ini
new file mode 100644
index 0000000..6ff4b50
--- /dev/null
+++ b/src/resources/language/defaults.ini
@@ -0,0 +1,33 @@
+[c]
+insert-spaces-instead-of-tabs = true
+tab-width = 2
+auto-indent = true
+
+[chdr]
+insert-spaces-instead-of-tabs = true
+tab-width = 2
+
+[javascript]
+insert-spaces-instead-of-tabs = true
+tab-width = 4
+
+[makefile]
+insert-spaces-instead-of-tabs = false
+tab-width = 8
+
+[python]
+insert-spaces-instead-of-tabs = true
+tab-width = 4
+
+[python3]
+insert-spaces-instead-of-tabs = true
+tab-width = 4
+
+[ruby]
+insert-spaces-instead-of-tabs = true
+tab-width = 2
+
+[xml]
+insert-spaces-instead-of-tabs = true
+tab-width = 2
+auto-indent = true


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