Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed default value of "search and store files relative to bibtex file" to true #6928

Merged
merged 6 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We completed the rebranding of `bibtexkey` as `citationkey` which was started in JabRef 5.1.
- JabRef no longer opens the entry editor with the first entry on startup [#6855](https://github.com/JabRef/jabref/issues/6855)
- Fetch by ID: (long) "SAO/NASA Astrophysics Data System" replaced by (short) "SAO/NASA ADS" [#6876](https://github.com/JabRef/jabref/pull/6876)
- We changed the default preference option "Search and store files relative to library file location" to on, as this seems to be a more intuitive behaviour. [#6863](https://github.com/JabRef/jabref/issues/6863)
- We changed the title of the window "Manage field names and content": to have the same title as the corresponding menu item [#6895](https://github.com/JabRef/jabref/pull/6895)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void openDatabases() {
new SharedDatabaseUIManager(mainFrame).openSharedDatabaseFromParserResult(pr);
} catch (SQLException | DatabaseNotSupportedException | InvalidDBMSConnectionPropertiesException |
NotASharedDatabaseException e) {
pr.getDatabaseContext().clearDatabaseFile(); // do not open the original file
pr.getDatabaseContext().clearDatabasePath(); // do not open the original file
pr.getDatabase().clearSharedDatabaseID();

LOGGER.error("Connection error", e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/SendAsEMailAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private String sendEmail() throws Exception {
// the unofficial "mailto:attachment" property
boolean openFolders = JabRefPreferences.getInstance().getBoolean(JabRefPreferences.OPEN_FOLDERS_OF_ATTACHED_FILES);

List<Path> fileList = FileUtil.getListOfLinkedFiles(entries, databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()));
List<Path> fileList = FileUtil.getListOfLinkedFiles(entries, databaseContext.getFileDirectories(Globals.prefs.getFilePreferences()));
for (Path path : fileList) {
attachments.add(path.toAbsolutePath().toString());
if (openFolders) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/desktop/JabRefDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void openExternalViewer(BibDatabaseContext databaseContext, String
Field field = initialField;
if (StandardField.PS.equals(field) || StandardField.PDF.equals(field)) {
// Find the default directory for this field type:
List<Path> directories = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences());
List<Path> directories = databaseContext.getFileDirectories(Globals.prefs.getFilePreferences());

Optional<Path> file = FileHelper.find(link, directories);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ private ExportResult exportToClipboard(Exporter exporter) throws Exception {
// Set the global variable for this database's file directory before exporting,
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()).stream().map(Path::toString).collect(Collectors.toList());
Globals.prefs.fileDirForDatabase = panel.getBibDatabaseContext()
.getFileDirectories(Globals.prefs.getFilePreferences());

// Add chosen export type to last used pref, to become default
Globals.prefs.put(JabRefPreferences.LAST_USED_EXPORT, exporter.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AutoSetFileLinksUtil {
private ExternalFileTypes externalFileTypes;

public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) {
this(databaseContext.getFileDirectoriesAsPaths(filePreferences), autoLinkPreferences, externalFileTypes);
this(databaseContext.getFileDirectories(filePreferences), autoLinkPreferences, externalFileTypes);
}

private AutoSetFileLinksUtil(List<Path> directories, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void addLinkedFileFromURL(BibDatabaseContext databaseContext, URL url, B
downloadTask.onSuccess(destination -> {
LinkedFile downloadedFile = LinkedFilesEditorViewModel.fromFile(
destination,
databaseContext.getFileDirectoriesAsPaths(preferences.getFilePreferences()),
databaseContext.getFileDirectories(preferences.getFilePreferences()),
ExternalFileTypes.getInstance());
entry.addFile(downloadedFile);
dialogService.notify(Localization.lang("Finished downloading full text document for entry %0.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void addFilesToEntry(BibEntry entry, List<Path> files) {
FileUtil.getFileExtension(file).ifPresent(ext -> {
ExternalFileType type = externalFileTypes.getExternalFileTypeByExt(ext)
.orElse(new UnknownExternalFileType(ext));
Path relativePath = FileUtil.relativize(file, bibDatabaseContext.getFileDirectoriesAsPaths(filePreferences));
LinkedFile linkedfile = new LinkedFile("", relativePath.toString(), type.getName());
Path relativePath = FileUtil.relativize(file, bibDatabaseContext.getFileDirectories(filePreferences));
LinkedFile linkedfile = new LinkedFile("", relativePath, type.getName());
entry.addFile(linkedfile);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public void download() {
URLDownload urlDownload = new URLDownload(linkedFile.getLink());
BackgroundTask<Path> downloadTask = prepareDownloadTask(targetDirectory.get(), urlDownload);
downloadTask.onSuccess(destination -> {
LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, databaseContext.getFileDirectoriesAsPaths(filePreferences), externalFileTypes);
LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, databaseContext.getFileDirectories(filePreferences), externalFileTypes);

List<LinkedFile> linkedFiles = entry.getFiles();
int oldFileIndex = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public class LinkedFilesEditor extends HBox implements FieldEditorFX {
private final BibDatabaseContext databaseContext;
private final UiThreadObservableList<LinkedFileViewModel> decoratedModelList;

public LinkedFilesEditor(Field field, DialogService dialogService, BibDatabaseContext databaseContext, TaskExecutor taskExecutor, SuggestionProvider<?> suggestionProvider,
public LinkedFilesEditor(Field field,
DialogService dialogService,
BibDatabaseContext databaseContext,
TaskExecutor taskExecutor,
SuggestionProvider<?> suggestionProvider,
FieldCheckers fieldCheckers,
JabRefPreferences preferences) {
this.viewModel = new LinkedFilesEditorViewModel(field, suggestionProvider, dialogService, databaseContext, taskExecutor, fieldCheckers, preferences);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public static LinkedFile fromFile(Path file, List<Path> fileDirectories, Externa
.getExternalFileTypeByExt(fileExtension)
.orElse(new UnknownExternalFileType(fileExtension));
Path relativePath = FileUtil.relativize(file, fileDirectories);
return new LinkedFile("", relativePath.toString(), suggestedFileType.getName());
return new LinkedFile("", relativePath, suggestedFileType.getName());
}

public LinkedFileViewModel fromFile(Path file) {
List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(preferences.getFilePreferences());
List<Path> fileDirectories = databaseContext.getFileDirectories(preferences.getFilePreferences());

LinkedFile linkedFile = fromFile(file, fileDirectories, externalFileTypes);
return new LinkedFileViewModel(
Expand Down Expand Up @@ -150,7 +150,7 @@ public void addNewFile() {
.withInitialDirectory(workingDirectory)
.build();

List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(preferences.getFilePreferences());
List<Path> fileDirectories = databaseContext.getFileDirectories(preferences.getFilePreferences());
dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> {
LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, externalFileTypes);
files.add(new LinkedFileViewModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void execute() {

dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> {
LinkedFile linkedFile = LinkedFilesEditorViewModel.fromFile(newFile,
databaseContext.getFileDirectoriesAsPaths(preferencesService.getFilePreferences()),
databaseContext.getFileDirectories(preferencesService.getFilePreferences()),
ExternalFileTypes.getInstance());

LinkedFileEditDialogView dialog = new LinkedFileEditDialogView(linkedFile);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/filelist/FileListEntry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.filelist;

import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;

Expand Down Expand Up @@ -48,7 +49,7 @@ public String toString() {
}

public LinkedFile toParsedFileField() {
return new LinkedFile(getDescription(), getLink(), getType().isPresent() ? getType().get().getName() : "");
return new LinkedFile(getDescription(), Path.of(getLink()), getType().isPresent() ? getType().get().getName() : "");
calixtus marked this conversation as resolved.
Show resolved Hide resolved
}

public String getDescription() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ public ObjectProperty<ExternalFileType> selectedExternalFileTypeProperty() {
}

public LinkedFile getNewLinkedFile() {
return new LinkedFile(description.getValue(), link.getValue(), monadicSelectedExternalFileType.getValue().map(ExternalFileType::toString).orElse(""));
return new LinkedFile(description.getValue(), Path.of(link.getValue()), monadicSelectedExternalFileType.getValue().map(ExternalFileType::toString).orElse(""));
}

private String relativize(Path filePath) {
List<Path> fileDirectories = database.getFileDirectoriesAsPaths(preferences.getFilePreferences());
List<Path> fileDirectories = database.getFileDirectories(preferences.getFilePreferences());
return FileUtil.relativize(filePath, fileDirectories).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private ParserResult loadDatabase(Path file) throws Exception {
new SharedDatabaseUIManager(frame).openSharedDatabaseFromParserResult(result);
} catch (SQLException | DatabaseNotSupportedException | InvalidDBMSConnectionPropertiesException |
NotASharedDatabaseException e) {
result.getDatabaseContext().clearDatabaseFile(); // do not open the original file
result.getDatabaseContext().clearDatabasePath(); // do not open the original file
result.getDatabase().clearSharedDatabaseID();
LOGGER.error("Connection error", e);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public LinkedFilesTabViewModel(DialogService dialogService, PreferencesService p
public void setValues() {
// External files preferences / Attached files preferences / File preferences
mainFileDirectoryProperty.setValue(initialFilePreferences.getFileDirectory().orElse(Path.of("")).toString());
useBibLocationAsPrimaryProperty.setValue(initialFilePreferences.isBibLocationAsPrimary());
useBibLocationAsPrimaryProperty.setValue(initialFilePreferences.shouldStoreFilesRelativeToBib());
searchFilesOnOpenProperty.setValue(initialFilePreferences.shouldSearchFilesOnOpen());
openBrowseOnCreateProperty.setValue(initialFilePreferences.shouldOpenBrowseOnCreate());
fileNamePatternProperty.setValue(initialFilePreferences.getFileNamePattern());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public List<FieldChange> cleanup(BibEntry entry) {
} else {
// only try to transform local file path to relative one
newFileName = FileUtil
.relativize(Path.of(oldFileName), databaseContext.getFileDirectoriesAsPaths(filePreferences))
.relativize(Path.of(oldFileName), databaseContext.getFileDirectories(filePreferences))
.toString();
}
LinkedFile newFileEntry = fileEntry;
if (!oldFileName.equals(newFileName)) {
newFileEntry = new LinkedFile(fileEntry.getDescription(), newFileName, fileEntry.getFileType());
newFileEntry = new LinkedFile(fileEntry.getDescription(), Path.of(newFileName), fileEntry.getFileType());
changed = true;
}
newFileList.add(newFileEntry);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jabref.logic.cleanup;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -36,16 +36,16 @@ public List<FieldChange> cleanup(BibEntry entry) {
List<LinkedFile> fileList = new ArrayList<>(entry.getFiles());
int oldItemCount = fileList.size();
for (Map.Entry<Field, String> field : fields.entrySet()) {
entry.getField(field.getKey()).ifPresent(o -> {
if (o.trim().isEmpty()) {
entry.getField(field.getKey()).ifPresent(fieldContent -> {
if (fieldContent.trim().isEmpty()) {
return;
}
File f = new File(o);
LinkedFile flEntry = new LinkedFile(f.getName(), o, field.getValue());
Path path = Path.of(fieldContent);
LinkedFile flEntry = new LinkedFile(path.getFileName().toString(), path, field.getValue());
fileList.add(flEntry);

entry.clearField(field.getKey());
changes.add(new FieldChange(entry, field.getKey(), o, null));
changes.add(new FieldChange(entry, field.getKey(), fieldContent, null));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public boolean renameToName(String targetFileName, boolean overwriteExistingFile
}

private String relativize(Path path) {
List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(filePreferences);
List<Path> fileDirectories = databaseContext.getFileDirectories(filePreferences);
return FileUtil.relativize(path, fileDirectories).toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -118,7 +119,7 @@ public static BibEntry parseSpringerJSONtoBibtex(JSONObject springerJsonEntry) {
urls.forEach(data -> {
JSONObject url = (JSONObject) data;
if (url.optString("format").equalsIgnoreCase("pdf")) {
entry.addFile(new LinkedFile("online", url.optString("value"), "PDF"));
entry.addFile(new LinkedFile("online", Path.of(url.optString("value")), "PDF"));
calixtus marked this conversation as resolved.
Show resolved Hide resolved
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
Expand All @@ -21,7 +23,6 @@

import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.Importer;
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.Parser;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.fileformat.endnote.Abstract;
Expand Down Expand Up @@ -158,23 +159,14 @@ private void initUnmarshaller() throws JAXBException {
}

private static EntryType convertRefNameToType(String refName) {
switch (refName.toLowerCase().trim()) {
case "artwork":
return StandardEntryType.Misc;
case "generic":
return StandardEntryType.Misc;
case "electronic article":
return IEEETranEntryType.Electronic;
case "book section":
return StandardEntryType.InBook;
case "book":
return StandardEntryType.Book;
case "journal article":
return StandardEntryType.Article;

default:
return StandardEntryType.Article;
}
return switch (refName.toLowerCase().trim()) {
case "artwork", "generic" -> StandardEntryType.Misc;
case "electronic article" -> IEEETranEntryType.Electronic;
case "book section" -> StandardEntryType.InBook;
case "book" -> StandardEntryType.Book;
// case "journal article" -> StandardEntryType.Article;
default -> StandardEntryType.Article;
};
}

private BibEntry parseRecord(Record record) {
Expand Down Expand Up @@ -247,7 +239,14 @@ private List<LinkedFile> getLinkedFiles(Record record) {
return OptionalUtil.toStream(urls)
.flatMap(pdfUrls -> pdfUrls.getUrl().stream())
.flatMap(url -> OptionalUtil.toStream(getUrlValue(url)))
.map(url -> new LinkedFile("", url, "PDF"))
.map(url -> {
try {
return new LinkedFile(new URL(url), "PDF");
} catch (MalformedURLException e) {
LOGGER.info("Unable to parse {}", url);
return null;
}
})
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -295,7 +294,7 @@ private String clean(String input) {
}

@Override
public List<BibEntry> parseEntries(InputStream inputStream) throws ParseException {
public List<BibEntry> parseEntries(InputStream inputStream) {
try {
return importDatabase(
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))).getDatabase().getEntries();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public ParserResult importDatabase(Path filePath, Charset defaultEncoding) {
ParserResult parserResult = new ParserResult(result);
Optional<BibEntry> entry = new DoiFetcher(importFormatPreferences).performSearchById(doi.get().getDOI());
entry.ifPresent(parserResult.getDatabase()::insertEntry);
entry.ifPresent(bibEntry -> bibEntry.addFile(new LinkedFile("", filePath.toAbsolutePath().toString(), "PDF")));
entry.ifPresent(bibEntry -> bibEntry.addFile(new LinkedFile("", filePath.toAbsolutePath(), "PDF")));
return parserResult;
}

Expand All @@ -226,7 +226,7 @@ public ParserResult importDatabase(Path filePath, Charset defaultEncoding) {
return ParserResult.fromErrorMessage(e.getMessage());
}

result.forEach(entry -> entry.addFile(new LinkedFile("", filePath.toAbsolutePath().toString(), "PDF")));
result.forEach(entry -> entry.addFile(new LinkedFile("", filePath.toAbsolutePath(), "PDF")));
return new ParserResult(result);
}

Expand Down
Loading