home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / pref / nsPrefWindow.js < prev    next >
Text File  |  2003-06-08  |  16KB  |  398 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.   this.chromeRegistry = null;
  51.   this.observerService= null;
  52.   
  53.   this.cancelHandlers = [];
  54.   this.okHandlers     = [];  
  55.     
  56.   // set up window
  57.   this.onload();
  58. }
  59.  
  60. nsPrefWindow.prototype =
  61.   {
  62.     onload:
  63.       function ()
  64.         {
  65.           try 
  66.             {
  67.               this.pref = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
  68.               this.chromeRegistry = Components.classes["@mozilla.org/chrome/chrome-registry;1"].getService(Components.interfaces.nsIXULChromeRegistry);
  69.               this.observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  70.             }
  71.           catch(e) 
  72.             {
  73.               dump("*** Failed to create prefs object\n");
  74.               return;
  75.             }
  76.         },
  77.  
  78.       init: 
  79.         function ()
  80.           {        
  81.             if( window.queuedTag )
  82.               {
  83.                 this.onpageload( window.queuedTag );
  84.               }
  85.   
  86.             if( window.arguments[1] )
  87.               this.openBranch( window.arguments[1], window.arguments[2] );
  88.           },
  89.                   
  90.       onOK:
  91.         function ()
  92.           {
  93.             var tag = document.getElementById( hPrefWindow.contentFrame ).getAttribute("tag");
  94.             if( tag == "" )
  95.               {
  96.                 tag = document.getElementById( hPrefWindow.contentFrame ).getAttribute("src");
  97.               }
  98.             hPrefWindow.wsm.savePageData( tag );
  99.             for( var i = 0; i < hPrefWindow.okHandlers.length; i++ )
  100.               try {
  101.                 hPrefWindow.okHandlers[i]();
  102.               } catch (e) {
  103.                 debug("some silly ok handler /*"+hPrefWindow.okHandlers[i]+"*/ failed: "+ e);
  104.               }
  105.             hPrefWindow.savePrefs();
  106.           },
  107.         
  108.       onCancel:
  109.         function ()
  110.           {
  111.             for( var i = 0; i < hPrefWindow.cancelHandlers.length; i++ )
  112.               try {
  113.                 hPrefWindow.cancelHandlers[i]();
  114.               } catch (e) {
  115.                 debug("some silly cancel handler /*"+hPrefWindow.cancelHandlers[i]+"*/ failed: "+ e);
  116.               }
  117.           },
  118.  
  119.       registerOKCallbackFunc:
  120.         function ( aFunctionReference )
  121.           { 
  122.             this.okHandlers[this.okHandlers.length] = aFunctionReference;
  123.           },
  124.  
  125.       registerCancelCallbackFunc:
  126.         function ( aFunctionReference )
  127.           {
  128.             this.cancelHandlers[this.cancelHandlers.length] = aFunctionReference;
  129.           },
  130.       getPrefIsLocked:
  131.         function ( aPrefString )
  132.           {
  133.             return hPrefWindow.pref.PrefIsLocked(aPrefString);
  134.           },
  135.       getPref:
  136.         function ( aPrefType, aPrefString, aDefaultFlag )
  137.           {
  138.             var pref = hPrefWindow.pref;
  139.             try
  140.               {
  141.                 switch ( aPrefType )
  142.                   {
  143.                     case "bool":
  144.                       return !aDefaultFlag ? pref.GetBoolPref( aPrefString ) : pref.GetDefaultBoolPref( aPrefString );
  145.                     case "int":
  146.                       return !aDefaultFlag ? pref.GetIntPref( aPrefString ) : pref.GetDefaultIntPref( aPrefString );
  147.                     case "localizedstring":
  148.                       return pref.getLocalizedUnicharPref( aPrefString );
  149.                     case "color":
  150.                     case "string":
  151.                     default:
  152.                          return !aDefaultFlag ? pref.CopyUnicharPref( aPrefString ) : pref.CopyDefaultUnicharPref( aPrefString );
  153.                   }
  154.               }
  155.             catch (e)
  156.               {
  157.                 if( _DEBUG ) 
  158.                   {
  159.                     dump("*** no default pref for " + aPrefType + " pref: " + aPrefString + "\n");
  160.                     dump(e + "\n");
  161.                   }
  162.               }
  163.             return "!/!ERROR_UNDEFINED_PREF!/!";
  164.           }    ,
  165.  
  166.       setPref:
  167.         function ( aPrefType, aPrefString, aValue )
  168.           {
  169.             try
  170.               {
  171.                 switch ( aPrefType )
  172.                   {
  173.                     case "bool":
  174.                       hPrefWindow.pref.SetBoolPref( aPrefString, aValue );
  175.                       break;
  176.                     case "int":
  177.                       hPrefWindow.pref.SetIntPref( aPrefString, aValue );
  178.                       break;
  179.                     case "color":
  180.                     case "string":
  181.                     case "localizedstring":
  182.                     default:
  183.                       hPrefWindow.pref.SetUnicharPref( aPrefString, aValue );
  184.                       break;
  185.                   }
  186.               }
  187.             catch (e)
  188.               {
  189.                 dump(e + "\n");
  190.               }
  191.           },
  192.           
  193.       savePrefs:
  194.         function ()
  195.           {
  196.             for( var pageTag in this.wsm.dataManager.pageData )
  197.               {
  198.                 var pageData = this.wsm.dataManager.getPageData( pageTag );
  199.                 if ("initialized" in pageData && pageData.initialized)
  200.                   {
  201.                 for( var elementID in pageData )
  202.                   {
  203.                     if (elementID == "initialized") continue;
  204.                     var itemObject = pageData[elementID];
  205.                     if (typeof(itemObject) != "object") break;
  206.                     if ( "prefstring" in itemObject && itemObject.prefstring )
  207.                       {
  208.                         var elt = itemObject.localname;
  209.                         var prefattribute = itemObject.prefattribute;
  210.                         if (!prefattribute) {
  211.                           if (elt == "radiogroup" || elt == "textbox" || elt == "menulist")
  212.                             prefattribute = "value";
  213.                           else if (elt == "checkbox")
  214.                             prefattribute = "checked";
  215.                           else if (elt == "button")
  216.                             prefattribute = "disabled";
  217.                         }
  218.                         
  219.                         var value = itemObject[prefattribute];
  220.                         var preftype = itemObject.preftype;
  221.                         if (!preftype) {
  222.                           if (elt == "textbox")
  223.                             preftype = "string";
  224.                           else if (elt == "checkbox" || elt == "button")
  225.                             preftype = "bool";
  226.                           else if (elt == "radiogroup" || elt == "menulist")
  227.                             preftype = "int";
  228.                         }
  229.                         switch( preftype )
  230.                           {
  231.                             case "bool":
  232.                               if( value == "true" && typeof(value) == "string" )
  233.                                 value = true;
  234.                               else if( value == "false" && typeof(value) == "string" )
  235.                                 value = false;
  236.                               break;
  237.                             case "int":
  238.                               value = parseInt(value);                              
  239.                               break;
  240.                             case "color":
  241.                               if( toString(value) == "" )
  242.                                 {
  243.                                   dump("*** ERROR CASE: illegal attempt to set an empty color pref. ignoring.\n");
  244.                                   break;
  245.                                 }
  246.                             case "string":
  247.                             case "localizedstring":
  248.                             default:
  249.                               if( typeof(value) != "string" )
  250.                                 {
  251.                                   value = toString(value);
  252.                                 }
  253.                               break;
  254.                           }
  255.  
  256.                         if( value != this.getPref( preftype, itemObject.prefstring ) )
  257.                           {
  258.                             this.setPref( preftype, itemObject.prefstring, value );
  259.                           }
  260.                       }
  261.                   }
  262.               }
  263.               }
  264.               try 
  265.                 {
  266.                   this.pref.savePrefFile(null);
  267.                 }
  268.               catch (e)
  269.                 {
  270.                   try
  271.                     {
  272.                       var prefUtilBundle = document.getElementById("bundle_prefutilities");
  273.                       var alertText = prefUtilBundle.getString("prefSaveFailedAlert");
  274.                       var titleText = prefUtilBundle.getString("prefSaveFailedTitle");
  275.                       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  276.                                                     .getService(Components.interfaces.nsIPromptService);
  277.                       promptService.alert(window, titleText, alertText);
  278.                     }
  279.                   catch (e)
  280.                     {
  281.                       dump(e + "\n");
  282.                     }
  283.                 }
  284.           },                        
  285.  
  286.       switchPage:
  287.         function ()
  288.           {
  289.             var prefPanelTree = document.getElementById( "prefsTree" );
  290.             var selectedItem = prefPanelTree.contentView.getItemAtIndex(prefPanelTree.currentIndex);
  291.  
  292.             var oldURL = document.getElementById( this.contentFrame ).getAttribute("tag");
  293.             if( !oldURL )
  294.               {
  295.                 oldURL = document.getElementById( this.contentFrame ).getAttribute("src");
  296.               }
  297.             this.wsm.savePageData( oldURL );      // save data from the current page. 
  298.             var newURL = selectedItem.firstChild.firstChild.getAttribute("url");
  299.             var newTag = selectedItem.firstChild.firstChild.getAttribute("tag");
  300.             if( newURL != oldURL )
  301.               {
  302.                 document.getElementById( this.contentFrame ).setAttribute( "src", newURL );
  303.                 if( !newTag )
  304.                   document.getElementById( this.contentFrame ).removeAttribute( "tag" );
  305.                 else
  306.                   document.getElementById( this.contentFrame ).setAttribute( "tag", newTag );
  307.               }
  308.           },
  309.               
  310.       onpageload: 
  311.         function ( aPageTag )
  312.           {
  313.             var header = document.getElementById("header");
  314.             header.setAttribute("title",
  315.                                 window.frames[this.contentFrame].document.documentElement.getAttribute("headertitle"));
  316.             if( !(aPageTag in this.wsm.dataManager.pageData) )
  317.               {
  318.                 var prefElements = window.frames[this.contentFrame].document.getElementsByAttribute( "prefstring", "*" );
  319.                 this.wsm.dataManager.pageData[aPageTag] = [];
  320.                 for( var i = 0; i < prefElements.length; i++ )
  321.                   {
  322.                     var prefstring    = prefElements[i].getAttribute( "prefstring" );
  323.                     var prefid        = prefElements[i].getAttribute( "id" );
  324.                     var preftype      = prefElements[i].getAttribute( "preftype" );
  325.                     var elt = prefElements[i].localName;
  326.                     if (!preftype) {
  327.                       if (elt == "textbox")
  328.                         preftype = "string";
  329.                       else if (elt == "checkbox" || elt == "button")
  330.                         preftype = "bool";
  331.                       else if (elt == "radiogroup" || elt == "menulist")
  332.                         preftype = "int";
  333.                     }
  334.                     var prefdefval    = prefElements[i].getAttribute( "prefdefval" );
  335.                     var prefattribute = prefElements[i].getAttribute( "prefattribute" );
  336.                     if (!prefattribute) {
  337.                       if (elt == "radiogroup" || elt == "textbox" || elt == "menulist")
  338.                         prefattribute = "value";
  339.                       else if (elt == "checkbox")
  340.                         prefattribute = "checked";
  341.                       else if (elt == "button")
  342.                         prefattribute = "disabled";
  343.                     }
  344.                     var prefvalue;
  345.                     switch( preftype )
  346.                       {
  347.                         case "bool":
  348.                           prefvalue = this.getPref( preftype, prefstring );
  349.                           break;
  350.                         case "int":
  351.                           prefvalue = this.getPref( preftype, prefstring );
  352.                           break;
  353.                         case "string":
  354.                         case "localizedstring":
  355.                         case "color":                          
  356.                         default: 
  357.                           prefvalue = this.getPref( preftype, prefstring );
  358.                           break;
  359.                       }
  360.                     if( prefvalue == "!/!ERROR_UNDEFINED_PREF!/!" )
  361.                       {
  362.                         prefvalue = prefdefval;
  363.                       }
  364.                     var root = this.wsm.dataManager.getItemData( aPageTag, prefid ); 
  365.                     root[prefattribute] = prefvalue;              
  366.                     var isPrefLocked = this.getPrefIsLocked(prefstring);
  367.                     if (isPrefLocked)
  368.                       root.disabled = "true";
  369.                     root.localname = prefElements[i].localName;
  370.                   }
  371.               }      
  372.             this.wsm.setPageData( aPageTag );  // do not set extra elements, accept hard coded defaults
  373.             
  374.             if( 'Startup' in window.frames[ this.contentFrame ])
  375.               {
  376.                 window.frames[ this.contentFrame ].Startup();
  377.               }
  378.             this.wsm.dataManager.pageData[aPageTag].initialized=true;
  379.           },
  380.  
  381.     openBranch:
  382.       function ( aComponentName, aSelectItem )
  383.         {
  384.           var panelTree = document.getElementById( "prefsTree" );
  385.           var selectItem = document.getElementById( aSelectItem );
  386.           var selectItemroot = document.getElementById( aComponentName );
  387.           var parentIndex = panelTree.contentView.getIndexOfItem( selectItemroot );
  388.           if (parentIndex != -1 && !panelTree.view.isContainerOpen(parentIndex))
  389.              panelTree.view.toggleOpenState(parentIndex);
  390.           var index = panelTree.view.getIndexOfItem( selectItem );
  391.           if (index == -1)
  392.             return;
  393.           panelTree.treeBoxObject.selection.select( index );
  394.         }
  395.  
  396.   };
  397.  
  398.