settings.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. import {Logger} from 'ts-log';
  18. import {LogService} from './log';
  19. /**
  20. * The settings service can update variables for settings and persist them to
  21. * LocalStorage.
  22. */
  23. export class SettingsService {
  24. private static STORAGE_KEY_PREFIX = 'settings-';
  25. private readonly log: Logger;
  26. private storage: Storage;
  27. public static $inject = ['$window', 'LogService'];
  28. constructor($window: ng.IWindowService, logService: LogService) {
  29. this.log = logService.getLogger('Settings-S');
  30. this.storage = $window.localStorage;
  31. }
  32. /**
  33. * Store settings key-value pair in LocalStorage.
  34. */
  35. public storeUntrustedKeyValuePair(key: string, value: string): void {
  36. this.log.debug('Storing settings key:', key);
  37. this.storage.setItem(SettingsService.STORAGE_KEY_PREFIX + key, value);
  38. }
  39. /**
  40. * Retrieve settings key-value pair from LocalStorage.
  41. *
  42. * If the `alwaysCreate` flag is set to `true`, then the key is created
  43. * with an empty value if it does not yet exist.
  44. */
  45. public retrieveUntrustedKeyValuePair(key: string, alwaysCreate: boolean = true): string {
  46. this.log.debug('Retrieving settings key:', key);
  47. if (this.hasUntrustedKeyValuePair(key)) {
  48. return this.storage.getItem(SettingsService.STORAGE_KEY_PREFIX + key);
  49. } else {
  50. if (alwaysCreate) {
  51. this.storeUntrustedKeyValuePair(key, '');
  52. }
  53. return '';
  54. }
  55. }
  56. /**
  57. * Remove settings key-value pair from LocalStorage if it exists.
  58. */
  59. public removeUntrustedKeyValuePair(key: string): void {
  60. this.log.debug('Removing settings key:', key);
  61. this.storage.removeItem(SettingsService.STORAGE_KEY_PREFIX + key);
  62. }
  63. /**
  64. * Return whether key-value pair is present in LocalStorage.
  65. *
  66. * Note that this will return `true` for empty values!
  67. */
  68. private hasUntrustedKeyValuePair(key: string): boolean {
  69. const item: string = this.storage.getItem(SettingsService.STORAGE_KEY_PREFIX + key);
  70. return item !== null;
  71. }
  72. }