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

Add 'Send to Kindle' feature #9995

Merged
merged 8 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
65 changes: 25 additions & 40 deletions src/main/java/org/jabref/gui/SendAsEMailAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.awt.Desktop;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
Expand All @@ -13,11 +12,7 @@
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.FieldWriter;
import org.jabref.logic.exporter.BibWriter;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.OS;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -71,29 +66,40 @@ private String sendEmail() throws Exception {
return Localization.lang("This operation requires one or more entries to be selected.");
}

StringWriter rawEntries = new StringWriter();
BibWriter bibWriter = new BibWriter(rawEntries, OS.NEWLINE);
BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get();
List<BibEntry> entries = stateManager.getSelectedEntries();
URI uriMailTo = getUriMailTo(entries);

// write the entries via this writer to "rawEntries" (being a StringWriter), which used later to form the email content
BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new FieldWriter(preferencesService.getFieldPreferences()), Globals.entryTypesManager);
Desktop desktop = Desktop.getDesktop();
desktop.mail(uriMailTo);

for (BibEntry entry : entries) {
try {
bibtexEntryWriter.write(entry, bibWriter, databaseContext.getMode());
} catch (IOException e) {
LOGGER.warn("Problem creating BibTeX file for mailing.", e);
}
return String.format("%s: %d", Localization.lang("Entries added to an email"), entries.size());
}

private URI getUriMailTo(List<BibEntry> entries) throws URISyntaxException {
StringBuilder mailTo = new StringBuilder();

mailTo.append(getEmailAddress());
mailTo.append("?Body=").append(getBody());
mailTo.append("&Subject=").append(getSubject());

List<String> attachments = getAttachments(entries);
for (String path : attachments) {
mailTo.append("&Attachment=\"").append(path);
mailTo.append("\"");
}

List<String> attachments = new ArrayList<>();
return new URI("mailto", mailTo.toString(), null);
}

private List<String> getAttachments(List<BibEntry> entries) {
// open folders is needed to indirectly support email programs, which cannot handle
// the unofficial "mailto:attachment" property
boolean openFolders = preferencesService.getExternalApplicationsPreferences().shouldAutoOpenEmailAttachmentsFolder();

BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get();
List<Path> fileList = FileUtil.getListOfLinkedFiles(entries, databaseContext.getFileDirectories(preferencesService.getFilePreferences()));

List<String> attachments = new ArrayList<>();
for (Path path : fileList) {
attachments.add(path.toAbsolutePath().toString());
if (openFolders) {
Expand All @@ -104,33 +110,12 @@ private String sendEmail() throws Exception {
}
}
}

URI uriMailTo = getUriMailTo(rawEntries, attachments);

Desktop desktop = Desktop.getDesktop();
desktop.mail(uriMailTo);

return String.format("%s: %d", Localization.lang("Entries added to an email"), entries.size());
}

private URI getUriMailTo(StringWriter rawEntries, List<String> attachments) throws URISyntaxException {
StringBuilder mailTo = new StringBuilder();

mailTo.append(getEmailAddress());
mailTo.append("?Body=").append(getBody(rawEntries));
mailTo.append("&Subject=").append(getSubject());

for (String path : attachments) {
mailTo.append("&Attachment=\"").append(path);
mailTo.append("\"");
}

return new URI("mailto", mailTo.toString(), null);
return attachments;
}

protected abstract String getEmailAddress();

protected abstract String getSubject();

protected abstract String getBody(StringWriter rawEntries);
protected abstract String getBody();
}
4 changes: 1 addition & 3 deletions src/main/java/org/jabref/gui/SendAsKindleEmailAction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.jabref.gui;

import java.io.StringWriter;

import org.jabref.gui.actions.ActionHelper;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.PreferencesService;
Expand Down Expand Up @@ -30,7 +28,7 @@ protected String getSubject() {
}

@Override
protected String getBody(StringWriter rawEntries) {
protected String getBody() {
return Localization.lang("Send to Kindle");
}
}
32 changes: 31 additions & 1 deletion src/main/java/org/jabref/gui/SendAsStandardEmailAction.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
package org.jabref.gui;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import org.jabref.gui.actions.ActionHelper;
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.FieldWriter;
import org.jabref.logic.exporter.BibWriter;
import org.jabref.logic.util.OS;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Sends the selected entries to any specifiable email
* by populating the email body
*/
public class SendAsStandardEmailAction extends SendAsEMailAction {
private static final Logger LOGGER = LoggerFactory.getLogger(SendAsStandardEmailAction.class);
private final PreferencesService preferencesService;
private final StateManager stateManager;

public SendAsStandardEmailAction(DialogService dialogService, PreferencesService preferencesService, StateManager stateManager) {
super(dialogService, preferencesService, stateManager);
this.preferencesService = preferencesService;
this.stateManager = stateManager;
this.executable.bind(ActionHelper.needsEntriesSelected(stateManager));
}

Expand All @@ -29,7 +43,23 @@ protected String getSubject() {
}

@Override
protected String getBody(StringWriter rawEntries) {
protected String getBody() {
List<BibEntry> entries = stateManager.getSelectedEntries();
BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get();
StringWriter rawEntries = new StringWriter();
BibWriter bibWriter = new BibWriter(rawEntries, OS.NEWLINE);

// write the entries via this writer to "rawEntries" (being a StringWriter), which is used later to form the email content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be removed, as the usage is clear from method namegetBody()

BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new FieldWriter(preferencesService.getFieldPreferences()), Globals.entryTypesManager);

for (BibEntry entry : entries) {
try {
bibtexEntryWriter.write(entry, bibWriter, databaseContext.getMode());
} catch (IOException e) {
LOGGER.warn("Problem creating BibTeX file for mailing.", e);
}
}

return rawEntries.toString();
}
}