home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / communicator / pref / nsPrefWindow.js < prev    next >
Encoding:
Text File  |  2002-04-10  |  14.7 KB  |  387 lines

  1. /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*-
  2.  * 
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  *
  18.  * Contributor(s):
  19.  *   Ben "Count XULula" Goodger <ben@netscape.com>
  20.  */
  21.  
  22. const _DEBUG = false; 
  23.  
  24. /** PrefWindow IV
  25.  *  =============
  26.  *  This is a general page switcher and pref loader.
  27.  *  =>> CHANGES MUST BE REVIEWED BY ben@netscape.com!! <<=
  28.  **/ 
  29.  
  30. var queuedTag; 
  31. function initPanel ( aPrefTag )
  32.   {
  33.     if( hPrefWindow )
  34.       hPrefWindow.onpageload( aPrefTag )
  35.     else
  36.       queuedTag = aPrefTag;
  37.   } 
  38.  
  39. window.doneLoading = false; 
  40.  
  41. function nsPrefWindow( frame_id )
  42. {
  43.   if ( !frame_id )
  44.     throw "Error: frame_id not supplied!";
  45.  
  46.   this.contentFrame   = frame_id
  47.   this.wsm            = new nsWidgetStateManager( frame_id );
  48.   this.wsm.attributes = ["preftype", "prefstring", "prefattribute", "disabled"];
  49.   this.pref           = null;
  50.   
  51.   this.cancelHandlers = [];
  52.   this.okHandlers     = [];  
  53.     
  54.   // set up window
  55.   this.onload();
  56. }
  57.  
  58. nsPrefWindow.prototype =
  59.   {
  60.     onload:
  61.       function ()
  62.         {
  63.           try 
  64.             {
  65.               this.pref = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
  66.             }
  67.           catch(e) 
  68.             {
  69.               dump("*** Failed to create prefs object\n");
  70.               return;
  71.             }
  72.         },
  73.  
  74.       init: 
  75.         function ()
  76.           {        
  77.             if( window.queuedTag )
  78.               {
  79.                 this.onpageload( window.queuedTag );
  80.               }
  81.   
  82.             if( window.arguments[1] )
  83.               this.openBranch( window.arguments[1], window.arguments[2] );
  84.           },
  85.                   
  86.       onOK:
  87.         function ()
  88.           {
  89.             for( var i = 0; i < hPrefWindow.okHandlers.length; i++ )
  90.               {
  91.                 hPrefWindow.okHandlers[i]();
  92.               }
  93.               
  94.             var tag = document.getElementById( hPrefWindow.contentFrame ).getAttribute("tag");
  95.             if( tag == "" )
  96.               {
  97.                 tag = document.getElementById( hPrefWindow.contentFrame ).getAttribute("src");
  98.               }
  99.             hPrefWindow.wsm.savePageData( tag );
  100.             hPrefWindow.savePrefs();
  101.           },
  102.         
  103.       onCancel:
  104.         function ()
  105.           {
  106.             for( var i = 0; i < hPrefWindow.cancelHandlers.length; i++ )
  107.               {
  108.                 hPrefWindow.cancelHandlers[i]();
  109.               }
  110.           },
  111.  
  112.       registerOKCallbackFunc:
  113.         function ( aFunctionReference )
  114.           { 
  115.             this.okHandlers[this.okHandlers.length] = aFunctionReference;
  116.           },
  117.  
  118.       registerCancelCallbackFunc:
  119.         function ( aFunctionReference )
  120.           {
  121.             this.cancelHandlers[this.cancelHandlers.length] = aFunctionReference;
  122.           },
  123.       getPrefIsLocked:
  124.         function ( aPrefString )
  125.           {
  126.             return hPrefWindow.pref.PrefIsLocked(aPrefString);
  127.           },
  128.       getPref:
  129.         function ( aPrefType, aPrefString, aDefaultFlag )
  130.           {
  131.             var pref = hPrefWindow.pref;
  132.             try
  133.               {
  134.                 switch ( aPrefType )
  135.                   {
  136.                     case "bool":
  137.                       return !aDefaultFlag ? pref.GetBoolPref( aPrefString ) : pref.GetDefaultBoolPref( aPrefString );
  138.                     case "int":
  139.                       return !aDefaultFlag ? pref.GetIntPref( aPrefString ) : pref.GetDefaultIntPref( aPrefString );
  140.                     case "localizedstring":
  141.                       return pref.getLocalizedUnicharPref( aPrefString );
  142.                     case "color":
  143.                     case "string":
  144.                     default:
  145.                          return !aDefaultFlag ? pref.CopyUnicharPref( aPrefString ) : pref.CopyDefaultUnicharPref( aPrefString );
  146.                   }
  147.               }
  148.             catch (e)
  149.               {
  150.                 if( _DEBUG ) 
  151.                   {
  152.                     dump("*** no default pref for " + aPrefType + " pref: " + aPrefString + "\n");
  153.                     dump(e + "\n");
  154.                   }
  155.               }
  156.             return "!/!ERROR_UNDEFINED_PREF!/!";
  157.           }    ,
  158.  
  159.       setPref:
  160.         function ( aPrefType, aPrefString, aValue )
  161.           {
  162.             try
  163.               {
  164.                 switch ( aPrefType )
  165.                   {
  166.                     case "bool":
  167.                       hPrefWindow.pref.SetBoolPref( aPrefString, aValue );
  168.                       break;
  169.                     case "int":
  170.                       hPrefWindow.pref.SetIntPref( aPrefString, aValue );
  171.                       break;
  172.                     case "color":
  173.                     case "string":
  174.                     case "localizedstring":
  175.                     default:
  176.                       hPrefWindow.pref.SetUnicharPref( aPrefString, aValue );
  177.                       break;
  178.                   }
  179.               }
  180.             catch (e)
  181.               {
  182.                 dump(e + "\n");
  183.               }
  184.           },
  185.           
  186.       savePrefs:
  187.         function ()
  188.           {
  189.             for( var pageTag in this.wsm.dataManager.pageData )
  190.               {
  191.                 var pageData = this.wsm.dataManager.getPageData( pageTag );
  192.                 if (pageData.initialized)
  193.                   {
  194.                 for( var elementID in pageData )
  195.                   {
  196.                     if (elementID == "initialized") continue;
  197.                     var itemObject = pageData[elementID];
  198.                     if (typeof(itemObject) != "object") break;
  199.                     if ( "prefstring" in itemObject && itemObject.prefstring )
  200.                       {
  201.                         var elt = itemObject.localname;
  202.                         var prefattribute = itemObject.prefattribute;
  203.                         if (!prefattribute) {
  204.                           if (elt == "radiogroup" || elt == "textbox" || elt == "menulist")
  205.                             prefattribute = "value";
  206.                           else if (elt == "checkbox")
  207.                             prefattribute = "checked";
  208.                           else if (elt == "button")
  209.                             prefattribute = "disabled";
  210.                         }
  211.                         
  212.                         var value = itemObject[prefattribute];
  213.                         var preftype = itemObject.preftype;
  214.                         if (!preftype) {
  215.                           if (elt == "textbox")
  216.                             preftype = "string";
  217.                           else if (elt == "checkbox" || elt == "button")
  218.                             preftype = "bool";
  219.                           else if (elt == "radiogroup" || elt == "menulist")
  220.                             preftype = "int";
  221.                         }
  222.                         switch( preftype )
  223.                           {
  224.                             case "bool":
  225.                               if( value == "true" && typeof(value) == "string" )
  226.                                 value = true;
  227.                               else if( value == "false" && typeof(value) == "string" )
  228.                                 value = false;
  229.                               break;
  230.                             case "int":
  231.                               value = parseInt(value);                              
  232.                               break;
  233.                             case "color":
  234.                               if( toString(value) == "" )
  235.                                 {
  236.                                   dump("*** ERROR CASE: illegal attempt to set an empty color pref. ignoring.\n");
  237.                                   break;
  238.                                 }
  239.                             case "string":
  240.                             case "localizedstring":
  241.                             default:
  242.                               if( typeof(value) != "string" )
  243.                                 {
  244.                                   value = toString(value);
  245.                                 }
  246.                               break;
  247.                           }
  248.  
  249.                         if( value != this.getPref( preftype, itemObject.prefstring ) )
  250.                           {
  251.                             this.setPref( preftype, itemObject.prefstring, value );
  252.                           }
  253.                       }
  254.                   }
  255.               }
  256.               }
  257.               try 
  258.                 {
  259.                   this.pref.savePrefFile(null);
  260.                 }
  261.               catch (e)
  262.                 {
  263.                   try
  264.                     {
  265.                       var prefUtilBundle = document.getElementById("bundle_prefutilities");
  266.                       var alertText = prefUtilBundle.getString("prefSaveFailedAlert");
  267.                       var titleText = prefUtilBundle.getString("prefSaveFailedTitle");
  268.                       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  269.                                                     .getService(Components.interfaces.nsIPromptService);
  270.                       promptService.alert(window, titleText, alertText);
  271.                     }
  272.                   catch (e)
  273.                     {
  274.                       dump(e + "\n");
  275.                     }
  276.                 }
  277.           },                        
  278.  
  279.       switchPage:
  280.         function ()
  281.           {
  282.             var prefPanelTree = document.getElementById( "prefsTree" );
  283.             var selectedItem = prefPanelTree.contentView.getItemAtIndex(prefPanelTree.currentIndex);
  284.  
  285.             var oldURL = document.getElementById( this.contentFrame ).getAttribute("tag");
  286.             if( !oldURL )
  287.               {
  288.                 oldURL = document.getElementById( this.contentFrame ).getAttribute("src");
  289.               }
  290.             this.wsm.savePageData( oldURL );      // save data from the current page. 
  291.             var newURL = selectedItem.firstChild.firstChild.getAttribute("url");
  292.             var newTag = selectedItem.firstChild.firstChild.getAttribute("tag");
  293.             if( newURL != oldURL )
  294.               {
  295.                 document.getElementById( this.contentFrame ).setAttribute( "src", newURL );
  296.                 if( !newTag )
  297.                   document.getElementById( this.contentFrame ).removeAttribute( "tag" );
  298.                 else
  299.                   document.getElementById( this.contentFrame ).setAttribute( "tag", newTag );
  300.               }
  301.           },
  302.               
  303.       onpageload: 
  304.         function ( aPageTag )
  305.           {
  306.             var header = document.getElementById("header");
  307.             header.setAttribute("title",
  308.                                 window.frames[this.contentFrame].document.documentElement.getAttribute("headertitle"));
  309.             if( !(aPageTag in this.wsm.dataManager.pageData) )
  310.               {
  311.                 var prefElements = window.frames[this.contentFrame].document.getElementsByAttribute( "prefstring", "*" );
  312.                 this.wsm.dataManager.pageData[aPageTag] = [];
  313.                 for( var i = 0; i < prefElements.length; i++ )
  314.                   {
  315.                     var prefstring    = prefElements[i].getAttribute( "prefstring" );
  316.                     var prefid        = prefElements[i].getAttribute( "id" );
  317.                     var preftype      = prefElements[i].getAttribute( "preftype" );
  318.                     var elt = prefElements[i].localName;
  319.                     if (!preftype) {
  320.                       if (elt == "textbox")
  321.                         preftype = "string";
  322.                       else if (elt == "checkbox" || elt == "button")
  323.                         preftype = "bool";
  324.                       else if (elt == "radiogroup" || elt == "menulist")
  325.                         preftype = "int";
  326.                     }
  327.                     var prefdefval    = prefElements[i].getAttribute( "prefdefval" );
  328.                     var prefattribute = prefElements[i].getAttribute( "prefattribute" );
  329.                     if (!prefattribute) {
  330.                       if (elt == "radiogroup" || elt == "textbox" || elt == "menulist")
  331.                         prefattribute = "value";
  332.                       else if (elt == "checkbox")
  333.                         prefattribute = "checked";
  334.                       else if (elt == "button")
  335.                         prefattribute = "disabled";
  336.                     }
  337.                     var prefvalue;
  338.                     switch( preftype )
  339.                       {
  340.                         case "bool":
  341.                           prefvalue = this.getPref( preftype, prefstring );
  342.                           break;
  343.                         case "int":
  344.                           prefvalue = this.getPref( preftype, prefstring );
  345.                           break;
  346.                         case "string":
  347.                         case "localizedstring":
  348.                         case "color":                          
  349.                         default: 
  350.                           prefvalue = this.getPref( preftype, prefstring );
  351.                           break;
  352.                       }
  353.                     if( prefvalue == "!/!ERROR_UNDEFINED_PREF!/!" )
  354.                       {
  355.                         prefvalue = prefdefval;
  356.                       }
  357.                     var root = this.wsm.dataManager.getItemData( aPageTag, prefid ); 
  358.                     root[prefattribute] = prefvalue;              
  359.                     var isPrefLocked = this.getPrefIsLocked(prefstring);
  360.                     if (isPrefLocked)
  361.                       root.disabled = "true";
  362.                     root.localname = prefElements[i].localName;
  363.                   }
  364.               }      
  365.             this.wsm.setPageData( aPageTag );  // do not set extra elements, accept hard coded defaults
  366.             
  367.             if( 'Startup' in window.frames[ this.contentFrame ])
  368.               {
  369.                 window.frames[ this.contentFrame ].Startup();
  370.               }
  371.             this.wsm.dataManager.pageData[aPageTag].initialized=true;
  372.           },
  373.  
  374.     openBranch:
  375.       function ( aComponentName, aSelectItem )
  376.         {
  377.           var panelTree = document.getElementById( "prefsTree" );
  378.           var selectItem = document.getElementById( aSelectItem );
  379.           var index = panelTree.contentView.getIndexOfItem( selectItem );
  380.           if ( !panelTree.view.isContainerOpen( index ) )
  381.             panelTree.view.toggleOpenState(index);
  382.           panelTree.treeBoxObject.selection.select( index );
  383.         }
  384.  
  385.   };
  386.  
  387.