Skip to content

Commit

Permalink
Have relative paths when creating BibTeX from a PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Aug 4, 2024
1 parent f592819 commit 17112f1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

- We fixed an issue where the "Check for updates" preference was not saved. [#11485](https://github.com/JabRef/jabref/pull/11485)
- We fixed an issue where an exception was thrown after changing "show preview as a tab" in the preferences. [#11515](https://github.com/JabRef/jabref/pull/11515)
- We fixed an issue where JabRef put file paths as absolute path when an entry was created using drag and drop of a PDF file. [#11173](https://github.com/JabRef/jabref/issues/11173)
- We fixed an issue that online and offline mode for new library creation were handled incorrectly. [#11565](https://github.com/JabRef/jabref/pull/11565)
- We fixed an issue with colors in the search bar when dark theme is enabled. [#11569](https://github.com/JabRef/jabref/issues/11569)
- We fixed an issue where a new unsaved library was not marked with an asterisk. [#11519](https://github.com/JabRef/jabref/pull/11519)
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/gui/externalfiles/ImportHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.model.util.OptionalUtil;
import org.jabref.preferences.FilePreferences;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
Expand Down Expand Up @@ -99,7 +100,7 @@ public ExternalFilesEntryLinker getLinker() {
return linker;
}

public BackgroundTask<List<ImportFilesResultItemViewModel>> importFilesInBackground(final List<Path> files) {
public BackgroundTask<List<ImportFilesResultItemViewModel>> importFilesInBackground(final List<Path> files, final BibDatabaseContext bibDatabaseContext, final FilePreferences filePreferences) {
return new BackgroundTask<>() {
private int counter;
private final List<ImportFilesResultItemViewModel> results = new ArrayList<>();
Expand Down Expand Up @@ -130,7 +131,7 @@ protected List<ImportFilesResultItemViewModel> call() {
}

if (!pdfEntriesInFile.isEmpty()) {
entriesToAdd.addAll(pdfEntriesInFile);
entriesToAdd.addAll(FileUtil.relativize(pdfEntriesInFile, bibDatabaseContext, filePreferences));
addResultToList(file, true, Localization.lang("File was successfully imported as a new entry"));
} else {
entriesToAdd.add(createEmptyEntryWithLink(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void startImport() {
}
resultList.clear();

importFilesBackgroundTask = importHandler.importFilesInBackground(fileList)
importFilesBackgroundTask = importHandler.importFilesInBackground(fileList, bibDatabase, preferences.getFilePreferences())
.onRunning(() -> {
progressValueProperty.bind(importFilesBackgroundTask.workDonePercentageProperty());
progressTextProperty.bind(importFilesBackgroundTask.messageProperty());
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.BibtexString;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.FilePreferences;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
Expand Down Expand Up @@ -84,6 +85,7 @@ public class MainTable extends TableView<BibEntryTableViewModel> {
private final BibEntryTypesManager entryTypesManager;
private final TaskExecutor taskExecutor;
private final UndoManager undoManager;
private final FilePreferences filePreferences;
private long lastKeyPressTime;
private String columnSearchTerm;

Expand All @@ -110,6 +112,8 @@ public MainTable(MainTableDataModel model,
this.entryTypesManager = entryTypesManager;
this.taskExecutor = taskExecutor;
this.undoManager = libraryTab.getUndoManager();
this.filePreferences = preferencesService.getFilePreferences();

MainTablePreferences mainTablePreferences = preferencesService.getMainTablePreferences();

importHandler = new ImportHandler(
Expand Down Expand Up @@ -492,8 +496,7 @@ private void handleOnDragDropped(TableRow<BibEntryTableViewModel> row, BibEntryT
// Center -> link files to entry
// Depending on the pressed modifier, move/copy/link files to drop target
switch (ControlHelper.getDroppingMouseLocation(row, event)) {
case TOP, BOTTOM ->
importHandler.importFilesInBackground(files).executeWith(taskExecutor);
case TOP, BOTTOM -> importHandler.importFilesInBackground(files, database, filePreferences).executeWith(taskExecutor);
case CENTER -> {
BibEntry entry = target.getEntry();
switch (event.getTransferMode()) {
Expand Down Expand Up @@ -524,9 +527,8 @@ private void handleOnDragDroppedTableView(DragEvent event) {
boolean success = false;

if (event.getDragboard().hasFiles()) {
List<Path> files = event.getDragboard().getFiles().stream().map(File::toPath).collect(Collectors.toList());
importHandler.importFilesInBackground(files).executeWith(taskExecutor);

List<Path> files = event.getDragboard().getFiles().stream().map(File::toPath).toList();
importHandler.importFilesInBackground(files, this.database, filePreferences).executeWith(taskExecutor);
success = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public class ImportFormatReader {

public ImportFormatReader(ImporterPreferences importerPreferences,
ImportFormatPreferences importFormatPreferences,
CitationKeyPatternPreferences citationKeyPatternPreferences, FileUpdateMonitor fileUpdateMonitor) {
CitationKeyPatternPreferences citationKeyPatternPreferences,
FileUpdateMonitor fileUpdateMonitor) {
this.importerPreferences = importerPreferences;
this.importFormatPreferences = importFormatPreferences;
this.fileUpdateMonitor = fileUpdateMonitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jabref.logic.importer.util.FileFieldParser;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.StandardFileType;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
Expand Down Expand Up @@ -130,6 +131,8 @@ public ParserResult importDatabase(Path filePath) throws IOException {
}
}

// We use the absolute path here as we do not know the context where this import will be used.
// The caller is responsible for making the path relative if necessary.
entry.addFile(new LinkedFile("", filePath, StandardFileType.PDF.getName()));
return new ParserResult(List.of(entry));
}
Expand Down Expand Up @@ -169,7 +172,7 @@ public List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
try {
ParserResult result = importDatabase(filePath.get());
if (!result.isEmpty()) {
return result.getDatabase().getEntries();
return FileUtil.relativize(result.getDatabase().getEntries(), databaseContext, filePreferences);
}
} catch (IOException e) {
LOGGER.error("Cannot read {}", filePath.get(), e);
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.field.StandardField;
import org.jabref.preferences.FilePreferences;

import org.slf4j.Logger;
Expand Down Expand Up @@ -268,6 +270,30 @@ public static Path relativize(Path path, BibDatabaseContext databaseContext, Fil
return relativize(path, fileDirectories);
}

/**
* Relativizes all BibEntries given to (!) the given database context
* <p>
* ⚠ Modifies the entries in the list ⚠
*/
public static List<BibEntry> relativize(List<BibEntry> entries, BibDatabaseContext databaseContext, FilePreferences filePreferences) {
List<Path> fileDirectories = databaseContext.getFileDirectories(filePreferences);

return entries.stream()
.map(entry -> {
if (entry.hasField(StandardField.FILE)) {
List<LinkedFile> updatedLinkedFiles = entry.getFiles().stream().map(linkedFile -> {
if (!linkedFile.isOnlineLink()) {
String newPath = FileUtil.relativize(Path.of(linkedFile.getLink()), fileDirectories).toString();
linkedFile.setLink(newPath);
}
return linkedFile;
}).toList();
entry.setFiles(updatedLinkedFiles);
}
return entry;
}).toList();
}

/**
* Returns the list of linked files. The files have the absolute filename
*
Expand Down

0 comments on commit 17112f1

Please sign in to comment.