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);
   }
 }
 private void startNewDownloads() {
   boolean isLimited = true;
   int currentAllowedDownloadCount = 0;
   {
     final int allowedConcurrentDownloads =
         Core.frostSettings.getIntValue(SettingsClass.DOWNLOAD_MAX_THREADS);
     if (allowedConcurrentDownloads <= 0) {
       isLimited = false;
     } else {
       int runningDownloads = 0;
       for (final FrostDownloadItem dlItem : downloadModelItems.values()) {
         if (!dlItem.isExternal() && dlItem.getState() == FrostDownloadItem.STATE_PROGRESS) {
           runningDownloads++;
         }
       }
       currentAllowedDownloadCount = allowedConcurrentDownloads - runningDownloads;
       if (currentAllowedDownloadCount < 0) {
         currentAllowedDownloadCount = 0;
       }
     }
   }
   {
     while (!isLimited || currentAllowedDownloadCount > 0) {
       final FrostDownloadItem dlItem =
           FileTransferManager.inst().getDownloadManager().selectNextDownloadItem();
       if (dlItem == null) {
         break;
       }
       // start the download
       if (startDownload(dlItem)) {
         currentAllowedDownloadCount--;
       }
     }
   }
 }
 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 persistentRequestUpdated(final FcpPersistentPut uploadRequest) {
   final FrostUploadItem ui = uploadModelItems.get(uploadRequest.getIdentifier());
   if (ui == null) {
     // not (yet) in our model
     return;
   }
   applyState(ui, uploadRequest);
 }
 private void showExternalDownloadItems() {
   final Map<String, FcpPersistentGet> items = persistentQueue.getDownloadRequests();
   for (final FcpPersistentGet downloadRequest : items.values()) {
     if (!downloadModelItems.containsKey(downloadRequest.getIdentifier())) {
       addExternalItem(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 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 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");
       }
     }
   }
 }
 public void persistentRequestModified(final FcpPersistentGet downloadRequest) {
   if (downloadModelItems.containsKey(downloadRequest.getIdentifier())) {
     final FrostDownloadItem dlItem = downloadModelItems.get(downloadRequest.getIdentifier());
     applyPriority(dlItem, downloadRequest);
   }
 }
 public void persistentRequestModified(final FcpPersistentPut uploadRequest) {
   if (uploadModelItems.containsKey(uploadRequest.getIdentifier())) {
     final FrostUploadItem ulItem = uploadModelItems.get(uploadRequest.getIdentifier());
     ulItem.setPriority(uploadRequest.getPriority());
   }
 }
  /** Must be called after the upload and download model is initialized! */
  public PersistenceManager(final UploadModel um, final DownloadModel dm) throws Throwable {

    showExternalItemsDownload =
        Core.frostSettings.getBoolValue(SettingsClass.GQ_SHOW_EXTERNAL_ITEMS_DOWNLOAD);
    showExternalItemsUpload =
        Core.frostSettings.getBoolValue(SettingsClass.GQ_SHOW_EXTERNAL_ITEMS_UPLOAD);

    if (FcpHandler.inst().getFreenetNode() == null) {
      throw new Exception("No freenet nodes defined");
    }
    final NodeAddress na = FcpHandler.inst().getFreenetNode();
    fcpConn = FcpListenThreadConnection.createInstance(na);
    fcpTools = new FcpMultiRequestConnectionFileTransferTools(fcpConn);

    Core.frostSettings.addPropertyChangeListener(
        new PropertyChangeListener() {
          public void propertyChange(final PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(SettingsClass.GQ_SHOW_EXTERNAL_ITEMS_DOWNLOAD)) {
              showExternalItemsDownload =
                  Core.frostSettings.getBoolValue(SettingsClass.GQ_SHOW_EXTERNAL_ITEMS_DOWNLOAD);
              if (showExternalItemsDownload) {
                // get external items
                showExternalDownloadItems();
              }
            } else if (evt.getPropertyName().equals(SettingsClass.GQ_SHOW_EXTERNAL_ITEMS_UPLOAD)) {
              showExternalItemsUpload =
                  Core.frostSettings.getBoolValue(SettingsClass.GQ_SHOW_EXTERNAL_ITEMS_UPLOAD);
              if (showExternalItemsUpload) {
                // get external items
                showExternalUploadItems();
              }
            }
          }
        });

    uploadModel = um;
    downloadModel = dm;

    // initially get all items from model
    for (int x = 0; x < uploadModel.getItemCount(); x++) {
      final FrostUploadItem ul = (FrostUploadItem) uploadModel.getItemAt(x);
      if (ul.getGqIdentifier() != null) {
        uploadModelItems.put(ul.getGqIdentifier(), ul);
      }
    }
    for (int x = 0; x < downloadModel.getItemCount(); x++) {
      final FrostDownloadItem ul = (FrostDownloadItem) downloadModel.getItemAt(x);
      if (ul.getGqIdentifier() != null) {
        downloadModelItems.put(ul.getGqIdentifier(), ul);
      }
    }

    // enqueue listeners to keep updated about the model items
    uploadModel.addOrderedModelListener(
        new SortedModelListener<FrostUploadItem>() {
          public void modelCleared() {
            for (final FrostUploadItem ul : uploadModelItems.values()) {
              if (ul.isExternal() == false) {
                fcpTools.removeRequest(ul.getGqIdentifier());
              }
            }
            uploadModelItems.clear();
          }

          public void itemAdded(final int position, final FrostUploadItem item) {
            uploadModelItems.put(item.getGqIdentifier(), item);
            if (!item.isExternal()) {
              // maybe start immediately
              startNewUploads();
            }
          }

          public void itemChanged(final int position, final FrostUploadItem item) {}

          public void itemsRemoved(final int[] positions, final List<FrostUploadItem> items) {
            for (final FrostUploadItem item : items) {
              uploadModelItems.remove(item.getGqIdentifier());
              if (item.isExternal() == false) {
                fcpTools.removeRequest(item.getGqIdentifier());
              }
            }
          }
        });

    downloadModel.addOrderedModelListener(
        new SortedModelListener<FrostDownloadItem>() {
          public void modelCleared() {
            for (final FrostDownloadItem ul : downloadModelItems.values()) {
              if (ul.isExternal() == false) {
                fcpTools.removeRequest(ul.getGqIdentifier());
              }
            }
            downloadModelItems.clear();
          }

          public void itemAdded(final int position, final FrostDownloadItem item) {
            downloadModelItems.put(item.getGqIdentifier(), item);
            if (!item.isExternal()) {
              // maybe start immediately
              startNewDownloads();
            }
          }

          public void itemChanged(final int position, final FrostDownloadItem item) {}

          public void itemsRemoved(final int[] positions, final List<FrostDownloadItem> items) {
            for (final FrostDownloadItem item : items) {
              downloadModelItems.remove(item.getGqIdentifier());
              if (item.isExternal() == false) {
                fcpTools.removeRequest(item.getGqIdentifier());
              }
            }
          }
        });

    directTransferQueue = new DirectTransferQueue();
    directTransferThread = new DirectTransferThread();

    persistentQueue = new FcpPersistentQueue(fcpTools, this);
  }