2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015.
5 # This file is part of VimFx.
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.
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.
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/>.
21 defaults = require('./defaults')
23 { classes: Cc, interfaces: Ci } = Components
25 prefs = Services.prefs
26 vimfxBranch = prefs.getBranch(defaults.BRANCH)
27 vimfxDefaultBranch = prefs.getDefaultBranch(defaults.BRANCH)
29 isPrefSet = (key) -> vimfxBranch.prefHasUserValue(key)
31 getBranchPref = (branch, key) ->
32 return switch branch.getPrefType(key)
34 branch.getBoolPref(key)
36 branch.getIntPref(key)
37 when branch.PREF_STRING
38 branch.getComplexValue(key, Ci.nsISupportsString).data
42 return getBranchPref(vimfxBranch, key)
44 return defaults.all[key]
46 getFirefoxPref = getBranchPref.bind(undefined, prefs)
48 setBranchPref = (branch, key, value) ->
51 branch.setBoolPref(key, value)
53 branch.setIntPref(key, value)
55 str = Cc['@mozilla.org/supports-string;1']
56 .createInstance(Ci.nsISupportsString)
58 branch.setComplexValue(key, Ci.nsISupportsString, str)
60 branch.clearUserPref(key)
62 setPref = setBranchPref.bind(undefined, vimfxBranch)
63 setDefaultPref = setBranchPref.bind(undefined, vimfxDefaultBranch)
64 setFirefoxPref = setBranchPref.bind(undefined, prefs)
66 withFirefoxPrefAs = (pref, temporaryValue, fn) ->
67 previousValue = getFirefoxPref(pref)
68 setFirefoxPref(pref, temporaryValue)
70 setFirefoxPref(pref, previousValue)
73 setDefaultPref(key, value) for key, value of defaults.options
76 exports.isPrefSet = isPrefSet
77 exports.getPref = getPref
78 exports.getFirefoxPref = getFirefoxPref
79 exports.setPref = setPref
80 exports.setFirefoxPref = setFirefoxPref
81 exports.withFirefoxPrefAs = withFirefoxPrefAs
82 exports.setDefaultPrefs = setDefaultPrefs