Skip to content

Commit

Permalink
Download many files in one go
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Aug 26, 2016
1 parent 7cb1e75 commit c36fd29
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 72 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#

### Changed
- Added integrity check for fields with BibTeX keys, e.g., `crossref` and `related`, to check that the key exists
- [#1496](https://github.com/JabRef/jabref/issues/1496) Keep track of which entry a downloaded file belongs to
- Made it possible to download multiple entries in one action

### Fixed
- Fixed NullPointerException when opening search result window for an untitled database
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/net/sf/jabref/external/DownloadExternalFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.sf.jabref.logic.net.URLDownload;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
Expand All @@ -43,15 +44,16 @@ public class DownloadExternalFile {

private final JabRefFrame frame;
private final BibDatabaseContext databaseContext;
private final String bibtexKey;
private final BibEntry entry;
private FileListEntryEditor editor;
private boolean downloadFinished;
private boolean dontShowDialog;

public DownloadExternalFile(JabRefFrame frame, BibDatabaseContext databaseContext, String bibtexKey) {

public DownloadExternalFile(JabRefFrame frame, BibDatabaseContext databaseContext, BibEntry entry) {
this.frame = frame;
this.databaseContext = databaseContext;
this.bibtexKey = bibtexKey;
this.entry = entry;
}

/**
Expand Down Expand Up @@ -155,8 +157,8 @@ public void download(URL url, final DownloadCallback callback) throws IOExceptio
}
final String suggestDir = directory == null ? System.getProperty("user.home") : directory;
File file = new File(new File(suggestDir), suggestedName);
FileListEntry entry = new FileListEntry("", file.getCanonicalPath(), suggestedType);
editor = new FileListEntryEditor(frame, entry, true, false, databaseContext);
FileListEntry fileListEntry = new FileListEntry("", file.getCanonicalPath(), suggestedType);
editor = new FileListEntryEditor(frame, fileListEntry, true, false, databaseContext);
editor.getProgressBar().setIndeterminate(true);
editor.setOkEnabled(false);
editor.setExternalConfirm(closeEntry -> {
Expand All @@ -181,7 +183,8 @@ public void download(URL url, final DownloadCallback callback) throws IOExceptio
}
// Editor closed. Go on:
if (editor.okPressed()) {
File toFile = directory == null ? new File(entry.link) : expandFilename(directory, entry.link);
File toFile = directory == null ? new File(fileListEntry.link) : expandFilename(directory,
fileListEntry.link);
String dirPrefix;
if (directory == null) {
dirPrefix = null;
Expand All @@ -202,12 +205,13 @@ public void download(URL url, final DownloadCallback callback) throws IOExceptio

// If the local file is in or below the main file directory, change the
// path to relative:
if ((directory != null) && entry.link.startsWith(directory) &&
(entry.link.length() > dirPrefix.length())) {
entry = new FileListEntry(entry.description, entry.link.substring(dirPrefix.length()), entry.type);
if ((directory != null) && fileListEntry.link.startsWith(directory)
&& (fileListEntry.link.length() > dirPrefix.length())) {
fileListEntry = new FileListEntry(fileListEntry.description,
fileListEntry.link.substring(dirPrefix.length()), fileListEntry.type);
}

callback.downloadComplete(entry);
callback.downloadComplete(fileListEntry);
} catch (IOException ex) {
LOGGER.warn("Problem downloading file", ex);
}
Expand Down Expand Up @@ -255,8 +259,7 @@ private void downloadFinished() {

// FIXME: will break download if no bibtexkey is present!
private String getSuggestedFileName(String suffix) {
String plannedName = FileUtil.createFileNameFromPattern(databaseContext.getDatabase(),
frame.getCurrentBasePanel().getSelectedEntries().get(0),
String plannedName = FileUtil.createFileNameFromPattern(databaseContext.getDatabase(), entry,
Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN),
Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader));

Expand Down
91 changes: 51 additions & 40 deletions src/main/java/net/sf/jabref/external/FindFullTextAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import javax.swing.JOptionPane;

Expand All @@ -27,8 +31,7 @@ public class FindFullTextAction extends AbstractWorker {
private static final Log LOGGER = LogFactory.getLog(FindFullTextAction.class);

private final BasePanel basePanel;
private BibEntry entry;
private Optional<URL> result;
private final Map<Optional<URL>, BibEntry> downloads = new ConcurrentHashMap<>();

public FindFullTextAction(BasePanel basePanel) {
this.basePanel = basePanel;
Expand All @@ -41,52 +44,60 @@ public void init() throws Throwable {

@Override
public void run() {
if (basePanel.getSelectedEntries().size() != 1) {
basePanel.output(Localization.lang("This operation requires exactly one item to be selected."));
result = Optional.empty();
} else {
entry = basePanel.getSelectedEntries().get(0);
for (BibEntry entry : basePanel.getSelectedEntries()) {
FulltextFetchers fft = new FulltextFetchers();
result = fft.findFullTextPDF(entry);
downloads.put(fft.findFullTextPDF(entry), entry);
}
}

@Override
public void update() {
if (result.isPresent()) {
List<String> dirs = basePanel.getBibDatabaseContext()
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
if (dirs.isEmpty()) {
JOptionPane.showMessageDialog(basePanel.frame(),
Localization.lang("Main file directory not set!") + " " + Localization.lang("Preferences")
+ " -> " + Localization.lang("External programs"),
Localization.lang("Directory not found"), JOptionPane.ERROR_MESSAGE);
return;
}
String bibtexKey = entry.getCiteKey();
// TODO: this needs its own thread as it blocks the UI!
DownloadExternalFile def = new DownloadExternalFile(basePanel.frame(), basePanel.getBibDatabaseContext(), bibtexKey);
try {
def.download(result.get(), file -> {
FileListTableModel tm = new FileListTableModel();
entry.getFieldOptional(FieldName.FILE).ifPresent(tm::setContent);
tm.addEntry(tm.getRowCount(), file);
String newValue = tm.getStringRepresentation();
UndoableFieldChange edit = new UndoableFieldChange(entry, FieldName.FILE,
entry.getFieldOptional(FieldName.FILE).orElse(null), newValue);
entry.setField(FieldName.FILE, newValue);
basePanel.getUndoManager().addEdit(edit);
basePanel.markBaseChanged();
});
} catch (IOException e) {
LOGGER.warn("Problem downloading file", e);
List<Optional<URL>> remove = new ArrayList<>();
for (Entry<Optional<URL>, BibEntry> download : downloads.entrySet()) {
BibEntry entry = download.getValue();
Optional<URL> result = download.getKey();
if (result.isPresent()) {
List<String> dirs = basePanel.getBibDatabaseContext()
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
if (dirs.isEmpty()) {
JOptionPane.showMessageDialog(basePanel.frame(),
Localization.lang("Main file directory not set!") + " " + Localization.lang("Preferences")
+ " -> " + Localization.lang("External programs"),
Localization.lang("Directory not found"), JOptionPane.ERROR_MESSAGE);
return;
}
// TODO: this needs its own thread as it blocks the UI!
DownloadExternalFile def = new DownloadExternalFile(basePanel.frame(),
basePanel.getBibDatabaseContext(), entry);
try {
def.download(result.get(), file -> {
FileListTableModel tm = new FileListTableModel();
entry.getFieldOptional(FieldName.FILE).ifPresent(tm::setContent);
tm.addEntry(tm.getRowCount(), file);
String newValue = tm.getStringRepresentation();
UndoableFieldChange edit = new UndoableFieldChange(entry, FieldName.FILE,
entry.getFieldOptional(FieldName.FILE).orElse(null), newValue);
entry.setField(FieldName.FILE, newValue);
basePanel.getUndoManager().addEdit(edit);
basePanel.markBaseChanged();
});
} catch (IOException e) {
LOGGER.warn("Problem downloading file", e);
}
basePanel.output(Localization.lang("Finished downloading full text document for entry %0",
entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
} else {
String title = Localization.lang("Full text document download failed");
String message = Localization.lang("Full text document download failed for entry %0",
entry.getCiteKeyOptional().orElse(Localization.lang("undefined")));

basePanel.output(message);
JOptionPane.showMessageDialog(basePanel.frame(), message, title, JOptionPane.ERROR_MESSAGE);
}
basePanel.output(Localization.lang("Finished downloading full text document"));
remove.add(result);
}
else {
String message = Localization.lang("Full text document download failed");
basePanel.output(message);
JOptionPane.showMessageDialog(basePanel.frame(), message, message, JOptionPane.ERROR_MESSAGE);
for (Optional<URL> result : remove) {
downloads.remove(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ private void downloadFile() {
}
}
DownloadExternalFile def = new DownloadExternalFile(frame,
frame.getCurrentBasePanel().getBibDatabaseContext(), bibtexKey.orElse(null));
frame.getCurrentBasePanel().getBibDatabaseContext(), entryEditor.getEntry());
try {
def.download(this);
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1176,11 +1176,11 @@ public void actionPerformed(ActionEvent actionEvent) {
bibtexKey = entry.getCiteKeyOptional();
}
}
DownloadExternalFile def = new DownloadExternalFile(frame, bibDatabaseContext, bibtexKey.get());
DownloadExternalFile def = new DownloadExternalFile(frame, bibDatabaseContext, entry);
try {
def.download(this);
} catch (IOException ex) {
LOGGER.warn("Could not downlod file", ex);
LOGGER.warn("Could not download file", ex);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,6 @@ Sort_the_following_fields_as_numeric_fields=Sorter_følgende_felter_som_numerisk
Line_%0\:_Found_corrupted_BibTeX_key.=Linje_%0\:_Fandt_ødelagt_BibTeX-nøgle.
Line_%0\:_Found_corrupted_BibTeX_key_(contains_whitespaces).=Linje_%0\:_Fandt_ødelagt_BibTeX-nøgle_(indeholder_blanktegn).
Line_%0\:_Found_corrupted_BibTeX_key_(comma_missing).=Linje_%0\:_Fandt_ødelagt_BibTeX-nøgle_(manglende_komma).
Finished_downloading_full_text_document=Download_af_fuldtekst-dokument_afsluttet
Full_text_document_download_failed=Download_af_fuldtekst-dokument_mislykkedes
Update_to_current_column_order=Brug_nuværende_kolonnerækkefølge

Expand Down Expand Up @@ -1772,3 +1771,6 @@ Opens_JabRef's_website=
Opens_a_link_where_the_current_development_version_can_be_downloaded=
See_what_has_been_changed_in_the_JabRef_versions=
Referenced_BibTeX_key_does_not_exist=

Finished_downloading_full_text_document_for_entry_%0=
Full_text_document_download_failed_for_entry_%0=
4 changes: 3 additions & 1 deletion src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,6 @@ Sort_the_following_fields_as_numeric_fields=Sortiere_folgende_Felder_als_numeris
Line_%0\:_Found_corrupted_BibTeX_key.=Zeile_%0\:_Beschädigter_BibTeX-Key_gefunden.
Line_%0\:_Found_corrupted_BibTeX_key_(contains_whitespaces).=Zeile_%0\:_Beschädigter_BibTeX-Key_gefunden_(enthält_Leerzeichen).
Line_%0\:_Found_corrupted_BibTeX_key_(comma_missing).=Zeile_%0\:_Beschädigter_BibTeX-Key_gefunden_(Komma_fehlt).
Finished_downloading_full_text_document=Herunterladen_des_Volltext-Dokuments_abgeschlossen
Full_text_document_download_failed=Herunterladen_des_Volltext-Beitrags_fehlgeschlagen
Update_to_current_column_order=Aktuelle_Spaltenanordnung_verwenden

Expand Down Expand Up @@ -2484,3 +2483,6 @@ Opens_a_link_where_the_current_development_version_can_be_downloaded=Öffnet_ein
See_what_has_been_changed_in_the_JabRef_versions=Beschreibt_was_in_den_verschiedenen_JabRef-Versionen_geändert_wurde

Referenced_BibTeX_key_does_not_exist=Referenzierter_BibTeX-Key_existiert_nicht

Finished_downloading_full_text_document_for_entry_%0=
Full_text_document_download_failed_for_entry_%0=
3 changes: 2 additions & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,6 @@ Sort_the_following_fields_as_numeric_fields=Sort_the_following_fields_as_numeric
Line_%0\:_Found_corrupted_BibTeX_key.=Line_%0\:_Found_corrupted_BibTeX_key.
Line_%0\:_Found_corrupted_BibTeX_key_(contains_whitespaces).=Line_%0\:_Found_corrupted_BibTeX_key_(contains_whitespaces).
Line_%0\:_Found_corrupted_BibTeX_key_(comma_missing).=Line_%0\:_Found_corrupted_BibTeX_key_(comma_missing).
Finished_downloading_full_text_document=Finished_downloading_full_text_document
Full_text_document_download_failed=Full_text_document_download_failed
Update_to_current_column_order=Update_to_current_column_order
Download_from_URL=Download_from_URL
Expand Down Expand Up @@ -2304,3 +2303,5 @@ Opens_JabRef's_website=Opens_JabRef's_website
Opens_a_link_where_the_current_development_version_can_be_downloaded=Opens_a_link_where_the_current_development_version_can_be_downloaded
See_what_has_been_changed_in_the_JabRef_versions=See_what_has_been_changed_in_the_JabRef_versions
Referenced_BibTeX_key_does_not_exist=Referenced_BibTeX_key_does_not_exist
Finished_downloading_full_text_document_for_entry_%0=Finished_downloading_full_text_document_for_entry_%0
Full_text_document_download_failed_for_entry_%0=Full_text_document_download_failed_for_entry_%0
4 changes: 3 additions & 1 deletion src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@ Sort_the_following_fields_as_numeric_fields=Ordenar_los_siguientes_campos_como_c
Line_%0\:_Found_corrupted_BibTeX_key.=Línea_%0\:_Se_ha_encontrado_una_clave_BibTeX_corrupta.
Line_%0\:_Found_corrupted_BibTeX_key_(contains_whitespaces).=Línea_%0\:_Se_ha_encontrado_una_clave_BibTeX_corrupta_(contiene_espacios_en_blanco)
Line_%0\:_Found_corrupted_BibTeX_key_(comma_missing).=Línea_%0\:_Se_ha_encontrado_clave_BibTeX_corrupta_(falta_coma).
Finished_downloading_full_text_document=Se_ha_finalizado_la_descarga_del_texto_completo_del_documento
Full_text_document_download_failed=Falló_la_descarga_del_texto_completo_del_artículo
Update_to_current_column_order=Actualizar_al_orden_de_columnas_actual
Rename_field=Renombrar_campo
Expand Down Expand Up @@ -1673,3 +1672,6 @@ Opens_JabRef's_website=
Opens_a_link_where_the_current_development_version_can_be_downloaded=
See_what_has_been_changed_in_the_JabRef_versions=
Referenced_BibTeX_key_does_not_exist=

Finished_downloading_full_text_document_for_entry_%0=
Full_text_document_download_failed_for_entry_%0=
4 changes: 3 additions & 1 deletion src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,6 @@ Back=
Sort_the_following_fields_as_numeric_fields=
Line_%0\:_Found_corrupted_BibTeX_key_(contains_whitespaces).=
Line_%0\:_Found_corrupted_BibTeX_key_(comma_missing).=
Finished_downloading_full_text_document=
Full_text_document_download_failed=
Update_to_current_column_order=

Expand Down Expand Up @@ -2453,3 +2452,6 @@ Opens_JabRef's_website=
Opens_a_link_where_the_current_development_version_can_be_downloaded=
See_what_has_been_changed_in_the_JabRef_versions=
Referenced_BibTeX_key_does_not_exist=

Finished_downloading_full_text_document_for_entry_%0=
Full_text_document_download_failed_for_entry_%0=
4 changes: 3 additions & 1 deletion src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,6 @@ Sort_the_following_fields_as_numeric_fields=Trier_les_champs_suivants_comme_des_
Line_%0\:_Found_corrupted_BibTeX_key.=Ligne_%0_\:_Clef_BibTeX_corrompue_trouvée.
Line_%0\:_Found_corrupted_BibTeX_key_(contains_whitespaces).=Ligne_%0_\:_Clef_BibTeX_corrompue_trouvée_(contient_des_espaces).
Line_%0\:_Found_corrupted_BibTeX_key_(comma_missing).=Ligne_%0_\:_Clef_BibTeX_corrompue_trouvée_(virgule_manquante).
Finished_downloading_full_text_document=Téléchargement_du_document_cité_terminé
Full_text_document_download_failed=Echec_du_téléchargement_du_document_cité
Update_to_current_column_order=Enregistrer_l'ordre_actuel_des_colonnes

Expand Down Expand Up @@ -1722,3 +1721,6 @@ Opens_a_link_where_the_current_development_version_can_be_downloaded=Ouvre_un_li
See_what_has_been_changed_in_the_JabRef_versions=Afficher_les_changements_dans_les_versions_de_JabRef

Referenced_BibTeX_key_does_not_exist=

Finished_downloading_full_text_document_for_entry_%0=
Full_text_document_download_failed_for_entry_%0=
4 changes: 3 additions & 1 deletion src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,6 @@ Sort_the_following_fields_as_numeric_fields=Urutkan_bidang_berikut_sepeerti_angk
Line_%0\:_Found_corrupted_BibTeX_key.=Baris_%0\:_Ditemukan_kunci_BibTeX_ada_kesalahan.
Line_%0\:_Found_corrupted_BibTeX_key_(contains_whitespaces).=Baris_%0\:_Ditemukan_kunci_BibTeX_ada_kesalahan_(mengandung_spasi_kosong).
Line_%0\:_Found_corrupted_BibTeX_key_(comma_missing).=Baris_%0\:_Ditemukan_kunci_BibTeX_ada_kesalahan_(tidak_ada_koma).
Finished_downloading_full_text_document=Selesai_muaturun_dokumen_teks_lengkap
Full_text_document_download_failed=Gagal_muaturun_artikel_teks_lengkap
Update_to_current_column_order=Perbarui_sebuai_urutan_kolom_sekarang
Rename_field=Ganti_nama_bidang
Expand Down Expand Up @@ -1689,3 +1688,6 @@ Opens_JabRef's_website=
Opens_a_link_where_the_current_development_version_can_be_downloaded=
See_what_has_been_changed_in_the_JabRef_versions=
Referenced_BibTeX_key_does_not_exist=

Finished_downloading_full_text_document_for_entry_%0=
Full_text_document_download_failed_for_entry_%0=
4 changes: 3 additions & 1 deletion src/main/resources/l10n/JabRef_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,6 @@ Sort_the_following_fields_as_numeric_fields=Ordina_i_campi_seguenti_come_campi_n
Line_%0\:_Found_corrupted_BibTeX_key.=Riga_%0\:_chiave_BibTeX_corrotta.
Line_%0\:_Found_corrupted_BibTeX_key_(contains_whitespaces).=Riga_%0\:_chiave_BibTeX_corrotta_(contiene_spazi).
Line_%0\:_Found_corrupted_BibTeX_key_(comma_missing).=Riga_%0\:_chiave_BibTeX_corrotta_(virgola_mancante).
Finished_downloading_full_text_document=Terminato_il_download_del_documento_citato
Full_text_document_download_failed=Fallito_il_download_del_documento_citato
Update_to_current_column_order=Salvare_l'ordine_delle_colonne_attuale

Expand Down Expand Up @@ -1790,3 +1789,6 @@ Opens_JabRef's_website=
Opens_a_link_where_the_current_development_version_can_be_downloaded=
See_what_has_been_changed_in_the_JabRef_versions=
Referenced_BibTeX_key_does_not_exist=

Finished_downloading_full_text_document_for_entry_%0=
Full_text_document_download_failed_for_entry_%0=
Loading

0 comments on commit c36fd29

Please sign in to comment.