private void showExternalDownloadItems() { final Map<String, FcpPersistentGet> items = persistentQueue.getDownloadRequests(); for (final FcpPersistentGet downloadRequest : items.values()) { if (!downloadModelItems.containsKey(downloadRequest.getIdentifier())) { addExternalItem(downloadRequest); } } }
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 void persistentRequestUpdated(final FcpPersistentGet downloadRequest) { final FrostDownloadItem dl = downloadModelItems.get(downloadRequest.getIdentifier()); if (dl == null) { // not (yet) in our model return; } applyState(dl, downloadRequest); }
public void persistentRequestAdded(final FcpPersistentGet downloadRequest) { final FrostDownloadItem dlItem = downloadModelItems.get(downloadRequest.getIdentifier()); if (dlItem != null) { // own item added to global queue, or existing external item applyState(dlItem, downloadRequest); } else { if (showExternalItemsDownload) { addExternalItem(downloadRequest); } } }
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); }
public void persistentRequestModified(final FcpPersistentGet downloadRequest) { if (downloadModelItems.containsKey(downloadRequest.getIdentifier())) { final FrostDownloadItem dlItem = downloadModelItems.get(downloadRequest.getIdentifier()); applyPriority(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 } } } }