private void applyPriority(final FrostDownloadItem dlItem, final FcpPersistentGet getReq) {
   // apply externally changed priority
   if (dlItem.getPriority() != getReq.getPriority()) {
     if (Core.frostSettings.getBoolValue(SettingsClass.FCP2_ENFORCE_FROST_PRIO_FILE_DOWNLOAD)) {
       // reset priority with our current value
       fcpTools.changeRequestPriority(getReq.getIdentifier(), dlItem.getPriority());
     } else {
       // apply to downloaditem
       dlItem.setPriority(getReq.getPriority());
     }
   }
 }
  public boolean startDownload(final FrostDownloadItem dlItem) {

    if (dlItem == null || dlItem.getState() != FrostDownloadItem.STATE_WAITING) {
      return false;
    }

    dlItem.setDownloadStartedTime(System.currentTimeMillis());

    dlItem.setState(FrostDownloadItem.STATE_PROGRESS);

    final String gqid = dlItem.getGqIdentifier();
    final File targetFile = new File(dlItem.getDownloadFilename());
    boolean isDda =
        fcpTools.startPersistentGet(dlItem.getKey(), gqid, targetFile, dlItem.getPriority());
    dlItem.setDirect(!isDda);

    return true;
  }
  public boolean startUpload(final FrostUploadItem ulItem) {
    if (ulItem == null || ulItem.getState() != FrostUploadItem.STATE_WAITING) {
      return false;
    }

    ulItem.setUploadStartedMillis(System.currentTimeMillis());

    ulItem.setState(FrostUploadItem.STATE_PROGRESS);

    // start the upload
    final boolean doMime;
    final boolean setTargetFileName;
    if (ulItem.isSharedFile()) {
      doMime = false;
      setTargetFileName = false;
    } else {
      doMime = true;
      setTargetFileName = true;
    }

    // try to start using DDA
    boolean isDda =
        fcpTools.startPersistentPutUsingDda(
            ulItem.getGqIdentifier(),
            ulItem.getFile(),
            ulItem.getFileName(),
            doMime,
            setTargetFileName,
            ulItem.getCompress(),
            ulItem.getFreenetCompatibilityMode(),
            ulItem.getPriority());

    if (!isDda) {
      // upload was not startet because DDA is not allowed...
      // if UploadManager selected this file then it is not already in progress!
      directTransferQueue.appendItemToQueue(ulItem);
    }
    return true;
  }
  /** Apply the states of FcpRequestPut to the FrostUploadItem. */
  private void applyState(final FrostUploadItem ulItem, final FcpPersistentPut putReq) {

    // when cancelled and we expect this, don't set failed; don't even set the old priority!
    if (ulItem.isInternalRemoveExpected() && putReq.isFailed()) {
      final int returnCode = putReq.getCode();
      if (returnCode == 25) {
        return;
      }
    }

    if (directPUTsWithoutAnswer.contains(ulItem.getGqIdentifier())) {
      // we got an answer
      directPUTsWithoutAnswer.remove(ulItem.getGqIdentifier());
    }

    // apply externally changed priority
    if (ulItem.getPriority() != putReq.getPriority()) {
      if (Core.frostSettings.getBoolValue(SettingsClass.FCP2_ENFORCE_FROST_PRIO_FILE_UPLOAD)) {
        // reset priority with our current value
        fcpTools.changeRequestPriority(putReq.getIdentifier(), ulItem.getPriority());
      } else {
        // apply to uploaditem
        ulItem.setPriority(putReq.getPriority());
      }
    }

    if (!putReq.isProgressSet() && !putReq.isSuccess() && !putReq.isFailed()) {
      if (ulItem.getState() == FrostUploadItem.STATE_WAITING) {
        ulItem.setState(FrostUploadItem.STATE_PROGRESS);
      }
      return;
    }

    if (putReq.isProgressSet()) {
      final int doneBlocks = putReq.getDoneBlocks();
      final int totalBlocks = putReq.getTotalBlocks();
      final boolean isFinalized = putReq.isFinalized();
      if (totalBlocks > 0) {
        ulItem.setDoneBlocks(doneBlocks);
        ulItem.setTotalBlocks(totalBlocks);
        ulItem.setFinalized(isFinalized);
        ulItem.fireValueChanged();
      }
      if (ulItem.getState() != FrostUploadItem.STATE_PROGRESS) {
        ulItem.setState(FrostUploadItem.STATE_PROGRESS);
      }
    }
    if (putReq.isSuccess()) {
      // maybe progress was not completely sent
      ulItem.setFinalized(true);
      if (ulItem.getTotalBlocks() > 0 && ulItem.getDoneBlocks() != ulItem.getTotalBlocks()) {
        ulItem.setDoneBlocks(ulItem.getTotalBlocks());
      }
      final String chkKey = putReq.getUri();
      if (ulItem.isExternal()) {
        ulItem.setState(FrostUploadItem.STATE_DONE);
        ulItem.setKey(chkKey);
      } else {
        final FcpResultPut result = new FcpResultPut(FcpResultPut.Success, chkKey);
        FileTransferManager.inst().getUploadManager().notifyUploadFinished(ulItem, result);
      }
    }
    if (putReq.isFailed()) {
      final String desc = putReq.getCodeDesc();
      if (ulItem.isExternal()) {
        ulItem.setState(FrostUploadItem.STATE_FAILED);
        ulItem.setErrorCodeDescription(desc);
      } else {
        final int returnCode = putReq.getCode();
        final boolean isFatal = putReq.isFatal();

        final FcpResultPut result;
        if (returnCode == 9) {
          result = new FcpResultPut(FcpResultPut.KeyCollision, returnCode, desc, isFatal);
        } else if (returnCode == 5) {
          result = new FcpResultPut(FcpResultPut.Retry, returnCode, desc, isFatal);
        } else {
          result = new FcpResultPut(FcpResultPut.Error, returnCode, desc, isFatal);
        }
        FileTransferManager.inst().getUploadManager().notifyUploadFinished(ulItem, result);
      }
    }
  }
  /** 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
        }
      }
    }
  }
 public void removeRequests(final List<String> requests) {
   for (final String id : requests) {
     fcpTools.removeRequest(id);
   }
 }