From 02ca2a4d7cdded94ca9ee2d4e09561cbb3dba32b Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 19 Apr 2015 21:17:26 -0700 Subject: [PATCH] GUAC-1053: Add preference service, storing the default mouse emulation mode and input method. --- .../client/controllers/clientController.js | 3 +- .../app/client/types/ClientProperties.js | 9 +- .../settings/services/preferenceService.js | 106 ++++++++++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 guacamole/src/main/webapp/app/settings/services/preferenceService.js diff --git a/guacamole/src/main/webapp/app/client/controllers/clientController.js b/guacamole/src/main/webapp/app/client/controllers/clientController.js index 5986fdfe1..0b9ad5f2f 100644 --- a/guacamole/src/main/webapp/app/client/controllers/clientController.js +++ b/guacamole/src/main/webapp/app/client/controllers/clientController.js @@ -34,6 +34,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams var $location = $injector.get('$location'); var guacClientManager = $injector.get('guacClientManager'); var guacNotification = $injector.get('guacNotification'); + var preferenceService = $injector.get('preferenceService'); var userPageService = $injector.get('userPageService'); /** @@ -196,7 +197,7 @@ angular.module('client').controller('clientController', ['$scope', '$routeParams * * @type String */ - inputMethod : 'none', + inputMethod : preferenceService.preferences.inputMethod, /** * The current scroll state of the menu. diff --git a/guacamole/src/main/webapp/app/client/types/ClientProperties.js b/guacamole/src/main/webapp/app/client/types/ClientProperties.js index 529022cb0..e20665b19 100644 --- a/guacamole/src/main/webapp/app/client/types/ClientProperties.js +++ b/guacamole/src/main/webapp/app/client/types/ClientProperties.js @@ -23,8 +23,11 @@ /** * A service for generating new guacClient properties objects. */ -angular.module('client').factory('ClientProperties', [function defineClientProperties() { - +angular.module('client').factory('ClientProperties', ['$injector', function defineClientProperties($injector) { + + // Required services + var preferenceService = $injector.get('preferenceService'); + /** * Object used for interacting with a guacClient directive. * @@ -81,7 +84,7 @@ angular.module('client').factory('ClientProperties', [function defineClientPrope * * @type Boolean */ - this.emulateAbsoluteMouse = template.emulateAbsoluteMouse || true; + this.emulateAbsoluteMouse = template.emulateAbsoluteMouse || preferenceService.preferences.emulateAbsoluteMouse; /** * The relative Y coordinate of the scroll offset of the display within diff --git a/guacamole/src/main/webapp/app/settings/services/preferenceService.js b/guacamole/src/main/webapp/app/settings/services/preferenceService.js new file mode 100644 index 000000000..ab946908b --- /dev/null +++ b/guacamole/src/main/webapp/app/settings/services/preferenceService.js @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2015 Glyptodon LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * A service for setting and retrieving browser-local preferences. Preferences + * may be any JSON-serializable type. + */ +angular.module('settings').factory('preferenceService', ['$injector', + function preferenceService($injector) { + + // Required services + var $window = $injector.get('$window'); + var $rootScope = $injector.get('$rootScope'); + + var service = {}; + + // The parameter name for getting the history from local storage + var GUAC_PREFERENCES_STORAGE_KEY = "GUAC_PREFERENCES"; + + /** + * All currently-set preferences, as name/value pairs. Each property name + * corresponds to the name of a preference. + * + * @type Object. + */ + service.preferences = { + + /** + * Whether translation of touch to mouse events should emulate an + * absolute pointer device, or a relative pointer device. + * + * @type Boolean + */ + emulateAbsoluteMouse : true, + + /** + * The default input method. This may be either "none", "osk", or + * "text". + * + * @type String + */ + inputMethod : 'none' + + }; + + /** + * Persists the current values of all preferences, if possible. + */ + service.save = function save() { + + // Save updated preferences, ignore inability to use localStorage + try { + if (localStorage) + localStorage.setItem(GUAC_PREFERENCES_STORAGE_KEY, JSON.stringify(service.preferences)); + } + catch (ignore) {} + + }; + + // Get stored preferences, ignore inability to use localStorage + try { + + if (localStorage) { + var preferencesJSON = localStorage.getItem(GUAC_PREFERENCES_STORAGE_KEY); + if (preferencesJSON) + service.preferences = JSON.parse(preferencesJSON); + } + + } + catch (ignore) {} + + // Persist settings when window is unloaded + $window.addEventListener('unload', service.save); + + // Persist settings upon navigation + $rootScope.$on('$routeChangeSuccess', function handleNavigate() { + service.save(); + }); + + // Persist settings upon logout + $rootScope.$on('guacLogout', function handleLogout() { + service.save(); + }); + + return service; + +}]);