]> git.gir.st - VimFx.git/blob - extension/lib/prefs.coffee
Merge pull request #498 from mozillazg/zh-CN
[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 = (key) ->
41 if isPrefSet(key)
42 return getBranchPref(vimfxBranch, key)
43 else
44 return defaults.all[key]
45
46 getFirefoxPref = getBranchPref.bind(undefined, prefs)
47
48 setBranchPref = (branch, key, value) ->
49 switch typeof value
50 when 'boolean'
51 branch.setBoolPref(key, value)
52 when 'number'
53 branch.setIntPref(key, value)
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 branch.clearUserPref(key)
61
62 setPref = setBranchPref.bind(undefined, vimfxBranch)
63 setDefaultPref = setBranchPref.bind(undefined, vimfxDefaultBranch)
64 setFirefoxPref = setBranchPref.bind(undefined, prefs)
65
66 withFirefoxPrefAs = (pref, temporaryValue, fn) ->
67 previousValue = getFirefoxPref(pref)
68 setFirefoxPref(pref, temporaryValue)
69 fn()
70 setFirefoxPref(pref, previousValue)
71
72 setDefaultPrefs = ->
73 setDefaultPref(key, value) for key, value of defaults.options
74 return
75
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
Imprint / Impressum