1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
import { ipcRenderer, remote } from 'electron';
import { action, computed, observable, reaction } from 'mobx';
import localStorage from 'mobx-localstorage';
import { DEFAULT_APP_SETTINGS, FILE_SYSTEM_SETTINGS_TYPES, LOCAL_SERVER } from '../config';
import { API } from '../environment';
import { getLocale } from '../helpers/i18n-helpers';
import { SPELLCHECKER_LOCALES } from '../i18n/languages';
import Request from './lib/Request';
import Store from './lib/Store';
const debug = require('debug')('Ferdi:SettingsStore');
export default class SettingsStore extends Store {
@observable updateAppSettingsRequest = new Request(this.api.local, 'updateAppSettings');
fileSystemSettingsTypes = FILE_SYSTEM_SETTINGS_TYPES;
@observable _fileSystemSettingsCache = {
app: DEFAULT_APP_SETTINGS,
proxy: {},
};
constructor(...args) {
super(...args);
// Register action handlers
this.actions.settings.update.listen(this._update.bind(this));
this.actions.settings.remove.listen(this._remove.bind(this));
ipcRenderer.on('appSettings', (event, resp) => {
debug('Get appSettings resolves', resp.type, resp.data);
Object.assign(this._fileSystemSettingsCache[resp.type], resp.data);
});
this.fileSystemSettingsTypes.forEach((type) => {
ipcRenderer.send('getAppSettings', type);
});
}
async setup() {
await this._migrate();
reaction(
() => this.all.app.autohideMenuBar,
() => {
const currentWindow = remote.getCurrentWindow();
currentWindow.setMenuBarVisibility(!this.all.app.autohideMenuBar);
currentWindow.autoHideMenuBar = this.all.app.autohideMenuBar;
},
);
reaction(
() => this.all.app.server,
(server) => {
if (server === LOCAL_SERVER) {
ipcRenderer.send('startLocalServer');
}
},
{
fireImmediately: true,
},
);
reaction(
() => this.all.app.locked,
() => {
const { router } = window.ferdi.stores;
if (this.all.app.locked && this.all.app.lockingFeatureEnabled) {
// App just got locked, redirect to unlock screen
router.push('/auth/locked');
} else if (router.location.pathname.includes('/auth/locked')) {
// App is unlocked but user is still on locked screen
// Redirect to homepage
router.push('/');
}
},
);
const isLoggedIn = Boolean(localStorage.getItem('authToken'));
console.log(this.all.app.lockingFeatureEnabled);
// FIXME: sometime this is true, sometime this is false
// We should lock at startup once we know lockingFeatureEnabled is loaded in the settings
if (isLoggedIn && this.all.app.lockingFeatureEnabled) {
this.actions.settings.update({
type: 'app',
data: {
locked: true,
},
});
}
// Inactivity lock timer
let inactivityTimer;
remote.getCurrentWindow().on('blur', () => {
if (this.all.app.inactivityLock !== 0) {
inactivityTimer = setTimeout(() => {
this.actions.settings.update({
type: 'app',
data: {
locked: true,
},
});
}, this.all.app.inactivityLock * 1000 * 60);
}
});
remote.getCurrentWindow().on('focus', () => {
if (inactivityTimer) {
clearTimeout(inactivityTimer);
}
});
}
@computed get app() {
return this._fileSystemSettingsCache.app || DEFAULT_APP_SETTINGS;
}
@computed get proxy() {
return this._fileSystemSettingsCache.proxy || {};
}
@computed get service() {
return localStorage.getItem('service') || {
activeService: '',
};
}
@computed get stats() {
return localStorage.getItem('stats') || {
activeService: '',
};
}
@computed get migration() {
return localStorage.getItem('migration') || {};
}
@computed get all() {
return {
app: this.app,
proxy: this.proxy,
service: this.service,
stats: this.stats,
migration: this.migration,
};
}
@action async _update({ type, data }) {
const appSettings = this.all;
if (!this.fileSystemSettingsTypes.includes(type)) {
debug('Update settings', type, data, this.all);
localStorage.setItem(type, Object.assign(appSettings[type], data));
} else {
debug('Update settings on file system', type, data);
ipcRenderer.send('updateAppSettings', {
type,
data,
});
Object.assign(this._fileSystemSettingsCache[type], data);
}
}
@action async _remove({ type, key }) {
if (type === 'app') return; // app keys can't be deleted
const appSettings = this.all[type];
if (Object.hasOwnProperty.call(appSettings, key)) {
delete appSettings[key];
this.actions.settings.update({
type,
data: appSettings,
});
}
}
// Helper
async _migrate() {
const legacySettings = localStorage.getItem('app') || {};
if (!this.all.migration['5.0.0-beta.17-settings']) {
this.actions.settings.update({
type: 'app',
data: {
autoLaunchInBackground: legacySettings.autoLaunchInBackground,
runInBackground: legacySettings.runInBackground,
enableSystemTray: legacySettings.enableSystemTray,
minimizeToSystemTray: legacySettings.minimizeToSystemTray,
server: API,
isAppMuted: legacySettings.isAppMuted,
enableGPUAcceleration: legacySettings.enableGPUAcceleration,
showMessageBadgeWhenMuted: legacySettings.showMessageBadgeWhenMuted,
showDisabledServices: legacySettings.showDisabledServices,
enableSpellchecking: legacySettings.enableSpellchecking,
},
});
this.actions.settings.update({
type: 'service',
data: {
activeService: legacySettings.activeService,
},
});
this.actions.settings.update({
type: 'migration',
data: {
'5.0.0-beta.17-settings': true,
},
});
localStorage.removeItem('app');
debug('Migrated settings to split stores');
}
if (!this.all.migration['5.0.0-beta.19-settings']) {
const spellcheckerLanguage = getLocale({
locale: this.stores.settings.app.locale,
locales: SPELLCHECKER_LOCALES,
defaultLocale: DEFAULT_APP_SETTINGS.spellcheckerLanguage,
fallbackLocale: DEFAULT_APP_SETTINGS.spellcheckerLanguage,
});
this.actions.settings.update({
type: 'app',
data: {
spellcheckerLanguage,
},
});
this.actions.settings.update({
type: 'migration',
data: {
'5.0.0-beta.19-settings': true,
},
});
}
}
}
|