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

source: modules/vI_prefs.js @ 509348

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

code formatting (no code changes)

  • Property mode set to 100644
File size: 6.0 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 {
28  classes: Cc,
29  interfaces: Ci,
30  utils: Cu,
31  results: Cr
32} = Components;
33
34Cu.import("resource://v_identity/vI_log.js");
35let Log = setupLogging("virtualIdentity.prefs");
36
37prefroot = Cc["@mozilla.org/preferences-service;1"]
38  .getService(Ci.nsIPrefService)
39  .getBranch(null);
40
41//     .QueryInterface(Components.interfaces.nsIPrefBranch2),
42
43vIprefroot = Cc["@mozilla.org/preferences-service;1"]
44  .getService(Ci.nsIPrefService)
45  .getBranch("extensions.virtualIdentity.");
46
47// there should be one global prefs-object per window / context
48// it will hold all preferences, single prefs can be changed,
49// even without influencing the global pref settings
50var vIprefs = {
51  _localPrefs: [], // array to store accessed prefs (and enable to change them)
52  _localObservers: [], // array of local observers { pref: aPrefName, observer: aFunction }
53
54  _retrievePref: function (aPrefName) {
55    switch (vIprefroot.getPrefType(aPrefName)) {
56    case vIprefroot.PREF_STRING:
57      this._localPrefs[aPrefName] = vIprefroot.getCharPref(aPrefName);
58      break;
59    case vIprefroot.PREF_INT:
60      this._localPrefs[aPrefName] = vIprefroot.getIntPref(aPrefName);
61      break;
62    case vIprefroot.PREF_BOOL:
63      this._localPrefs[aPrefName] = vIprefroot.getBoolPref(aPrefName);
64      break;
65    case vIprefroot.PREF_INVALID:
66      Log.error("_retrievePref pref '" + aPrefName + "' not available");
67      this._localPrefs[aPrefName] = null;
68    }
69  },
70
71  _storePref: function (aPrefName) {
72    switch (vIprefroot.getPrefType(aPrefName)) {
73    case vIprefroot.PREF_STRING:
74      vIprefroot.setCharPref(aPrefName, this._localPrefs[aPrefName]);
75      break;
76    case vIprefroot.PREF_INT:
77      vIprefroot.setIntPref(aPrefName, this._localPrefs[aPrefName]);
78      break;
79    case vIprefroot.PREF_BOOL:
80      vIprefroot.setBoolPref(aPrefName, this._localPrefs[aPrefName]);
81      break;
82    case vIprefroot.PREF_INVALID:
83      Log.error("_storePref pref '" + aPrefName + "' not available");
84    }
85  },
86
87  get: function (aPrefName) {
88    if (!(aPrefName in this._localPrefs))
89      this._retrievePref(aPrefName);
90    return this._localPrefs[aPrefName];
91  },
92  set: function (aPrefName, aPrefValue) {
93    if (!(aPrefName in this._localPrefs))
94      this._retrievePref(aPrefName);
95    this._localPrefs[aPrefName] = aPrefValue;
96    //       Log.debug("changed pref " + aPrefName + " to " + aPrefValue)
97    for each(let [, prefObserver] in Iterator(this._localObservers)) {
98      //         Log.debug("check prefobserver " + prefObserver.pref + " against " + aPrefName)
99      if (prefObserver.pref == aPrefName) {
100        prefObserver.observe(prefObserver.context, aPrefValue, "nsPref:changed", aPrefName);
101      }
102    }
103  },
104  commit: function (aPrefName, aPrefValue) {
105    if (aPrefValue)
106      this.set(aPrefName, aPrefValue);
107    if (aPrefName in this._localPrefs)
108      this._storePref(aPrefName);
109  },
110  clearUserPref: function (aPrefName) {
111    Log.error(Colors.red, "XXXX not yet implemented clearUserPref!", Colors.default);
112  },
113  addObserver: function (aPrefName, aFunction, aSelf) {
114    this._localObservers.push({
115      pref: aPrefName,
116      observe: aFunction,
117      context: aSelf
118    });
119    //       Log.debug("added observer for " + aPrefName);
120  },
121  removeObserver: function (aPrefName, aFunction) {
122    for each(let [i, prefObserver] in Iterator(this._localObservers)) {
123      if (prefObserver.pref == aPrefName && prefObserver.observe == aFunction) {
124        this._localObservers.splice(i, 1)
125        break;
126      }
127    }
128  },
129  observe: function (subject, topic, aPrefName) {
130    Log.debug("prefChange observed : " + aPrefName)
131    this._retrievePref(aPrefName);
132    for each(let [, prefObserver] in Iterator(this._localObservers)) {
133      if (prefObserver.pref == aPrefName) {
134        //           Log.debug("found observer, calling : " + prefObserver.observe)
135        prefObserver.observe(prefObserver.context, subject, topic, aPrefName);
136        //           Log.debug("found observer, calling : " + prefObserver.observe + " done")
137      }
138    }
139  },
140  dropLocalChanges: function () {
141    for each(let [aPrefName, aPrefValue] in Iterator(this._localPrefs)) {
142      this._retrievePref(aPrefName);
143      if (aPrefValue != this._localPrefs[aPrefName]) {
144        for each(let [, prefObserver] in Iterator(this._localObservers)) {
145          if (prefObserver.pref == aPrefName) {
146            prefObserver.observe(prefObserver.context, aPrefValue, "nsPref:changed", aPrefName);
147          }
148        }
149      }
150    }
151  }
152}
153
154// always try to (re)init the object
155vIprefBranch2 = Cc["@mozilla.org/preferences-service;1"]
156  .getService(Ci.nsIPrefService)
157  .getBranch("extensions.virtualIdentity.")
158  .QueryInterface(Components.interfaces.nsIPrefBranch2);
159vIprefBranch2.addObserver("", vIprefs, false);
160
161// mainWindow = Cc["@mozilla.org/appshell/window-mediator;1"]
162//   .getService(Ci.nsIWindowMediator)
163//   .getMostRecentWindow(null);
164// mainWindow.addEventListener("unload", function () {
165//     vIprefBranch2.removeObserver("", vIprefs, false);
166//   }, false);
Note: See TracBrowser for help on using the repository browser.