Skip to content

Commit

Permalink
Added a download checkbox to the import dialog (JabRef#6381)
Browse files Browse the repository at this point in the history
* Added a download checkbox to the import dialog

When importing entries, users now have a checkbox available to download
files linked via url.

* Added languages

* removed translations

* Fixed style problems

* Added preference in import tab

* Fixed style

* Removed singleton-access to preferences

* Downloaded file is inserted before link

* Fix style

* Consider FileDirPattern when downloading fulltext

* Added changes

Co-authored-by: Benedikt Tutzer <benedikt.tutzer@gmail.com>
  • Loading branch information
btut and btut committed May 4, 2020
1 parent 0e695d6 commit 7cb7ea6
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added support for basic markdown in custom formatted previews [#6194](https://github.com/JabRef/jabref/issues/6194)
- We now show the number of items found and selected to import in the online search dialog. [#6248](https://github.com/JabRef/jabref/pull/6248)
- We created a new install screen for macOS. [#5759](https://github.com/JabRef/jabref/issues/5759)
- We implemented an option to download fulltext files while importing. [#6381](https://github.com/JabRef/jabref/pull/6381)

### Changed

Expand All @@ -26,6 +27,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We changed the buttons for import/export/show all/reset of preferences to smaller icon buttons in the preferences dialog. [#6130](https://github.com/JabRef/jabref/pull/6130)
- We moved the functionality "Manage field names & content" from the "Library" menu to the "Edit" menu, because it affects the selected entries and not the whole library
- We merged the functionality "Append contents from a BibTeX library into the currently viewed library" into the "Import into database" functionality. Fixes [#6049](https://github.com/JabRef/jabref/issues/6049).
- We changed the directory where fulltext downloads are stored to the directory set in the import-tab in preferences. [#6381](https://github.com/JabRef/jabref/pull/6381)
- We improved the error message for invalid jstyles. [#6303](https://github.com/JabRef/jabref/issues/6303)

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.io.FileNameUniqueness;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.logic.xmp.XmpPreferences;
import org.jabref.logic.xmp.XmpUtilWriter;
import org.jabref.model.database.BibDatabaseContext;
Expand Down Expand Up @@ -415,6 +416,7 @@ public void download() {
LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, databaseContext.getFileDirectoriesAsPaths(filePreferences), externalFileTypes);
linkedFile.setLink(newLinkedFile.getLink());
linkedFile.setFileType(newLinkedFile.getFileType());
entry.addFile(0, newLinkedFile);
});
downloadProgress.bind(downloadTask.workDonePercentageProperty());
taskExecutor.execute(downloadTask);
Expand All @@ -431,8 +433,9 @@ public BackgroundTask<Path> prepareDownloadTask(Path targetDirectory, URLDownloa
String suggestedTypeName = externalFileType.getName();
linkedFile.setFileType(suggestedTypeName);
String suggestedName = linkedFileHandler.getSuggestedFileName(externalFileType.getExtension());
suggestedName = FileNameUniqueness.getNonOverWritingFileName(targetDirectory, suggestedName);
return targetDirectory.resolve(suggestedName);
String fulltextDir = FileUtil.createDirNameFromPattern(databaseContext.getDatabase(), entry, filePreferences.getFileDirPattern());
suggestedName = FileNameUniqueness.getNonOverWritingFileName(targetDirectory.resolve(fulltextDir), suggestedName);
return targetDirectory.resolve(fulltextDir).resolve(suggestedName);
})
.then(destination -> new FileDownloadTask(urlDownload.getSource(), destination))
.onFailure(exception -> dialogService.showErrorDialogAndWait("Download failed", exception));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
Expand Down Expand Up @@ -38,6 +39,7 @@
<Label fx:id="selectedItems" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
</GridPane>
</HBox>
<CheckBox fx:id="downloadLinkedOnlineFiles" text="%Download linked online files"/>
</VBox>
</content>
<ButtonType fx:id="importButton" buttonData="OK_DONE" text="%Import entries"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Tooltip;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class ImportEntriesDialog extends BaseDialog<Boolean> {
public ButtonType importButton;
public Label totalItems;
public Label selectedItems;
public CheckBox downloadLinkedOnlineFiles;
private final BackgroundTask<ParserResult> task;
private ImportEntriesViewModel viewModel;
@Inject private TaskExecutor taskExecutor;
Expand Down Expand Up @@ -77,9 +79,11 @@ public ImportEntriesDialog(BibDatabaseContext database, BackgroundTask<ParserRes
Button btn = (Button) this.getDialogPane().lookupButton(importButton);
btn.disableProperty().bind(booleanBind);

downloadLinkedOnlineFiles.setSelected(preferences.getFilePreferences().getDownloadLinkedFiles());

setResultConverter(button -> {
if (button == importButton) {
viewModel.importEntries(entriesListView.getCheckModel().getCheckedItems());
viewModel.importEntries(entriesListView.getCheckModel().getCheckedItems(), downloadLinkedOnlineFiles.isSelected());
} else {
dialogService.notify(Localization.lang("Import canceled"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jabref.gui.duplicationFinder.DuplicateResolverDialog;
import org.jabref.gui.externalfiles.ImportHandler;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.gui.groups.GroupTreeNodeViewModel;
import org.jabref.gui.groups.UndoableAddOrRemoveGroup;
import org.jabref.gui.undo.NamedCompound;
Expand All @@ -31,6 +32,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexString;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.metadata.MetaData;
import org.jabref.model.util.FileUpdateMonitor;
Expand All @@ -44,6 +46,7 @@ public class ImportEntriesViewModel extends AbstractViewModel {
private static final Logger LOGGER = LoggerFactory.getLogger(ImportEntriesViewModel.class);

private final StringProperty message;
private final TaskExecutor taskExecutor;
private final BibDatabaseContext databaseContext;
private final DialogService dialogService;
private final UndoManager undoManager;
Expand All @@ -58,6 +61,7 @@ public class ImportEntriesViewModel extends AbstractViewModel {
* @param task the task executed for parsing the selected files(s).
*/
public ImportEntriesViewModel(BackgroundTask<ParserResult> task, TaskExecutor taskExecutor, BibDatabaseContext databaseContext, DialogService dialogService, UndoManager undoManager, PreferencesService preferences, StateManager stateManager, FileUpdateMonitor fileUpdateMonitor) {
this.taskExecutor = taskExecutor;
this.databaseContext = databaseContext;
this.dialogService = dialogService;
this.undoManager = undoManager;
Expand Down Expand Up @@ -99,7 +103,7 @@ public boolean hasDuplicate(BibEntry entry) {
*
* @param entriesToImport subset of the entries contained in parserResult
*/
public void importEntries(List<BibEntry> entriesToImport) {
public void importEntries(List<BibEntry> entriesToImport, boolean downloadFiles) {
// Check if we are supposed to warn about duplicates.
// If so, then see if there are duplicates, and warn if yes.
if (preferences.shouldWarnAboutDuplicatesForImport()) {
Expand All @@ -126,6 +130,15 @@ public void importEntries(List<BibEntry> entriesToImport) {
buildImportHandlerThenImportEntries(entriesToImport);
}

if (downloadFiles) {
for (BibEntry bibEntry : entriesToImport) {
for (LinkedFile linkedFile : bibEntry.getFiles()) {
LinkedFileViewModel linkedFileViewModel = new LinkedFileViewModel(linkedFile, bibEntry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), ExternalFileTypes.getInstance());
linkedFileViewModel.download();
}
}
}

NamedCompound namedCompound = new NamedCompound(Localization.lang("Import file"));
namedCompound.addEdit(new UndoableInsertEntries(databaseContext.getDatabase(), entriesToImport));

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/gui/preferences/ImportTab.fxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
Expand Down Expand Up @@ -30,5 +31,6 @@
<Label text="%File directory pattern" GridPane.rowIndex="1"/>
<TextField fx:id="fileDirPattern" GridPane.columnIndex="1" GridPane.rowIndex="1"
prefWidth="300" minWidth="300" maxWidth="300"/>
<CheckBox fx:id="downloadLinkedFiles" text="%Download linked online files" GridPane.rowIndex="2"/>
</GridPane>
</fx:root>
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/preferences/ImportTabView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.preferences;

import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;

Expand All @@ -13,6 +14,7 @@ public class ImportTabView extends AbstractPreferenceTabView<ImportTabViewModel>

@FXML private ComboBox<String> fileNamePattern;
@FXML private TextField fileDirPattern;
@FXML private CheckBox downloadLinkedFiles;

public ImportTabView(JabRefPreferences preferences) {
this.preferences = preferences;
Expand All @@ -28,6 +30,7 @@ public void initialize() {
fileNamePattern.valueProperty().bindBidirectional(viewModel.fileNamePatternProperty());
fileNamePattern.itemsProperty().bind(viewModel.defaultFileNamePatternsProperty());
fileDirPattern.textProperty().bindBidirectional(viewModel.fileDirPatternProperty());
downloadLinkedFiles.selectedProperty().bindBidirectional(viewModel.downloadLinkedFilesProperty());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.util.ArrayList;
import java.util.List;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
Expand All @@ -19,6 +21,7 @@ public class ImportTabViewModel implements PreferenceTabViewModel {
private final ListProperty<String> defaultFileNamePatternsProperty = new SimpleListProperty<>(FXCollections.observableArrayList(DEFAULT_FILENAME_PATTERNS));
private final StringProperty fileNamePatternProperty = new SimpleStringProperty();
private final StringProperty fileDirPatternProperty = new SimpleStringProperty();
private final BooleanProperty downloadLinkedFilesProperty = new SimpleBooleanProperty();

private final DialogService dialogService;
private final JabRefPreferences preferences;
Expand All @@ -32,12 +35,14 @@ public ImportTabViewModel(DialogService dialogService, JabRefPreferences prefere
public void setValues() {
fileNamePatternProperty.setValue(preferences.get(JabRefPreferences.IMPORT_FILENAMEPATTERN));
fileDirPatternProperty.setValue(preferences.get(JabRefPreferences.IMPORT_FILEDIRPATTERN));
downloadLinkedFilesProperty.setValue(preferences.getBoolean(JabRefPreferences.DOWNLOAD_LINKED_FILES));
}

@Override
public void storeSettings() {
preferences.put(JabRefPreferences.IMPORT_FILENAMEPATTERN, fileNamePatternProperty.getValue());
preferences.put(JabRefPreferences.IMPORT_FILEDIRPATTERN, fileDirPatternProperty.getValue());
preferences.putBoolean(JabRefPreferences.DOWNLOAD_LINKED_FILES, downloadLinkedFilesProperty.getValue());
}

@Override
Expand All @@ -55,4 +60,6 @@ public List<String> getRestartWarnings() {
public StringProperty fileNamePatternProperty() { return fileNamePatternProperty; }

public StringProperty fileDirPatternProperty() { return fileDirPatternProperty; }

public BooleanProperty downloadLinkedFilesProperty() { return downloadLinkedFilesProperty; }
}
6 changes: 6 additions & 0 deletions src/main/java/org/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,12 @@ public Optional<FieldChange> addFile(LinkedFile file) {
return setFiles(linkedFiles);
}

public Optional<FieldChange> addFile(int index, LinkedFile file) {
List<LinkedFile> linkedFiles = getFiles();
linkedFiles.add(index, file);
return setFiles(linkedFiles);
}

public ObservableMap<Field, String> getFieldsObservable() {
return fields;
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/jabref/model/metadata/FilePreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ public class FilePreferences {
private final boolean bibLocationAsPrimary;
private final String fileNamePattern;
private final String fileDirPattern;
private final boolean downloadLinkedFiles;

public FilePreferences(String user,
String mainFileDirectory,
boolean bibLocationAsPrimary,
String fileNamePattern,
String fileDirPattern) {
String fileDirPattern,
boolean downloadLinkedFiles) {
this.user = user;
this.mainFileDirectory = mainFileDirectory;
this.bibLocationAsPrimary = bibLocationAsPrimary;
this.fileNamePattern = fileNamePattern;
this.fileDirPattern = fileDirPattern;
this.downloadLinkedFiles = downloadLinkedFiles;
}

public String getUser() {
Expand All @@ -48,4 +51,6 @@ public String getFileNamePattern() {
public String getFileDirPattern() {
return fileDirPattern;
}

public boolean getDownloadLinkedFiles() { return downloadLinkedFiles; }
}
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public class JabRefPreferences implements PreferencesService {
public static final String CLEANUP_FORMATTERS = "CleanUpFormatters";
public static final String IMPORT_FILENAMEPATTERN = "importFileNamePattern";
public static final String IMPORT_FILEDIRPATTERN = "importFileDirPattern";
public static final String DOWNLOAD_LINKED_FILES = "downloadLinkedFiles";
public static final String NAME_FORMATTER_VALUE = "nameFormatterFormats";
public static final String NAME_FORMATER_KEY = "nameFormatterNames";
public static final String PUSH_TO_APPLICATION = "pushToApplication";
Expand Down Expand Up @@ -629,6 +630,8 @@ private JabRefPreferences() {
defaults.put(IMPORT_FILENAMEPATTERN, ImportTabViewModel.DEFAULT_FILENAME_PATTERNS[1]);
// Default empty String to be backwards compatible
defaults.put(IMPORT_FILEDIRPATTERN, "");
// Don't download files by default
defaults.put(DOWNLOAD_LINKED_FILES, false);

customImports = new CustomImportList(this);

Expand Down Expand Up @@ -1201,7 +1204,8 @@ public FilePreferences getFilePreferences() {
get(MAIN_FILE_DIRECTORY),
getBoolean(BIB_LOC_AS_PRIMARY_DIR),
get(IMPORT_FILENAMEPATTERN),
get(IMPORT_FILEDIRPATTERN));
get(IMPORT_FILEDIRPATTERN),
getBoolean(DOWNLOAD_LINKED_FILES));
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,7 @@ Import\ canceled=Import canceled
Select\ all\ new\ entries=Select all new entries
Total\ items\ found\:=Total items found:
Selected\ items\:=Selected items:
Download\ linked\ online\ files=Download linked online files
Select\ the\ entries\ to\ be\ imported\:=Select the entries to be imported\:
Add\ new\ String=Add new String
Remove\ selected\ Strings=Remove selected Strings
Expand Down

0 comments on commit 7cb7ea6

Please sign in to comment.