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 / popupManager.js < prev    next >
Text File  |  2003-06-08  |  13KB  |  429 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  *
  3.  * The Original Code is Mozilla Communicator client code.
  4.  *
  5.  * The Initial Developer of the Original Code is 
  6.  * Netscape Communications Corporation.
  7.  * Portions created by the Initial Developer are Copyright (C) 2002
  8.  * the Initial Developer. All Rights Reserved.
  9.  *
  10.  * Alternatively, the contents of this file may be used under the terms of
  11.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  12.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  13.  * in which case the provisions of the GPL or the LGPL are applicable instead
  14.  * of those above. If you wish to allow use of your version of this file only
  15.  * under the terms of either the GPL or the LGPL, and not to allow others to
  16.  * use your version of this file under the terms of the NPL, indicate your
  17.  * decision by deleting the provisions above and replace them with the notice
  18.  * and other provisions required by the GPL or the LGPL. If you do not delete
  19.  * the provisions above, a recipient may use your version of this file under
  20.  * the terms of any one of the NPL, the GPL or the LGPL.
  21.  *
  22.  * ***** END LICENSE BLOCK ***** */
  23.  
  24. var popupManager = null;
  25.  
  26. var permissions = [];
  27.  
  28. var listCapability; // the capability of sites on the currently viewed list
  29. // TRUE: current popup policy is BLOCK ALL WITH EXCEPTIONS - sites on
  30. // the whitelist are allowed and have permission.capability = true
  31. // FALSE: current popup policy is ALLOW ALL WITH EXCEPTIONS - sites on
  32. // the blacklist are blocked and have permission.capability = false
  33.  
  34. const POPUP_TYPE = 2;
  35.  
  36. var additions = [];
  37. var removals = [];
  38.  
  39. const SIMPLEURI_CONTRACTID = "@mozilla.org/network/simple-uri;1";
  40.  
  41. var sortColumn = "host";
  42. var lastSort = false;
  43.  
  44. var permissionsTreeView = {
  45.     rowCount: 0,
  46.     setTree: function(tree) {},
  47.     getImageSrc: function(row, column) {},
  48.     getProgressMode: function(row, column) {},
  49.     getCellValue: function(row, column) {},
  50.     getCellText: function(row, column) {
  51.       var rv = permissions[row].host;
  52.       return rv;
  53.   },
  54.   isSeparator: function(index) { return false; },
  55.   isSorted: function() { return false; },
  56.   isContainer: function(index) { return false; },
  57.   cycleHeader: function(aColId, aElt) {},
  58.   getRowProperties: function(row, column,prop) {},
  59.   getColumnProperties: function(column, columnElement, prop) {},
  60.   getCellProperties: function(row, prop) {}
  61. };
  62.  
  63. var permissionsTree;
  64. var popupStringBundle;
  65.  
  66. function Startup() {
  67.   popupManager = Components.classes["@mozilla.org/PopupWindowManager;1"]
  68.                            .getService(Components.interfaces.nsIPopupWindowManager);
  69.  
  70.   permissionsTree = document.getElementById("permissionsTree");
  71.  
  72.   popupStringBundle = document.getElementById("popupStringBundle");
  73.   var title;
  74.  
  75.   if (window.arguments[0]) {
  76.     document.getElementById("blockExcept").hidden = false;
  77.     lastSort = (permissionsTree.getAttribute("lastSortWhitelist") == "true");
  78.     title = popupStringBundle.getString("whitelistTitle");
  79.   }
  80.   else {
  81.     document.getElementById("allowExcept").hidden = false;
  82.     lastSort = (permissionsTree.getAttribute("lastSortBlacklist") == "true");
  83.     title = popupStringBundle.getString("blacklistTitle");
  84.   }
  85.  
  86.   document.getElementById("popupManager").setAttribute("title", title);
  87.  
  88.   listCapability = window.arguments[0];
  89.  
  90.   loadPermissions(permissions);
  91.   loadTree();
  92.    
  93.   if (window.arguments[1]) { // dialog opened from statusbar icon
  94.     if (listCapability) {
  95.       document.getElementById("addSiteBox").value = window.arguments[1];
  96.     }
  97.     else {
  98.      // pre-pend '.' so we always match on host boundaries. Otherwise 
  99.      // we might think notfoo.com matches foo.com
  100.       var currentLoc = '.'+window.arguments[1];
  101.       var nextHost;
  102.       var inList;
  103.  
  104.       var matchIndex;
  105.       var matchLength = 0;
  106.  
  107.       for (var i = 0; i < permissionsTreeView.rowCount; i++) {
  108.         nextHost = '.'+permissions[i].host;
  109.  
  110.         if (currentLoc.length < nextHost.length)
  111.           continue; // can't be a match, list host is more specific
  112.  
  113.         // look for an early out exact match -- check length first for speed
  114.         if (currentLoc.length == nextHost.length && nextHost == currentLoc) {
  115.           inList = true;
  116.           matchIndex = i;
  117.           break;
  118.         }
  119.  
  120.         if (nextHost == currentLoc.substr(currentLoc.length - nextHost.length)) { 
  121.           inList = true;
  122.           if (listCapability) // host is already on whitelist, don't prefill
  123.             break;
  124.  
  125.           if (nextHost.length > matchLength) {
  126.             matchIndex = i;
  127.             matchLength = nextHost.length;
  128.           }
  129.         }        
  130.       }
  131.       
  132.       if (inList) // host is in blacklist, select for removal
  133.         permissionsTree.treeBoxObject.view.selection.select(matchIndex);
  134.     }
  135.   }
  136.  
  137.   document.documentElement.addEventListener("keypress", onReturnHit, true);
  138.  
  139.   window.sizeToContent();
  140. }
  141.  
  142. function onAccept() {
  143.   finalizeChanges();
  144.   
  145.   var unblocked; 
  146.  
  147.   if (listCapability) {
  148.     unblocked = additions;
  149.     permissionsTree.setAttribute("lastSortWhitelist", !lastSort);
  150.   }
  151.   else {
  152.     unblocked = removals;
  153.     permissionsTree.setAttribute("lastSortBlacklist", !lastSort);
  154.   }
  155.  
  156.   var nextLocation;
  157.   var nextUnblocked;
  158.  
  159.   var windowMediator = Components.classes['@mozilla.org/appshell/window-mediator;1']
  160.                                  .getService(Components.interfaces.nsIWindowMediator);
  161.   var enumerator = windowMediator.getEnumerator("navigator:browser");
  162.  
  163.   //if a site that is currently open is unblocked, make icon go away
  164.   while(enumerator.hasMoreElements()) {
  165.     var win = enumerator.getNext();
  166.     
  167.     var browsers = win.getBrowser().browsers;
  168.     for (var i = 0; i < browsers.length; i++) {
  169.       try {
  170.         nextLocation = browsers[i].currentURI.hostPort;
  171.       }
  172.       catch(ex) { 
  173.         //blank window
  174.       }
  175.  
  176.       if (nextLocation) {
  177.         nextLocation = '.'+nextLocation;
  178.         for (var j in unblocked) {
  179.           nextUnblocked = '.'+unblocked[j];
  180.  
  181.           if (nextUnblocked.length > nextLocation.length)
  182.              continue; // can't be a match
  183.  
  184.           if (nextUnblocked == 
  185.               nextLocation.substr(nextLocation.length - nextUnblocked.length)) {
  186.             browsers[i].popupDomain = null;
  187.             win.document.getElementById("popupIcon").hidden = true;
  188.           }
  189.         }
  190.       }
  191.     } 
  192.   }
  193.  
  194.   if (window.arguments[2])
  195.     window.opener.setButtons();
  196.  
  197.  
  198.   return true;                                           
  199. }
  200.  
  201. function Permission(host, number) {
  202.   this.host = host;
  203.   this.number = number;
  204. }
  205.  
  206. function loadPermissions(table) {
  207.   var enumerator = popupManager.getEnumerator();
  208.   var count = 0;
  209.   
  210.   while (enumerator.hasMoreElements()) {
  211.     var permission = enumerator.getNext()
  212.                                .QueryInterface(Components.interfaces.nsIPermission);
  213.     if (permission.capability == listCapability) {
  214.       var host = permission.host;
  215.       table[count] = new Permission(host,count++);
  216.     }
  217.   }
  218. }
  219.  
  220. function loadTree() {
  221.   var rowCount = permissions.length;
  222.   permissionsTreeView.rowCount = rowCount;
  223.   permissionsTree.treeBoxObject.view = permissionsTreeView;
  224.   permissionColumnSort();
  225.   
  226.   if (permissions.length == 0)
  227.     document.getElementById("removeAllPermissions").setAttribute("disabled","true");
  228.   else
  229.     document.getElementById("removeAllPermissions").removeAttribute("disabled");
  230. }
  231.  
  232. function permissionColumnSort() {
  233.   lastSort = 
  234.     SortTree(permissionsTree, permissionsTreeView, permissions,
  235.              sortColumn, sortColumn, lastSort);
  236. }
  237.  
  238. function permissionSelected() {
  239.   var selections = GetTreeSelections(permissionsTree);
  240.   if (selections.length) {
  241.     document.getElementById("removePermission").removeAttribute("disabled");
  242.   }
  243. }
  244.  
  245. function deletePermissions() {
  246.   var selections = GetTreeSelections(permissionsTree);
  247.   
  248.   for (var s = selections.length - 1; s >= 0; s--) {
  249.     var i = selections[s];
  250.  
  251.     var host = permissions[i].host;
  252.     updatePendingRemovals(host);
  253.  
  254.     permissions[i] = null;
  255.   }
  256.  
  257.   for (var j = 0; j < permissions.length; j++) {
  258.     if (permissions[j] == null) {
  259.       var k = j;
  260.       while ((k < permissions.length) && (permissions[k] == null)) {
  261.         k++;
  262.       }
  263.       permissions.splice(j, k-j);
  264.     }
  265.   }
  266.  
  267.   var box = permissionsTree.treeBoxObject;
  268.   var firstRow = box.getFirstVisibleRow();
  269.   if (firstRow > (permissions.length - 1) ) {
  270.     firstRow = permissions.length - 1;
  271.   }
  272.   permissionsTreeView.rowCount = permissions.length;
  273.   box.rowCountChanged(0, permissions.length);
  274.   box.scrollToRow(firstRow);
  275.  
  276.   if (permissions.length) {
  277.     var nextSelection = (selections[0] < permissions.length) ? selections[0] : permissions.length - 1;
  278.     box.view.selection.select(-1); 
  279.     box.view.selection.select(nextSelection);
  280.   } 
  281.   else {
  282.     document.getElementById("removePermission").setAttribute("disabled", "true")
  283.     document.getElementById("removeAllPermissions").setAttribute("disabled","true");
  284.  
  285.     permissionsTree.treeBoxObject.view.selection.select(-1); 
  286.   }
  287. }
  288.  
  289. function deleteAllPermissions() {
  290.   for (var i = 0; i < permissions.length; i++) {
  291.     var host = permissions[i].host;
  292.     updatePendingRemovals(host);
  293.   }
  294.  
  295.   permissions.length = 0;
  296.   clearTree();
  297. }
  298.  
  299. function updatePendingRemovals(host) {
  300.   if (additions[host] != null)
  301.     additions[host] = null;
  302.   else
  303.     removals[host] = host;
  304. }
  305.  
  306. function clearTree() {
  307.   permissionsTree.treeBoxObject.view.selection.select(-1); 
  308.  
  309.   permissionsTreeView.rowCount = 0;
  310.   permissionsTree.treeBoxObject.invalidate();
  311.  
  312.   document.getElementById("removePermission").setAttribute("disabled", "true")
  313.   document.getElementById("removeAllPermissions").setAttribute("disabled","true");
  314. }
  315.  
  316. function finalizeChanges() {
  317.   var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  318.                             .getService(Components.interfaces.nsIIOService);
  319.   
  320.   //note: the scheme will be taken off later, it is being added now only to
  321.   //create the uri for add/remove
  322.   for (var i in additions) {
  323.     var host = additions[i];
  324.     if (host != null) {
  325.       host = "http://" + host;
  326.       var uri = ioService.newURI(host, null, null);
  327.       popupManager.add(uri, listCapability);
  328.     }
  329.   }
  330.  
  331.   for (var i in removals) {
  332.     var host = removals[i];
  333.     if (host != null) {
  334.       host = "http://" + host;
  335.       var uri = ioService.newURI(host, null, null);
  336.       popupManager.remove(uri);
  337.     }
  338.   }
  339.  
  340. }
  341.  
  342. function handlePermissionKeyPress(e) {
  343.   if (e.keyCode == 46) {
  344.     deletePermissions();
  345.   }
  346. }
  347.  
  348. function addPermission() {
  349.   var addSiteBox = document.getElementById("addSiteBox");
  350.   var host = addSiteBox.value;
  351.   
  352.   if (host != "") {
  353.     host = host.replace(/^\s*([-\w]*:\/*)?/, ""); // trim any leading space and scheme    
  354.     
  355.     var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  356.                                   .getService(Components.interfaces.nsIPromptService); 
  357.     var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  358.                               .getService(Components.interfaces.nsIIOService);
  359.     
  360.     try {
  361.       var uri = ioService.newURI("http://"+host, null, null);
  362.     }
  363.     catch(ex) {
  364.       var msgInvalid = popupStringBundle.getFormattedString("alertInvalid", [host]);
  365.       if (promptService)
  366.         promptService.alert(window, "", msgInvalid);
  367.       addSiteBox.value = "";
  368.       return;
  369.     }
  370.  
  371.     if (!host) {
  372.       addSiteBox.value = "";
  373.       return;
  374.     }
  375.  
  376.     var length = permissions.length;   
  377.  
  378.     var isDuplicate = false;
  379.     for (var i = 0; i < length; i++) {
  380.       if (permissions[i].host == host) {
  381.         var msgDuplicate = popupStringBundle.getFormattedString("alertDuplicate", [host]); 
  382.         if (promptService)
  383.           promptService.alert(window, "", msgDuplicate);
  384.         isDuplicate = true;
  385.         break;
  386.       }
  387.     }
  388.  
  389.     if (!isDuplicate) {
  390.       var newPermission = new Permission(host, length);
  391.       permissions.push(newPermission);
  392.  
  393.       lastSort = !lastSort; //keep same sort direction
  394.       loadTree();
  395.     
  396.       if (removals[host] != null)
  397.         removals[host] = null;
  398.       else
  399.         additions[host] = host;
  400.     }
  401.  
  402.     addSiteBox.value = "";
  403.   }
  404. }
  405.  
  406. function onReturnHit(event) {
  407.   var focusedElement = document.commandDispatcher.focusedElement;
  408.   var addSiteBox = document.getElementById("addSiteBox");
  409.   if (event.keyCode == 13) {
  410.     if (focusedElement) {
  411.       if (focusedElement.id == "permissionsTree")
  412.         return;
  413.       else {
  414.         event.preventBubble();
  415.         if (focusedElement == addSiteBox.inputField) {
  416.           var addSiteButton = document.getElementById("addSiteButton");
  417.           addSiteButton.doCommand();
  418.         }
  419.       }
  420.     }
  421.   }
  422. }
  423.  
  424. function doHelpButton() {
  425.   openHelp("pop_up_blocking_prefs");
  426.   return true;
  427. }
  428.  
  429.