Skip to content

Commit

Permalink
Issue 9646: Right-click context menu "Attach file from URL" (#9648)
Browse files Browse the repository at this point in the history
* implement "Attach feile from URL" in RightCLickMenu

* add changelog

* fix checkstyle violations

* fix some checkstyles

* add l10n

* checkstyle

* extract duplicate to new class

* Refactored to remove utility class

---------

Co-authored-by: Siedlerchr <siedlerkiller@gmail.com>
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 6, 2023
1 parent ee93bfb commit 51b36c1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
### Added

- We added a field showing the BibTeX/biblatex source for added and deleted entries in the "External Changes Resolver" dialog. [#9509](https://github.com/JabRef/jabref/issues/9509)

- Add "Attach file from URL" to right-click context menu which downloads file from URL and stores it with reference library.



Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/actions/StandardActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum StandardActions implements Action {
SEARCH_SHORTSCIENCE(Localization.lang("Search ShortScience")),
MERGE_WITH_FETCHED_ENTRY(Localization.lang("Get bibliographic data from %0", "DOI/ISBN/...")),
ATTACH_FILE(Localization.lang("Attach file"), IconTheme.JabRefIcons.ATTACH_FILE),
ATTACH_FILE_FROM_URL(Localization.lang("Attach file from URL"), IconTheme.JabRefIcons.DOWNLOAD_FILE),
PRIORITY(Localization.lang("Priority"), IconTheme.JabRefIcons.PRIORITY),
CLEAR_PRIORITY(Localization.lang("Clear priority")),
PRIORITY_HIGH(Localization.lang("Set priority to high"), IconTheme.JabRefIcons.PRIORITY_HIGH),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.DialogService;
import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.gui.externalfiles.AutoSetFileLinksUtil;
import org.jabref.gui.externalfiletype.CustomExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.externalfiletype.UnknownExternalFileType;
import org.jabref.gui.linkedfile.AttachFileFromURLAction;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.FileDialogConfiguration;
Expand Down Expand Up @@ -214,11 +214,11 @@ public void fetchFulltext() {
preferences.getImportFormatPreferences(),
preferences.getImporterPreferences());
Optional<String> urlField = entry.getField(StandardField.URL);
Boolean download_success = false;
boolean download_success = false;
if (urlField.isPresent()) {
download_success = downloadFile(urlField.get());
}
if (!urlField.isPresent() || !download_success) {
if (urlField.isEmpty() || !download_success) {
BackgroundTask
.wrap(() -> fetcher.findFullTextPDF(entry))
.onRunning(() -> fulltextLookupInProgress.setValue(true))
Expand All @@ -235,22 +235,8 @@ public void fetchFulltext() {
}

public void addFromURL() {
String clipText = ClipBoardManager.getContents();
Optional<String> urlText;
String urlField = entry.getField(StandardField.URL).orElse("");
if (clipText.startsWith("http://") || clipText.startsWith("https://") || clipText.startsWith("ftp://")) {
urlText = dialogService.showInputDialogWithDefaultAndWait(
Localization.lang("Download file"), Localization.lang("Enter URL to download"), clipText);
} else if (urlField.startsWith("http://") || urlField.startsWith("https://") || urlField.startsWith("ftp://")) {
urlText = dialogService.showInputDialogWithDefaultAndWait(
Localization.lang("Download file"), Localization.lang("Enter URL to download"), urlField);
} else {
urlText = dialogService.showInputDialogAndWait(
Localization.lang("Download file"), Localization.lang("Enter URL to download"));
}
if (urlText.isPresent()) {
downloadFile(urlText.get());
}
AttachFileFromURLAction.getUrlForDownloadFromClipBoardOrEntry(dialogService, entry)
.ifPresent(this::downloadFile);
}

private void addFromURLAndDownload(URL url) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.jabref.gui.linkedfile;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
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.PreferencesService;

public class AttachFileFromURLAction extends SimpleCommand {

private final StateManager stateManager;
private final DialogService dialogService;
private final PreferencesService preferencesService;
private final TaskExecutor taskExecutor;

public AttachFileFromURLAction(DialogService dialogService,
StateManager stateManager,
TaskExecutor taskExecutor,
PreferencesService preferencesService) {
this.stateManager = stateManager;
this.dialogService = dialogService;
this.taskExecutor = taskExecutor;
this.preferencesService = preferencesService;

this.executable.bind(ActionHelper.needsEntriesSelected(1, stateManager));
}

@Override
public void execute() {
if (stateManager.getActiveDatabase().isEmpty()) {
dialogService.notify(Localization.lang("This operation requires an open library."));
return;
}

if (stateManager.getSelectedEntries().size() != 1) {
dialogService.notify(Localization.lang("This operation requires exactly one item to be selected."));
return;
}

BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get();

BibEntry entry = stateManager.getSelectedEntries().get(0);

Optional<String> urlforDownload = getUrlForDownloadFromClipBoardOrEntry(dialogService, entry);

if (urlforDownload.isEmpty()) {
return;
}

try {
URL url = new URL(urlforDownload.get());
LinkedFileViewModel onlineFile = new LinkedFileViewModel(
new LinkedFile(url, ""),
entry,
databaseContext,
taskExecutor,
dialogService,
preferencesService);
onlineFile.download();
} catch (MalformedURLException exception) {
dialogService.showErrorDialogAndWait(Localization.lang("Invalid URL"), exception);
}
}

public static Optional<String> getUrlForDownloadFromClipBoardOrEntry(DialogService dialogService, BibEntry entry) {
String clipText = ClipBoardManager.getContents();
Optional<String> urlText;
String urlField = entry.getField(StandardField.URL).orElse("");
if (clipText.startsWith("http://") || clipText.startsWith("https://") || clipText.startsWith("ftp://")) {
urlText = dialogService.showInputDialogWithDefaultAndWait(
Localization.lang("Download file"), Localization.lang("Enter URL to download"), clipText);
} else if (urlField.startsWith("http://") || urlField.startsWith("https://") || urlField.startsWith("ftp://")) {
urlText = dialogService.showInputDialogWithDefaultAndWait(
Localization.lang("Download file"), Localization.lang("Enter URL to download"), urlField);
} else {
urlText = dialogService.showInputDialogAndWait(
Localization.lang("Download file"), Localization.lang("Enter URL to download"));
}
return urlText;
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jabref.gui.exporter.ExportToClipboardAction;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.linkedfile.AttachFileAction;
import org.jabref.gui.linkedfile.AttachFileFromURLAction;
import org.jabref.gui.menus.ChangeEntryTypeMenu;
import org.jabref.gui.mergeentries.MergeEntriesAction;
import org.jabref.gui.mergeentries.MergeWithFetchedEntryAction;
Expand Down Expand Up @@ -70,8 +71,10 @@ public static ContextMenu create(BibEntryTableViewModel entry,
new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.ATTACH_FILE, new AttachFileAction(libraryTab, dialogService, stateManager, preferencesService.getFilePreferences())),
factory.createMenuItem(StandardActions.ATTACH_FILE_FROM_URL, new AttachFileFromURLAction(dialogService, stateManager, taskExecutor, preferencesService)),
factory.createMenuItem(StandardActions.OPEN_FOLDER, new OpenFolderAction(dialogService, stateManager, preferencesService)),
factory.createMenuItem(StandardActions.OPEN_EXTERNAL_FILE, new OpenExternalFileAction(dialogService, stateManager, preferencesService)),

factory.createMenuItem(StandardActions.OPEN_URL, new OpenUrlAction(dialogService, stateManager, preferencesService)),
factory.createMenuItem(StandardActions.SEARCH_SHORTSCIENCE, new SearchShortScienceAction(dialogService, stateManager, preferencesService)),

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 @@ -1156,6 +1156,7 @@ One\ entry\ needed\ a\ clean\ up=One entry needed a clean up

Group\ tree\ could\ not\ be\ parsed.\ If\ you\ save\ the\ BibTeX\ library,\ all\ groups\ will\ be\ lost.=Group tree could not be parsed. If you save the BibTeX library, all groups will be lost.
Attach\ file=Attach file
Attach\ file\ from\ URL=Attach file from URL
Setting\ all\ preferences\ to\ default\ values.=Setting all preferences to default values.
Resetting\ preference\ key\ '%0'=Resetting preference key '%0'
Unable\ to\ clear\ preferences.=Unable to clear preferences.
Expand Down

0 comments on commit 51b36c1

Please sign in to comment.