public void persistentRequestError(final String id, final NodeMessage nm) {
   if (uploadModelItems.containsKey(id)) {
     final FrostUploadItem item = uploadModelItems.get(id);
     item.setEnabled(false);
     item.setState(FrostUploadItem.STATE_FAILED);
     item.setErrorCodeDescription(nm.getStringValue("CodeDescription"));
   } else if (downloadModelItems.containsKey(id)) {
     final FrostDownloadItem item = downloadModelItems.get(id);
     item.setEnabled(false);
     item.setState(FrostDownloadItem.STATE_FAILED);
     item.setErrorCodeDescription(nm.getStringValue("CodeDescription"));
   } else {
     System.out.println("persistentRequestError: ID not in any model: " + id);
   }
 }
  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 void persistentRequestRemoved(final FcpPersistentGet downloadRequest) {
   if (downloadModelItems.containsKey(downloadRequest.getIdentifier())) {
     final FrostDownloadItem dlItem = downloadModelItems.get(downloadRequest.getIdentifier());
     if (dlItem.isExternal()) {
       SwingUtilities.invokeLater(
           new Runnable() {
             public void run() {
               List<FrostDownloadItem> itemList = new ArrayList<FrostDownloadItem>();
               itemList.add(dlItem);
               downloadModel.removeItems(itemList);
             }
           });
     } else {
       if (dlItem.isInternalRemoveExpected()) {
         dlItem.setInternalRemoveExpected(false); // clear flag
       } else if (dlItem.getState() != FrostDownloadItem.STATE_DONE) {
         dlItem.setEnabled(false);
         dlItem.setState(FrostDownloadItem.STATE_FAILED);
         dlItem.setErrorCodeDescription("Disappeared from global queue");
       }
     }
   }
 }
 private void addExternalItem(final FcpPersistentGet downloadRequest) {
   // direct downloads maybe have no filename, use identifier
   String fileName = downloadRequest.getFilename();
   if (fileName == null) {
     fileName = downloadRequest.getIdentifier();
   } else if (fileName.indexOf('/') > -1 || fileName.indexOf('\\') > -1) {
     // filename contains directories, use only filename
     final String stmp = new File(fileName).getName();
     if (stmp.length() > 0) {
       fileName = stmp; // use plain filename
     }
   }
   final FrostDownloadItem dlItem = new FrostDownloadItem(fileName, downloadRequest.getUri());
   dlItem.setExternal(true);
   dlItem.setGqIdentifier(downloadRequest.getIdentifier());
   dlItem.setState(FrostDownloadItem.STATE_PROGRESS);
   SwingUtilities.invokeLater(
       new Runnable() {
         public void run() {
           downloadModel.addExternalItem(dlItem);
         }
       });
   applyState(dlItem, downloadRequest);
 }
  /** 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
        }
      }
    }
  }