|
| 1 | +package org.jabref.gui.copyfiles; |
| 2 | + |
| 3 | +import java.nio.file.Path; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.function.BiFunction; |
| 9 | + |
| 10 | +import javafx.beans.Observable; |
| 11 | +import javafx.beans.binding.Bindings; |
| 12 | + |
| 13 | +import org.jabref.gui.DialogService; |
| 14 | +import org.jabref.gui.actions.SimpleCommand; |
| 15 | +import org.jabref.gui.util.DirectoryDialogConfiguration; |
| 16 | +import org.jabref.logic.FilePreferences; |
| 17 | +import org.jabref.logic.l10n.Localization; |
| 18 | +import org.jabref.logic.util.io.FileUtil; |
| 19 | +import org.jabref.model.database.BibDatabaseContext; |
| 20 | +import org.jabref.model.entry.LinkedFile; |
| 21 | + |
| 22 | +public class CopyLinkedFilesAction extends SimpleCommand { |
| 23 | + |
| 24 | + private final List<LinkedFile> linkedFiles; |
| 25 | + private final DialogService dialogService; |
| 26 | + private final BibDatabaseContext databaseContext; |
| 27 | + private final FilePreferences filePreferences; |
| 28 | + |
| 29 | + private final BiFunction<Path, Path, Path> resolvePathFilename = (dir, file) -> dir.resolve(file.getFileName()); |
| 30 | + |
| 31 | + public CopyLinkedFilesAction(LinkedFile linkedFile, |
| 32 | + DialogService dialogService, |
| 33 | + BibDatabaseContext databaseContext, |
| 34 | + FilePreferences filePreferences) { |
| 35 | + this(List.of(linkedFile), dialogService, databaseContext, filePreferences); |
| 36 | + } |
| 37 | + |
| 38 | + public CopyLinkedFilesAction(Collection<LinkedFile> linkedFiles, |
| 39 | + DialogService dialogService, |
| 40 | + BibDatabaseContext databaseContext, |
| 41 | + FilePreferences filePreferences) { |
| 42 | + this.linkedFiles = new ArrayList<>(linkedFiles); |
| 43 | + this.dialogService = dialogService; |
| 44 | + this.databaseContext = databaseContext; |
| 45 | + this.filePreferences = filePreferences; |
| 46 | + |
| 47 | + this.executable.bind(Bindings.createBooleanBinding( |
| 48 | + () -> this.linkedFiles.stream().allMatch(this::isLocalExisting), |
| 49 | + dependencies(this.linkedFiles))); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void execute() { |
| 54 | + DirectoryDialogConfiguration dirDialogConfiguration = new DirectoryDialogConfiguration.Builder() |
| 55 | + .withInitialDirectory(filePreferences.getWorkingDirectory()) |
| 56 | + .build(); |
| 57 | + |
| 58 | + Optional<Path> exportDir = dialogService.showDirectorySelectionDialog(dirDialogConfiguration); |
| 59 | + if (exportDir.isEmpty()) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + int copiedFiles = 0; |
| 64 | + int failedCount = 0; |
| 65 | + |
| 66 | + for (LinkedFile file : linkedFiles) { |
| 67 | + Optional<Path> srcOpt = file.findIn(databaseContext, filePreferences); |
| 68 | + if (srcOpt.isEmpty()) { |
| 69 | + failedCount++; |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + Path src = srcOpt.get(); |
| 74 | + Path dst = resolvePathFilename.apply(exportDir.get(), src); |
| 75 | + |
| 76 | + if (FileUtil.copyFile(src, dst, false)) { |
| 77 | + copiedFiles++; |
| 78 | + } else { |
| 79 | + failedCount++; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + String target = exportDir.map(Path::toString).orElse(""); |
| 84 | + |
| 85 | + if (linkedFiles.size() == 1) { |
| 86 | + if (copiedFiles == 1) { |
| 87 | + dialogService.notify(Localization.lang("Successfully copied %0 file(s) to %1.", copiedFiles, target)); |
| 88 | + } else { |
| 89 | + dialogService.notify(Localization.lang("Could not copy file to %0, maybe the file is already existing?", target)); |
| 90 | + } |
| 91 | + } else { |
| 92 | + if (failedCount == 0) { |
| 93 | + dialogService.notify(Localization.lang("Successfully copied %0 file(s) to %1.", copiedFiles, target)); |
| 94 | + } else { |
| 95 | + dialogService.notify(Localization.lang("Copied %0 file(s). Failed: %1", copiedFiles, failedCount)); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private boolean isLocalExisting(LinkedFile lf) { |
| 101 | + return !lf.isOnlineLink() && lf.findIn(databaseContext, filePreferences).isPresent(); |
| 102 | + } |
| 103 | + |
| 104 | + private static Observable[] dependencies(Collection<LinkedFile> files) { |
| 105 | + return files.stream().map(LinkedFile::linkProperty).toArray(Observable[]::new); |
| 106 | + } |
| 107 | +} |
0 commit comments