]> git.gir.st - VimFx.git/blob - extension/lib/prefs.coffee
Lay the foundation for using multi-process Firefox
[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 defaults = require('./defaults')
22
23 { prefs } = Services
24
25 branches =
26 addon:
27 user: prefs.getBranch(defaults.BRANCH)
28 default: prefs.getDefaultBranch(defaults.BRANCH)
29 root:
30 user: prefs
31 default: prefs.getDefaultBranch('')
32
33 get = (branch, key) ->
34 return switch branch.getPrefType(key)
35 when branch.PREF_BOOL
36 branch.getBoolPref(key)
37 when branch.PREF_INT
38 branch.getIntPref(key)
39 when branch.PREF_STRING
40 branch.getComplexValue(key, Ci.nsISupportsString).data
41
42 set = (branch, key, value) ->
43 switch typeof value
44 when 'boolean'
45 branch.setBoolPref(key, value)
46 when 'number'
47 branch.setIntPref(key, value) # `value` will be `Math.floor`ed.
48 when 'string'
49 str = Cc['@mozilla.org/supports-string;1']
50 .createInstance(Ci.nsISupportsString)
51 str.data = value
52 branch.setComplexValue(key, Ci.nsISupportsString, str)
53 else
54 if value == null
55 branch.clearUserPref(key)
56 else
57 throw new Error("VimFx: Prefs may only be set to a boolean, number,
58 string or null. Got: #{ typeof value }")
59
60 has = (branch, key) ->
61 branch.prefHasUserValue(key)
62
63 tmp = (branch, pref, temporaryValue, fn) ->
64 previousValue = if has(branch, pref) then get(branch, pref) else null
65 set(branch, pref, temporaryValue)
66 fn()
67 set(branch, pref, previousValue)
68
69 observe = (branch, domain, callback) ->
70 observer = {observe: (branch, topic, changedPref) -> callback(changedPref)}
71 branch.addObserver(domain, observer, false)
72 module.onShutdown(->
73 branch.removeObserver(domain, observer)
74 )
75
76 module.exports =
77 get: get.bind(null, branches.addon.user)
78 set: set.bind(null, branches.addon.user)
79 has: has.bind(null, branches.addon.user)
80 tmp: tmp.bind(null, branches.addon.user)
81 observe: observe.bind(null, branches.addon.user)
82 default:
83 get: get.bind(null, branches.addon.default)
84 set: set.bind(null, branches.addon.default)
85 _init: ->
86 @set(key, value) for key, value of defaults.all_prefs
87 return
88 root:
89 get: get.bind(null, branches.root.user)
90 set: set.bind(null, branches.root.user)
91 has: has.bind(null, branches.root.user)
92 tmp: tmp.bind(null, branches.root.user)
93 default:
94 get: get.bind(null, branches.root.default)
95 set: set.bind(null, branches.root.default)
96 unbound: {get, set, has, tmp, observe}
Imprint / Impressum