root/trunk/modules/preferences.jsm

Revision 1069, 5.6 kB (checked in by MaierMan, 4 months ago)

Relocate makeObserver

Line 
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is DownThemAll Preferences module.
15  *
16  * The Initial Developer of the Original Code is Nils Maier
17  * Portions created by the Initial Developer are Copyright (C) 2008
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  *   Nils Maier <MaierMan@web.de>
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */
36
37 const EXPORTED_SYMBOLS = [
38   'get',
39   'getExt',
40   'set',
41   'setExt',
42   'hasUserValue',
43   'hasUserValueExt',
44   'getChildren',
45   'getChildrenExt',
46   'reset',
47   'resetExt',
48   'resetBranch',
49   'resetBranchExt',
50   'resetAllExt',
51   'addObserver',
52   'removeObserver',
53   'makeObserver'
54 ];
55
56 const EXT = 'extensions.dta.';
57
58 const Cc = Components.classes;
59 const Ci = Components.interfaces;
60 const nsIPrefBranch = Ci.nsIPrefBranch;
61 const nsIPrefBranch2 = Ci.nsIPrefBranch2;
62
63 const PREF_STRING = nsIPrefBranch.PREF_STRING;
64 const PREF_INT = nsIPrefBranch.PREF_INT;
65 const PREF_BOOL = nsIPrefBranch.PREF_BOOL;
66
67 const SupportsString = Components.Constructor('@mozilla.org/supports-string;1', 'nsISupportsString');
68
69 const prefs = Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefBranch);
70
71 function get(key, defaultValue){
72   try {
73     let rv;
74     switch (prefs.getPrefType(key)) {
75       case PREF_INT:
76         rv = prefs.getIntPref(key);
77         break;
78       case PREF_BOOL:
79         rv = prefs.getBoolPref(key);
80         break;
81       default:
82         rv = getMultiByte(key);
83         break;
84     }
85     if (rv != undefined) {
86       return rv;
87     }
88   }
89   catch (ex) {
90     // no-op
91   }
92  
93   return defaultValue;
94 }
95  
96 function getExt(key, defaultValue) {
97     return get(EXT + key, defaultValue);
98 }
99
100 function set(key, value){
101   if (typeof value == 'number' || value instanceof Number) {
102     return prefs.setIntPref(key, value);
103   }
104   if (typeof value == 'boolean' || value instanceof Boolean) {
105     return prefs.setBoolPref(key, value);
106   }
107   return setMultiByte(key, value);
108 }
109
110 function setExt(key, value){
111   return set(EXT + key, value);
112 }
113
114 function getMultiByte(key, defaultValue){
115   try {
116     return prefs.getComplexValue(key, Ci.nsISupportsString).data;
117   }
118   catch (ex) {
119     // no-op
120   }
121   return defaultValue;
122 }
123
124 function setMultiByte(key, value) {
125   let str = new SupportsString();
126   str.data = value.toString();
127   prefs.setComplexValue(key, Ci.nsISupportsString, str);
128 }
129
130 function hasUserValue(key) {
131   try {
132     return prefs.prefHasUserValue(key);
133   }
134   catch (ex) {
135     // no-op
136   }
137   return false;
138 }
139
140 function hasUserValueExt(key) {
141   return hasUserValue(EXT + key);
142 }
143
144 function getChildren(key) {
145   return prefs.getChildList(key, {});
146 }
147
148 function getChildrenExt(key) {
149   return getChildren(EXT + key);
150 }
151
152 function reset(key) {
153   try {
154     return prefs.clearUserPref(key);
155   }
156   catch (ex) {
157     // no-op
158   }
159   return false;
160 }
161
162
163 function resetExt(key) {
164   if (key.search(new RegExp('/^' + EXT + '/')) != 0) {
165     key = EXT + key;
166   }
167   return reset(key);
168 }
169
170 function resetBranch(branch) {
171   try {
172     prefs.resetBranch(branch);
173   }
174   catch (ex) {
175     // BEWARE: not yet implemented in XPCOM 1.8/trunk.
176     let children = prefs.getChildList(branch, {});
177     for each (let key in children) {
178       reset(key);
179     }
180   }
181 }
182
183 function resetBranchExt(branch) {
184   resetBranch('extension.dta.' + branch);
185 }
186
187 function resetAllExt() {
188   resetBranchExt('');
189 }
190
191 function addObserver(branch, obj) {
192   makeObserver(obj);
193   prefs.QueryInterface(nsIPrefBranch2).addObserver(branch, obj, true);
194 }
195
196 function removeObserver(branch, obj) {
197   prefs.QueryInterface(nsIPrefBranch2).removeObserver(branch, obj);
198 }
199
200 function makeObserver(obj) {
201   try {
202     if (
203       obj.QueryInterface(Ci.nsISupportsWeakReference)
204       && obj.QueryInterface(Ci.nsIObserver)
205     ) {
206       return;
207     }
208   }
209   catch (ex) {
210     // fall-through
211   }
212   let __QueryInterface = obj.QueryInterface;
213   obj.QueryInterface = function(iid) {
214     try {
215       if (
216         iid.equals(Components.interfaces.nsISupports)
217         || iid.equals(Components.interfaces.nsISupportsWeakReference)
218         || iid.equals(Components.interfaces.nsIWeakReference)
219         || iid.equals(Components.interfaces.nsIObserver)
220       ) {
221         return obj;
222       }
223       if (__QueryInterface) {
224         debug("calling original: " + iid);
225         return __QueryInterface.call(this, iid);
226       }
227       throw Components.results.NS_ERROR_NO_INTERFACE;
228     }
229     catch (ex) {
230       debug("requested interface not available: " + iid);
231       throw ex;
232     }
233   };
234   // nsiWeakReference
235   obj.QueryReferent = function(iid) {
236     return obj.QueryInterface(iid);
237   };
238   // nsiSupportsWeakReference
239   obj.GetWeakReference = function() {
240     return obj;
241   }; 
242 }
Note: See TracBrowser for help on using the browser.