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