gpii-windows
Version:
Components of the GPII personalization infrastructure for use on Microsoft's "Windows" ™
207 lines (183 loc) • 6.24 kB
JavaScript
/*
* System Settings Handler.
* Deals with settings found in the the built-in "System Settings" app.
*
* Copyright 2017 Raising the Floor - International
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* 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.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
;
var fluid = require("gpii-universal"),
child_process = require("child_process");
var gpii = fluid.registerNamespace("gpii");
var windows = fluid.registerNamespace("gpii.windows");
fluid.registerNamespace("gpii.windows.systemSettingsHandler");
/**
* Executes the settings helper application.
*
* See the documentation for the application for information about what is send and received.
*
* @param {Object} settings The JSON to pass to the application. An array of objects containing settingID, method, and
* parameters.
* @param {Object} options Options
* @param {String} options.exePath The settings helper executable (default: SettingsHelper.exe)
* @param {Array<String>} options.exeArgs Array of arguments to pass to the executable.
* @return {Promise} A promise, resolving with the JSON returned from the application when it completes.
*/
windows.executeHelper = function (settings, options) {
var defaultOptions = {
exePath: "SettingsHelper.exe",
exeArgs: []
};
options = fluid.extend(defaultOptions, options);
fluid.log("systemSettingsHandler", settings);
var promise = fluid.promise();
var child = child_process.execFile(options.exePath, options.exeArgs, function (err, stdout, stderr) {
if (stderr) {
fluid.log("SettingsHelper.exe:", stderr);
}
if (err) {
fluid.log(err);
promise.reject({
isError: true,
err: err
});
} else {
fluid.log("systemSettingsHandler return", stdout);
var result = JSON.parse(stdout);
promise.resolve(result);
}
});
child.stdin.setEncoding("utf-8");
child.stdin.write(JSON.stringify(settings));
child.stdin.end();
return promise;
};
/**
* The settings handler.
*
* @param {Boolean} get true to 'get', otherwise 'set'.
* @param {Object} payload The payload.
* @return {Promise} Resolves when the setting has been retrieved/applied.
*/
windows.systemSettingsHandler.invokeHandler = function (get, payload) {
var settings = [];
var getSettings = [];
fluid.each(payload.settings, function (value, settingID) {
var asyncSetting = fluid.get(payload, "options.Async");
var checkSetting = fluid.get(payload, "options.CheckResult");
var setting = {
settingID: settingID,
method: get ? "GetValue" : "SetValue",
async: asyncSetting,
checkResult: checkSetting
};
if (value.value !== undefined) {
setting.parameters = fluid.makeArray(value.value);
var getSetting = {
settingID: settingID,
method: "GetValue",
async: asyncSetting,
checkResult: checkSetting
};
getSettings.push(getSetting);
}
settings.push(setting);
});
var promise = fluid.promise();
windows.executeHelper(settings).then(function (response) {
var results = {};
var failedSettings = [];
fluid.each(response, function (setting) {
if (setting.isError) {
failedSettings.push("Setting " + setting.settingID + " failed: " + setting.errorMessage);
} else if (get) {
results[setting.settingID] = {
value: setting.returnValue
};
} else {
results[setting.settingID] = {
oldValue: {
value: setting.returnValue
},
newValue: {
value: payload.settings[setting.settingID].value
}
};
}
});
if (failedSettings.length) {
promise.reject(failedSettings);
} else {
promise.resolve(results);
}
}, promise.reject);
return promise;
};
/**
* Setter for the system settings handler.
*
* The payload should look something like this:
* {
* "settings": {
* "SystemSettings_Accessibility_Keyboard_IsOSKEnabled": {
* "value": true
* },
* "SystemSettings_Accessibility_Magnifier_IsEnabled": {
* "value": true
* }
* }
* }
* Result:
* {
* "SystemSettings_Accessibility_Keyboard_IsOSKEnabled": {
* "oldValue": {
* "value": true,
* },
* "newValue": {
* "value": true
* }
* }
* "SystemSettings_Accessibility_Magnifier_IsEnabled": { ... }
* }
* @param {Object} payload The payload.
* @return {Promise} Resolves with the response.
*/
windows.systemSettingsHandler.setImpl = function (payload) {
return windows.systemSettingsHandler.invokeHandler(false, payload);
};
/**
* Getter for the system settings handler.
*
* @param {Object} payload The payload.
* @return {Promise} Resolves with the response.
*/
windows.systemSettingsHandler.getImpl = function (payload) {
return windows.systemSettingsHandler.invokeHandler(true, payload);
};
/**
* Invoke the settings handler.
*
* @param {Object} payload The payload
* @return {Promise} Resolves with the response.
*/
windows.systemSettingsHandler.get = function (payload) {
return gpii.settingsHandlers.invokeSettingsHandler(windows.systemSettingsHandler.getImpl, payload);
};
/**
* Invoke the settings handler.
*
* @param {Object} payload The payload
* @return {Promise} Resolves with the response.
*/
windows.systemSettingsHandler.set = function (payload) {
return gpii.settingsHandlers.invokeSettingsHandler(windows.systemSettingsHandler.setImpl, payload);
};