[caribou] libcaribou: Enable keyboard creation from a file



commit 927311ecc396ecfed66c4da29cd45cdf5155d24f
Author: Daiki Ueno <dueno src gnome org>
Date:   Fri Jan 16 17:30:29 2015 +0900

    libcaribou: Enable keyboard creation from a file
    
    https://bugzilla.gnome.org/show_bug.cgi?id=690436

 libcaribou/keyboard-model.vala   |   30 +++++++++++++++++++++++-------
 libcaribou/xml-deserializer.vala |   33 ++++++++++++++++++++++++++-------
 2 files changed, 49 insertions(+), 14 deletions(-)
---
diff --git a/libcaribou/keyboard-model.vala b/libcaribou/keyboard-model.vala
index f1c8dbb..c74ab1c 100644
--- a/libcaribou/keyboard-model.vala
+++ b/libcaribou/keyboard-model.vala
@@ -9,6 +9,7 @@ namespace Caribou {
     public class KeyboardModel : Object, IKeyboardObject {
         public string active_group { get; private set; default = ""; }
         public string keyboard_type { get; construct; }
+        public string keyboard_file { get; construct; }
 
         private DisplayAdapter xadapter;
         private Gee.HashMap<string, GroupModel> groups;
@@ -19,16 +20,31 @@ namespace Caribou {
         public signal void group_removed (string name);
 
         construct {
-            assert (keyboard_type != null);
-
             xadapter = DisplayAdapter.get_default ();
-            xadapter.group_changed.connect (on_group_changed);
-            xadapter.config_changed.connect (on_config_changed);
-
             groups = new Gee.HashMap<string, GroupModel> ();
-            on_config_changed ();
-
             active_mod_keys = new Gee.HashSet<KeyModel> ();
+
+            if (keyboard_file != null) {
+                GroupModel grp =
+                    XmlDeserializer.load_group_from_file (keyboard_file);
+                if (grp != null) {
+                    grp.key_clicked.connect (on_key_clicked);
+                    grp.key_pressed.connect (on_key_pressed);
+                    grp.key_released.connect (on_key_released);
+                }
+
+                // Use dummy group/variant names.
+                groups.set ("us", grp);
+                group_added ("us");
+                active_group = GroupModel.create_group_name ("us", "");
+            } else {
+                assert (keyboard_type != null);
+
+                xadapter.group_changed.connect (on_group_changed);
+                xadapter.config_changed.connect (on_config_changed);
+
+                on_config_changed ();
+            }
         }
 
         private void on_config_changed () {
diff --git a/libcaribou/xml-deserializer.vala b/libcaribou/xml-deserializer.vala
index 3e4cfe3..d16e41b 100644
--- a/libcaribou/xml-deserializer.vala
+++ b/libcaribou/xml-deserializer.vala
@@ -62,22 +62,21 @@ namespace Caribou {
                 "Could not find layout file for %s %s", group, variant);
         }
 
-        public static GroupModel? load_group (string keyboard_type,
-                                              string group, string variant) {
+        public static GroupModel? load_group_from_file (string filename) {
             Xml.Doc* doc;
 
             try {
-                string fn = get_layout_file (keyboard_type, group, variant);
-                doc = Xml.Parser.parse_file (fn);
+                doc = Xml.Parser.parse_file (filename);
                 if (doc == null)
                     throw new IOError.FAILED (
-                        "Cannot load XML text reader for %s", fn);
+                        "Cannot load XML text reader for %s", filename);
             } catch (GLib.Error e) {
-                stdout.printf ("Failed to load XML: %s\n", e.message);
+                stdout.printf ("Failed to load %s: %s\n", filename, e.message);
                 return null;
             }
 
-            GroupModel grp = new GroupModel (group, variant);
+            // Use dummy group/variant names.
+            GroupModel grp = new GroupModel ("us", "");
             Xml.Node* node = doc->children;
 
             create_levels_from_xml (grp, node);
@@ -88,6 +87,26 @@ namespace Caribou {
             return grp;
         }
 
+        public static GroupModel? load_group (string keyboard_type,
+                                              string group, string variant) {
+            string filename;
+            try {
+                filename = get_layout_file (keyboard_type, group, variant);
+            } catch (GLib.Error e) {
+                stdout.printf ("Failed to load keyboard file %s/%s: %s\n",
+                               keyboard_type,
+                               GroupModel.create_group_name (group, variant),
+                               e.message);
+                return null;
+            }
+            var grp = load_group_from_file (filename);
+            if (grp != null) {
+                grp.group = group;
+                grp.variant = variant;
+            }
+            return grp;
+        }
+
         public static void create_levels_from_xml (GroupModel group,
                                                    Xml.Node* node) {
             assert (node->name == "layout");


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