whitespace
[conkeror.git] / modules / pref.js
blob78042e8b32572da32f50c6e3739046781d54dc66
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2009,2011 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 in_module(null);
12 function set_branch_pref (branch, name, value) {
13     if (typeof(value) == "string") {
14         branch.setCharPref(name, value);
15     } else if (typeof(value) == "number") {
16         branch.setIntPref(name, value);
17     } else if (typeof(value) == "boolean") {
18         branch.setBoolPref(name, value);
19     }
22 function default_pref (name, value) {
23     var branch = preferences.getDefaultBranch(null);
24     set_branch_pref(branch, name, value);
27 function user_pref (name, value) {
28     var branch = preferences.getBranch(null);
29     set_branch_pref(branch, name, value);
32 function get_branch_pref (branch, name) {
33     switch (branch.getPrefType(name)) {
34     case branch.PREF_STRING:
35         return branch.getCharPref(name);
36     case branch.PREF_INT:
37         return branch.getIntPref(name);
38     case branch.PREF_BOOL:
39         return branch.getBoolPref(name);
40     default:
41         return null;
42     }
45 function get_localized_pref (name) {
46     try {
47         return preferences.getBranch(null).getComplexValue(name, Ci.nsIPrefLocalizedString).data;
48     } catch (e) {
49         return null;
50     }
53 function get_pref (name) {
54     var branch = preferences.getBranch(null);
55     return get_branch_pref(branch, name);
58 function get_default_pref (name) {
59     var branch = preferences.getDefaultBranch(null);
60     return get_branch_pref(branch, name);
63 function clear_pref (name) {
64     var branch = preferences.getBranch(null);
65     return branch.clearUserPref(name);
68 function clear_default_pref (name) {
69     var branch = preferences.getDefaultBranch(null);
70     return branch.deleteBranch(name);
73 function pref_has_user_value (name) {
74     var branch = preferences.getBranch(null);
75     return branch.prefHasUserValue(name);
78 function pref_has_default_value (name) {
79     var branch = preferences.getDefaultBranch(null);
80     return branch.prefHasUserValue(name);
83 function session_pref (name, value) {
84     try {
85         clear_pref (name);
86     } catch (e) {}
87     return default_pref(name, value);
90 function watch_pref (pref, hook) {
91     /* Extract pref into branch.pref */
92     let match = pref.match(/^(.*[.])?([^.]*)$/);
93     let br = match[1];
94     let key = match[2];
95     let branch = preferences.getBranch(br).QueryInterface(Ci.nsIPrefBranch2);
96     let observer = {
97         observe: function (subject, topic, data) {
98             if (topic == "nsPref:changed" && data == key) {
99                 hook();
100             }
101         }
102     };
103     branch.addObserver("", observer, false);
106 provide("pref");