/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is CachingPreferences.
 *
 * The Initial Developer of the Original Code is Mozilla.
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Myk Melez <myk@mozilla.org>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

EXPORTED_SYMBOLS = ["CachingPreferences"];

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;

function CachingPreferences(aPrefBranch) {
  if (aPrefBranch)
    this._prefBranch = aPrefBranch;
}

// Set the prototype to itself so new instances inherit the class's properties.
CachingPreferences.prototype = CachingPreferences;

CachingPreferences._prefBranch = "";
CachingPreferences._prefs = {};

// Preference Service
CachingPreferences.__defineGetter__("_prefSvc",
  function() {
    let prefSvc = Cc["@mozilla.org/preferences-service;1"].
                  getService(Ci.nsIPrefService).
                  getBranch(this._prefBranch).
                  QueryInterface(Ci.nsIPrefBranch2);
    delete this._prefSvc;
    this._prefSvc = prefSvc;
    return this._prefSvc;
  }
);

// Observer Service
CachingPreferences.__defineGetter__("_obsSvc",
  function() {
    let obsSvc = Cc["@mozilla.org/observer-service;1"].
                 getService(Ci.nsIObserverService);
    delete this._obsSvc;
    this._obsSvc = obsSvc;
    return this._obsSvc;
  }
);


/**
  * Get the value of a pref, if any; otherwise return the default value.
  *
  * @param   aPrefName      the name of the pref to get
  * @param   aDefaultValue  the default value, if any
  *
  * @returns the value of the pref, if any; otherwise the default value
  */
CachingPreferences.get =
function(aPrefName, aDefaultValue) {
  // We can't check for |aPrefName.constructor == Array| here, since we have
  // a different global object, so we check the constructor name instead.
  if (typeof aPrefName == "object" && aPrefName.constructor.name == Array.name)
    return aPrefName.map(function(v) { return this.get(v) }, this);

  let prefName = this._prefBranch + aPrefName;

  if (!(prefName in this._prefs)) {
    try {
      switch (this._prefSvc.getPrefType(prefName)) {
        case Ci.nsIPrefBranch.PREF_STRING:
          this._prefs[prefName] = this._prefSvc.getCharPref(prefName);
          break;

        case Ci.nsIPrefBranch.PREF_INT:
          this._prefs[prefName] = this._prefSvc.getIntPref(prefName);
          break;

        case Ci.nsIPrefBranch.PREF_BOOL:
          this._prefs[prefName] = this._prefSvc.getBoolPref(prefName);
          break;
      }
    }
    catch (ex) {
      this._prefs[prefName] == undefined;
    }
  }

  return (typeof this._prefs[prefName] != "undefined") ? this._prefs[prefName]
                                                       : aDefaultValue;
};

CachingPreferences.set =
function(aPrefName, aPrefValue) {
  // We can't check for |aPrefName.constructor == Object| here, since we have
  // a different global object, so we check the constructor name instead.
  if (typeof aPrefName == "object" && aPrefName.constructor.name == Object.name)
    for (let [name, value] in Iterator(aPrefName))
      this.set(name, value);
  else {
    let prefName = this._prefBranch + aPrefName;

    switch (typeof aPrefValue) {
      case "number":
        this._prefSvc.setIntPref(prefName, aPrefValue);
        break;

      case "boolean":
        this._prefSvc.setBoolPref(prefName, aPrefValue);
        break;

      case "string":
      default:
        this._prefSvc.setCharPref(prefName, aPrefValue);
        break;
    }

    this._prefs[prefName] = aPrefValue;
  }
};

// nsISupports
CachingPreferences.QueryInterface =
function(aIID) {
  if (aIID.equals(Ci.nsIObserver) ||
      aIID.equals(Ci.nsISupportsWeakReference) ||
      aIID.equals(Ci.nsISupports))
    return this;
  
  throw Cr.NS_ERROR_NO_INTERFACE;
};

// nsIObserver

CachingPreferences.observe =
function(aSubject, aTopic, aData) {
  switch (aTopic) {
    case "nsPref:changed":
      delete this._prefs[aData];
      break;

    case "profile-after-change":
      this._prefs = {};
      break;
  }
};

CachingPreferences._prefSvc.addObserver("", CachingPreferences, true);
CachingPreferences._obsSvc.addObserver(CachingPreferences, "profile-after-change", true);
