home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / browser.jar / content / browser / pref / nsPrefWindow.js next >
Encoding:
Text File  |  2005-07-29  |  12.8 KB  |  344 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.             // MERC (rpaul) after pref change update statusbar engine icon
  81.             top.opener.UpdateStatusBarEngineIcon(top.opener.gBrowser.mCurrentTab);
  82.           },
  83.         
  84.       onCancel:
  85.         function ()
  86.           {
  87.             for( var i = 0; i < hPrefWindow.cancelHandlers.length; i++ )
  88.               {
  89.                 hPrefWindow.cancelHandlers[i]();
  90.               }
  91.           },
  92.  
  93.       registerOKCallbackFunc:
  94.         function ( aFunctionReference )
  95.           { 
  96.             this.okHandlers[this.okHandlers.length] = aFunctionReference;
  97.           },
  98.  
  99.       registerCancelCallbackFunc:
  100.         function ( aFunctionReference )
  101.           {
  102.             this.cancelHandlers[this.cancelHandlers.length] = aFunctionReference;
  103.           },
  104.           
  105.       getPrefIsLocked:
  106.         function ( aPrefString )
  107.           {
  108.             return hPrefWindow.pref.PrefIsLocked(aPrefString);
  109.           },
  110.       getPref:
  111.         function ( aPrefType, aPrefString, aDefaultFlag )
  112.           {
  113.             var pref = hPrefWindow.pref;
  114.             try
  115.               {
  116.                 switch ( aPrefType )
  117.                   {
  118.                     case "bool":
  119.                       return !aDefaultFlag ? pref.GetBoolPref( aPrefString ) : pref.GetDefaultBoolPref( aPrefString );
  120.                     case "int":
  121.                       return !aDefaultFlag ? pref.GetIntPref( aPrefString ) : pref.GetDefaultIntPref( aPrefString );
  122.                     case "localizedstring":
  123.                       return pref.getLocalizedUnicharPref( aPrefString );
  124.                     case "color":
  125.                     case "string":
  126.                     default:
  127.                          return !aDefaultFlag ? pref.CopyUnicharPref( aPrefString ) : pref.CopyDefaultUnicharPref( aPrefString );
  128.                   }
  129.               }
  130.             catch (e)
  131.               {
  132.                 if( _DEBUG ) 
  133.                   {
  134.                     dump("*** no default pref for " + aPrefType + " pref: " + aPrefString + "\n");
  135.                     dump(e + "\n");
  136.                   }
  137.               }
  138.             return "!/!ERROR_UNDEFINED_PREF!/!";
  139.           }    ,
  140.  
  141.       setPref:
  142.         function ( aPrefType, aPrefString, aValue )
  143.           {
  144.             try
  145.               {
  146.                 switch ( aPrefType )
  147.                   {
  148.                     case "bool":
  149.                       hPrefWindow.pref.SetBoolPref( aPrefString, aValue );
  150.                       break;
  151.                     case "int":
  152.                       hPrefWindow.pref.SetIntPref( aPrefString, aValue );
  153.                       break;
  154.                     case "color":
  155.                     case "string":
  156.                     case "localizedstring":
  157.                     default:
  158.                       hPrefWindow.pref.SetUnicharPref( aPrefString, aValue );
  159.                       break;
  160.                   }
  161.               }
  162.             catch (e)
  163.               {
  164.                 dump(e + "\n");
  165.               }
  166.           },
  167.           
  168.       savePrefs:
  169.         function ()
  170.           {
  171.             for( var pageTag in this.wsm.dataManager.pageData )
  172.               {
  173.                 var pageData = this.wsm.dataManager.getPageData( pageTag );
  174.                 if ("initialized" in pageData && pageData.initialized && "elementIDs" in pageData)
  175.                   {
  176.                 for( var elementID in pageData.elementIDs )
  177.                   {
  178.                     var itemObject = pageData.elementIDs[elementID];
  179.                     if ( "prefstring" in itemObject && itemObject.prefstring )
  180.                       {
  181.                         var elt = itemObject.localname;
  182.                         var prefattribute = itemObject.prefattribute;
  183.                         if (!prefattribute) {
  184.                           if (elt == "checkbox")
  185.                             prefattribute = "checked";
  186.                           else if (elt == "button")
  187.                             prefattribute = "disabled";
  188.                           else
  189.                             prefattribute = "value";
  190.                         }
  191.                         
  192.                         var value = itemObject[prefattribute];
  193.                         var preftype = itemObject.preftype;
  194.                         if (!preftype) {
  195.                           if (elt == "textbox")
  196.                             preftype = "string";
  197.                           else if (elt == "checkbox" || elt == "button")
  198.                             preftype = "bool";
  199.                           else if (elt == "radiogroup" || elt == "menulist")
  200.                             preftype = "int";
  201.                         }
  202.                         switch( preftype )
  203.                           {
  204.                             case "bool":
  205.                               if( value == "true" && typeof(value) == "string" )
  206.                                 value = true;
  207.                               else if( value == "false" && typeof(value) == "string" )
  208.                                 value = false;
  209.                               break;
  210.                             case "int":
  211.                               value = parseInt(value);                              
  212.                               break;
  213.                             case "color":
  214.                               if( toString(value) == "" )
  215.                                 {
  216.                                   dump("*** ERROR CASE: illegal attempt to set an empty color pref. ignoring.\n");
  217.                                   break;
  218.                                 }
  219.                             case "string":
  220.                             case "localizedstring":
  221.                             default:
  222.                               if( typeof(value) != "string" )
  223.                                 {
  224.                                   value = toString(value);
  225.                                 }
  226.                               break;
  227.                           }
  228.  
  229.                         if( value != this.getPref( preftype, itemObject.prefstring ) )
  230.                           {
  231.                             this.setPref( preftype, itemObject.prefstring, value );
  232.                           }
  233.                       }
  234.                   }
  235.               }
  236.               }
  237.               try 
  238.                 {
  239.                   this.pref.savePrefFile(null);
  240.                 }
  241.               catch (e)
  242.                 {
  243.                   try
  244.                     {
  245.                       var prefUtilBundle = document.getElementById("bundle_prefutilities");
  246.                       var alertText = prefUtilBundle.getString("prefSaveFailedAlert");
  247.                       var titleText = prefUtilBundle.getString("prefSaveFailedTitle");
  248.                       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  249.                                                     .getService(Components.interfaces.nsIPromptService);
  250.                       promptService.alert(window, titleText, alertText);
  251.                     }
  252.                   catch (e)
  253.                     {
  254.                       dump(e + "\n");
  255.                     }
  256.                 }
  257.           },                        
  258.  
  259.       switchPage:
  260.         function (aNewURL, aNewTag)
  261.           {
  262.             var oldURL = document.getElementById( this.contentFrame ).getAttribute("tag");
  263.             if( !oldURL )
  264.               {
  265.                 oldURL = document.getElementById( this.contentFrame ).getAttribute("src");
  266.               }
  267.             this.wsm.savePageData( oldURL, null );      // save data from the current page. 
  268.             if( aNewURL != oldURL )
  269.               {
  270.                 document.getElementById( this.contentFrame ).setAttribute( "src", aNewURL );
  271.                 if( !aNewTag )
  272.                   document.getElementById( this.contentFrame ).removeAttribute( "tag" );
  273.                 else
  274.                   document.getElementById( this.contentFrame ).setAttribute( "tag", aNewTag );
  275.               }
  276.           },
  277.               
  278.       onpageload: 
  279.         function ( aPageTag, aWindow )
  280.           {
  281.             if (!aWindow)
  282.               aWindow = window.frames[this.contentFrame];
  283.             
  284.             // Only update the header title for panels that are loaded in the main dialog, 
  285.             // not sub-dialogs. (Removing this check will cause the header display area to
  286.             // be cleared whenever you open a sub-dialog that uses WSM)
  287.             if (aWindow == window.frames[this.contentFrame]) {
  288.               var header = document.getElementById("header");
  289.               header.setAttribute("title",
  290.                                   aWindow.document.documentElement.getAttribute("headertitle"));
  291.             }
  292.             
  293.             var pageData = this.wsm.dataManager.getPageData(aPageTag);
  294.             if(!('initialized' in pageData))
  295.               {
  296.                 var prefElements = aWindow.document.getElementsByAttribute( "prefstring", "*" );
  297.                 
  298.                 for( var i = 0; i < prefElements.length; i++ )
  299.                   {
  300.                     var prefstring    = prefElements[i].getAttribute( "prefstring" );
  301.                     var prefid        = prefElements[i].getAttribute( "id" );
  302.                     var preftype      = prefElements[i].getAttribute( "preftype" );
  303.                     var elt = prefElements[i].localName;
  304.                     if (!preftype) {
  305.                       if (elt == "textbox")
  306.                         preftype = "string";
  307.                       else if (elt == "checkbox" || elt == "button")
  308.                         preftype = "bool";
  309.                       else if (elt == "radiogroup" || elt == "menulist")
  310.                         preftype = "int";
  311.                     }
  312.                     var prefdefval    = prefElements[i].getAttribute( "prefdefval" );
  313.                     var prefattribute = prefElements[i].getAttribute( "prefattribute" );
  314.                     if (!prefattribute) {
  315.                       if (elt == "checkbox")
  316.                         prefattribute = "checked";
  317.                       else if (elt == "button")
  318.                         prefattribute = "disabled";
  319.                       else
  320.                         prefattribute = "value";
  321.                     }
  322.                     var prefvalue = this.getPref( preftype, prefstring );
  323.                     if( prefvalue == "!/!ERROR_UNDEFINED_PREF!/!" )
  324.                       {
  325.                         prefvalue = prefdefval;
  326.                       }
  327.                     var root = this.wsm.dataManager.getItemData( aPageTag, prefid ); 
  328.                     root[prefattribute] = prefvalue;              
  329.                     var isPrefLocked = this.getPrefIsLocked(prefstring);
  330.                     if (isPrefLocked)
  331.                       root.disabled = "true";
  332.                     root.localname = prefElements[i].localName;
  333.                   }
  334.               }      
  335.             this.wsm.setPageData( aPageTag, aWindow );  // do not set extra elements, accept hard coded defaults
  336.             if( 'Startup' in aWindow)
  337.               {
  338.                 aWindow.Startup();
  339.               }
  340.             this.wsm.dataManager.pageData[aPageTag].initialized = true;
  341.           }
  342.   };
  343.  
  344.