This is just some static backup of the original site, don't expect every link to work!

source: modules/vI_prefs.js @ fe0698

ng_0.9
Last change on this file since fe0698 was fe0698, checked in by rene <rene@…>, 11 years ago

cleaned debugging from newline - thats standard

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15
16    The Original Code is the Virtual Identity Extension.
17
18    The Initial Developer of the Original Code is Rene Ejury.
19    Portions created by the Initial Developer are Copyright (C) 2007
20    the Initial Developer. All Rights Reserved.
21
22    Contributor(s):
23 * ***** END LICENSE BLOCK ***** */
24
25var EXPORTED_SYMBOLS = ["vIprefs", "prefroot"]
26
27const {classes: Cc, interfaces: Ci, utils: Cu, results : Cr} = Components;
28
29Cu.import("resource://v_identity/vI_log.js");
30let Log = setupLogging("virtualIdentity.prefs");
31
32prefroot = Cc["@mozilla.org/preferences-service;1"]
33  .getService(Ci.nsIPrefService)
34  .getBranch(null);
35
36//     .QueryInterface(Components.interfaces.nsIPrefBranch2),
37
38vIprefroot = Cc["@mozilla.org/preferences-service;1"]
39  .getService(Ci.nsIPrefService)
40  .getBranch("extensions.virtualIdentity.");
41
42// there should be one global prefs-object per window / context
43// it will hold all preferences, single prefs can be changed,
44// even without influencing the global pref settings
45var vIprefs = {
46    _localPrefs: [],        // array to store accessed prefs (and enable to change them)
47    _localObservers: [],    // array of local observers { pref: aPrefName, observer: aFunction }
48   
49    _retrievePref: function(aPrefName) {
50      switch (vIprefroot.getPrefType(aPrefName)) {
51        case vIprefroot.PREF_STRING:
52          this._localPrefs[aPrefName] = vIprefroot.getCharPref(aPrefName);
53          break;
54        case vIprefroot.PREF_INT:
55          this._localPrefs[aPrefName] = vIprefroot.getIntPref(aPrefName);
56          break;
57        case vIprefroot.PREF_BOOL:
58          this._localPrefs[aPrefName] = vIprefroot.getBoolPref(aPrefName);
59          break;
60        case vIprefroot.PREF_INVALID:
61          Log.error("_retrievePref pref '" + aPrefName + "' not available");
62          this._localPrefs[aPrefName] = null;
63      }
64    },
65
66    _storePref: function(aPrefName) {
67      switch (vIprefroot.getPrefType(aPrefName)) {
68        case vIprefroot.PREF_STRING:
69          vIprefroot.setCharPref(aPrefName, this._localPrefs[aPrefName]);
70          break;
71        case vIprefroot.PREF_INT:
72          vIprefroot.setIntPref(aPrefName, this._localPrefs[aPrefName]);
73          break;
74        case vIprefroot.PREF_BOOL:
75          vIprefroot.setBoolPref(aPrefName, this._localPrefs[aPrefName]);
76          break;
77        case vIprefroot.PREF_INVALID:
78          Log.error("_storePref pref '" + aPrefName + "' not available");
79      }
80    },
81
82    get: function(aPrefName) {
83      if (!(aPrefName in this._localPrefs))
84        this._retrievePref(aPrefName);
85      return this._localPrefs[aPrefName];
86    },
87    set: function(aPrefName, aPrefValue) {
88      if (!(aPrefName in this._localPrefs))
89        this._retrievePref(aPrefName);
90      this._localPrefs[aPrefName] = aPrefValue;
91//       Log.debug("changed pref " + aPrefName + " to " + aPrefValue)
92      for each (let [, prefObserver] in Iterator(this._localObservers)) {
93//         Log.debug("check prefobserver " + prefObserver.pref + " against " + aPrefName)
94        if (prefObserver.pref == aPrefName) {
95          prefObserver.observe(prefObserver.context, aPrefValue, "nsPref:changed", aPrefName);
96        }
97      }
98    },
99    commit: function(aPrefName, aPrefValue) {
100      if (aPrefValue)
101        this.set(aPrefName, aPrefValue);
102      if (aPrefName in this._localPrefs)
103        this._storePref(aPrefName);
104    },
105    clearUserPref: function(aPrefName) {
106      Log.error(Colors.red, "XXXX not yet implemented clearUserPref!", Colors.default);
107    },
108    addObserver: function(aPrefName, aFunction, aSelf) {
109      this._localObservers.push({ pref: aPrefName, observe: aFunction, context: aSelf });
110//       Log.debug("added observer for " + aPrefName);
111    },
112    removeObserver: function(aPrefName, aFunction) {
113      for each (let [i, prefObserver] in Iterator(this._localObservers)) {
114        if (prefObserver.pref == aPrefName && prefObserver.observe == aFunction) {
115          this._localObservers.splice(i,1)
116          break;
117        }
118      }
119    },
120    observe: function(subject, topic, aPrefName) {
121      Log.debug("prefChange observed : " + aPrefName)
122      this._retrievePref(aPrefName);
123      for each (let [, prefObserver] in Iterator(this._localObservers)) {
124        if (prefObserver.pref == aPrefName) {
125//           Log.debug("found observer, calling : " + prefObserver.observe)
126          prefObserver.observe(prefObserver.context, subject, topic, aPrefName);
127//           Log.debug("found observer, calling : " + prefObserver.observe + " done")
128        }
129      }
130    },
131    dropLocalChanges: function() {
132      for each (let [aPrefName, aPrefValue] in Iterator(this._localPrefs)) {
133        this._retrievePref(aPrefName);
134        if (aPrefValue != this._localPrefs[aPrefName]) {
135          for each (let [, prefObserver] in Iterator(this._localObservers)) {
136            if (prefObserver.pref == aPrefName) {
137              prefObserver.observe(prefObserver.context, aPrefValue, "nsPref:changed", aPrefName);
138            }
139          }
140        }
141      }
142    }
143}
144
145// always try to (re)init the object
146vIprefBranch2 = Cc["@mozilla.org/preferences-service;1"]
147  .getService(Ci.nsIPrefService)
148  .getBranch("extensions.virtualIdentity.")
149  .QueryInterface(Components.interfaces.nsIPrefBranch2);
150vIprefBranch2.addObserver("", vIprefs, false);
151
152// mainWindow = Cc["@mozilla.org/appshell/window-mediator;1"]
153//   .getService(Ci.nsIWindowMediator)
154//   .getMostRecentWindow(null);
155// mainWindow.addEventListener("unload", function () {
156//     vIprefBranch2.removeObserver("", vIprefs, false);
157//   }, false);
Note: See TracBrowser for help on using the repository browser.