From 502c3fbe86b43232cc1bc8a241990ff67868b9d4 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Mon, 28 Sep 2015 21:25:42 +0300 Subject: [PATCH] Vala example: GLib.Settings added. --- vala/gsettings/GLib.Settings.vala | 62 +++++++++++++++++++ .../ws.backbone.gsettings.glib.gschema.xml | 29 +++++++++ 2 files changed, 91 insertions(+) create mode 100644 vala/gsettings/GLib.Settings.vala create mode 100644 vala/gsettings/ws.backbone.gsettings.glib.gschema.xml diff --git a/vala/gsettings/GLib.Settings.vala b/vala/gsettings/GLib.Settings.vala new file mode 100644 index 0000000..4053eb8 --- /dev/null +++ b/vala/gsettings/GLib.Settings.vala @@ -0,0 +1,62 @@ +public static int main (string[] args) { + try { + // Custom location: + string settings_dir = Path.get_dirname (args[0]); + SettingsSchemaSource sss = new SettingsSchemaSource.from_directory (settings_dir, null, false); + SettingsSchema schema = sss.lookup ("org.example.glib-settings-schema-source", false); + if (sss.lookup == null) { + stdout.printf ("ID not found."); + return 0; + } + + Settings settings = new Settings.full (schema, null, null); + + // Default location: (XDG_DATA_DIRS) + // Settings settings = new Settings ("org.example.glib-settings-schema-source"); + + + + // Output: ``Hello, earthlings`` + string greeting = settings.get_string ("greeting"); + stdout.printf ("%s\n", greeting); + + // Output: ``99`` + int bottles = settings.get_int ("bottles-of-beer"); + stdout.printf ("%d\n", bottles); + + // Output: ``false`` + bool lighting = settings.get_boolean ("lighting"); + stdout.printf ("%s\n", lighting.to_string ()); + + + + // Change notification for any key in the schema + settings.changed.connect ((key) => { + print ("Key '%s' changed\n", key); + }); + + // Change notification for a single key + settings.changed["greeting"].connect (() => { + print ("New greeting: %s\n", settings.get_string ("greeting")); + }); + + + // Setting keys + + // Output: + // ``Key 'bottles-of-beer' changed`` + // ``Key 'lighting' changed`` + // ``Key 'greeting' changed`` + // ``New greeting: hello, world`` + settings.set_int ("bottles-of-beer", bottles - 1); + settings.set_boolean ("lighting", !lighting); + settings.set_string ("greeting", "hello, world"); + + + stdout.puts ("\nPlease start 'dconf-editor' and edit keys in /org/example/my-app/\n"); + new MainLoop ().run (); + } catch (Error e) { + stdout.printf ("Error: %s\n", e.message); + } + return 0; +} diff --git a/vala/gsettings/ws.backbone.gsettings.glib.gschema.xml b/vala/gsettings/ws.backbone.gsettings.glib.gschema.xml new file mode 100644 index 0000000..a4d432b --- /dev/null +++ b/vala/gsettings/ws.backbone.gsettings.glib.gschema.xml @@ -0,0 +1,29 @@ + + + + + "Hello, earthlings" + A greeting + + Greeting of the invading martians + + + + + 99 + Bottles of beer + + Number of bottles of beer on the wall + + + + + false + Is the light switched on? + + State of an imaginary light switch. + + + + +