/* prefarray.js   v1.0
 * Allows you to store arrays in Mozilla preferences easily.
 * Copyright (c) 2005 Nickolay Ponomarev <asqueella@gmail.com>
 * Thanks to David Vasilevsky for the idea and initial implementation.
 *
 * WARNING: This code under development.
 *
 * You may use this code in any way you want, but please send your changes back
 * to me if you think they may be useful to anybody else. Also please change
 * the object's name if you change the code before using it in an extension.
 *
 * If you decide to use it intact in your code, please drop me a line.
 *
 * - This extends PrefsWrapper1, so you must include prefutils.js 
 *   *before* this file
 * - We store array in a subbranch. Value of each item is stored in a separate
 *   pref in that branch, name of it equals the index of the item in the array.
 * - aType parameter is case-sensitive, the following values are allowed:
 *     "Int", "Char", "Bool", "Unichar", "File"
 *   Each "leaf" item in the array must be of a compatible type. I.e. each item
 *   in the array must be either an array or an item of specified type. Example:
 *    [1, [2,[3]], 4] is an example of array that can be stored with "Int" type
 * - Caveat: to delete an array pref, you must use deleteBranch instead of 
 *   clearUserPref.
 *
 * Quick Examples:
 *  // include prefutils.js
 *  // include prefarray.js
 *  prefs = new PrefsWrapper1("extensions.myext.");
 *  // #1  writing an array of strings
 *  prefs.setArrayPref("array2", ["one", "two", "three"], "Unichar");
 *  // #2  Slightly more complex
 *  prefs.setArrayPref("array3", [["one"], ["two", "three"]], "Unichar");
 *  // #3  reading an array of strings
 *  alert(prefs.getArrayPref("array2", "Unichar").toSource());
 *  // #4  reading individual elements
 *  alert(prefs.getUnicharPref("array2.0"));   // alerts "one"
 *  alert(prefs.getUnicharPref("array3.1.0")); // alerts "two"
 *  // #5  clear user value
 *  prefs.deleteBranch("array2");
 *
 * Documentation is available at http://mozilla.doslash.org/prefutils
 */

// Do not define the same version twice
if(typeof(PrefsWrapper1) == "function" && 
   !("getArrayPref" in PrefsWrapper1.prototype)) {

  PrefsWrapper1.prototype.getArrayPref = function(aPrefName, aType) {
    var result = [];
    var subBranch = this.getSubBranch(aPrefName + ".");
    var items = subBranch.getChildList("", {});
    for(var i in items) {
      var path = items[i].split(".");
      var lastIdx = path.length - 1;
      var node = result;
      for(var j in path) {
        if(!(path[j] in node)) {
          if(j != lastIdx)
            node[path[j]] = [];
          else
            node[path[j]] = subBranch["get" + aType + "Pref"](items[i]); // leaf
        }
        node = node[path[j]];
      }
    }
    return result;
  };

  PrefsWrapper1.prototype.setArrayPref = function(aPrefName, aArray, aType) {
    var subBranch = this.getSubBranch(aPrefName + ".");
    subBranch.deleteBranch("");
    // get rid of recursion?
    for(var i in aArray) {
      if(aArray[i] instanceof Array) { // a subarray
        subBranch.setArrayPref(i, aArray[i], aType);
      } else { // scalar type
        subBranch["set" + aType + "Pref"](i, aArray[i]);
      }
    }
  };
}

