home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 March / PCpro_2006_03.ISO / files / freeware / greasemonkey-0.6.4-fx.xpi / chrome / chromeFiles / content / prefmanager.js < prev    next >
Encoding:
Text File  |  2005-11-30  |  3.4 KB  |  126 lines

  1.  
  2.  
  3. var GM_prefRoot = new GM_PrefManager();
  4.  
  5. /**
  6.  * Simple API on top of preferences for greasemonkey. 
  7.  * Construct an instance by passing the startPoint of a preferences subtree. 
  8.  * "greasemonkey." prefix is assumed.
  9.  */
  10. function GM_PrefManager(startPoint) {
  11.   if (!startPoint) {
  12.     startPoint = "";
  13.   }
  14.   
  15.   startPoint = "greasemonkey." + startPoint;
  16.  
  17.   var pref = Components.classes["@mozilla.org/preferences-service;1"].
  18.                 getService(Components.interfaces.nsIPrefService).
  19.                 getBranch(startPoint);
  20.  
  21.   var observers = {};
  22.  
  23.   /**
  24.    * whether a preference exists
  25.    */   
  26.   this.exists = function(prefName) {
  27.     return pref.getPrefType(prefName) != 0;
  28.   }
  29.  
  30.   /**
  31.    * returns the named preference, or defaultValue if it does not exist
  32.    */
  33.   this.getValue = function(prefName, defaultValue) {
  34.     var prefType = pref.getPrefType(prefName);
  35.  
  36.     // underlying preferences object throws an exception if pref doesn't exist
  37.     if (prefType == pref.PREF_INVALID) {
  38.       return defaultValue;
  39.     }
  40.     
  41.     switch (prefType) {
  42.       case pref.PREF_STRING:
  43.         return pref.getCharPref(prefName);
  44.       case pref.PREF_BOOL:
  45.         return pref.getBoolPref(prefName);
  46.       case pref.PREF_INT:
  47.         return pref.getIntPref(prefName);
  48.     }
  49.   }
  50.   
  51.   /**
  52.    * sets the named preference to the specified value. values must be strings,
  53.    * booleans, or integers.
  54.    */
  55.   this.setValue = function(prefName, value) {
  56.     var prefType = typeof(value);
  57.     
  58.     switch (prefType) {
  59.       case "string":
  60.       case "boolean":
  61.         break;
  62.       case "number":
  63.         if (value % 1 != 0) {
  64.           throw new Error("Cannot set preference to non integral number");
  65.         }
  66.         break;
  67.       default:
  68.         throw new Error("Cannot set preference with datatype: " + prefType);
  69.     }
  70.     
  71.     // underlying preferences object throws an exception if new pref has a 
  72.     // different type than old one. i think we should not do this, so delete
  73.     // old pref first if this is the case.
  74.     if (this.exists(prefName) && prefType != typeof(this.getValue(prefName))) {
  75.       this.remove(prefName);
  76.     }
  77.     
  78.     // set new value using correct method
  79.     switch (prefType) {
  80.       case "string":
  81.         pref.setCharPref(prefName, value);
  82.         break;
  83.       case "boolean":
  84.         pref.setBoolPref(prefName, value);
  85.         break;
  86.       case "number":
  87.         pref.setIntPref(prefName, Math.floor(value));
  88.         break;
  89.     }
  90.   }
  91.   
  92.   /**
  93.    * deletes the named preference or subtree
  94.    */
  95.   this.remove = function(prefName) {
  96.     pref.deleteBranch(prefName);
  97.   }
  98.   
  99.   /**
  100.    * call a function whenever the named preference subtree changes
  101.    */
  102.   this.watch = function(prefName, watcher) {
  103.     // construct an observer
  104.     var observer = {
  105.       observe:function(subject, topic, prefName) {
  106.         watcher(prefName);
  107.       }
  108.     };
  109.     
  110.     // store the observer in case we need to remove it later
  111.     observers[watcher] = observer;
  112.  
  113.     pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal).
  114.       addObserver(prefName, observer, false);
  115.   }
  116.   
  117.   /**
  118.    * stop watching
  119.    */
  120.   this.unwatch = function(prefName, watcher) {
  121.     if (observers[watcher]) {
  122.       pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal).
  123.         removeObserver(prefName, observers[watcher]);
  124.     }
  125.   }
  126. }