]> git.gir.st - VimFx.git/blob - extension/lib/prefs.coffee
Improve error reporting in test runner
[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 { classes: Cc, interfaces: Ci } = Components
24
25 prefs = Services.prefs
26 vimfxBranch = prefs.getBranch(defaults.BRANCH)
27 vimfxDefaultBranch = prefs.getDefaultBranch(defaults.BRANCH)
28
29 isPrefSet = (key) -> vimfxBranch.prefHasUserValue(key)
30
31 getBranchPref = (branch, key) ->
32 return switch branch.getPrefType(key)
33 when branch.PREF_BOOL
34 branch.getBoolPref(key)
35 when branch.PREF_INT
36 branch.getIntPref(key)
37 when branch.PREF_STRING
38 branch.getComplexValue(key, Ci.nsISupportsString).data
39
40 getPref = getBranchPref.bind(undefined, vimfxBranch)
41 getFirefoxPref = getBranchPref.bind(undefined, prefs)
42
43 setBranchPref = (branch, key, value) ->
44 switch typeof value
45 when 'boolean'
46 branch.setBoolPref(key, value)
47 when 'number'
48 branch.setIntPref(key, value)
49 when 'string'
50 str = Cc['@mozilla.org/supports-string;1']
51 .createInstance(Ci.nsISupportsString)
52 str.data = value
53 branch.setComplexValue(key, Ci.nsISupportsString, str)
54 else
55 branch.clearUserPref(key)
56
57 setPref = setBranchPref.bind(undefined, vimfxBranch)
58 setDefaultPref = setBranchPref.bind(undefined, vimfxDefaultBranch)
59 setFirefoxPref = setBranchPref.bind(undefined, prefs)
60
61 withFirefoxPrefAs = (pref, temporaryValue, fn) ->
62 previousValue = getFirefoxPref(pref)
63 setFirefoxPref(pref, temporaryValue)
64 fn()
65 setFirefoxPref(pref, previousValue)
66
67 setDefaultPrefs = ->
68 setDefaultPref(key, value) for key, value of defaults.all
69 return
70
71 exports.isPrefSet = isPrefSet
72 exports.getPref = getPref
73 exports.getFirefoxPref = getFirefoxPref
74 exports.setPref = setPref
75 exports.setFirefoxPref = setFirefoxPref
76 exports.withFirefoxPrefAs = withFirefoxPrefAs
77 exports.setDefaultPrefs = setDefaultPrefs
Imprint / Impressum