settings.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * The settings service can update variables for settings and persist them to
  19. * LocalStorage.
  20. */
  21. export class SettingsService {
  22. private $log: ng.ILogService;
  23. private static STORAGE_KEY_PREFIX = 'settings-';
  24. private logTag: string = '[SettingsService]';
  25. private storage: Storage;
  26. public static $inject = ['$log', '$window'];
  27. constructor($log: ng.ILogService, $window: ng.IWindowService) {
  28. this.$log = $log;
  29. this.storage = $window.localStorage;
  30. }
  31. /**
  32. * Store settings key-value pair in LocalStorage.
  33. */
  34. public storeUntrustedKeyValuePair(key: string, value: string): void {
  35. this.$log.debug(this.logTag, 'Storing settings key:', key);
  36. this.storage.setItem(SettingsService.STORAGE_KEY_PREFIX + key, value);
  37. }
  38. /**
  39. * Retrieve settings key-value pair from LocalStorage.
  40. *
  41. * If the `alwaysCreate` flag is set to `true`, then the key is created
  42. * with an empty value if it does not yet exist.
  43. */
  44. public retrieveUntrustedKeyValuePair(key: string, alwaysCreate: boolean = true): string {
  45. this.$log.debug(this.logTag, 'Retrieving settings key:', key);
  46. if (this.hasUntrustedKeyValuePair(key)) {
  47. return this.storage.getItem(SettingsService.STORAGE_KEY_PREFIX + key);
  48. } else {
  49. if (alwaysCreate) {
  50. this.storeUntrustedKeyValuePair(key, '');
  51. }
  52. return '';
  53. }
  54. }
  55. /**
  56. * Remove settings key-value pair from LocalStorage if it exists.
  57. */
  58. public removeUntrustedKeyValuePair(key: string): void {
  59. this.$log.debug(this.logTag, 'Removing settings key:', key);
  60. this.storage.removeItem(SettingsService.STORAGE_KEY_PREFIX + key);
  61. }
  62. /**
  63. * Return whether key-value pair is present in LocalStorage.
  64. *
  65. * Note that this will return `true` for empty values!
  66. */
  67. private hasUntrustedKeyValuePair(key: string): boolean {
  68. const item: string = this.storage.getItem(SettingsService.STORAGE_KEY_PREFIX + key);
  69. return item !== null;
  70. }
  71. }