aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/SettingsStore.js
diff options
context:
space:
mode:
authorLibravatar Ricardo Cino <ricardo@cino.io>2022-06-24 21:25:05 +0200
committerLibravatar Vijay Aravamudhan <vraravam@users.noreply.github.com>2022-06-25 05:50:00 +0530
commit2d71e61e46394d75d9f52ba1f4c273ed6d3c9cfd (patch)
tree7c0172945f962609637d03e7de885a254dbec8a4 /src/stores/SettingsStore.js
parentchore: improve todo menu behaviour on fresh install (#359) (diff)
downloadferdium-app-2d71e61e46394d75d9f52ba1f4c273ed6d3c9cfd.tar.gz
ferdium-app-2d71e61e46394d75d9f52ba1f4c273ed6d3c9cfd.tar.zst
ferdium-app-2d71e61e46394d75d9f52ba1f4c273ed6d3c9cfd.zip
chore: convert the last few stores to typescript
Diffstat (limited to 'src/stores/SettingsStore.js')
-rw-r--r--src/stores/SettingsStore.js227
1 files changed, 0 insertions, 227 deletions
diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js
deleted file mode 100644
index 7afecd9df..000000000
--- a/src/stores/SettingsStore.js
+++ /dev/null
@@ -1,227 +0,0 @@
1import { ipcRenderer } from 'electron';
2import { getCurrentWindow } from '@electron/remote';
3import { action, computed, observable, reaction } from 'mobx';
4import localStorage from 'mobx-localstorage';
5import {
6 DEFAULT_APP_SETTINGS,
7 FILE_SYSTEM_SETTINGS_TYPES,
8 LOCAL_SERVER,
9} from '../config';
10import { hash } from '../helpers/password-helpers';
11import Request from './lib/Request';
12import Store from './lib/Store';
13
14const debug = require('../preload-safe-debug')('Ferdium:SettingsStore');
15
16export default class SettingsStore extends Store {
17 @observable updateAppSettingsRequest = new Request(
18 this.api.local,
19 'updateAppSettings',
20 );
21
22 loaded = false;
23
24 fileSystemSettingsTypes = FILE_SYSTEM_SETTINGS_TYPES;
25
26 @observable _fileSystemSettingsCache = {
27 app: DEFAULT_APP_SETTINGS,
28 proxy: {},
29 };
30
31 constructor(...args) {
32 super(...args);
33
34 // Register action handlers
35 this.actions.settings.update.listen(this._update.bind(this));
36 this.actions.settings.remove.listen(this._remove.bind(this));
37 }
38
39 async setup() {
40 await this._migrate();
41
42 reaction(
43 () => this.all.app.autohideMenuBar,
44 () => {
45 const currentWindow = getCurrentWindow();
46 currentWindow.setMenuBarVisibility(!this.all.app.autohideMenuBar);
47 currentWindow.autoHideMenuBar = this.all.app.autohideMenuBar;
48 },
49 );
50
51 reaction(
52 () => this.all.app.server,
53 server => {
54 if (server === LOCAL_SERVER) {
55 ipcRenderer.send('startLocalServer');
56 }
57 },
58 { fireImmediately: true },
59 );
60
61 // Inactivity lock timer
62 let inactivityTimer;
63 getCurrentWindow().on('blur', () => {
64 if (
65 this.all.app.lockingFeatureEnabled &&
66 this.all.app.inactivityLock !== 0
67 ) {
68 inactivityTimer = setTimeout(() => {
69 this.actions.settings.update({
70 type: 'app',
71 data: {
72 locked: true,
73 },
74 });
75 }, this.all.app.inactivityLock * 1000 * 60);
76 }
77 });
78 getCurrentWindow().on('focus', () => {
79 if (inactivityTimer) {
80 clearTimeout(inactivityTimer);
81 }
82 });
83
84 ipcRenderer.on('appSettings', (event, resp) => {
85 // Lock on startup if enabled in settings
86 if (
87 !this.loaded &&
88 resp.type === 'app' &&
89 resp.data.lockingFeatureEnabled
90 ) {
91 process.nextTick(() => {
92 if (!this.all.app.locked) {
93 this.all.app.locked = true;
94 }
95 });
96 }
97 debug('Get appSettings resolves', resp.type, resp.data);
98 Object.assign(this._fileSystemSettingsCache[resp.type], resp.data);
99 this.loaded = true;
100 ipcRenderer.send('initialAppSettings', resp);
101 });
102
103 for (const type of this.fileSystemSettingsTypes) {
104 ipcRenderer.send('getAppSettings', type);
105 }
106 }
107
108 @computed get app() {
109 return this._fileSystemSettingsCache.app || DEFAULT_APP_SETTINGS;
110 }
111
112 @computed get proxy() {
113 return this._fileSystemSettingsCache.proxy || {};
114 }
115
116 @computed get service() {
117 return (
118 localStorage.getItem('service') || {
119 activeService: '',
120 }
121 );
122 }
123
124 @computed get stats() {
125 return (
126 localStorage.getItem('stats') || {
127 activeService: '',
128 }
129 );
130 }
131
132 @computed get migration() {
133 return localStorage.getItem('migration') || {};
134 }
135
136 @computed get all() {
137 return {
138 app: this.app,
139 proxy: this.proxy,
140 service: this.service,
141 stats: this.stats,
142 migration: this.migration,
143 };
144 }
145
146 @action async _update({ type, data }) {
147 const appSettings = this.all;
148 if (!this.fileSystemSettingsTypes.includes(type)) {
149 debug('Update settings', type, data, this.all);
150 localStorage.setItem(type, Object.assign(appSettings[type], data));
151 } else {
152 debug('Update settings on file system', type, data);
153 ipcRenderer.send('updateAppSettings', {
154 type,
155 data,
156 });
157
158 Object.assign(this._fileSystemSettingsCache[type], data);
159 }
160 }
161
162 @action async _remove({ type, key }) {
163 if (type === 'app') return; // app keys can't be deleted
164
165 const appSettings = this.all[type];
166 if (Object.hasOwnProperty.call(appSettings, key)) {
167 delete appSettings[key];
168
169 this.actions.settings.update({
170 type,
171 data: appSettings,
172 });
173 }
174 }
175
176 _ensureMigrationAndMarkDone(migrationName, callback) {
177 if (!this.all.migration[migrationName]) {
178 callback();
179
180 const data = {};
181 data[migrationName] = true;
182 this.actions.settings.update({
183 type: 'migration',
184 data,
185 });
186 }
187 }
188
189 // Helper
190 async _migrate() {
191 this._ensureMigrationAndMarkDone('password-hashing', () => {
192 if (this.stores.settings.app.lockedPassword !== '') {
193 const legacySettings = localStorage.getItem('app') || {};
194 this.actions.settings.update({
195 type: 'app',
196 data: {
197 lockedPassword: hash(String(legacySettings.lockedPassword)),
198 },
199 });
200 }
201
202 debug('Migrated password-hashing settings');
203 });
204
205 this._ensureMigrationAndMarkDone('5.6.0-beta.6-settings', () => {
206 this.actions.settings.update({
207 type: 'app',
208 data: {
209 searchEngine: DEFAULT_APP_SETTINGS.searchEngine,
210 },
211 });
212
213 debug('Migrated default search engine settings');
214 });
215
216 this._ensureMigrationAndMarkDone('user-agent-settings', () => {
217 this.actions.settings.update({
218 type: 'app',
219 data: {
220 userAgentPref: DEFAULT_APP_SETTINGS.userAgentPref,
221 },
222 });
223
224 debug('Migrated default user-agent settings');
225 });
226 }
227}