home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / phoenx05.zip / phoenix / chrome / browser.jar / content / browser / downloads / downloadPanel.js < prev    next >
Text File  |  2002-11-17  |  7KB  |  235 lines

  1. const kObserverServiceProgID = "@mozilla.org/observer-service;1";
  2. const NC_NS = "http://home.netscape.com/NC-rdf#";
  3.  
  4. var gDownloadManager;
  5. var gDownloadHistoryView;
  6.  
  7. const dlObserver = {
  8.   observe: function(subject, topic, state) {
  9.     if (topic != "dl-progress") return;
  10.     var dl = subject.QueryInterface(Components.interfaces.nsIDownload);
  11.     var elt = document.getElementById(dl.target.path);
  12.     
  13.     var percentComplete = dl.percentComplete;
  14.     if (percentComplete == -1) {
  15.       if (!elt.hasAttribute("progressmode"))
  16.         elt.setAttribute("progressmode", "undetermined");
  17.       if (elt.hasAttribute("progress"))
  18.         elt.removeAttribute("progress");
  19.     }
  20.     else {
  21.       elt.setAttribute("progress", percentComplete);
  22.       if (elt.hasAttribute("progressmode"))
  23.         elt.removeAttribute("progressmode");
  24.     }
  25.   }
  26. };
  27.  
  28. function Startup() {
  29.   var downloadView = document.getElementById("downloadView");
  30.  
  31.   const dlmgrContractID = "@mozilla.org/download-manager;1";
  32.   const dlmgrIID = Components.interfaces.nsIDownloadManager;
  33.   gDownloadManager = Components.classes[dlmgrContractID].getService(dlmgrIID);
  34.  
  35.   var ds = gDownloadManager.datasource;
  36.  
  37.   downloadView.database.AddDataSource(ds);
  38.   downloadView.builder.rebuild();
  39.  
  40.   gDownloadHistoryView = document.getElementById("downloadHistoryView");
  41.   gDownloadHistoryView.database.AddDataSource(ds);
  42.   gDownloadHistoryView.builder.rebuild();
  43.  
  44.   var observerService = Components.classes[kObserverServiceProgID]
  45.                                   .getService(Components.interfaces.nsIObserverService);
  46.   observerService.addObserver(dlObserver, "dl-progress", false);
  47.   observerService.addObserver(dlObserver, "dl-done", false);
  48.   observerService.addObserver(dlObserver, "dl-cancel", false);
  49.   observerService.addObserver(dlObserver, "dl-failed", false);
  50.   
  51.   window.setTimeout(onRebuild, 0);
  52. }
  53.  
  54. function onRebuild() {
  55.   gDownloadHistoryView.controllers.appendController(downloadViewController);
  56. }
  57.  
  58. function Shutdown() {
  59.   var observerService = Components.classes[kObserverServiceProgID]
  60.                                   .getService(Components.interfaces.nsIObserverService);
  61.   observerService.removeObserver(dlObserver, "dl-progress");
  62.   observerService.removeObserver(dlObserver, "dl-done");
  63.   observerService.removeObserver(dlObserver, "dl-cancel");
  64.   observerService.removeObserver(dlObserver, "dl-failed");
  65. }
  66.  
  67. var downloadDNDObserver =
  68. {
  69.   onDragOver: function (aEvent, aFlavour, aDragSession)
  70.   {
  71.     aDragSession.canDrop = true;
  72.   },
  73.   
  74.   onDrop: function(aEvent, aXferData, aDragSession)
  75.   {
  76.     var split = aXferData.data.split("\n");
  77.     var url = split[0];
  78.     var name = split[1];
  79.     saveURL(url, name, null, true, true);
  80.   },
  81.   _flavourSet: null,  
  82.   getSupportedFlavours: function ()
  83.   {
  84.     if (!this._flavourSet) {
  85.       this._flavourSet = new FlavourSet();
  86.       this._flavourSet.appendFlavour("text/x-moz-url");
  87.       this._flavourSet.appendFlavour("text/unicode");
  88.     }
  89.     return this._flavourSet;
  90.   }
  91. }
  92.  
  93. function onSelect(aEvent) {
  94.   window.updateCommands("list-select");
  95. }
  96.   
  97. var downloadViewController = {
  98.   supportsCommand: function dVC_supportsCommand (aCommand)
  99.   {
  100.     switch (aCommand) {
  101.     case "cmd_properties":
  102.     case "cmd_remove":
  103.     case "cmd_openfile":
  104.     case "cmd_showinshell":
  105.     case "cmd_selectAll":
  106.       return true;
  107.     }
  108.     return false;
  109.   },
  110.   
  111.   isCommandEnabled: function dVC_isCommandEnabled (aCommand)
  112.   {    
  113.     var selectionCount = gDownloadHistoryView.selectedCount;
  114.     if (!selectionCount) return false;
  115.  
  116.     var selectedItem = gDownloadHistoryView.selectedItem;
  117.     switch (aCommand) {
  118.     case "cmd_openfile":
  119.     case "cmd_showinshell":
  120.     case "cmd_properties":
  121.       return selectionCount == 1;
  122.     case "cmd_remove":
  123.       return selectionCount;
  124.     case "cmd_selectAll":
  125.       return gDownloadHistoryView.getRowCount() != selectionCount;
  126.     default:
  127.       return false;
  128.     }
  129.   },
  130.   
  131.   doCommand: function dVC_doCommand (aCommand)
  132.   {
  133.     var selectedItem, selectedItems, file, i;
  134.  
  135.     switch (aCommand) {
  136.     case "cmd_openfile":
  137.       selectedItem = gDownloadHistoryView.selectedItem;
  138.       file = getFileForItem(selectedItem);
  139.       file.launch();
  140.       break;
  141.     case "cmd_showinshell":
  142.       selectedItem = gDownloadHistoryView.selectedItem;
  143.       file = getFileForItem(selectedItem);
  144.       
  145.       file.reveal();
  146.       break;
  147.     case "cmd_properties":
  148.       selectedItem = gDownloadHistoryView.selectedItem;
  149.       window.openDialog("chrome://browser/content/downloads/downloadProperties.xul",
  150.                         "_blank", "modal,centerscreen,chrome,resizable=no", selectedItem.id);
  151.       break;
  152.     case "cmd_remove":
  153.       selectedItems = gDownloadHistoryView.selectedItems;
  154.       var selectedIndex = gDownloadHistoryView.selectedIndex;
  155.       gDownloadManager.startBatchUpdate();
  156.       
  157.       // Notify the datasource that we're about to begin a batch operation
  158.       var observer = gDownloadHistoryView.builder.QueryInterface(Components.interfaces.nsIRDFObserver);
  159.       var ds = gDownloadHistoryView.database;
  160.       observer.beginUpdateBatch(ds);
  161.       
  162.       for (i = 0; i <= selectedItems.length - 1; ++i) {
  163.         gDownloadManager.removeDownload(selectedItems[i].id);
  164.       }
  165.  
  166.       gDownloadManager.endBatchUpdate();
  167.       observer.endUpdateBatch(ds);
  168.       var remote = gDownloadManager.datasource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  169.       remote.Flush();
  170.       gDownloadHistoryView.builder.rebuild();
  171.       var rowCount = gDownloadHistoryView.getRowCount();
  172.       if (selectedIndex > ( rowCount- 1))
  173.         selectedIndex = rowCount - 1;
  174.  
  175.       gDownloadHistoryView.selectedIndex = selectedIndex;
  176.       break;
  177.     case "cmd_selectAll":
  178.       gDownloadHistoryView.selectAll();
  179.       break;
  180.     default:
  181.     }
  182.   },  
  183.   
  184.   onEvent: function dVC_onEvent (aEvent)
  185.   {
  186.     switch (aEvent) {
  187.     case "list-select":
  188.       this.onCommandUpdate();
  189.     }
  190.   },
  191.  
  192.   onCommandUpdate: function dVC_onCommandUpdate ()
  193.   {
  194.     var cmds = ["cmd_properties", "cmd_remove",
  195.                 "cmd_openfile", "cmd_showinshell"];
  196.     for (var command in cmds)
  197.       goUpdateCommand(cmds[command]);
  198.   }
  199. };
  200.  
  201. function getFileForItem(aElement)
  202. {
  203.   return createLocalFile(aElement.id);
  204. }
  205.  
  206. function createLocalFile(aFilePath) 
  207. {
  208.   var lfContractID = "@mozilla.org/file/local;1";
  209.   var lfIID = Components.interfaces.nsILocalFile;
  210.   var lf = Components.classes[lfContractID].createInstance(lfIID);
  211.   lf.initWithPath(aFilePath);
  212.   return lf;
  213. }
  214.  
  215. function buildContextMenu()
  216. {
  217.   var selectionCount = gDownloadHistoryView.selectedCount;
  218.   if (!selectionCount)
  219.     return false;
  220.  
  221.   var launchItem = document.getElementById("menuitem_launch");
  222.   var launchSep = document.getElementById("menuseparator_launch");
  223.   var removeItem = document.getElementById("menuitem_remove");
  224.   var showItem = document.getElementById("menuitem_show");
  225.   var propsItem = document.getElementById("menuitem_properties");
  226.   var propsSep = document.getElementById("menuseparator_properties");
  227.   showItem.hidden = selectionCount != 1;
  228.   launchItem.hidden = selectionCount != 1;
  229.   launchSep.hidden = selectionCount != 1;
  230.   propsItem.hidden = selectionCount != 1;
  231.   propsSep.hidden = selectionCount != 1;
  232.   return true;
  233. }
  234.     
  235.