home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 May / PCWorld_2003-05_cd.bin / Komunik / phoenix / chrome / browser.jar / content / browser / pref / nsPrefWindow.js next >
Text File  |  2002-11-01  |  14KB  |  355 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. function initPanel ( aPrefTag )
  12.   {
  13.     if( hPrefWindow )
  14.       hPrefWindow.onpageload( aPrefTag )
  15.     else
  16.       queuedTag = aPrefTag;
  17.   } 
  18.  
  19. window.doneLoading = false; 
  20.  
  21. function nsPrefWindow( frame_id )
  22. {
  23.   if ( !frame_id )
  24.     throw "Error: frame_id not supplied!";
  25.  
  26.   this.contentFrame   = frame_id;
  27.   this.wsm            = new nsWidgetStateManager( frame_id );
  28.   this.wsm.attributes = ["preftype", "prefstring", "prefattribute", "disabled"];
  29.   this.pref           = null;
  30.   
  31.   this.cancelHandlers = [];
  32.   this.okHandlers     = [];  
  33.     
  34.   // set up window
  35.   this.onload();
  36. }
  37.  
  38. nsPrefWindow.prototype =
  39.   {
  40.     onload:
  41.       function ()
  42.         {
  43.           try 
  44.             {
  45.               this.pref = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
  46.             }
  47.           catch(e) 
  48.             {
  49.               dump("*** Failed to create prefs object\n");
  50.               return;
  51.             }
  52.         },
  53.  
  54.       init: 
  55.         function ()
  56.           {        
  57.             if( window.queuedTag )
  58.               {
  59.                 this.onpageload( window.queuedTag );
  60.               }
  61.   
  62.             if( window.arguments[1] )
  63.               this.openBranch( window.arguments[1], window.arguments[2] );
  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 );
  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.       getPrefIsLocked:
  103.         function ( aPrefString )
  104.           {
  105.             return hPrefWindow.pref.PrefIsLocked(aPrefString);
  106.           },
  107.       getPref:
  108.         function ( aPrefType, aPrefString, aDefaultFlag )
  109.           {
  110.             var pref = hPrefWindow.pref;
  111.             try
  112.               {
  113.                 switch ( aPrefType )
  114.                   {
  115.                     case "bool":
  116.                       return !aDefaultFlag ? pref.GetBoolPref( aPrefString ) : pref.GetDefaultBoolPref( aPrefString );
  117.                     case "int":
  118.                       return !aDefaultFlag ? pref.GetIntPref( aPrefString ) : pref.GetDefaultIntPref( aPrefString );
  119.                     case "localizedstring":
  120.                       return pref.getLocalizedUnicharPref( aPrefString );
  121.                     case "color":
  122.                     case "string":
  123.                     default:
  124.                          return !aDefaultFlag ? pref.CopyUnicharPref( aPrefString ) : pref.CopyDefaultUnicharPref( aPrefString );
  125.                   }
  126.               }
  127.             catch (e)
  128.               {
  129.                 if( _DEBUG ) 
  130.                   {
  131.                     dump("*** no default pref for " + aPrefType + " pref: " + aPrefString + "\n");
  132.                     dump(e + "\n");
  133.                   }
  134.               }
  135.             return "!/!ERROR_UNDEFINED_PREF!/!";
  136.           }    ,
  137.  
  138.       setPref:
  139.         function ( aPrefType, aPrefString, aValue )
  140.           {
  141.             try
  142.               {
  143.                 switch ( aPrefType )
  144.                   {
  145.                     case "bool":
  146.                       hPrefWindow.pref.SetBoolPref( aPrefString, aValue );
  147.                       break;
  148.                     case "int":
  149.                       hPrefWindow.pref.SetIntPref( aPrefString, aValue );
  150.                       break;
  151.                     case "color":
  152.                     case "string":
  153.                     case "localizedstring":
  154.                     default:
  155.                       hPrefWindow.pref.SetUnicharPref( aPrefString, aValue );
  156.                       break;
  157.                   }
  158.               }
  159.             catch (e)
  160.               {
  161.                 dump(e + "\n");
  162.               }
  163.           },
  164.           
  165.       savePrefs:
  166.         function ()
  167.           {
  168.             for( var pageTag in this.wsm.dataManager.pageData )
  169.               {
  170.                 var pageData = this.wsm.dataManager.getPageData( pageTag );
  171.                 if ("initialized" in pageData && pageData.initialized)
  172.                   {
  173.                 for( var elementID in pageData )
  174.                   {
  175.                     if (elementID == "initialized") continue;
  176.                     var itemObject = pageData[elementID];
  177.                     if (typeof(itemObject) != "object") break;
  178.                     if ( "prefstring" in itemObject && itemObject.prefstring )
  179.                       {
  180.                         var elt = itemObject.localname;
  181.                         var prefattribute = itemObject.prefattribute;
  182.                         if (!prefattribute) {
  183.                           if (elt == "radiogroup" || elt == "textbox" || elt == "menulist")
  184.                             prefattribute = "value";
  185.                           else if (elt == "checkbox")
  186.                             prefattribute = "checked";
  187.                           else if (elt == "button")
  188.                             prefattribute = "disabled";
  189.                         }
  190.                         
  191.                         var value = itemObject[prefattribute];
  192.                         var preftype = itemObject.preftype;
  193.                         if (!preftype) {
  194.                           if (elt == "textbox")
  195.                             preftype = "string";
  196.                           else if (elt == "checkbox" || elt == "button")
  197.                             preftype = "bool";
  198.                           else if (elt == "radiogroup" || elt == "menulist")
  199.                             preftype = "int";
  200.                         }
  201.                         switch( preftype )
  202.                           {
  203.                             case "bool":
  204.                               if( value == "true" && typeof(value) == "string" )
  205.                                 value = true;
  206.                               else if( value == "false" && typeof(value) == "string" )
  207.                                 value = false;
  208.                               break;
  209.                             case "int":
  210.                               value = parseInt(value);                              
  211.                               break;
  212.                             case "color":
  213.                               if( toString(value) == "" )
  214.                                 {
  215.                                   dump("*** ERROR CASE: illegal attempt to set an empty color pref. ignoring.\n");
  216.                                   break;
  217.                                 }
  218.                             case "string":
  219.                             case "localizedstring":
  220.                             default:
  221.                               if( typeof(value) != "string" )
  222.                                 {
  223.                                   value = toString(value);
  224.                                 }
  225.                               break;
  226.                           }
  227.  
  228.                         if( value != this.getPref( preftype, itemObject.prefstring ) )
  229.                           {
  230.                             this.setPref( preftype, itemObject.prefstring, value );
  231.                           }
  232.                       }
  233.                   }
  234.               }
  235.               }
  236.               try 
  237.                 {
  238.                   this.pref.savePrefFile(null);
  239.                 }
  240.               catch (e)
  241.                 {
  242.                   try
  243.                     {
  244.                       var prefUtilBundle = document.getElementById("bundle_prefutilities");
  245.                       var alertText = prefUtilBundle.getString("prefSaveFailedAlert");
  246.                       var titleText = prefUtilBundle.getString("prefSaveFailedTitle");
  247.                       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  248.                                                     .getService(Components.interfaces.nsIPromptService);
  249.                       promptService.alert(window, titleText, alertText);
  250.                     }
  251.                   catch (e)
  252.                     {
  253.                       dump(e + "\n");
  254.                     }
  255.                 }
  256.           },                        
  257.  
  258.       switchPage:
  259.         function ()
  260.           {
  261.             var prefPanelTree = document.getElementById( "prefsTree" );
  262.             var selectedItem = prefPanelTree.contentView.getItemAtIndex(prefPanelTree.currentIndex);
  263.  
  264.             var oldURL = document.getElementById( this.contentFrame ).getAttribute("tag");
  265.             if( !oldURL )
  266.               {
  267.                 oldURL = document.getElementById( this.contentFrame ).getAttribute("src");
  268.               }
  269.             this.wsm.savePageData( oldURL );      // save data from the current page. 
  270.             var newURL = selectedItem.firstChild.firstChild.getAttribute("url");
  271.             var newTag = selectedItem.firstChild.firstChild.getAttribute("tag");
  272.             if( newURL != oldURL )
  273.               {
  274.                 document.getElementById( this.contentFrame ).setAttribute( "src", newURL );
  275.                 if( !newTag )
  276.                   document.getElementById( this.contentFrame ).removeAttribute( "tag" );
  277.                 else
  278.                   document.getElementById( this.contentFrame ).setAttribute( "tag", newTag );
  279.               }
  280.           },
  281.               
  282.       onpageload: 
  283.         function ( aPageTag )
  284.           {
  285.             var header = document.getElementById("header");
  286.             header.setAttribute("title",
  287.                                 window.frames[this.contentFrame].document.documentElement.getAttribute("headertitle"));
  288.             if( !(aPageTag in this.wsm.dataManager.pageData) )
  289.               {
  290.                 var prefElements = window.frames[this.contentFrame].document.getElementsByAttribute( "prefstring", "*" );
  291.                 this.wsm.dataManager.pageData[aPageTag] = [];
  292.                 for( var i = 0; i < prefElements.length; i++ )
  293.                   {
  294.                     var prefstring    = prefElements[i].getAttribute( "prefstring" );
  295.                     var prefid        = prefElements[i].getAttribute( "id" );
  296.                     var preftype      = prefElements[i].getAttribute( "preftype" );
  297.                     var elt = prefElements[i].localName;
  298.                     if (!preftype) {
  299.                       if (elt == "textbox")
  300.                         preftype = "string";
  301.                       else if (elt == "checkbox" || elt == "button")
  302.                         preftype = "bool";
  303.                       else if (elt == "radiogroup" || elt == "menulist")
  304.                         preftype = "int";
  305.                     }
  306.                     var prefdefval    = prefElements[i].getAttribute( "prefdefval" );
  307.                     var prefattribute = prefElements[i].getAttribute( "prefattribute" );
  308.                     if (!prefattribute) {
  309.                       if (elt == "radiogroup" || elt == "textbox" || elt == "menulist")
  310.                         prefattribute = "value";
  311.                       else if (elt == "checkbox")
  312.                         prefattribute = "checked";
  313.                       else if (elt == "button")
  314.                         prefattribute = "disabled";
  315.                     }
  316.                     var prefvalue = this.getPref( preftype, prefstring );
  317.                     if( prefvalue == "!/!ERROR_UNDEFINED_PREF!/!" )
  318.                       {
  319.                         prefvalue = prefdefval;
  320.                       }
  321.                     var root = this.wsm.dataManager.getItemData( aPageTag, prefid ); 
  322.                     root[prefattribute] = prefvalue;              
  323.                     var isPrefLocked = this.getPrefIsLocked(prefstring);
  324.                     if (isPrefLocked)
  325.                       root.disabled = "true";
  326.                     root.localname = prefElements[i].localName;
  327.                   }
  328.               }      
  329.             this.wsm.setPageData( aPageTag );  // do not set extra elements, accept hard coded defaults
  330.             
  331.             if( 'Startup' in window.frames[ this.contentFrame ])
  332.               {
  333.                 window.frames[ this.contentFrame ].Startup();
  334.               }
  335.             this.wsm.dataManager.pageData[aPageTag].initialized=true;
  336.           },
  337.  
  338.     openBranch:
  339.       function ( aComponentName, aSelectItem )
  340.         {
  341.           var panelTree = document.getElementById( "prefsTree" );
  342.           var selectItem = document.getElementById( aSelectItem );
  343.           var selectItemroot = document.getElementById( aComponentName );
  344.           var parentIndex = panelTree.contentView.getIndexOfItem( selectItemroot );
  345.           if (parentIndex != -1 && !panelTree.view.isContainerOpen(parentIndex))
  346.              panelTree.view.toggleOpenState(parentIndex);
  347.           var index = panelTree.view.getIndexOfItem( selectItem );
  348.           if (index == -1)
  349.             return;
  350.           panelTree.treeBoxObject.selection.select( index );
  351.         }
  352.  
  353.   };
  354.  
  355.