2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 # Copyright Wang Zhuochun 2013.
6 # This file is part of VimFx.
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.
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.
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/>.
22 { classes: Cc, interfaces: Ci } = Components
24 PREF_BRANCH = 'extensions.VimFx.'
25 DEFAULT_PREFS_FILE = 'defaults/preferences/defaults.js'
27 # Default values for preferences are now specified in
28 # defaults/preferences/defaults.js.
30 prefs = Services.prefs
31 vimfxBranch = prefs.getBranch(PREF_BRANCH)
33 getBranchPref = (branch, key, defaultValue = undefined) ->
34 type = branch.getPrefType(key)
37 branch.getBoolPref(key)
39 branch.getIntPref(key)
40 when branch.PREF_STRING
41 branch.getComplexValue(key, Ci.nsISupportsString).data
46 return vimfxBranch.prefHasUserValue(key)
48 getPref = getBranchPref.bind(undefined, vimfxBranch)
49 getFirefoxPref = getBranchPref.bind(undefined, prefs.getBranch(''))
51 setBranchPref = (branch, key, value) ->
54 branch.setBoolPref(key, value)
56 branch.setIntPref(key, value)
58 branch.setCharPref(key, value)
60 branch.clearUserPref(key)
62 setPref = setBranchPref.bind(undefined, vimfxBranch)
63 setFirefoxPref = setBranchPref.bind(undefined, prefs.getBranch(''))
65 withFirefoxPrefAs = (pref, temporaryValue, fn) ->
66 previousValue = getFirefoxPref(pref)
67 setFirefoxPref(pref, temporaryValue)
69 setFirefoxPref(pref, previousValue)
72 baseUri = Services.io.newURI(__SCRIPT_URI_SPEC__, null, null)
73 uri = Services.io.newURI(DEFAULT_PREFS_FILE, null, baseUri)
75 branch = prefs.getDefaultBranch('')
76 scope = {pref: setBranchPref.bind(undefined, branch)}
77 Services.scriptloader.loadSubScript(uri.spec, scope)
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