gschema-test/src/GLib.Settings.vala

66 lines
2.3 KiB
Vala

extern const string GETTEXT_PACKAGE;
public static int main (string[] args) {
try {
// Custom location:
File exec_file = File.new_for_path (Environment.find_program_in_path (args[0]));
string settings_dir = Path.build_path (Path.DIR_SEPARATOR_S, exec_file.get_parent().get_parent().get_path(), "share/glib-2.0/schemas");
string w32dhack_sdir = settings_dir+"/gschema-test-"+Config.VERSION_MAJOR.to_string();
if (File.new_for_path(w32dhack_sdir+"/gschemas.compiled").query_exists ())
settings_dir = w32dhack_sdir;
// l18n location:
string localedir = Path.build_path (Path.DIR_SEPARATOR_S, exec_file.get_parent().get_parent().get_path(), "share/locale");
Intl.bindtextdomain (GETTEXT_PACKAGE, localedir);
Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
SettingsSchemaSource sss = new SettingsSchemaSource.from_directory (settings_dir, null, false);
SettingsSchema schema = sss.lookup ("ws.backbone.gschema-test-"+Config.VERSION_MAJOR.to_string(), false);
if (sss.lookup == null) {
stdout.printf ("ID not found.");
return 0;
}
Settings settings = new Settings.full (schema, null, null);
// 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 /ws/backbone/gschema-test-"+Config.VERSION_MAJOR.to_string()+"/\n");
new MainLoop ().run ();
} catch (Error e) {
stdout.printf ("Error: %s\n", e.message);
}
return 0;
}