From 0336d99e0987f7a9efa45578674cda7cbda235d5 Mon Sep 17 00:00:00 2001 From: fujiwarat Date: Mon, 5 Nov 2018 19:04:51 +0900 Subject: [PATCH] Migrate IBusConfig to GSettings https://github.com/ueno/ibus-skk/issues/61 --- configure.ac | 5 +- src/Makefile.am | 5 ++ src/engine.vala | 23 +++-- ...rg.freedesktop.ibus.engine.skk.gschema.xml | 54 ++++++++++++ src/preferences.vala | 87 ++++++++----------- src/setup.vala | 49 +++++------ 6 files changed, 135 insertions(+), 88 deletions(-) create mode 100644 src/org.freedesktop.ibus.engine.skk.gschema.xml diff --git a/configure.ac b/configure.ac index 2e1abf0..aeb6eff 100644 --- a/configure.ac +++ b/configure.ac @@ -3,7 +3,7 @@ # ibus-skk - The SKK engine for IBus # # Copyright (c) 2007-2008 Huang Peng -# Copyright (C) 2009-2017 Daiki Ueno +# Copyright (C) 2009-2018 Daiki Ueno # # 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 @@ -80,6 +80,9 @@ GETTEXT_PACKAGE=ibus-skk AC_SUBST(GETTEXT_PACKAGE) AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Define to the read-only architecture-independent data directory.]) +# glib stuff +GLIB_GSETTINGS + # OUTPUT files AC_CONFIG_FILES([ po/Makefile.in Makefile diff --git a/src/Makefile.am b/src/Makefile.am index 26e1cad..d47b868 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -95,6 +95,10 @@ skk.xml.in: skk.xml.in.in @INTLTOOL_XML_NOMERGE_RULE@ +gsettings_SCHEMAS = org.freedesktop.ibus.engine.skk.gschema.xml + +@GSETTINGS_RULES@ + GITIGNOREFILES = \ $(libutil_la_SOURCES:.vala=.c) \ $(ibus_engine_skk_SOURCES:.vala=.c) \ @@ -120,6 +124,7 @@ EXTRA_DIST = \ ibus-1.0.deps \ ibus-skk-preferences.ui.in \ $(desktop_in_in_files) \ + $(gsettings_SCHEMAS) \ skk.xml.in.in \ $(NULL) diff --git a/src/engine.vala b/src/engine.vala index a55d4ca..0232811 100644 --- a/src/engine.vala +++ b/src/engine.vala @@ -1,6 +1,6 @@ /* - * Copyright (C) 2011-2017 Daiki Ueno - * Copyright (C) 2011-2017 Red Hat, Inc. + * Copyright (C) 2011-2018 Daiki Ueno + * Copyright (C) 2011-2018 Red Hat, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -302,35 +302,35 @@ class SkkEngine : IBus.Engine { void apply_preferences () { Variant? variant; - variant = preferences.get ("auto_start_henkan_keywords"); + variant = preferences.get ("auto-start-henkan-keywords"); assert (variant != null); context.auto_start_henkan_keywords = variant.get_strv (); - variant = preferences.get ("period_style"); + variant = preferences.get ("period-style"); assert (variant != null); context.period_style = (Skk.PeriodStyle) variant.get_int32 (); - variant = preferences.get ("page_size"); + variant = preferences.get ("page-size"); assert (variant != null); lookup_table.set_page_size (variant.get_int32 ()); - variant = preferences.get ("pagination_start"); + variant = preferences.get ("pagination-start"); assert (variant != null); page_start = (uint) variant.get_int32 (); - variant = preferences.get ("initial_input_mode"); + variant = preferences.get ("initial-input-mode"); assert (variant != null); context.input_mode = (Skk.InputMode) variant.get_int32 (); - variant = preferences.get ("show_annotation"); + variant = preferences.get ("show-annotation"); assert (variant != null); show_annotation = variant.get_boolean (); - variant = preferences.get ("egg_like_newline"); + variant = preferences.get ("egg-like-newline"); assert (variant != null); context.egg_like_newline = variant.get_boolean (); - variant = preferences.get ("typing_rule"); + variant = preferences.get ("typing-rule"); assert (variant != null); try { context.typing_rule = new Skk.Rule (variant.get_string ()); @@ -555,8 +555,7 @@ class SkkEngine : IBus.Engine { bus.disconnected.connect (() => { IBus.quit (); }); - var config = bus.get_config (); - SkkEngine.preferences = new Preferences (config); + SkkEngine.preferences = new Preferences (); SkkEngine.dictionaries = new ArrayList (); SkkEngine.reload_dictionaries (); SkkEngine.preferences.value_changed.connect ((name, value) => { diff --git a/src/org.freedesktop.ibus.engine.skk.gschema.xml b/src/org.freedesktop.ibus.engine.skk.gschema.xml new file mode 100644 index 0000000..6c3c0ed --- /dev/null +++ b/src/org.freedesktop.ibus.engine.skk.gschema.xml @@ -0,0 +1,54 @@ + + + + + [] + + + + + ["を", "、", "。", ".", ",", "?", "」", + "!", ";", ":", ")", ";", ":", ")", + "”", "】", "』", "》", "〉", "}", "]", + "〕", "}", "]", "?", ".", ",", "!"] + + + + + + 0 + + + + + 7 + + + + + 4 + + + + + false + + + + + 0 + + + + + false + + + + + 'default' + + + + + diff --git a/src/preferences.vala b/src/preferences.vala index c7f196a..3cb2202 100644 --- a/src/preferences.vala +++ b/src/preferences.vala @@ -1,6 +1,6 @@ /* - * Copyright (C) 2011-2017 Daiki Ueno - * Copyright (C) 2011-2017 Red Hat, Inc. + * Copyright (C) 2011-2018 Daiki Ueno + * Copyright (C) 2011-2018 Red Hat, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -20,21 +20,22 @@ using Gee; public class Preferences : Object { - IBus.Config config; + Settings settings; Map _default = new HashMap (); Map current = new HashMap (); - public void load () { - Variant? values = config.get_values ("engine/skk"); - if (values != null) { - var iter = values.iterator (); - Variant? entry = null; - while ((entry = iter.next_value ()) != null) { - string name; - Variant value; - entry.get ("{sv}", out name, out value); - current.set (name, value); + public void load (bool is_default) { + SettingsSchemaSource schema_source = SettingsSchemaSource.get_default (); + string schema_id = settings.schema_id; + SettingsSchema schema = schema_source.lookup (schema_id, false); + foreach (unowned string key in schema.list_keys()) { + if (is_default) { + _default.set (key, settings.get_default_value (key)); + } else { + Variant? value = settings.get_user_value (key); + if (value != null) + current.set (key, value); } } } @@ -42,10 +43,10 @@ public class Preferences : Object { public void save () { var iter = current.map_iterator (); while (iter.next ()) { - config.set_value ("engine/skk", - iter.get_key (), - iter.get_value ()); + settings.set_value (iter.get_key (), + iter.get_value ()); } + Settings.sync (); } public new Variant? @get (string name) { @@ -57,17 +58,15 @@ public class Preferences : Object { } public new void @set (string name, Variant value) { - current.set (name, value); + Variant? _value = current.get (name); + if (_value == null || !_value.equal (value)) + current.set (name, value); } - static const string[] AUTO_START_HENKAN_KEYWORDS = { - "を", "、", "。", ".", ",", "?", "」", - "!", ";", ":", ")", ";", ":", ")", - "”", "】", "』", "》", "〉", "}", "]", - "〕", "}", "]", "?", ".", ",", "!" - }; + public Preferences () { + settings = new Settings ("org.freedesktop.ibus.engine.skk"); + load (true); - public Preferences (IBus.Config config) { ArrayList dictionaries = new ArrayList (); dictionaries.add ( "type=file,file=%s/ibus-skk/user.dict,mode=readwrite".printf ( @@ -78,38 +77,26 @@ public class Preferences : Object { "type=server,host=localhost,port=1178"); _default.set ("dictionaries", new Variant.strv (dictionaries.to_array ())); - _default.set ("auto_start_henkan_keywords", - new Variant.strv (AUTO_START_HENKAN_KEYWORDS)); - _default.set ("period_style", - new Variant.int32 ((int32) Skk.PeriodStyle.JA_JA)); - _default.set ("page_size", - new Variant.int32 (7)); - _default.set ("pagination_start", - new Variant.int32 (4)); - _default.set ("show_annotation", - new Variant.boolean (true)); - _default.set ("initial_input_mode", - new Variant.int32 (Skk.InputMode.HIRAGANA)); - _default.set ("egg_like_newline", - new Variant.boolean (false)); - _default.set ("typing_rule", - new Variant.string ("default")); - this.config = config; - load (); - config.value_changed.connect (value_changed_cb); + load (false); + settings.changed.connect (value_changed_cb); } public signal void value_changed (string name, Variant value); - void value_changed_cb (IBus.Config config, - string section, - string name, - Variant value) + void value_changed_cb (Settings settings, + string name) { - if (section == "engine/skk") { - current.set (name, value); - value_changed (name, value); + Variant? value = settings.get_user_value (name); + if (value == null) { + current.unset (name); + value = _default.get (name); + } else { + // save() will call this callback and should not change the current. + Variant? _value = current.get (name); + if (_value == null || !_value.equal (value)) + current.set (name, value); } + value_changed (name, value); } } diff --git a/src/setup.vala b/src/setup.vala index 22e3995..669a1c8 100644 --- a/src/setup.vala +++ b/src/setup.vala @@ -1,6 +1,6 @@ /* - * Copyright (C) 2011-2017 Daiki Ueno - * Copyright (C) 2011-2017 Red Hat, Inc. + * Copyright (C) 2011-2018 Daiki Ueno + * Copyright (C) 2011-2018 Red Hat, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -392,34 +392,34 @@ class Setup : Object { Variant? variant; - variant = preferences.get ("auto_start_henkan_keywords"); + variant = preferences.get ("auto-start-henkan-keywords"); assert (variant != null); auto_start_henkan_keywords_entry.text = string.joinv ("", variant.dup_strv ()); - variant = preferences.get ("egg_like_newline"); + variant = preferences.get ("egg-like-newline"); assert (variant != null); egg_like_newline_checkbutton.active = variant.get_boolean (); - variant = preferences.get ("page_size"); + variant = preferences.get ("page-size"); assert (variant != null); page_size_spinbutton.set_range (7.0, 16.0); page_size_spinbutton.set_increments (1.0, 1.0); page_size_spinbutton.value = (double) variant.get_int32 (); - variant = preferences.get ("pagination_start"); + variant = preferences.get ("pagination-start"); assert (variant != null); pagination_start_spinbutton.set_range (0.0, 7.0); pagination_start_spinbutton.set_increments (1.0, 1.0); pagination_start_spinbutton.value = (double) variant.get_int32 (); - variant = preferences.get ("show_annotation"); + variant = preferences.get ("show-annotation"); assert (variant != null); show_annotation_checkbutton.active = variant.get_boolean (); - load_combobox ("period_style", period_style_combobox, 1); - load_combobox ("initial_input_mode", initial_input_mode_combobox, 1); + load_combobox ("period-style", period_style_combobox, 1); + load_combobox ("initial-input-mode", initial_input_mode_combobox, 1); - variant = preferences.get ("typing_rule"); + variant = preferences.get ("typing-rule"); assert (variant != null); var model = (Gtk.ListStore) typing_rule_combobox.get_model (); Gtk.TreeIter iter; @@ -473,19 +473,19 @@ class Setup : Object { out uc)) { keywords.add (uc.to_string ()); } - preferences.set ("auto_start_henkan_keywords", + preferences.set ("auto-start-henkan-keywords", keywords.to_array ()); - preferences.set ("egg_like_newline", + preferences.set ("egg-like-newline", egg_like_newline_checkbutton.active); - preferences.set ("page_size", + preferences.set ("page-size", (int) page_size_spinbutton.value); - preferences.set ("pagination_start", + preferences.set ("pagination-start", (int) pagination_start_spinbutton.value); - preferences.set ("show_annotation", + preferences.set ("show-annotation", show_annotation_checkbutton.active); - save_combobox ("period_style", + save_combobox ("period-style", period_style_combobox, 1); - save_combobox ("initial_input_mode", + save_combobox ("initial-input-mode", initial_input_mode_combobox, 1); Gtk.TreeIter iter; @@ -493,7 +493,7 @@ class Setup : Object { var model = (Gtk.ListStore) typing_rule_combobox.get_model (); string rule; model.get (iter, 0, out rule, -1); - preferences.set ("typing_rule", rule); + preferences.set ("typing-rule", rule); } preferences.save (); } @@ -546,15 +546,14 @@ class Setup : Object { } public static int main (string[] args) { - Gtk.init (ref args); - IBus.init (); + Gtk.init (ref args); + IBus.init (); Skk.init (); var bus = new IBus.Bus (); - var config = bus.get_config (); - var setup = new Setup (new Preferences (config)); + var setup = new Setup (new Preferences ()); - setup.run (); - return 0; - } + setup.run (); + return 0; + } }