add a test, use Services
[mandelbrot.git] / content / mandelbrot.js
index 68cdffe30e20cb902589518683a53182fefd3903..7fd6b6e6f554f4f33b7dfa0e590ca55440fa8b78 100644 (file)
  *
  * ***** END LICENSE BLOCK ***** */
 
+Components.utils.import("resource://gre/modules/Services.jsm");
+
 var gColorPalette = [];
-var gPref = Components.classes["@mozilla.org/preferences-service;1"]
-                      .getService(Components.interfaces.nsIPrefService)
-                      .getBranch(null);
 var gStartTime = 0;
 var gMbrotBundle;
 var gCurrentImageData;
@@ -61,6 +60,8 @@ function Startup() {
   img.addEventListener("touchcancel", imgEvHandler, false);
   img.addEventListener("touchleave", imgEvHandler, false);
   img.addEventListener("touchmove", imgEvHandler, false);
+
+  Services.obs.notifyObservers(window, "mandelbrot-loaded", null);
 }
 
 function getAdjustPref(prefname) {
@@ -70,48 +71,48 @@ function getAdjustPref(prefname) {
     case "image.height":
       value = 0;
       try {
-        value = gPref.getIntPref("mandelbrot." + prefname);
+        value = Services.prefs.getIntPref("mandelbrot." + prefname);
       }
       catch (e) { }
       if ((value < 10) || (value > 5000)) {
         value = 300;
-        gPref.setIntPref("mandelbrot." + prefname, value);
+        Services.prefs.setIntPref("mandelbrot." + prefname, value);
       }
       return value;
     case "last_image.Cr_*":
       let Cr_min = -2.0;
       let Cr_max = 1.0;
       try {
-        Cr_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"));
-        Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
+        Cr_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_min"));
+        Cr_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_max"));
       }
       catch (e) { }
       if ((Cr_min < -3) || (Cr_min > 2) ||
           (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
         Cr_min = -2.0; Cr_max = 1.0;
       }
-      gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
-      gPref.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
+      Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
+      Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
       return {Cr_min: Cr_min, Cr_max: Cr_max};
     case "last_image.Ci_*":
       let Ci_min = -1.5;
       let Ci_max = 1.5;
       try {
-        Ci_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"));
-        Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
+        Ci_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_min"));
+        Ci_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_max"));
       }
       catch (e) { }
       if ((Ci_min < -2.5) || (Ci_min > 2.5) ||
           (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) {
         Ci_min = -1.5; Ci_max = 1.5;
       }
-      gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
-      gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
+      Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
+      Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
       return {Ci_min: Ci_min, Ci_max: Ci_max};
     case "iteration_max":
       value = 500;
       try {
-        value = gPref.getIntPref("mandelbrot." + prefname);
+        value = Services.prefs.getIntPref("mandelbrot." + prefname);
       }
       catch (e) {
         setIter(value);
@@ -124,7 +125,7 @@ function getAdjustPref(prefname) {
     case "use_algorithm":
       value = "numeric";
       try {
-        value = gPref.getCharPref("mandelbrot." + prefname);
+        value = Services.prefs.getCharPref("mandelbrot." + prefname);
       }
       catch (e) {
         setAlgorithm(value);
@@ -133,7 +134,7 @@ function getAdjustPref(prefname) {
    case "color_palette":
       value = "kairo";
       try {
-        value = gPref.getCharPref("mandelbrot." + prefname);
+        value = Services.prefs.getCharPref("mandelbrot." + prefname);
       }
       catch(e) {
         setPalette(value);
@@ -142,10 +143,10 @@ function getAdjustPref(prefname) {
    case "syncProportions":
       value = true;
       try {
-        value = gPref.getBoolPref("mandelbrot." + prefname);
+        value = Services.prefs.getBoolPref("mandelbrot." + prefname);
       }
       catch(e) {
-        gPref.setBoolPref("mandelbrot." + prefname, value);
+        Services.prefs.setBoolPref("mandelbrot." + prefname, value);
       }
       return value;
     default:
@@ -179,10 +180,10 @@ function adjustCoordsAndDraw(aC_min, aC_max) {
 
   let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight);
 
-  gPref.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2);
-  gPref.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2);
-  gPref.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2);
-  gPref.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2);
+  Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2);
+  Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2);
+  Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2);
+  Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2);
 
   drawImage();
 }
@@ -620,7 +621,7 @@ function callBookmark(evtarget) {
     connection.close();
 
     if (iterMax && C_min && C_max) {
-      gPref.setIntPref("mandelbrot.iteration_max", iterMax);
+      Services.prefs.setIntPref("mandelbrot.iteration_max", iterMax);
       adjustCoordsAndDraw(C_min, C_max);
     }
   }
@@ -686,7 +687,7 @@ function updateIterMenu() {
 }
 
 function setIter(aIter) {
-  gPref.setIntPref("mandelbrot.iteration_max", aIter);
+  Services.prefs.setIntPref("mandelbrot.iteration_max", aIter);
 }
 
 function updatePaletteMenu() {
@@ -708,7 +709,7 @@ function updatePaletteMenu() {
 }
 
 function setPalette(aPaletteID) {
-  gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
+  Services.prefs.setCharPref("mandelbrot.color_palette", aPaletteID);
   gColorPalette = getColorPalette(aPaletteID);
 }
 
@@ -729,12 +730,12 @@ function updateDebugMenu() {
   let scope = (document.getElementById("mandelbrotWindow").nodeName == "page") ? "content" : "chrome";
   try {
     // This throws in versions that don't have JaegerMonkey yet --> catch block
-    gPref.getBoolPref("javascript.options.methodjit." + scope);
+    Services.prefs.getBoolPref("javascript.options.methodjit." + scope);
 
     // We have JaegerMonkey, i.e. two prefs for trace/method JIT
     for each (let type in ["tracejit", "methodjit"]) {
       let jitMenuItem = document.getElementById(type + "Enabled");
-      jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options." + type + "." + scope));
+      jitMenuItem.setAttribute("checked", Services.prefs.getBoolPref("javascript.options." + type + "." + scope));
     }
   }
   catch (e) {
@@ -743,15 +744,15 @@ function updateDebugMenu() {
       document.getElementById(type + "Enabled").hidden = true;
     let jitMenuItem = document.getElementById("jitEnabled");
     jitMenuItem.hidden = false;
-    jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit." + scope));
+    jitMenuItem.setAttribute("checked", Services.prefs.getBoolPref("javascript.options.jit." + scope));
   }
 }
 
 function toggleJITState(jitMenuItem, jittype) {
   let scope = (document.getElementById("mandelbrotWindow").nodeName == "page") ? "content" : "chrome";
   let jitpref = "javascript.options." + jittype + "jit." + scope;
-  let jitEnabled = !gPref.getBoolPref(jitpref);
-  gPref.setBoolPref(jitpref, jitEnabled)
+  let jitEnabled = !Services.prefs.getBoolPref(jitpref);
+  Services.prefs.setBoolPref(jitpref, jitEnabled)
   jitMenuItem.setAttribute("checked", jitEnabled ? "true" : "false");
 }
 
@@ -772,7 +773,7 @@ function updateAlgoMenu() {
 }
 
 function setAlgorithm(algoID) {
-  gPref.setCharPref("mandelbrot.use_algorithm", algoID);
+  Services.prefs.setCharPref("mandelbrot.use_algorithm", algoID);
 }
 
 function initImgSettings() {
@@ -806,15 +807,15 @@ function closeImgSettings() {
 function saveImgSettings() {
   // Get values to prefs.
   for each (let coord in ["Cr_min", "Cr_max", "Ci_min", "Ci_max"]) {
-    gPref.setCharPref("mandelbrot.last_image." + coord,
-                      document.getElementById("is_" + coord).value);
+    Services.prefs.setCharPref("mandelbrot.last_image." + coord,
+                               document.getElementById("is_" + coord).value);
   }
   for each (let dim in ["width", "height"]) {
-    gPref.setIntPref("mandelbrot.image." + dim,
-                     document.getElementById("is_img_" + dim).value);
+    Services.prefs.setIntPref("mandelbrot.image." + dim,
+                              document.getElementById("is_img_" + dim).value);
   }
-  gPref.setBoolPref("mandelbrot.syncProportions",
-                    document.getElementById("is_syncProp").checked);
+  Services.prefs.setBoolPref("mandelbrot.syncProportions",
+                             document.getElementById("is_syncProp").checked);
 }
 
 function checkISValue(textbox, type) {