Ejemplo n.º 1
0
  /** Apply the states of FcpRequestGet to the FrostDownloadItem. */
  private void applyState(final FrostDownloadItem dlItem, final FcpPersistentGet getReq) {
    // when cancelled and we expect this, don't set failed; don't even set the old priority!
    if (dlItem.isInternalRemoveExpected() && getReq.isFailed()) {
      final int returnCode = getReq.getCode();
      if (returnCode == 25) {
        return;
      }
    }

    applyPriority(dlItem, getReq);

    if (dlItem.isDirect() != getReq.isDirect()) {
      dlItem.setDirect(getReq.isDirect());
    }

    if (!getReq.isProgressSet() && !getReq.isSuccess() && !getReq.isFailed()) {
      if (dlItem.getState() == FrostDownloadItem.STATE_WAITING) {
        dlItem.setState(FrostDownloadItem.STATE_PROGRESS);
      }
      return;
    }

    if (getReq.isProgressSet()) {
      final int doneBlocks = getReq.getDoneBlocks();
      final int requiredBlocks = getReq.getRequiredBlocks();
      final int totalBlocks = getReq.getTotalBlocks();
      final boolean isFinalized = getReq.isFinalized();
      if (totalBlocks > 0) {
        dlItem.setDoneBlocks(doneBlocks);
        dlItem.setRequiredBlocks(requiredBlocks);
        dlItem.setTotalBlocks(totalBlocks);
        dlItem.setFinalized(isFinalized);
        dlItem.fireValueChanged();
      }
      if (dlItem.getState() != FrostDownloadItem.STATE_PROGRESS) {
        dlItem.setState(FrostDownloadItem.STATE_PROGRESS);
      }
    }
    if (getReq.isSuccess()) {
      // maybe progress was not completely sent
      dlItem.setFinalized(true);
      if (dlItem.getTotalBlocks() > 0 && dlItem.getDoneBlocks() < dlItem.getRequiredBlocks()) {
        dlItem.setDoneBlocks(dlItem.getRequiredBlocks());
        dlItem.fireValueChanged();
      }
      if (dlItem.isExternal()) {
        dlItem.setFileSize(getReq.getFilesize());
        dlItem.setState(FrostDownloadItem.STATE_DONE);
      } else {
        if (dlItem.isDirect()) {
          maybeEnqueueDirectGet(dlItem, getReq.getFilesize());
        } else {
          final FcpResultGet result = new FcpResultGet(true);
          final File targetFile = new File(dlItem.getDownloadFilename());
          FileTransferManager.inst()
              .getDownloadManager()
              .notifyDownloadFinished(dlItem, result, targetFile);
        }
      }
    }
    if (getReq.isFailed()) {
      final String desc = getReq.getCodeDesc();
      if (dlItem.isExternal()) {
        dlItem.setState(FrostDownloadItem.STATE_FAILED);
        dlItem.setErrorCodeDescription(desc);
      } else {
        final int returnCode = getReq.getCode();
        final boolean isFatal = getReq.isFatal();

        final String redirectURI = getReq.getRedirectURI();
        final FcpResultGet result = new FcpResultGet(false, returnCode, desc, isFatal, redirectURI);
        final File targetFile = new File(dlItem.getDownloadFilename());
        final boolean retry =
            FileTransferManager.inst()
                .getDownloadManager()
                .notifyDownloadFinished(dlItem, result, targetFile);
        if (retry) {
          fcpTools.removeRequest(getReq.getIdentifier());
          startDownload(dlItem); // restart immediately
        }
      }
    }
  }