1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 40× 40× 40× 40× 40× 40× 40× 40× 40× 2× 2× 24× 24× 24× 24× 192× 192× 24× 2× 16× 16× 16× 16× 128× 128× 128× 128× 16× 16× 2× 24× 2× 16× 2× 21× 2× 14× 2× 2× 2× | /* * GPII Orca Settings Handler * * Copyright 2013-2015 Emergya * Copyright 2015-2016 RtF-US * * Licensed under the New BSD license. You may not use this file except in * compliance with this License. * * You may obtain a copy of the License at * https://github.com/GPII/linux/blob/master/LICENSE.txt * * The research leading to these results has received funding from the European Union's * Seventh Framework Programme (FP7/2007-2013) * under grant agreement no. 289016. */ "use strict"; var fluid = require("universal"); var gpii = fluid.registerNamespace("gpii"); var fs = require("fs"); var spawn = require("child_process").spawn; var path = require("path"); var HOME = process.env.HOME; var XDG_DATA_HOME = process.env.XDG_DATA_HOME || path.resolve(HOME, ".local/share"); var orcaSettingsFile = path.resolve(XDG_DATA_HOME, "orca/user-settings.conf"); var ORCA_ARGS = ["--disable", "speech", "--disable", "braille", "--disable", "braille-monitor", "--disable", "main-window", "--disable", "splash-window"]; // When Orca is referencing a profile in a setting, it uses an array // containing the profile's name and id. // ie: startingProfile: ["Default", "default"] // // But profiles are stored in the 'profiles' section as an object, // which main key equals to the profile's id. As we need to retrieve a // specific profile under 'profiles' section, we need to get the second // value (the id) from the array. // var PROFILE_ID = 1; fluid.registerNamespace("gpii.orca"); fluid.defaults("gpii.orca.settingsHandler", { gradeNames: ["fluid.component"], invokers: { get: { funcName: "gpii.orca.settingsHandler.get", args: ["{that}", "{arguments}.0"] }, set: { funcName: "gpii.orca.settingsHandler.set", args: ["{that}", "{arguments}.0"] }, getImpl: { funcName: "gpii.orca.settingsHandler.getImpl", args: ["{that}", "{arguments}.0"] }, setImpl: { funcName: "gpii.orca.settingsHandler.setImpl", args: ["{that}", "{arguments}.0"] }, getSettings: { funcName: "gpii.orca.settingsHandler.getSettings", args: ["{that}", "{arguments}.0", "{arguments}.1"] }, setSettings: { funcName: "gpii.orca.settingsHandler.setSettings", args: ["{that}", "{arguments}.0"] }, makeConfigurable: { funcName: "gpii.orca.settingsHandler.makeConfigurable" }, loadSettings: { funcName: "gpii.orca.settingsHandler.loadSettings", args: "{that}" } } }); gpii.orca.settingsHandler.loadSettings = function (that) { var togo = fluid.promise(); var performRead = function () { try { var userSettings = fs.readFileSync(orcaSettingsFile); togo.resolve(JSON.parse(userSettings)); } catch (err) { var rej = { isError: true, message: "Unable to read Orca settings file: " + err }; togo.reject(rej); } }; try { fs.statSync(orcaSettingsFile); performRead(); } catch (err) { var makeConfigurable = that.makeConfigurable(); makeConfigurable.then(function () { performRead(); }, function (err) { togo.reject(err); }); } return togo; }; gpii.orca.settingsHandler.makeConfigurable = function () { var togo = fluid.promise(); var orcaSpawn = spawn("orca", ORCA_ARGS); var pass = 0; var maxPass = 10; var checkIfFileGetsCreated = function () { pass++; if (pass === maxPass) { orcaSpawn.kill("SIGKILL"); var err = "Time limit exceeded [" + maxPass * 500 + "ms] for creating Orca's configuration file"; var rej = { isError: true, message: err }; togo.reject(rej); } else { try { fs.statSync(orcaSettingsFile); orcaSpawn.kill("SIGKILL"); togo.resolve(); } catch (err) { gpii.invokeLater(checkIfFileGetsCreated, 500); } } }; gpii.invokeLater(checkIfFileGetsCreated, 500); return togo; }; gpii.orca.settingsHandler.getSettings = function (that, payload) { var loadSettings = that.loadSettings(); return fluid.promise.map(loadSettings, function (userSettings) { var profile = userSettings.general.activeProfile[PROFILE_ID]; var results = fluid.transform(payload.settings, function (value, key) { var currentValue = fluid.get(userSettings, ["profiles", profile, key]); return currentValue; }); return results; }); }; gpii.orca.settingsHandler.setSettings = function (that, payload) { var loadSettings = that.loadSettings(); return fluid.promise.map(loadSettings, function (userSettings) { var profile = userSettings.general.activeProfile[PROFILE_ID]; var results = fluid.transform(payload.settings, function (value, key) { var oldValue = fluid.get(userSettings, ["profiles", profile, key]); fluid.set(userSettings, ["profiles", profile, key], value); var newValue = fluid.get(userSettings, ["profiles", profile, key]); return { oldValue: oldValue, newValue: newValue }; }); fs.writeFileSync(orcaSettingsFile, JSON.stringify(userSettings, null, 4)); return results; }); }; gpii.orca.settingsHandler.getImpl = function (that, payload) { return that.getSettings(payload); }; gpii.orca.settingsHandler.setImpl = function (that, payload) { return that.setSettings(payload); }; gpii.orca.settingsHandler.get = function (that, payload) { return gpii.settingsHandlers.invokeSettingsHandler(that.getImpl, payload); }; gpii.orca.settingsHandler.set = function (that, payload) { return gpii.settingsHandlers.invokeSettingsHandler(that.setImpl, payload); }; var orcaSettingsHandler = gpii.orca.settingsHandler(); gpii.orca.get = orcaSettingsHandler.get; gpii.orca.set = orcaSettingsHandler.set; |