Search in sources :

Example 1 with FileDownloadTask

use of org.jabref.gui.externalfiles.FileDownloadTask in project jabref by JabRef.

the class LinkedFilesEditorViewModel method addFromURL.

private void addFromURL(URL url) {
    URLDownload urlDownload = new URLDownload(url);
    Optional<ExternalFileType> suggestedType = inferFileType(urlDownload);
    String suggestedTypeName = suggestedType.map(ExternalFileType::getName).orElse("");
    List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
    Path destination = constructSuggestedPath(suggestedType, fileDirectories);
    LinkedFileViewModel temporaryDownloadFile = new LinkedFileViewModel(new LinkedFile("", url, suggestedTypeName));
    files.add(temporaryDownloadFile);
    FileDownloadTask downloadTask = new FileDownloadTask(url, destination);
    temporaryDownloadFile.downloadProgressProperty().bind(downloadTask.progressProperty());
    downloadTask.setOnSucceeded(event -> {
        files.remove(temporaryDownloadFile);
        LinkedFile newLinkedFile = fromFile(destination, fileDirectories);
        files.add(new LinkedFileViewModel(newLinkedFile));
    });
    downloadTask.setOnFailed(event -> dialogService.showErrorDialogAndWait("", downloadTask.getException()));
    taskExecutor.execute(downloadTask);
}
Also used : UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) Path(java.nio.file.Path) LinkedFile(org.jabref.model.entry.LinkedFile) FileDownloadTask(org.jabref.gui.externalfiles.FileDownloadTask) URLDownload(org.jabref.logic.net.URLDownload)

Aggregations

Path (java.nio.file.Path)1 FileDownloadTask (org.jabref.gui.externalfiles.FileDownloadTask)1 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)1 UnknownExternalFileType (org.jabref.gui.externalfiletype.UnknownExternalFileType)1 URLDownload (org.jabref.logic.net.URLDownload)1 LinkedFile (org.jabref.model.entry.LinkedFile)1