home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2005 October / maximum-cd-2005-10.iso / Software / Apps / FirefoxSetup1.0.6.exe / browser.xpi / bin / chrome / browser.jar / content / browser / pref / nsPrefWindow.js next >
Encoding:
Text File  |  2004-07-08  |  13.0 KB  |  342 lines

  1.  
  2. const _DEBUG = false; 
  3.  
  4. /** PrefWindow IV
  5.  *  =============
  6.  *  This is a general page switcher and pref loader.
  7.  *  =>> CHANGES MUST BE REVIEWED BY ben@netscape.com!! <<=
  8.  **/ 
  9.  
  10. var queuedTag; 
  11. var queuedWindow;
  12. function initPanel ( aPrefTag, aWindow )
  13.   {
  14.     if( hPrefWindow )
  15.       hPrefWindow.onpageload( aPrefTag, aWindow )
  16.     else {
  17.       queuedTag = aPrefTag;
  18.       queuedWindow = aWindow;
  19.     }
  20.   } 
  21.  
  22. window.doneLoading = false; 
  23.  
  24. function nsPrefWindow( frame_id )
  25. {
  26.   if ( !frame_id )
  27.     throw "Error: frame_id not supplied!";
  28.  
  29.   this.contentFrame   = frame_id;
  30.   this.wsm            = new nsWidgetStateManager( frame_id );
  31.   this.wsm.attributes = ["preftype", "prefstring", "prefattribute", "disabled"];
  32.   this.pref           = null;
  33.   
  34.   this.cancelHandlers = [];
  35.   this.okHandlers     = [];  
  36.     
  37.   // set up window
  38.   this.onload();
  39. }
  40.  
  41. nsPrefWindow.prototype =
  42.   {
  43.     onload:
  44.       function ()
  45.         {
  46.           try 
  47.             {
  48.               this.pref = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
  49.             }
  50.           catch(e) 
  51.             {
  52.               dump("*** Failed to create prefs object\n");
  53.               return;
  54.             }
  55.         },
  56.  
  57.       init: 
  58.         function ()
  59.           {        
  60.             if( window.queuedTag )
  61.               {
  62.                 this.onpageload( window.queuedTag, window.queuedWindow );
  63.               }
  64.           },
  65.                   
  66.       onOK:
  67.         function ()
  68.           {
  69.             var tag = document.getElementById( hPrefWindow.contentFrame ).getAttribute("tag");
  70.             if( tag == "" )
  71.               {
  72.                 tag = document.getElementById( hPrefWindow.contentFrame ).getAttribute("src");
  73.               }
  74.             hPrefWindow.wsm.savePageData( tag, null );
  75.             for( var i = 0; i < hPrefWindow.okHandlers.length; i++ )
  76.               {
  77.                 hPrefWindow.okHandlers[i]();
  78.               }
  79.             hPrefWindow.savePrefs();
  80.           },
  81.         
  82.       onCancel:
  83.         function ()
  84.           {
  85.             for( var i = 0; i < hPrefWindow.cancelHandlers.length; i++ )
  86.               {
  87.                 hPrefWindow.cancelHandlers[i]();
  88.               }
  89.           },
  90.  
  91.       registerOKCallbackFunc:
  92.         function ( aFunctionReference )
  93.           { 
  94.             this.okHandlers[this.okHandlers.length] = aFunctionReference;
  95.           },
  96.  
  97.       registerCancelCallbackFunc:
  98.         function ( aFunctionReference )
  99.           {
  100.             this.cancelHandlers[this.cancelHandlers.length] = aFunctionReference;
  101.           },
  102.           
  103.       getPrefIsLocked:
  104.         function ( aPrefString )
  105.           {
  106.             return hPrefWindow.pref.PrefIsLocked(aPrefString);
  107.           },
  108.       getPref:
  109.         function ( aPrefType, aPrefString, aDefaultFlag )
  110.           {
  111.             var pref = hPrefWindow.pref;
  112.             try
  113.               {
  114.                 switch ( aPrefType )
  115.                   {
  116.                     case "bool":
  117.                       return !aDefaultFlag ? pref.GetBoolPref( aPrefString ) : pref.GetDefaultBoolPref( aPrefString );
  118.                     case "int":
  119.                       return !aDefaultFlag ? pref.GetIntPref( aPrefString ) : pref.GetDefaultIntPref( aPrefString );
  120.                     case "localizedstring":
  121.                       return pref.getLocalizedUnicharPref( aPrefString );
  122.                     case "color":
  123.                     case "string":
  124.                     default:
  125.                          return !aDefaultFlag ? pref.CopyUnicharPref( aPrefString ) : pref.CopyDefaultUnicharPref( aPrefString );
  126.                   }
  127.               }
  128.             catch (e)
  129.               {
  130.                 if( _DEBUG ) 
  131.                   {
  132.                     dump("*** no default pref for " + aPrefType + " pref: " + aPrefString + "\n");
  133.                     dump(e + "\n");
  134.                   }
  135.               }
  136.             return "!/!ERROR_UNDEFINED_PREF!/!";
  137.           }    ,
  138.  
  139.       setPref:
  140.         function ( aPrefType, aPrefString, aValue )
  141.           {
  142.             try
  143.               {
  144.                 switch ( aPrefType )
  145.                   {
  146.                     case "bool":
  147.                       hPrefWindow.pref.SetBoolPref( aPrefString, aValue );
  148.                       break;
  149.                     case "int":
  150.                       hPrefWindow.pref.SetIntPref( aPrefString, aValue );
  151.                       break;
  152.                     case "color":
  153.                     case "string":
  154.                     case "localizedstring":
  155.                     default:
  156.                       hPrefWindow.pref.SetUnicharPref( aPrefString, aValue );
  157.                       break;
  158.                   }
  159.               }
  160.             catch (e)
  161.               {
  162.                 dump(e + "\n");
  163.               }
  164.           },
  165.           
  166.       savePrefs:
  167.         function ()
  168.           {
  169.             for( var pageTag in this.wsm.dataManager.pageData )
  170.               {
  171.                 var pageData = this.wsm.dataManager.getPageData( pageTag );
  172.                 if ("initialized" in pageData && pageData.initialized && "elementIDs" in pageData)
  173.                   {
  174.                 for( var elementID in pageData.elementIDs )
  175.                   {
  176.                     var itemObject = pageData.elementIDs[elementID];
  177.                     if ( "prefstring" in itemObject && itemObject.prefstring )
  178.                       {
  179.                         var elt = itemObject.localname;
  180.                         var prefattribute = itemObject.prefattribute;
  181.                         if (!prefattribute) {
  182.                           if (elt == "checkbox")
  183.                             prefattribute = "checked";
  184.                           else if (elt == "button")
  185.                             prefattribute = "disabled";
  186.                           else
  187.                             prefattribute = "value";
  188.                         }
  189.                         
  190.                         var value = itemObject[prefattribute];
  191.                         var preftype = itemObject.preftype;
  192.                         if (!preftype) {
  193.                           if (elt == "textbox")
  194.                             preftype = "string";
  195.                           else if (elt == "checkbox" || elt == "button")
  196.                             preftype = "bool";
  197.                           else if (elt == "radiogroup" || elt == "menulist")
  198.                             preftype = "int";
  199.                         }
  200.                         switch( preftype )
  201.                           {
  202.                             case "bool":
  203.                               if( value == "true" && typeof(value) == "string" )
  204.                                 value = true;
  205.                               else if( value == "false" && typeof(value) == "string" )
  206.                                 value = false;
  207.                               break;
  208.                             case "int":
  209.                               value = parseInt(value);                              
  210.                               break;
  211.                             case "color":
  212.                               if( toString(value) == "" )
  213.                                 {
  214.                                   dump("*** ERROR CASE: illegal attempt to set an empty color pref. ignoring.\n");
  215.                                   break;
  216.                                 }
  217.                             case "string":
  218.                             case "localizedstring":
  219.                             default:
  220.                               if( typeof(value) != "string" )
  221.                                 {
  222.                                   value = toString(value);
  223.                                 }
  224.                               break;
  225.                           }
  226.  
  227.                         if( value != this.getPref( preftype, itemObject.prefstring ) )
  228.                           {
  229.                             this.setPref( preftype, itemObject.prefstring, value );
  230.                           }
  231.                       }
  232.                   }
  233.               }
  234.               }
  235.               try 
  236.                 {
  237.                   this.pref.savePrefFile(null);
  238.                 }
  239.               catch (e)
  240.                 {
  241.                   try
  242.                     {
  243.                       var prefUtilBundle = document.getElementById("bundle_prefutilities");
  244.                       var alertText = prefUtilBundle.getString("prefSaveFailedAlert");
  245.                       var titleText = prefUtilBundle.getString("prefSaveFailedTitle");
  246.                       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  247.                                                     .getService(Components.interfaces.nsIPromptService);
  248.                       promptService.alert(window, titleText, alertText);
  249.                     }
  250.                   catch (e)
  251.                     {
  252.                       dump(e + "\n");
  253.                     }
  254.                 }
  255.           },                        
  256.  
  257.       switchPage:
  258.         function (aNewURL, aNewTag)
  259.           {
  260.             var oldURL = document.getElementById( this.contentFrame ).getAttribute("tag");
  261.             if( !oldURL )
  262.               {
  263.                 oldURL = document.getElementById( this.contentFrame ).getAttribute("src");
  264.               }
  265.             this.wsm.savePageData( oldURL, null );      // save data from the current page. 
  266.             if( aNewURL != oldURL )
  267.               {
  268.                 document.getElementById( this.contentFrame ).setAttribute( "src", aNewURL );
  269.                 if( !aNewTag )
  270.                   document.getElementById( this.contentFrame ).removeAttribute( "tag" );
  271.                 else
  272.                   document.getElementById( this.contentFrame ).setAttribute( "tag", aNewTag );
  273.               }
  274.           },
  275.               
  276.       onpageload: 
  277.         function ( aPageTag, aWindow )
  278.           {
  279.             if (!aWindow)
  280.               aWindow = window.frames[this.contentFrame];
  281.             
  282.             // Only update the header title for panels that are loaded in the main dialog, 
  283.             // not sub-dialogs. (Removing this check will cause the header display area to
  284.             // be cleared whenever you open a sub-dialog that uses WSM)
  285.             if (aWindow == window.frames[this.contentFrame]) {
  286.               var header = document.getElementById("header");
  287.               header.setAttribute("title",
  288.                                   aWindow.document.documentElement.getAttribute("headertitle"));
  289.             }
  290.             
  291.             var pageData = this.wsm.dataManager.getPageData(aPageTag);
  292.             if(!('initialized' in pageData))
  293.               {
  294.                 var prefElements = aWindow.document.getElementsByAttribute( "prefstring", "*" );
  295.                 
  296.                 for( var i = 0; i < prefElements.length; i++ )
  297.                   {
  298.                     var prefstring    = prefElements[i].getAttribute( "prefstring" );
  299.                     var prefid        = prefElements[i].getAttribute( "id" );
  300.                     var preftype      = prefElements[i].getAttribute( "preftype" );
  301.                     var elt = prefElements[i].localName;
  302.                     if (!preftype) {
  303.                       if (elt == "textbox")
  304.                         preftype = "string";
  305.                       else if (elt == "checkbox" || elt == "button")
  306.                         preftype = "bool";
  307.                       else if (elt == "radiogroup" || elt == "menulist")
  308.                         preftype = "int";
  309.                     }
  310.                     var prefdefval    = prefElements[i].getAttribute( "prefdefval" );
  311.                     var prefattribute = prefElements[i].getAttribute( "prefattribute" );
  312.                     if (!prefattribute) {
  313.                       if (elt == "checkbox")
  314.                         prefattribute = "checked";
  315.                       else if (elt == "button")
  316.                         prefattribute = "disabled";
  317.                       else
  318.                         prefattribute = "value";
  319.                     }
  320.                     var prefvalue = this.getPref( preftype, prefstring );
  321.                     if( prefvalue == "!/!ERROR_UNDEFINED_PREF!/!" )
  322.                       {
  323.                         prefvalue = prefdefval;
  324.                       }
  325.                     var root = this.wsm.dataManager.getItemData( aPageTag, prefid ); 
  326.                     root[prefattribute] = prefvalue;              
  327.                     var isPrefLocked = this.getPrefIsLocked(prefstring);
  328.                     if (isPrefLocked)
  329.                       root.disabled = "true";
  330.                     root.localname = prefElements[i].localName;
  331.                   }
  332.               }      
  333.             this.wsm.setPageData( aPageTag, aWindow );  // do not set extra elements, accept hard coded defaults
  334.             if( 'Startup' in aWindow)
  335.               {
  336.                 aWindow.Startup();
  337.               }
  338.             this.wsm.dataManager.pageData[aPageTag].initialized = true;
  339.           }
  340.   };
  341.  
  342.