move some things out of utils.js into other files
[conkeror.git] / modules / pref.js
blob79133dddedb955e41d0ec9c611524c35e17f4892
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2009 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 function set_branch_pref (branch, name, value) {
11     if (typeof(value) == "string") {
12         branch.setCharPref(name, value);
13     } else if (typeof(value) == "number") {
14         branch.setIntPref(name, value);
15     } else if (typeof(value) == "boolean") {
16         branch.setBoolPref(name, value);
17     }
20 function default_pref (name, value) {
21     var branch = preferences.getDefaultBranch(null);
22     set_branch_pref(branch, name, value);
25 function user_pref (name, value) {
26     var branch = preferences.getBranch(null);
27     set_branch_pref(branch, name, value);
30 function get_branch_pref (branch, name) {
31     switch (branch.getPrefType(name)) {
32     case branch.PREF_STRING:
33         return branch.getCharPref(name);
34     case branch.PREF_INT:
35         return branch.getIntPref(name);
36     case branch.PREF_BOOL:
37         return branch.getBoolPref(name);
38     default:
39         return null;
40     }
43 function get_localized_pref (name) {
44     try {
45         return preferences.getBranch(null).getComplexValue(name, Ci.nsIPrefLocalizedString).data;
46     } catch (e) {
47         return null;
48     }
51 function get_pref (name) {
52     var branch = preferences.getBranch(null);
53     return get_branch_pref(branch, name);
56 function get_default_pref (name) {
57     var branch = preferences.getDefaultBranch(null);
58     return get_branch_pref(branch, name);
61 function clear_pref (name) {
62     var branch = preferences.getBranch(null);
63     return branch.clearUserPref(name);
66 function pref_has_user_value (name) {
67     var branch = preferences.getBranch(null);
68     return branch.prefHasUserValue(name);
71 function pref_has_default_value (name) {
72     var branch = preferences.getDefaultBranch(null);
73     return branch.prefHasUserValue(name);
76 function session_pref (name, value) {
77     try {
78         clear_pref (name);
79     } catch (e) {}
80     return default_pref(name, value);
83 function watch_pref (pref, hook) {
84     /* Extract pref into branch.pref */
85     let match = pref.match(/^(.*[.])?([^.]*)$/);
86     let br = match[1];
87     let key = match[2];
88     let branch = preferences.getBranch(br).QueryInterface(Ci.nsIPrefBranch2);
89     let observer = {
90         observe: function (subject, topic, data) {
91             if (topic == "nsPref:changed" && data == key) {
92                 hook();
93             }
94         }
95     };
96     branch.addObserver("", observer, false);