]> git.gir.st - VimFx.git/blob - extension/lib/prefs.coffee
Merge pull request #461 from lydell/scroll
[VimFx.git] / extension / lib / prefs.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 # Copyright Wang Zhuochun 2013.
5 #
6 # This file is part of VimFx.
7 #
8 # VimFx is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # VimFx is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
20 ###
21
22 { classes: Cc, interfaces: Ci } = Components
23
24 PREF_BRANCH = 'extensions.VimFx.'
25 DEFAULT_PREFS_FILE = 'defaults/preferences/defaults.js'
26
27 # Default values for preferences are now specified in
28 # defaults/preferences/defaults.js.
29
30 prefs = Services.prefs
31 vimfxBranch = prefs.getBranch(PREF_BRANCH)
32
33 getBranchPref = (branch, key, defaultValue = undefined) ->
34 type = branch.getPrefType(key)
35 return switch type
36 when branch.PREF_BOOL
37 branch.getBoolPref(key)
38 when branch.PREF_INT
39 branch.getIntPref(key)
40 when branch.PREF_STRING
41 branch.getComplexValue(key, Ci.nsISupportsString).data
42 else
43 defaultValue
44
45 isPrefSet = (key) ->
46 return vimfxBranch.prefHasUserValue(key)
47
48 getPref = getBranchPref.bind(undefined, vimfxBranch)
49 getFirefoxPref = getBranchPref.bind(undefined, prefs.getBranch(''))
50
51 setBranchPref = (branch, key, value) ->
52 switch typeof value
53 when 'boolean'
54 branch.setBoolPref(key, value)
55 when 'number'
56 branch.setIntPref(key, value)
57 when 'string'
58 branch.setCharPref(key, value)
59 else
60 branch.clearUserPref(key)
61
62 setPref = setBranchPref.bind(undefined, vimfxBranch)
63 setFirefoxPref = setBranchPref.bind(undefined, prefs.getBranch(''))
64
65 withFirefoxPrefAs = (pref, temporaryValue, fn) ->
66 previousValue = getFirefoxPref(pref)
67 setFirefoxPref(pref, temporaryValue)
68 fn()
69 setFirefoxPref(pref, previousValue)
70
71 setDefaultPrefs = ->
72 baseUri = Services.io.newURI(__SCRIPT_URI_SPEC__, null, null)
73 uri = Services.io.newURI(DEFAULT_PREFS_FILE, null, baseUri)
74
75 branch = prefs.getDefaultBranch('')
76 scope = {pref: setBranchPref.bind(undefined, branch)}
77 Services.scriptloader.loadSubScript(uri.spec, scope)
78
79 exports.isPrefSet = isPrefSet
80 exports.getPref = getPref
81 exports.getFirefoxPref = getFirefoxPref
82 exports.setPref = setPref
83 exports.setFirefoxPref = setFirefoxPref
84 exports.withFirefoxPrefAs = withFirefoxPrefAs
85 exports.setDefaultPrefs = setDefaultPrefs
Imprint / Impressum