home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 October / maximum-cd-2009-10.iso / DiscContents / Firefox Setup 3.5.exe / nonlocalized / chrome / toolkit.jar / content / passwordmgr / passwordManager.js next >
Encoding:
JavaScript  |  2009-06-24  |  10.5 KB  |  308 lines

  1. //@line 43 "e:\builds\moz2_slave\win32_build\build\toolkit\components\passwordmgr\content\passwordManager.js"
  2.  
  3. /*** =================== SAVED SIGNONS CODE =================== ***/
  4.  
  5. var kSignonBundle;
  6.  
  7. function SignonsStartup() {
  8.   kSignonBundle = document.getElementById("signonBundle");
  9.   document.getElementById("togglePasswords").label = kSignonBundle.getString("showPasswords");
  10.   document.getElementById("togglePasswords").accessKey = kSignonBundle.getString("showPasswordsAccessKey");
  11.   document.getElementById("signonsIntro").textContent = kSignonBundle.getString("loginsSpielAll");
  12.   LoadSignons();
  13.  
  14.   // filter the table if requested by caller
  15.   if (window.arguments &&
  16.       window.arguments[0] &&
  17.       window.arguments[0].filterString)
  18.     setFilter(window.arguments[0].filterString);
  19.  
  20.   FocusFilterBox();
  21. }
  22.  
  23. function setFilter(aFilterString) {
  24.   document.getElementById("filter").value = aFilterString;
  25.   _filterPasswords();
  26. }
  27.  
  28. var signonsTreeView = {
  29.   _filterSet : [],
  30.   _lastSelectedRanges : [],
  31.   selection: null, 
  32.  
  33.   rowCount : 0,
  34.   setTree : function(tree) {},
  35.   getImageSrc : function(row,column) {},
  36.   getProgressMode : function(row,column) {},
  37.   getCellValue : function(row,column) {},
  38.   getCellText : function(row,column) {
  39.     var signon = this._filterSet.length ? this._filterSet[row] : signons[row];
  40.     switch (column.id) {
  41.       case "siteCol":
  42.         return signon.httpRealm ?
  43.                (signon.hostname + " (" + signon.httpRealm + ")"):
  44.                signon.hostname;
  45.       case "userCol":
  46.         return signon.username || "";
  47.       case "passwordCol":
  48.         return signon.password || "";
  49.       default:
  50.         return "";
  51.     }
  52.   },
  53.   isSeparator : function(index) { return false; },
  54.   isSorted : function() { return false; },
  55.   isContainer : function(index) { return false; },
  56.   cycleHeader : function(column) {},
  57.   getRowProperties : function(row,prop) {},
  58.   getColumnProperties : function(column,prop) {},
  59.   getCellProperties : function(row,column,prop) {
  60.     if (column.element.getAttribute("id") == "siteCol")
  61.       prop.AppendElement(kLTRAtom);
  62.   }
  63.  };
  64.  
  65.  
  66. function LoadSignons() {
  67.   // loads signons into table
  68.   try {
  69.     signons = passwordmanager.getAllLogins({});
  70.   } catch (e) {
  71.     signons = [];
  72.   }
  73.   signonsTreeView.rowCount = signons.length;
  74.  
  75.   // sort and display the table
  76.   signonsTree.treeBoxObject.view = signonsTreeView;
  77.   SignonColumnSort('hostname');
  78.  
  79.   // disable "remove all signons" button if there are no signons
  80.   var element = document.getElementById("removeAllSignons");
  81.   var toggle = document.getElementById("togglePasswords");
  82.   if (signons.length == 0 || gSelectUserInUse) {
  83.     element.setAttribute("disabled","true");
  84.     toggle.setAttribute("disabled","true");
  85.   } else {
  86.     element.removeAttribute("disabled");
  87.     toggle.removeAttribute("disabled");
  88.   }
  89.  
  90.   return true;
  91. }
  92.  
  93. function SignonSelected() {
  94.   var selections = GetTreeSelections(signonsTree);
  95.   if (selections.length && !gSelectUserInUse) {
  96.     document.getElementById("removeSignon").removeAttribute("disabled");
  97.   }
  98. }
  99.  
  100. function DeleteSignon() {
  101.   var syncNeeded = (signonsTreeView._filterSet.length != 0);
  102.   DeleteSelectedItemFromTree(signonsTree, signonsTreeView,
  103.                              signonsTreeView._filterSet.length ? signonsTreeView._filterSet : signons,
  104.                              deletedSignons, "removeSignon", "removeAllSignons");
  105.   FinalizeSignonDeletions(syncNeeded);
  106. }
  107.  
  108. function DeleteAllSignons() {
  109.   var prompter = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  110.                            .getService(Components.interfaces.nsIPromptService);
  111.  
  112.   // Confirm the user wants to remove all passwords
  113.   var dummy = { value: false };
  114.   if (prompter.confirmEx(window,
  115.                          kSignonBundle.getString("removeAllPasswordsTitle"),
  116.                          kSignonBundle.getString("removeAllPasswordsPrompt"),
  117.                          prompter.STD_YES_NO_BUTTONS + prompter.BUTTON_POS_1_DEFAULT,
  118.                          null, null, null, null, dummy) == 1) // 1 == "No" button
  119.     return;
  120.  
  121.   var syncNeeded = (signonsTreeView._filterSet.length != 0);
  122.   DeleteAllFromTree(signonsTree, signonsTreeView,
  123.                         signonsTreeView._filterSet.length ? signonsTreeView._filterSet : signons,
  124.                         deletedSignons, "removeSignon", "removeAllSignons");
  125.   FinalizeSignonDeletions(syncNeeded);
  126. }
  127.  
  128. function TogglePasswordVisible() {
  129.   if (showingPasswords || ConfirmShowPasswords()) {
  130.     showingPasswords = !showingPasswords;
  131.     document.getElementById("togglePasswords").label = kSignonBundle.getString(showingPasswords ? "hidePasswords" : "showPasswords");
  132.     document.getElementById("togglePasswords").accessKey = kSignonBundle.getString(showingPasswords ? "hidePasswordsAccessKey" : "showPasswordsAccessKey");
  133.     document.getElementById("passwordCol").hidden = !showingPasswords;
  134.     _filterPasswords();
  135.   }
  136.  
  137.   // Notify observers that the password visibility toggling is
  138.   // completed.  (Mostly useful for tests)
  139.   Components.classes["@mozilla.org/observer-service;1"]
  140.             .getService(Components.interfaces.nsIObserverService)
  141.             .notifyObservers(null, "passwordmgr-password-toggle-complete", null);
  142. }
  143.  
  144. function AskUserShowPasswords() {
  145.   var prompter = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  146.   var dummy = { value: false };
  147.  
  148.   // Confirm the user wants to display passwords
  149.   return prompter.confirmEx(window,
  150.           null,
  151.           kSignonBundle.getString("noMasterPasswordPrompt"), prompter.STD_YES_NO_BUTTONS,
  152.           null, null, null, null, dummy) == 0;    // 0=="Yes" button
  153. }
  154.  
  155. function ConfirmShowPasswords() {
  156.   // This doesn't harm if passwords are not encrypted
  157.   var tokendb = Components.classes["@mozilla.org/security/pk11tokendb;1"]
  158.                     .createInstance(Components.interfaces.nsIPK11TokenDB);
  159.   var token = tokendb.getInternalKeyToken();
  160.  
  161.   // If there is no master password, still give the user a chance to opt-out of displaying passwords
  162.   if (token.checkPassword(""))
  163.     return AskUserShowPasswords();
  164.  
  165.   // So there's a master password. But since checkPassword didn't succeed, we're logged out (per nsIPK11Token.idl).
  166.   try {
  167.     // Relogin and ask for the master password.
  168.     token.login(true);  // 'true' means always prompt for token password. User will be prompted until
  169.                         // clicking 'Cancel' or entering the correct password.
  170.   } catch (e) {
  171.     // An exception will be thrown if the user cancels the login prompt dialog.
  172.     // User is also logged out of Software Security Device.
  173.   }
  174.  
  175.   return token.isLoggedIn();
  176. }
  177.  
  178. function FinalizeSignonDeletions(syncNeeded) {
  179.   for (var s=0; s<deletedSignons.length; s++) {
  180.     passwordmanager.removeLogin(deletedSignons[s]);
  181.   }
  182.   // If the deletion has been performed in a filtered view, reflect the deletion in the unfiltered table.
  183.   // See bug 405389.
  184.   if (syncNeeded) {
  185.     try {
  186.       signons = passwordmanager.getAllLogins({});
  187.     } catch (e) {
  188.       signons = [];
  189.     }
  190.   }
  191.   deletedSignons.length = 0;
  192. }
  193.  
  194. function HandleSignonKeyPress(e) {
  195.   if (e.keyCode == 46) {
  196.     DeleteSignon();
  197.   }
  198. }
  199.  
  200. var lastSignonSortColumn = "";
  201. var lastSignonSortAscending = false;
  202.  
  203. function SignonColumnSort(column) {
  204.   lastSignonSortAscending =
  205.     SortTree(signonsTree, signonsTreeView,
  206.                  signonsTreeView._filterSet.length ? signonsTreeView._filterSet : signons,
  207.                  column, lastSignonSortColumn, lastSignonSortAscending);
  208.   lastSignonSortColumn = column;
  209. }
  210.  
  211. function SignonClearFilter() {
  212.   var singleSelection = (signonsTreeView.selection.count == 1);
  213.  
  214.   // Clear the Tree Display
  215.   signonsTreeView.rowCount = 0;
  216.   signonsTree.treeBoxObject.rowCountChanged(0, -signonsTreeView._filterSet.length);
  217.   signonsTreeView._filterSet = [];
  218.  
  219.   // Just reload the list to make sure deletions are respected
  220.   lastSignonSortColumn = "";
  221.   lastSignonSortAscending = false;
  222.   LoadSignons();
  223.     
  224.   // Restore selection
  225.   if (singleSelection) {
  226.     signonsTreeView.selection.clearSelection();
  227.     for (let i = 0; i < signonsTreeView._lastSelectedRanges.length; ++i) {
  228.       var range = signonsTreeView._lastSelectedRanges[i];
  229.       signonsTreeView.selection.rangedSelect(range.min, range.max, true);
  230.     }
  231.   } else {
  232.     signonsTreeView.selection.select(0);
  233.   }
  234.   signonsTreeView._lastSelectedRanges = [];
  235.  
  236.   document.getElementById("signonsIntro").textContent = kSignonBundle.getString("loginsSpielAll");
  237. }
  238.  
  239. function FocusFilterBox() {
  240.   var filterBox = document.getElementById("filter");
  241.   if (filterBox.getAttribute("focused") != "true")
  242.     filterBox.focus();
  243. }
  244.  
  245. function SignonMatchesFilter(aSignon, aFilterValue) {
  246.   if (aSignon.hostname.toLowerCase().indexOf(aFilterValue) != -1)
  247.     return true;
  248.   if (aSignon.username &&
  249.       aSignon.username.toLowerCase().indexOf(aFilterValue) != -1)
  250.     return true;
  251.   if (aSignon.httpRealm &&
  252.       aSignon.httpRealm.toLowerCase().indexOf(aFilterValue) != -1)
  253.     return true;
  254.   if (showingPasswords && aSignon.password &&
  255.       aSignon.password.toLowerCase().indexOf(aFilterValue) != -1)
  256.     return true;
  257.  
  258.   return false;
  259. }
  260.  
  261. function FilterPasswords(aFilterValue, view) {
  262.   aFilterValue = aFilterValue.toLowerCase();
  263.   return signons.filter(function (s) SignonMatchesFilter(s, aFilterValue));
  264. }
  265.  
  266. function SignonSaveState() {
  267.   // Save selection
  268.   var seln = signonsTreeView.selection;
  269.   signonsTreeView._lastSelectedRanges = [];
  270.   var rangeCount = seln.getRangeCount();
  271.   for (var i = 0; i < rangeCount; ++i) {
  272.     var min = {}; var max = {};
  273.     seln.getRangeAt(i, min, max);
  274.     signonsTreeView._lastSelectedRanges.push({ min: min.value, max: max.value });
  275.   }
  276. }
  277.  
  278. function _filterPasswords()
  279. {
  280.   var filter = document.getElementById("filter").value;
  281.   if (filter == "") {
  282.     SignonClearFilter();
  283.     return;
  284.   }
  285.  
  286.   var newFilterSet = FilterPasswords(filter, signonsTreeView);
  287.   if (!signonsTreeView._filterSet.length) {
  288.     // Save Display Info for the Non-Filtered mode when we first
  289.     // enter Filtered mode. 
  290.     SignonSaveState();
  291.   }
  292.   signonsTreeView._filterSet = newFilterSet;
  293.  
  294.   // Clear the display
  295.   let oldRowCount = signonsTreeView.rowCount;
  296.   signonsTreeView.rowCount = 0;
  297.   signonsTree.treeBoxObject.rowCountChanged(0, -oldRowCount);
  298.   // Set up the filtered display
  299.   signonsTreeView.rowCount = signonsTreeView._filterSet.length;
  300.   signonsTree.treeBoxObject.rowCountChanged(0, signonsTreeView.rowCount);
  301.  
  302.   // if the view is not empty then select the first item
  303.   if (signonsTreeView.rowCount > 0)
  304.     signonsTreeView.selection.select(0);
  305.  
  306.   document.getElementById("signonsIntro").textContent = kSignonBundle.getString("loginsSpielFiltered");
  307. }
  308.