This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 973
/
settings.js
85 lines (68 loc) · 3.7 KB
/
settings.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const appConfig = require('./constants/appConfig')
const Immutable = require('immutable')
const settings = require('./constants/settings')
const {passwordManagers, defaultPasswordManager, extensionIds, displayNames} = require('./constants/passwordManagers')
const {bookmarksToolbarMode} = require('../app/common/constants/settingsEnums')
const passwordManagerDefault = (settingKey, settingsCollection) => {
const onePasswordEnabled = resolveValue(settings.ONE_PASSWORD_ENABLED, settingsCollection) === true
if (onePasswordEnabled) return passwordManagers.ONE_PASSWORD
const dashlaneEnabled = resolveValue(settings.DASHLANE_ENABLED, settingsCollection) === true
if (dashlaneEnabled) return passwordManagers.DASHLANE
const lastPassEnabled = resolveValue(settings.LAST_PASS_ENABLED, settingsCollection) === true
if (lastPassEnabled) return passwordManagers.LAST_PASS
const enpassEnabled = resolveValue(settings.ENPASS_ENABLED, settingsCollection) === true
if (enpassEnabled) return passwordManagers.ENPASS
const bitwardenEnabled = resolveValue(settings.BITWARDEN_ENABLED, settingsCollection) === true
if (bitwardenEnabled) return passwordManagers.BITWARDEN
const disabled = resolveValue(settings.PASSWORD_MANAGER_ENABLED, settingsCollection) === false
if (disabled) return passwordManagers.UNMANAGED
return defaultPasswordManager
}
const bookmarksBarDefault = (settingKey, settingsCollection) => {
const faviconsOnly = resolveValue(settings.SHOW_BOOKMARKS_TOOLBAR_ONLY_FAVICON, settingsCollection) === true
if (faviconsOnly) return bookmarksToolbarMode.FAVICONS_ONLY
const favicons = resolveValue(settings.SHOW_BOOKMARKS_TOOLBAR_FAVICON, settingsCollection) === true
if (favicons) return bookmarksToolbarMode.TEXT_AND_FAVICONS
return bookmarksToolbarMode.TEXT_ONLY
}
// Retrofit a new setting based on old values; we don't want to lose existing user settings.
const getDefaultSetting = (settingKey, settingsCollection) => {
switch (settingKey) {
case settings.ACTIVE_PASSWORD_MANAGER:
return passwordManagerDefault(settingKey, settingsCollection)
case settings.BOOKMARKS_TOOLBAR_MODE:
return bookmarksBarDefault(settingKey, settingsCollection)
}
return undefined
}
const resolveValue = (settingKey, settingsCollection) => {
const appStore = (process.type === 'browser'
? require('./stores/appStore').getState()
: require('./stores/appStoreRenderer').state) || Immutable.Map()
const appSettings = appStore.get('settings') || Immutable.Map()
if (settingsCollection && settingsCollection.constructor === Immutable.Map &&
settingsCollection.get(settingKey) !== undefined) {
return settingsCollection.get(settingKey)
}
if (settingsCollection && settingsCollection[settingKey] !== undefined) {
return settingsCollection[settingKey]
}
return appSettings.get(settingKey) !== undefined ? appSettings.get(settingKey) : appConfig.defaultSettings[settingKey]
}
module.exports.getSetting = (settingKey, settingsCollection) => {
const setting = resolveValue(settingKey, settingsCollection)
if (typeof setting !== 'undefined' && setting !== null) return setting
return getDefaultSetting(settingKey, settingsCollection)
}
module.exports.getActivePasswordManager = (settingsCollection) => {
const passwordManager = module.exports.getSetting(settings.ACTIVE_PASSWORD_MANAGER, settingsCollection)
let details = {
name: passwordManager,
extensionId: extensionIds[passwordManager],
displayName: displayNames[passwordManager]
}
return Immutable.fromJS(details)
}