private void addExternalItem(final FcpPersistentPut uploadRequest) {
    final FrostUploadItem ulItem = new FrostUploadItem();
    ulItem.setGqIdentifier(uploadRequest.getIdentifier());
    ulItem.setExternal(true);
    // direct uploads maybe have no filename, use identifier
    String fileName = uploadRequest.getFilename();
    if (fileName == null) {
      fileName = uploadRequest.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
      }
    }
    ulItem.setFile(new File(fileName));
    ulItem.setFileName(fileName);
    ulItem.setFileSize(uploadRequest.getFileSize());
    ulItem.setPriority(uploadRequest.getPriority());

    ulItem.setState(FrostUploadItem.STATE_PROGRESS);
    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            uploadModel.addExternalItem(ulItem);
          }
        });
    applyState(ulItem, uploadRequest);
  }
 private void showExternalUploadItems() {
   final Map<String, FcpPersistentPut> items = persistentQueue.getUploadRequests();
   for (final FcpPersistentPut uploadRequest : items.values()) {
     if (!uploadModelItems.containsKey(uploadRequest.getIdentifier())) {
       addExternalItem(uploadRequest);
     }
   }
 }
 public void persistentRequestUpdated(final FcpPersistentPut uploadRequest) {
   final FrostUploadItem ui = uploadModelItems.get(uploadRequest.getIdentifier());
   if (ui == null) {
     // not (yet) in our model
     return;
   }
   applyState(ui, uploadRequest);
 }
 public void persistentRequestAdded(final FcpPersistentPut uploadRequest) {
   final FrostUploadItem ulItem = uploadModelItems.get(uploadRequest.getIdentifier());
   if (ulItem != null) {
     // own item added to global queue, or existing external item
     applyState(ulItem, uploadRequest);
   } else {
     if (showExternalItemsUpload) {
       addExternalItem(uploadRequest);
     }
   }
 }
 public void persistentRequestRemoved(final FcpPersistentPut uploadRequest) {
   if (uploadModelItems.containsKey(uploadRequest.getIdentifier())) {
     final FrostUploadItem ulItem = uploadModelItems.get(uploadRequest.getIdentifier());
     if (ulItem.isExternal()) {
       SwingUtilities.invokeLater(
           new Runnable() {
             public void run() {
               List<FrostUploadItem> itemList = new ArrayList<FrostUploadItem>();
               itemList.add(ulItem);
               uploadModel.removeItems(itemList);
             }
           });
     } else {
       if (ulItem.isInternalRemoveExpected()) {
         ulItem.setInternalRemoveExpected(false); // clear flag
       } else if (ulItem.getState() != FrostUploadItem.STATE_DONE) {
         ulItem.setEnabled(false);
         ulItem.setState(FrostUploadItem.STATE_FAILED);
         ulItem.setErrorCodeDescription("Disappeared from global queue");
       }
     }
   }
 }
 public void persistentRequestModified(final FcpPersistentPut uploadRequest) {
   if (uploadModelItems.containsKey(uploadRequest.getIdentifier())) {
     final FrostUploadItem ulItem = uploadModelItems.get(uploadRequest.getIdentifier());
     ulItem.setPriority(uploadRequest.getPriority());
   }
 }
  /** 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);
      }
    }
  }