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

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Ben Goodger <ben@netscape.com> (Original Author)
  24.  *   Blake Ross <blakeross@telocity.com>
  25.  *   Jan Varga <varga@utcru.sk>
  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. const NC_NS = "http://home.netscape.com/NC-rdf#";
  42.  
  43. var gDownloadView = null;
  44. var gDownloadManager = null;
  45. var gRDFService = null;
  46. var gNC_File = null;
  47. var gStatusBar = null;
  48.  
  49. const dlObserver = {
  50.   observe: function(subject, topic, state) {
  51.     if (topic != "download-starting") return;
  52.     selectDownload(subject.QueryInterface(Components.interfaces.nsIDownload));
  53.   }
  54. };
  55.  
  56. function selectDownload(aDownload)
  57. {
  58.   var dlElt = document.getElementById(aDownload.target.path);
  59.   var dlIndex = gDownloadView.contentView.getIndexOfItem(dlElt);
  60.   gDownloadView.treeBoxObject.selection.select(dlIndex);
  61.   gDownloadView.treeBoxObject.ensureRowIsVisible(dlIndex);
  62. }
  63.  
  64. function DLManagerStartup()
  65. {  
  66.   if (!window.arguments.length)
  67.     return;
  68.  
  69.   try {
  70.     var observerService = Components.classes[kObserverServiceProgID]
  71.                                     .getService(Components.interfaces.nsIObserverService);
  72.     observerService.addObserver(dlObserver, "download-starting", false);
  73.   }
  74.   catch (ex) {
  75.   }
  76.  
  77.   const rdfSvcContractID = "@mozilla.org/rdf/rdf-service;1";
  78.   const rdfSvcIID = Components.interfaces.nsIRDFService;
  79.   gRDFService = Components.classes[rdfSvcContractID].getService(rdfSvcIID);
  80.   
  81.   gNC_File = gRDFService.GetResource(NC_NS + "File");
  82.  
  83.   gDownloadView = document.getElementById("downloadView");
  84.   setSortVariables(gDownloadView);
  85.   
  86.   const dlmgrContractID = "@mozilla.org/download-manager;1";
  87.   const dlmgrIID = Components.interfaces.nsIDownloadManager;
  88.   gDownloadManager = Components.classes[dlmgrContractID].getService(dlmgrIID);
  89.  
  90.   var ds = window.arguments[0];
  91.   gDownloadView.database.AddDataSource(ds);
  92.   gDownloadView.builder.rebuild();
  93.   window.setTimeout(onRebuild, 0);
  94.  
  95.   var key;
  96.   if ((navigator.platform.indexOf("Win") != -1) ||
  97.       (navigator.platform.indexOf("OS/2") != -1))
  98.     key = "Win";
  99.   else if (navigator.platform.indexOf("Mac") != -1)
  100.     key = "Mac";
  101.   else {
  102.     key = "Unix";
  103.     document.getElementById("btn_openfile").hidden = true;
  104.   }
  105.  
  106.   var bundle = document.getElementById("dlMgrBundle")
  107.   var label = bundle.getString("showInShellLabel" + key);
  108.   var accesskey = bundle.getString("showInShellAccesskey" + key);
  109.   var showBtn = document.getElementById("btn_showinshell");
  110.   showBtn.setAttribute("label", label);
  111.   showBtn.setAttribute("accesskey", accesskey);
  112. }
  113.  
  114. function onRebuild() {
  115.   gDownloadView.controllers.appendController(downloadViewController);
  116.   gDownloadView.focus();
  117.   
  118.   // If the window was opened automatically because
  119.   // a download started, select the new download
  120.   if (window.arguments.length > 1 && window.arguments[1]) {
  121.     var dl = window.arguments[1];
  122.     selectDownload(dl.QueryInterface(Components.interfaces.nsIDownload));
  123.   }
  124.   else if (gDownloadView.view && gDownloadView.view.rowCount > 0) {
  125.     // Select the first item in the view, if any.
  126.     gDownloadView.treeBoxObject.selection.select(0);
  127.   }
  128. }
  129.  
  130. function onSelect(aEvent) {
  131.   if (!gStatusBar)
  132.     gStatusBar = document.getElementById("statusbar-text");
  133.   
  134.   var selectionCount = gDownloadView.treeBoxObject.selection.count;
  135.   if (selectionCount == 1)
  136.     gStatusBar.label = getSelectedItem().id;
  137.   else
  138.     gStatusBar.label = "";
  139.  
  140.   window.updateCommands("tree-select");
  141. }
  142.   
  143. var downloadViewController = {
  144.   supportsCommand: function dVC_supportsCommand (aCommand)
  145.   {
  146.     switch (aCommand) {
  147.     case "cmd_properties":
  148.     case "cmd_pause":
  149.     case "cmd_cancel":
  150.     case "cmd_remove":
  151.     case "cmd_openfile":
  152.     case "cmd_showinshell":
  153.     case "cmd_selectAll":
  154.       return true;
  155.     }
  156.     return false;
  157.   },
  158.   
  159.   isCommandEnabled: function dVC_isCommandEnabled (aCommand)
  160.   {
  161.     if (!gDownloadView.treeBoxObject.selection) return false;
  162.     var selectionCount = gDownloadView.treeBoxObject.selection.count;
  163.     if (!selectionCount) return false;
  164.  
  165.     var selectedItem = getSelectedItem();
  166.     var isDownloading = selectedItem && gDownloadManager.getDownload(selectedItem.id);
  167.  
  168.     switch (aCommand) {
  169.     case "cmd_openfile":
  170.       try {
  171.         if (isDownloading || getFileForItem(selectedItem).isExecutable())
  172.           return false;
  173.       } catch(e) {
  174.         // Exception means file doesn't exist; launch is not allowed.
  175.         return false;
  176.       }      
  177.     case "cmd_showinshell":
  178.       // we can't reveal until the download is complete, because we have not given
  179.       // the file its final name until them.
  180.       return selectionCount == 1 && !isDownloading;
  181.     case "cmd_properties":
  182.       return selectionCount == 1 && isDownloading;
  183.     case "cmd_pause":
  184.       return false;
  185.     case "cmd_cancel":
  186.       // XXX check if selection is still in progress
  187.       //     how to handle multiple selection?
  188.       return isDownloading;
  189.     case "cmd_remove":
  190.       // XXX ensure selection isn't still in progress
  191.       //     and how to handle multiple selection?
  192.       return selectionCount > 0 && !isDownloading;
  193.     case "cmd_selectAll":
  194.       return gDownloadView.view.rowCount != selectionCount;
  195.     default:
  196.       return false;
  197.     }
  198.   },
  199.   
  200.   doCommand: function dVC_doCommand (aCommand)
  201.   {
  202.     var selectedItem, selectedItems;
  203.     var file, i;
  204.  
  205.     switch (aCommand) {
  206.     case "cmd_properties":
  207.       selectedItem = getSelectedItem();
  208.       if (selectedItem && gDownloadManager.getDownload(selectedItem.id))
  209.         gDownloadManager.openProgressDialogFor(selectedItem.id, window);
  210.       break;
  211.     case "cmd_openfile":
  212.       selectedItem = getSelectedItem();
  213.       if (selectedItem) {
  214.         file = getFileForItem(selectedItem);
  215.         file.launch();
  216.       }
  217.       break;
  218.     case "cmd_showinshell":
  219.       selectedItem = getSelectedItem();
  220.       if (selectedItem) {
  221.         file = getFileForItem(selectedItem);
  222.         
  223.         // on unix, open a browser window rooted at the parent
  224.         if ((navigator.platform.indexOf("Win") == -1) &&
  225.             (navigator.platform.indexOf("Mac") == -1) &&
  226.             (navigator.platform.indexOf("OS/2") == -1)) {
  227.           file = file.QueryInterface(Components.interfaces.nsIFile);
  228.           var parent = file.parent;
  229.           if (parent) {
  230.             const browserURL = "chrome://navigator/content/navigator.xul";
  231.             window.openDialog(browserURL, "_blank", "chrome,all,dialog=no", parent.path);
  232.           }
  233.         }
  234.         else {
  235.           file.reveal();
  236.         }
  237.       }
  238.       break;
  239.     case "cmd_pause":
  240.       break;
  241.     case "cmd_cancel":
  242.       // XXX we should probably prompt the user
  243.       selectedItems = getSelectedItems();
  244.       for (i = 0; i < selectedItems.length; i++)
  245.         gDownloadManager.cancelDownload(selectedItems[i].id);
  246.       window.updateCommands("tree-select");
  247.       break;
  248.     case "cmd_remove":
  249.       selectedItems = getSelectedItems();
  250.       // Figure out where to place the selection after deletion
  251.       var newSelectionPos = gDownloadView.contentView.getIndexOfItem(selectedItems[0]);
  252.       
  253.       gDownloadManager.startBatchUpdate();
  254.       
  255.       // Notify the datasource that we're about to begin a batch operation
  256.       var observer = gDownloadView.builder.QueryInterface(Components.interfaces.nsIRDFObserver);
  257.       var ds = gDownloadView.database;
  258.       observer.beginUpdateBatch(ds);
  259.       
  260.       for (i = 0; i <= selectedItems.length - 1; ++i) {
  261.         gDownloadManager.removeDownload(selectedItems[i].id);
  262.       }
  263.  
  264.       gDownloadManager.endBatchUpdate();
  265.       observer.endUpdateBatch(ds);
  266.       var remote = window.arguments[0].QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  267.       remote.Flush();
  268.       gDownloadView.builder.rebuild();
  269.       // If there's nothing on the panel now, skip setting the selection
  270.       if (gDownloadView.treeBoxObject.view && gDownloadView.treeBoxObject.view.rowCount > 0) {
  271.         // Select the item that replaced the first deleted one
  272.         if (newSelectionPos >= gDownloadView.treeBoxObject.view.rowCount)
  273.           newSelectionPos = gDownloadView.treeBoxObject.view.rowCount - 1;
  274.         gDownloadView.treeBoxObject.selection.select(newSelectionPos);
  275.         gDownloadView.treeBoxObject.ensureRowIsVisible(newSelectionPos);
  276.       }
  277.       else {
  278.         // Nothing on the panel, so clear the Status Bar
  279.         gStatusBar.label = "";
  280.       }
  281.       break;
  282.     case "cmd_selectAll":
  283.       gDownloadView.treeBoxObject.selection.selectAll();
  284.       break;
  285.     default:
  286.     }
  287.   },  
  288.   
  289.   onEvent: function dVC_onEvent (aEvent)
  290.   {
  291.     switch (aEvent) {
  292.     case "tree-select":
  293.       this.onCommandUpdate();
  294.     }
  295.   },
  296.  
  297.   onCommandUpdate: function dVC_onCommandUpdate ()
  298.   {
  299.     var cmds = ["cmd_properties", "cmd_pause", "cmd_cancel", "cmd_remove",
  300.                 "cmd_openfile", "cmd_showinshell"];
  301.     for (var command in cmds)
  302.       goUpdateCommand(cmds[command]);
  303.   }
  304. };
  305.  
  306. function getSelectedItem()
  307. {
  308.   if (gDownloadView.currentIndex != -1)
  309.     return gDownloadView.contentView.getItemAtIndex(gDownloadView.currentIndex);
  310.   return null;
  311. }
  312.  
  313. function getSelectedItems()
  314. {
  315.   var items = [];
  316.   var k = 0;
  317.  
  318.   var selection = gDownloadView.treeBoxObject.selection;
  319.   var rangeCount = selection.getRangeCount();
  320.   for (var i = 0; i < rangeCount; i++) {
  321.     var startIndex = {};
  322.     var endIndex = {};
  323.     selection.getRangeAt(i, startIndex, endIndex);
  324.     for (var j = startIndex.value; j <= endIndex.value; j++)
  325.       items[k++] = gDownloadView.contentView.getItemAtIndex(j);
  326.   }
  327.  
  328.   return items;
  329. }
  330.  
  331. function getFileForItem(aElement)
  332. {
  333.   var itemResource = gRDFService.GetUnicodeResource(aElement.id);
  334.   var fileResource = gDownloadView.database.GetTarget(itemResource, gNC_File, true);
  335.   fileResource = fileResource.QueryInterface(Components.interfaces.nsIRDFResource);
  336.   return createLocalFile(fileResource.Value);
  337. }
  338.  
  339. function createLocalFile(aFilePath) 
  340. {
  341.   var lfContractID = "@mozilla.org/file/local;1";
  342.   var lfIID = Components.interfaces.nsILocalFile;
  343.   var lf = Components.classes[lfContractID].createInstance(lfIID);
  344.   lf.initWithPath(aFilePath);
  345.   return lf;
  346. }
  347.  
  348. function DLManagerShutdown()
  349. {
  350.   try {
  351.     var observerService = Components.classes[kObserverServiceProgID]
  352.                      .getService(Components.interfaces.nsIObserverService);
  353.     observerService.removeObserver(dlObserver, "download-starting");
  354.   }
  355.   catch (ex) {
  356.   }
  357. }
  358.  
  359. function setSortVariables(tree)
  360. {
  361.   var node;
  362.   for (node = document.getElementById("Name"); node; node = node.nextSibling) {
  363.     if (node.getAttribute("sortActive") == "true")
  364.       break;
  365.   }
  366.   if (!node) {
  367.     node = document.getElementById("Progress");
  368.     node.setAttribute("sortActive", "true");
  369.     node.setAttribute("sortDirection", "descending");
  370.   }
  371.  
  372.   tree.setAttribute("sortActive", "true");
  373.   tree.setAttribute("sortDirection", node.getAttribute("sortDirection"));
  374.   tree.setAttribute("sortResource", node.getAttribute("resource"));
  375. }
  376.  
  377. function doSort(node)
  378. {
  379.   if (node.localName != "treecol")
  380.     return;
  381.  
  382.   var sortResource = node.getAttribute("resource");
  383.  
  384.   var sortDirection = node.getAttribute("sortDirection");
  385.   sortDirection = sortDirection == "ascending" ? "descending" : "ascending";
  386.  
  387.   try {
  388.     var sortService = Components.classes["@mozilla.org/xul/xul-sort-service;1"]
  389.                       .getService(Components.interfaces.nsIXULSortService);
  390.     sortService.sort(node, sortResource, sortDirection);
  391.   }
  392.   catch(ex) {
  393.   }
  394. }
  395.