]> git.gir.st - VimFx.git/blob - extension/lib/prefs.coffee
Enable the `missing_fat_arrow` coffeelint rule
[VimFx.git] / extension / lib / prefs.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 # This file provides an API a bit more easy to use than the very low-level
22 # Firefox prefs APIs.
23
24 defaults = require('./defaults')
25
26 { prefs } = Services
27
28 branches =
29 addon:
30 user: prefs.getBranch(defaults.BRANCH)
31 default: prefs.getDefaultBranch(defaults.BRANCH)
32 root:
33 user: prefs
34 default: prefs.getDefaultBranch('')
35
36 get = (branch, key) ->
37 return switch branch.getPrefType(key)
38 when branch.PREF_BOOL
39 branch.getBoolPref(key)
40 when branch.PREF_INT
41 branch.getIntPref(key)
42 when branch.PREF_STRING
43 branch.getComplexValue(key, Ci.nsISupportsString).data
44
45 set = (branch, key, value) ->
46 switch typeof value
47 when 'boolean'
48 branch.setBoolPref(key, value)
49 when 'number'
50 branch.setIntPref(key, value) # `value` will be `Math.floor`ed.
51 when 'string'
52 str = Cc['@mozilla.org/supports-string;1']
53 .createInstance(Ci.nsISupportsString)
54 str.data = value
55 branch.setComplexValue(key, Ci.nsISupportsString, str)
56 else
57 if value == null
58 branch.clearUserPref(key)
59 else
60 throw new Error("VimFx: Prefs may only be set to a boolean, number,
61 string or null. Got: #{ typeof value }")
62
63 has = (branch, key) ->
64 branch.prefHasUserValue(key)
65
66 tmp = (branch, pref, temporaryValue) ->
67 previousValue = if has(branch, pref) then get(branch, pref) else null
68 set(branch, pref, temporaryValue)
69 return -> set(branch, pref, previousValue)
70
71 observe = (branch, domain, callback) ->
72 observer = {observe: (branch, topic, changedPref) -> callback(changedPref)}
73 branch.addObserver(domain, observer, false)
74 module.onShutdown(->
75 branch.removeObserver(domain, observer)
76 )
77
78 module.exports =
79 get: get.bind(null, branches.addon.user)
80 set: set.bind(null, branches.addon.user)
81 has: has.bind(null, branches.addon.user)
82 tmp: tmp.bind(null, branches.addon.user)
83 observe: observe.bind(null, branches.addon.user)
84 default:
85 get: get.bind(null, branches.addon.default)
86 set: set.bind(null, branches.addon.default)
87 init: ->
88 for key, value of defaults.all_prefs
89 module.exports.default.set(key, value)
90 return
91 root:
92 get: get.bind(null, branches.root.user)
93 set: set.bind(null, branches.root.user)
94 has: has.bind(null, branches.root.user)
95 tmp: tmp.bind(null, branches.root.user)
96 default:
97 get: get.bind(null, branches.root.default)
98 set: set.bind(null, branches.root.default)
99 unbound: {get, set, has, tmp, observe}
Imprint / Impressum