isCommandEnabled: function()

in suite/components/downloads/content/downloadmanager.js [350:451]


  isCommandEnabled: function(aCommand)
  {
    var selectionCount = 0;
    if (gDownloadTreeView && gDownloadTreeView.selection)
      selectionCount = gDownloadTreeView.selection.count;

    var selItemData = [];
    if (selectionCount) {
      // walk all selected rows
      let start = {};
      let end = {};
      let numRanges = gDownloadTreeView.selection.getRangeCount();
      for (let rg = 0; rg < numRanges; rg++) {
        gDownloadTreeView.selection.getRangeAt(rg, start, end);
        for (let row = start.value; row <= end.value; row++)
          selItemData.push(gDownloadTreeView.getRowData(row));
      }
    }

    switch (aCommand) {
      case "cmd_play":
        if (!selectionCount)
          return false;
        for (let dldata of selItemData) {
          if (dldata.succeeded || (!dldata.stopped && !dldata.hasPartialData))
            return false;
        }
        return true;
      case "cmd_pause":
        if (!selectionCount)
          return false;
        for (let dldata of selItemData) {
          if (dldata.stopped || !dldata.hasPartialData)
            return false;
        }
        return true;
      case "cmd_resume":
        if (!selectionCount)
          return false;
        for (let dldata of selItemData) {
          if (!dldata.stopped || !dldata.hasPartialData)
            return false;
        }
        return true;
      case "cmd_open":
        return selectionCount == 1 &&
               selItemData[0].succeeded &&
               selItemData[0].target.exists;
      case "cmd_show":
        // target.exists is only set if the download finished and the target
        // is still located there.
        // For simplicity we just assume the target is there if the download
        // has not succeeded e.g. is still in progress. This might be wrong
        // but showDownload will deal with it.
        return selectionCount == 1 &&
               ((selItemData[0].succeeded &&
                 selItemData[0].target.exists) ||
                 !selItemData[0].succeeded);
      case "cmd_cancel":
        if (!selectionCount)
          return false;
        for (let dldata of selItemData) {
          if (dldata.stopped && !dldata.hasPartialData)
            return false;
        }
        return true;
      case "cmd_retry":
        if (!selectionCount)
          return false;
        for (let dldata of selItemData) {
          if (dldata.succeeded || !dldata.stopped || dldata.hasPartialData)
            return false;
        }
        return true;
      case "cmd_remove":
        if (!selectionCount)
          return false;
        for (let dldata of selItemData) {
          if (!dldata.stopped)
            return false;
        }
        return true;
      case "cmd_openReferrer":
        return selectionCount == 1 && !!selItemData[0].source.referrer;
      case "cmd_stop":
      case "cmd_copyLocation":
        return selectionCount > 0;
      case "cmd_properties":
        return selectionCount == 1;
      case "cmd_selectAll":
        return gDownloadTreeView.rowCount != selectionCount;
      case "cmd_clearList":
        // Since active downloads always sort before removable downloads,
        // we only need to check that the last download has stopped.
        return gDownloadTreeView.rowCount &&
               !gDownloadTreeView.getRowData(gDownloadTreeView.rowCount - 1).isActive;
      case "cmd_paste":
        return true;
      default:
        return false;
    }
  },