]> git.gir.st - VimFx.git/blob - extension/lib/prefs.coffee
Merge branch 'develop' of github.com:akhodakivskiy/VimFx into develop
[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.getCharPref(key)
42 else
43 defaultValue
44
45 isPrefSet = (key) ->
46 return vimfxBranch.prefHasUserValue(key)
47
48 getPref = getBranchPref.bind(undefined, vimfxBranch)
49
50 # Unicode String.
51 getComplexPref = (key) ->
52 return vimfxBranch.getComplexValue(key, Ci.nsISupportsString).data
53
54 setBranchPref = (branch, key, value) ->
55 switch typeof value
56 when 'boolean'
57 branch.setBoolPref(key, value)
58 when 'number'
59 branch.setIntPref(key, value)
60 when 'string'
61 branch.setCharPref(key, value)
62 else
63 branch.clearUserPref(key)
64
65 setPref = setBranchPref.bind(undefined, vimfxBranch)
66
67 setDefaultPrefs = ->
68 baseUri = Services.io.newURI(__SCRIPT_URI_SPEC__, null, null)
69 uri = Services.io.newURI(DEFAULT_PREFS_FILE, null, baseUri)
70
71 branch = prefs.getDefaultBranch('')
72 scope = {pref: setBranchPref.bind(undefined, branch)}
73 Services.scriptloader.loadSubScript(uri.spec, scope)
74
75 exports.isPrefSet = isPrefSet
76 exports.getPref = getPref
77 exports.getComplexPref = getComplexPref
78 exports.setPref = setPref
79 exports.setDefaultPrefs = setDefaultPrefs
Imprint / Impressum