home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / toolkit.jar / content / mozapps / autofill / UpdatePrompt.js < prev    next >
Text File  |  2005-07-29  |  18KB  |  522 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Netscape Public License
  5.  * Version 1.1 (the "License"); you may not use this file except in
  6.  * compliance with the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/NPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Communicator client code.
  15.  *
  16.  * The Initial Developer of the Original Code is 
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 1998
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *  Alec Flett  <alecf@netscape.com>
  23.  *  Ben Goodger <ben@netscape.com>
  24.  *  Blake Ross  <blakeross@telocity.com>
  25.  *  Joe Hewitt <hewitt@netscape.com>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the NPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the NPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. // parameters to gCommonDialogParam.Get() are defined in nsPIPromptService.idl
  42. var gCommonDialogParam = 
  43.   window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  44.   
  45. var gBrowser = null;
  46.   
  47. function showControls()
  48. {
  49.   // This is called before onload fires, so we can't be certain that any elements
  50.   // in the document have their bindings ready, so don't call any methods/properties
  51.   // here on xul elements that come from xbl bindings.
  52.   
  53.   gBrowser = document.getElementById("content");
  54.   
  55.   //Passcard name field
  56.   initTextbox("name",4,6,false);
  57.   
  58.   dump("5 = " + gCommonDialogParam.GetString(5)+'\n');
  59.   dump("6 text = " + gCommonDialogParam.GetString(6)+'\n');
  60.   dump("7 text = " + gCommonDialogParam.GetString(7)+'\n');
  61.   dump("8 = " + gCommonDialogParam.GetString(8)+'\n');
  62.   dump("4 text = " + gCommonDialogParam.GetString(4)+'\n');
  63.   dump("3 text = " + gCommonDialogParam.GetString(3)+'\n');
  64.   
  65.   dump("10 = " + gCommonDialogParam.GetString(10)+'\n');
  66.   dump("11 text = " + gCommonDialogParam.GetString(11)+'\n');
  67.   dump("12 text = " + gCommonDialogParam.GetString(12)+'\n');
  68.   dump("13 = " + gCommonDialogParam.GetString(13)+'\n');
  69.   dump("14 text = " + gCommonDialogParam.GetString(14)+'\n');
  70.   dump("15 text = " + gCommonDialogParam.GetString(15)+'\n');
  71. }
  72.  
  73. function setLabelForNode(aNode, aLabel, aIsLabelFlag)
  74. {
  75.   // This is for labels with possible accesskeys
  76.   var accessKeyIndex;
  77.   if (/^\&[^\&]/.test(aLabel)) { // access key is at the start
  78.    accessKeyIndex = 0;
  79.   } else {
  80.     accessKeyIndex = aLabel.search(/[^\&]\&[^\&]/) + 1;
  81.     if (accessKeyIndex == 0) {
  82.       accessKeyIndex = -1; // magic value for no accesskey
  83.     }
  84.   }
  85.  
  86.   // If a character has an & before it, then it should become an accesskey
  87.   if (accessKeyIndex >= 0 && accessKeyIndex < aLabel.length - 1) {
  88.     // This will also cause the accesskey attribute to be set via xbl
  89.     aNode.accessKey = aLabel.charAt(accessKeyIndex + 1);
  90.     // Set the label to the string without the &
  91.     aLabel = aLabel.substr(0, accessKeyIndex) + 
  92.              aLabel.substr(accessKeyIndex + 1);
  93.   }
  94.   aLabel = aLabel.replace(/\&\&/g, "&");
  95.   if (aIsLabelFlag) {    // Set text for <label> element
  96.     aNode.setAttribute("value", aLabel);
  97.   } else {    // Set text for other xul elements
  98.     aNode.label = aLabel;
  99.   }
  100. }
  101.  
  102. function updatePromptOnLoad()
  103. {
  104.   // set the window title
  105.   window.title = gCommonDialogParam.GetString(12);
  106.  
  107.   // set the number of command buttons
  108.   var nButtons = gCommonDialogParam.GetInt(2);
  109.   var dialog = document.documentElement;
  110.   switch (nButtons) {
  111.     case 1:
  112.       dialog.getButton("cancel").hidden = true;
  113.       break;
  114.     case 4:
  115.       dialog.getButton("extra2").hidden = false;
  116.     case 3:
  117.       dialog.getButton("extra1").hidden = false;
  118.   }
  119.  
  120.   // display the main text
  121.   var messageText = gCommonDialogParam.GetString(0);
  122.   var messageParent = document.getElementById("info.box");
  123.   var messageParagraphs = messageText.split("\n");
  124.  
  125.   for (var i = 0; i < messageParagraphs.length; i++) {
  126.     var descriptionNode = document.createElement("description");
  127.     var text = document.createTextNode(messageParagraphs[i]);
  128.     descriptionNode.appendChild(text);
  129.     messageParent.appendChild(descriptionNode);
  130.   }
  131.  
  132.   setElementText("info.header", gCommonDialogParam.GetString(3), true);
  133.  
  134.   // set the icon
  135.   var iconElement = document.getElementById("info.icon");
  136.   var iconClass = gCommonDialogParam.GetString(2);
  137.   if (!iconClass)
  138.     iconClass = "alert-icon";
  139.   iconElement.setAttribute("class", iconElement.getAttribute("class") + " " + iconClass);
  140.  
  141.   switch (nButtons) {
  142.     case 4:
  143.       setLabelForNode(document.documentElement.getButton("extra2"), gCommonDialogParam.GetString(11));
  144.       // fall through
  145.     case 3:
  146.       setLabelForNode(document.documentElement.getButton("extra1"), gCommonDialogParam.GetString(10));
  147.       // fall through
  148.     default:
  149.     case 2:
  150.       var string = gCommonDialogParam.GetString(9);
  151.       if (string)
  152.         setLabelForNode(document.documentElement.getButton("cancel"), string);
  153.       // fall through
  154.     case 1:
  155.       string = gCommonDialogParam.GetString(8);
  156.       if (string)
  157.         setLabelForNode(document.documentElement.getButton("accept"), string);
  158.       break;
  159.   }
  160.  
  161.   // set default result to cancelled
  162.   gCommonDialogParam.SetInt(0, 1); 
  163.  
  164.   // initialize the checkbox
  165.   setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
  166.   
  167.   //initialize the radiobuttons
  168.   setRadiobutton("radio0",gCommonDialogParam.GetString(13));
  169.   setRadiobutton("radio1",gCommonDialogParam.GetString(14));
  170.   setRadiobutton("radio2",gCommonDialogParam.GetString(15));
  171.   
  172.   //set radiogroup
  173.   setRadiogroup(0);
  174.       
  175.   //set dropdown list
  176.   setDropdown(gCommonDialogParam.GetString(17));
  177.   updatePasscardMenu(document.getElementById("dropdownMenulist0"));
  178.   
  179.   if (gCommonDialogParam.GetInt(3) == 0) // If no text fields
  180.   {
  181.     var dButton;
  182.     var defaultButton = gCommonDialogParam.GetInt(5);
  183.     switch (defaultButton) {
  184.       case 3:
  185.         dButton = document.documentElement.getButton("extra2");
  186.         break;
  187.       case 2:
  188.         dButton = document.documentElement.getButton("extra1");
  189.         break;
  190.       case 1:
  191.         dButton = document.documentElement.getButton("cancel");
  192.         break;
  193.       default:
  194.       case 0:
  195.         dButton = document.documentElement.getButton("accept");
  196.         break;
  197.     }
  198.     // move the default attribute and focus from the accept button
  199.     // to the one specified in the dialog params
  200.     document.documentElement.getButton("accept").setAttribute("default",false);
  201.     dButton.setAttribute("default", true);
  202.     dButton.focus();
  203.   }
  204.  
  205.   if (gCommonDialogParam.GetInt(6) != 0) // delay button enable
  206.   {
  207.     var delayInterval = 2000;
  208.     try {
  209.       var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  210.                   .getService(Components.interfaces.nsIPrefBranch);
  211.       delayInterval = prefs.getIntPref("security.dialog_enable_delay");
  212.     } catch (e) {}
  213.  
  214.     document.documentElement.getButton("accept").disabled = true;
  215.     document.documentElement.getButton("extra1").disabled = true;
  216.     document.documentElement.getButton("extra2").disabled = true;
  217.  
  218.     setTimeout(commonDialogReenableButtons, delayInterval);
  219.   }
  220.  
  221.   getAttention();
  222. }
  223.  
  224. function getWebNavigation()
  225. {
  226.   
  227.   try {
  228.     return gBrowser.webNavigation;
  229.   } catch (e) {
  230.     return null;
  231.   }
  232. }
  233.  
  234. function updatePasscardMenu(menu) {
  235.   
  236.   var passwordManager =
  237.     Components.classes["@mozilla.org/passwordmanager;1"].getService(Components.interfaces.nsIPasswordManager);
  238.   var enumerator = passwordManager.enumerator;
  239.   var realm = gCommonDialogParam.GetString(18);
  240.   var selectedPasscard = 0;
  241.   
  242.   document.getElementById("dropdown0Container").removeAttribute("collapsed");
  243.  
  244.   
  245.   // keep track of number of passcards found for the specific realm
  246.   var passcardCount = 0;
  247.  
  248.   var nextPassword;
  249.   var host;
  250.   var uniqueID;
  251.   var passcard;
  252.   var lastUsed;
  253.   
  254.   while (enumerator.hasMoreElements()) {
  255.     try {
  256.       nextPassword = enumerator.getNext();
  257.     } catch(e) {}
  258.     
  259.     // get all info for the passcard
  260.     nextPassword = nextPassword.QueryInterface(Components.interfaces.nsIPassword);
  261.     host = nextPassword.host;
  262.     uniqueID = nextPassword.uniqueID;
  263.     passcard = nextPassword.passcard;
  264.     lastUsed = nextPassword.lastUsed;
  265.     
  266.     if(realm == host) {
  267.       dump("***Passcard -- passcard: " + passcard +
  268.               " uniqueID: " + uniqueID + " lastUsed: " + lastUsed + "\n");
  269.       passcardCount++;
  270.       
  271.       // find the default passcard for realm
  272.       if(eval(lastUsed))
  273.         selectedPasscard = passcardCount-1; 
  274.       
  275.       menu.appendItem(passcard,uniqueID);
  276.     }
  277.   } // while
  278.   
  279.   menu.selectedIndex = selectedPasscard;
  280.   
  281. }
  282.  
  283. function commonDialogReenableButtons()
  284. {
  285.   document.documentElement.getButton("accept").disabled = false;
  286.   document.documentElement.getButton("extra1").disabled = false;
  287.   document.documentElement.getButton("extra2").disabled = false;
  288. }
  289.  
  290. function initTextbox(aName, aLabelIndex, aValueIndex, aAlwaysLabel)
  291. {
  292.   unHideElementById(aName+"Container");
  293.  
  294.   var label = aLabelIndex < 0 ? "" : gCommonDialogParam.GetString(aLabelIndex);
  295.   if (label || aAlwaysLabel && !label)
  296.     setElementText(aName+"Label", label);
  297.     
  298.   var value = aValueIndex < 0 ? "" : gCommonDialogParam.GetString(aValueIndex);
  299.   var textbox = document.getElementById(aName + "Textbox");
  300.   textbox.setAttribute("value", value);
  301. }
  302.  
  303. function setElementText(aElementID, aValue, aChildNodeFlag)
  304. {
  305.   var element = document.getElementById(aElementID);
  306.   if (!aChildNodeFlag && element) {
  307.     setLabelForNode(element, aValue, true);
  308.   } else if (aChildNodeFlag && element) {
  309.     element.appendChild(document.createTextNode(aValue));
  310.   }
  311. }
  312.  
  313. function setCheckbox(aChkMsg, aChkValue)
  314. {
  315.   if (aChkMsg) {
  316.     // XXX Would love to use hidden instead of collapsed, but the checkbox
  317.     // fails to size itself properly when I do this.
  318.     document.getElementById("checkboxContainer").removeAttribute("collapsed");
  319.     
  320.     var checkboxElement = document.getElementById("checkbox");
  321.     setLabelForNode(checkboxElement, aChkMsg);
  322.     checkboxElement.checked = aChkValue > 0;
  323.   }
  324. }
  325.  
  326. function setRadiobutton(aRBId, aRBMsg)
  327. {
  328.   if (aRBMsg) {
  329.     
  330.     document.getElementById(aRBId+"Container").removeAttribute("collapsed");
  331.     var rbElement = document.getElementById(aRBId);
  332.     setLabelForNode(rbElement, aRBMsg);
  333.   }
  334. }
  335.  
  336. function setDropdown(aDDMsg)
  337. {  
  338.   document.getElementById("dropdown1Container").removeAttribute("collapsed");
  339.   var ddElement = document.getElementById("dropdownLabel");
  340.   setLabelForNode(ddElement, aDDMsg,true);
  341.   
  342.   var stringBundle = document.getElementById("strings");
  343.   
  344.   var dropdownlist = document.getElementById("dropdownMenulist1");
  345.   dropdownlist.appendItem(stringBundle.getString("displayPromptText"),0);
  346.   dropdownlist.appendItem(stringBundle.getString("fillPasswordText"),1);
  347.   dropdownlist.appendItem(stringBundle.getString("autologinText"),2);
  348.   dropdownlist.appendItem(stringBundle.getString("doNothingText"),3);
  349.   
  350.   dropdownlist.selectedIndex = gCommonDialogParam.GetInt(11);
  351.   gCommonDialogParam.SetInt(11, dropdownlist.selectedItem.value);
  352. }
  353.  
  354. function setRadiogroup(aRBIndex)
  355. {
  356.   if (aRBIndex) {   
  357.     var rbGroupElement = document.getElementById("radiogroup");
  358.     rbGroupElement.selectedIndex = aRBIndex;
  359.   
  360.     gCommonDialogParam.SetInt(7, 1);
  361.     gCommonDialogParam.SetInt(8, 0);
  362.     gCommonDialogParam.SetInt(9, 0);
  363.     
  364.     document.getElementById("nameLabel").disabled = true;
  365.     document.getElementById("nameTextbox").disabled = true;
  366.     document.getElementById("checkbox").disabled = true;
  367.     document.getElementById("dropdownLabel").disabled = true;
  368.     document.getElementById("dropdownMenulist1").disabled = true;
  369.   }
  370. }
  371.  
  372. function unHideElementById(aElementID)
  373. {
  374.   var element = document.getElementById(aElementID);
  375.   element.hidden = false;
  376. }
  377.  
  378. function hideElementById(aElementID)
  379. {
  380.   var element = document.getElementById(aElementID)
  381.   element.hidden = true;
  382. }
  383.  
  384. function isVisible(aElementId)
  385. {
  386.   return document.getElementById(aElementId).hasAttribute("hidden");
  387. }
  388.  
  389. function onCheckboxClick(aCheckboxElement)
  390. {
  391.   gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
  392.   dump("checkbox value = " + gCommonDialogParam.GetInt(1)+'\n');
  393.  
  394.   //check if protect with master password and master password is not set
  395.   //radio1 should be selected
  396.   if(gCommonDialogParam.GetInt(1) && !IsMasterPasswordSet() && gCommonDialogParam.GetInt(8))
  397.   {
  398.     // if master password already exists, pass empty string to use default text
  399.     var dialogTitle = GetMasterPasswordText();
  400.     var obj = new Object( );
  401.     obj.res = "";
  402.           
  403.     dump("************calling mp***********");
  404.     
  405.     window.openDialog("chrome://browser/content/pref/pref-masterpass.xul",
  406.         "_blank", "chrome,dialog,modal", dialogTitle,obj);
  407.                             
  408.     dump("*******save prompt obj.res = " + obj.res);
  409.     
  410.     if(obj.res != "ok")
  411.     {
  412.         dump("*****saveprompt not ok*********");
  413.         setCheckbox(gCommonDialogParam.GetString(1), 0);
  414.         gCommonDialogParam.SetInt(1, 0);
  415.     }
  416.   }
  417. }
  418.  
  419. function onDropdown1Click(aDropdownElement)
  420. {
  421.   gCommonDialogParam.SetInt(11, aDropdownElement.selectedItem.value);
  422.   dump("option selected for 1 = " + gCommonDialogParam.GetInt(11)+'\n');
  423. }
  424.  
  425. function onDropdown0Click(aDropdownElement)
  426. {
  427.   gCommonDialogParam.SetInt(10, aDropdownElement.selectedItem.value);
  428.   dump("option selected for 0 = " + gCommonDialogParam.GetInt(10)+'\n');
  429. }
  430.  
  431. function onRadio2Selected()
  432. {
  433.   document.getElementById("nameLabel").disabled = true;
  434.   document.getElementById("nameTextbox").disabled = true;
  435.   document.getElementById("checkbox").disabled = true;
  436.   document.getElementById("dropdownLabel").disabled = true;
  437.   document.getElementById("dropdownMenulist1").disabled = true;
  438.   document.getElementById("dropdownMenulist0").disabled = true;
  439.   gCommonDialogParam.SetInt(7, 0);
  440.   gCommonDialogParam.SetInt(8, 0);
  441.   gCommonDialogParam.SetInt(9, 1);
  442.   dump("radio button 2 selected = " + gCommonDialogParam.GetInt(9)+'\n');
  443. }
  444.  
  445. function onRadio0Selected()
  446. {
  447.   document.getElementById("nameLabel").disabled = true;
  448.   document.getElementById("nameTextbox").disabled = true;
  449.   document.getElementById("checkbox").disabled = true;
  450.   document.getElementById("dropdownLabel").disabled = true;
  451.   document.getElementById("dropdownMenulist1").disabled = true;
  452.   document.getElementById("dropdownMenulist0").removeAttribute("disabled");
  453.   gCommonDialogParam.SetInt(7, 1);
  454.   gCommonDialogParam.SetInt(8, 0);
  455.   gCommonDialogParam.SetInt(9, 0);
  456.   dump("radio button 0 selected = " + gCommonDialogParam.GetInt(7)+'\n');
  457. }
  458.  
  459. function onRadio1Selected()
  460. {
  461.   document.getElementById("nameLabel").removeAttribute("disabled");
  462.   document.getElementById("nameTextbox").removeAttribute("disabled");
  463.   document.getElementById("checkbox").removeAttribute("disabled");
  464.   document.getElementById("dropdownLabel").removeAttribute("disabled");
  465.   document.getElementById("dropdownMenulist1").removeAttribute("disabled");
  466.   document.getElementById("dropdownMenulist0").disabled = true;
  467.   gCommonDialogParam.SetInt(7, 0);
  468.   gCommonDialogParam.SetInt(8, 1);
  469.   gCommonDialogParam.SetInt(9, 0);
  470.   dump("radio button 1 selected = " + gCommonDialogParam.GetInt(8)+'\n');
  471. }
  472.  
  473. function updatePromptOnAccept()
  474. {
  475.   dump('updatePromptOnAccept()\n');
  476.   gCommonDialogParam.SetInt(0, 0); // say that ok was pressed
  477.  
  478.   var numTextBoxes = gCommonDialogParam.GetInt(3);
  479.   var textboxIsPassword1 = gCommonDialogParam.GetInt(4) == 1;
  480.   
  481.   //set passcard name
  482.   gCommonDialogParam.SetString(6, document.getElementById("nameTextbox").value);
  483.   
  484.   dump("value in name textbox = " + gCommonDialogParam.GetString(6)+'\n');
  485.   
  486.   gCommonDialogParam.SetInt(10, document.getElementById("dropdownMenulist0").selectedItem.value);
  487.   gCommonDialogParam.SetInt(11, document.getElementById("dropdownMenulist1").selectedItem.value);
  488.   
  489.   if (numTextBoxes >= 1) {
  490.     var editField1;
  491.     if (textboxIsPassword1)
  492.       editField1 = document.getElementById("password1Textbox");
  493.     else
  494.       editField1 = document.getElementById("loginTextbox");
  495.     gCommonDialogParam.SetString(6, editField1.value);
  496.   }
  497.  
  498.   if (numTextBoxes == 2) {
  499.     var editField2;
  500.     if (textboxIsPassword1)
  501.       // we had two password fields
  502.       editField2 = document.getElementById("password2Textbox");
  503.     else
  504.       // we only had one password field (and one login field)
  505.       editField2 = document.getElementById("password1Textbox");
  506.     gCommonDialogParam.SetString(7, editField2.value);
  507.   }
  508.   return true;
  509. }
  510.  
  511. function updatePromptOnExtra1()
  512. {
  513.   gCommonDialogParam.SetInt(0, 2);
  514.   window.close();
  515. }
  516.  
  517. function updatePromptOnExtra2()
  518. {
  519.   gCommonDialogParam.SetInt(0, 3);
  520.   window.close();
  521. }
  522.