Merge pull request #32 from informatik-ag-ngl/f/save_attachment

Added ability to save attachments.
This commit is contained in:
delvh 2020-07-28 15:55:05 +02:00 committed by GitHub
commit 3e7a949be5
10 changed files with 198 additions and 29 deletions

View File

@ -77,6 +77,10 @@ public class Settings {
items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key."));
items.putIfAbsent("hideOnClose", new SettingsItem<>(true, "Hide on close", "Hides the chat window when it is closed."));
items.putIfAbsent("currentTheme", new SettingsItem<>("dark", "Current Theme Name", "The name of the currently selected theme."));
items.putIfAbsent("downloadLocation",
new SettingsItem<>(new File(System.getProperty("user.home") + "/Downloads/"), "Download location",
"The location where files will be saved to"));
items.putIfAbsent("autoSaveDownloads", new SettingsItem<>(false, "Save without asking?", "Should downloads be saved without asking?"));
}
/**
@ -120,6 +124,37 @@ public class Settings {
*/
public void setEnterToSend(boolean enterToSend) { ((SettingsItem<Boolean>) items.get("enterToSend")).set(enterToSend); }
/**
* @return whether Envoy will prompt a dialogue before saving an
* {@link envoy.data.Attachment}
* @since Envoy Client v0.2-beta
*/
public Boolean isDownloadSavedWithoutAsking() { return (Boolean) items.get("autoSaveDownloads").get(); }
/**
* Sets whether Envoy will prompt a dialogue before saving an
* {@link envoy.data.Attachment}.
*
* @param autosaveDownload whether a download should be saved without asking
* before
* @since Envoy Client v0.2-beta
*/
public void setDownloadSavedWithoutAsking(boolean autosaveDownload) { ((SettingsItem<Boolean>) items.get("autoSaveDownloads")).set(autosaveDownload); }
/**
* @return the path where downloads should be saved
* @since Envoy Client v0.2-beta
*/
public File getDownloadLocation() { return (File) items.get("downloadLocation").get(); }
/**
* Sets the path where downloads should be saved.
*
* @param downloadLocation the path to set
* @since Envoy Client v0.2-beta
*/
public void setDownloadLocation(File downloadLocation) { ((SettingsItem<File>) items.get("downloadLocation")).set(downloadLocation); }
/**
* @return the current on close mode.
* @since Envoy Client v0.3-alpha

View File

@ -27,6 +27,11 @@ public final class AudioRecorder {
*/
public static final AudioFormat DEFAULT_AUDIO_FORMAT = new AudioFormat(16000, 16, 1, true, false);
/**
* The format in which audio files will be saved.
*/
public static final String FILE_FORMAT = "wav";
private final AudioFormat format;
private final DataLine.Info info;
@ -78,7 +83,7 @@ public final class AudioRecorder {
line.start();
// Prepare temp file
tempFile = Files.createTempFile("recording", "wav");
tempFile = Files.createTempFile("recording", FILE_FORMAT);
// Start the recording
final var ais = new AudioInputStream(line);
@ -117,6 +122,6 @@ public final class AudioRecorder {
line.close();
try {
Files.deleteIfExists(tempFile);
} catch (IOException e) {}
} catch (final IOException e) {}
}
}

View File

@ -8,6 +8,8 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -240,9 +242,10 @@ public final class ChatScene implements Restorable {
this.client = client;
this.writeProxy = writeProxy;
MessageControl.setUser(localDB.getUser());
MessageControl.setSceneContext(sceneContext);
chatList.setItems(FXCollections.observableList(localDB.getChats()));
contactLabel.setText(localDB.getUser().getName());
MessageControl.setUser(localDB.getUser());
if (!client.isOnline()) updateInfoLabel("You are offline", "infoLabel-info");
recorder = new AudioRecorder();
@ -334,7 +337,9 @@ public final class ChatScene implements Restorable {
});
recorder.start();
} else {
pendingAttachment = new Attachment(recorder.finish(), AttachmentType.VOICE);
pendingAttachment = new Attachment(recorder.finish(), "Voice_recording_"
+ DateTimeFormatter.ofPattern("yyyy_MM_dd-HH_mm_ss").format(LocalDateTime.now()) + "." + AudioRecorder.FILE_FORMAT,
AttachmentType.VOICE);
recording = false;
Platform.runLater(() -> {
voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE)));
@ -385,7 +390,7 @@ public final class ChatScene implements Restorable {
// Create the pending attachment
try {
final var fileBytes = Files.readAllBytes(file.toPath());
pendingAttachment = new Attachment(fileBytes, type);
pendingAttachment = new Attachment(fileBytes, file.getName(), type);
checkPostConditions(false);
// Setting the preview image as image of the attachmentView
if (type == AttachmentType.PICTURE)

View File

@ -4,6 +4,7 @@ import javafx.fxml.FXML;
import javafx.scene.control.*;
import envoy.client.ui.SceneContext;
import envoy.client.ui.settings.DownloadSettingsPane;
import envoy.client.ui.settings.GeneralSettingsPane;
import envoy.client.ui.settings.SettingsPane;
@ -29,7 +30,10 @@ public class SettingsScene {
* @param sceneContext enables the user to return to the chat scene
* @since Envoy Client v0.1-beta
*/
public void initializeData(SceneContext sceneContext) { this.sceneContext = sceneContext; }
public void initializeData(SceneContext sceneContext) {
this.sceneContext = sceneContext;
settingsList.getItems().add(new DownloadSettingsPane(sceneContext));
}
@FXML
private void initialize() {

View File

@ -2,7 +2,7 @@ package envoy.client.ui.listcell;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.ByteArrayInputStream;
import java.io.*;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Map;
@ -16,9 +16,12 @@ import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import envoy.client.data.Settings;
import envoy.client.ui.AudioControl;
import envoy.client.ui.IconUtil;
import envoy.client.ui.SceneContext;
import envoy.data.Message;
import envoy.data.Message.MessageStatus;
import envoy.data.User;
@ -38,9 +41,12 @@ public class MessageControl extends Label {
private static User client;
private static SceneContext sceneContext;
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")
.withZone(ZoneId.systemDefault());
private static final Map<MessageStatus, Image> statusImages = IconUtil.loadByEnum(MessageStatus.class, 16);
private static final Settings settings = Settings.getInstance();
private static final Logger logger = EnvoyLog.getLogger(MessageControl.class);
/**
@ -116,11 +122,36 @@ public class MessageControl extends Label {
private void loadMessageInfoScene(Message message) { logger.log(Level.FINEST, "message info scene was requested for " + message); }
private void saveAttachment(Message message) { logger.log(Level.FINEST, "attachment saving was requested for " + message); }
private void saveAttachment(Message message) {
File file;
final var fileName = message.getAttachment().getName();
final var downloadLocation = settings.getDownloadLocation();
// Show save file dialog, if the user did not opt-out
if (!settings.isDownloadSavedWithoutAsking()) {
final var fileChooser = new FileChooser();
fileChooser.setInitialFileName(fileName);
fileChooser.setInitialDirectory(downloadLocation);
file = fileChooser.showSaveDialog(sceneContext.getStage());
} else file = new File(downloadLocation, fileName);
// A file was selected
if (file != null) try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(message.getAttachment().getData());
logger.log(Level.FINE, "Attachment of message was saved at " + file.getAbsolutePath());
} catch (final IOException e) {
logger.log(Level.WARNING, "Could not save attachment of " + message + ": ", e);
}
}
/**
* @param client the user who has logged in
* @since Envoy Client v0.1-beta
*/
public static void setUser(User client) { MessageControl.client = client; }
/**
* @param sceneContext the scene context storing the stage used in Envoy
* @since Envoy Client v0.1-beta
*/
public static void setSceneContext(SceneContext sceneContext) { MessageControl.sceneContext = sceneContext; }
}

View File

@ -0,0 +1,65 @@
package envoy.client.ui.settings;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import envoy.client.ui.SceneContext;
/**
* Displays options for downloading {@link envoy.data.Attachment}s.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>DownloadSettingsPane.java</strong><br>
* Created: <strong>27.07.2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Client v0.2-beta
*/
public class DownloadSettingsPane extends SettingsPane {
/**
* Constructs a new {@code DownloadSettingsPane}.
*
* @param sceneContext the {@code SceneContext} used to block input to the
* {@link javafx.stage.Stage} used in Envoy
* @since Envoy Client v0.2-beta
*/
public DownloadSettingsPane(SceneContext sceneContext) {
super("Download");
final var vbox = new VBox(15);
vbox.setPadding(new Insets(15));
// checkbox to disable asking
final var checkBox = new CheckBox(settings.getItems().get("autoSaveDownloads").getUserFriendlyName());
checkBox.setSelected(settings.isDownloadSavedWithoutAsking());
checkBox.setOnAction(e -> settings.setDownloadSavedWithoutAsking(checkBox.isSelected()));
vbox.getChildren().add(checkBox);
// Displaying the default path to save to
vbox.getChildren().add(new Label(settings.getItems().get("downloadLocation").getDescription() + ":"));
final var hbox = new HBox(20);
final var currentPath = new Label(settings.getDownloadLocation().getAbsolutePath());
hbox.getChildren().add(currentPath);
// Setting the default path
final var button = new Button("Select");
button.setOnAction(e -> {
final var directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("Select the directory where attachments should be saved to");
directoryChooser.setInitialDirectory(settings.getDownloadLocation());
final var selectedDirectory = directoryChooser.showDialog(sceneContext.getStage());
if (selectedDirectory != null) {
currentPath.setText(selectedDirectory.getAbsolutePath());
settings.setDownloadLocation(selectedDirectory);
}
});
hbox.getChildren().add(button);
vbox.getChildren().add(hbox);
getChildren().add(vbox);
}
}

View File

@ -5,7 +5,6 @@ import java.util.List;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import envoy.client.data.Settings;
import envoy.client.data.SettingsItem;
import envoy.client.event.ThemeChangeEvent;
import envoy.data.User.UserStatus;
@ -21,8 +20,6 @@ import envoy.event.EventBus;
*/
public class GeneralSettingsPane extends SettingsPane {
private static final Settings settings = Settings.getInstance();
/**
* @since Envoy Client v0.1-beta
*/

View File

@ -2,6 +2,8 @@ package envoy.client.ui.settings;
import javafx.scene.layout.Pane;
import envoy.client.data.Settings;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>SettingsPane.java</strong><br>
@ -14,6 +16,8 @@ public abstract class SettingsPane extends Pane {
protected String title;
protected static final Settings settings = Settings.getInstance();
protected SettingsPane(String title) { this.title = title; }
/**

View File

@ -30,7 +30,6 @@ public class Attachment implements Serializable {
*/
PICTURE,
/**
* This attachment type denotes a video.
*
@ -55,18 +54,21 @@ public class Attachment implements Serializable {
private final byte[] data;
private final AttachmentType type;
private final String name;
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L;
/**
* Constructs an attachment.
*
* @param data the data of the attachment
* @param name the name of the attachment
* @param type the type of the attachment
* @since Envoy Common v0.1-beta
*/
public Attachment(byte[] data, AttachmentType type) {
public Attachment(byte[] data, String name, AttachmentType type) {
this.data = data;
this.name = name;
this.type = type;
}
@ -81,4 +83,10 @@ public class Attachment implements Serializable {
* @since Envoy Common v0.1-beta
*/
public AttachmentType getType() { return type; }
/**
* @return the name
* @since Envoy Common v0.2-beta
*/
public String getName() { return name; }
}

View File

@ -76,6 +76,7 @@ public class Message {
protected envoy.data.Message.MessageStatus status;
protected AttachmentType attachmentType;
protected byte[] attachment;
protected String attachmentName;
protected boolean forwarded;
/**
@ -93,7 +94,7 @@ public class Message {
* @since Envoy Server Standalone v0.1-alpha
*/
public Message(envoy.data.Message message) {
PersistenceManager persistenceManager = PersistenceManager.getInstance();
final var persistenceManager = PersistenceManager.getInstance();
id = message.getID();
status = message.getStatus();
text = message.getText();
@ -104,8 +105,10 @@ public class Message {
recipient = persistenceManager.getContactByID(message.getRecipientID());
forwarded = message.isForwarded();
if (message.hasAttachment()) {
attachment = message.getAttachment().getData();
attachmentType = message.getAttachment().getType();
final var messageAttachment = message.getAttachment();
attachment = messageAttachment.getData();
attachmentName = messageAttachment.getName();
attachmentType = messageAttachment.getType();
}
}
@ -123,13 +126,13 @@ public class Message {
* @since Envoy Server Standalone v0.1-beta
*/
MessageBuilder prepareBuilder() {
var builder = new MessageBuilder(sender.getID(), recipient.getID(), id).setText(text)
final var builder = new MessageBuilder(sender.getID(), recipient.getID(), id).setText(text)
.setCreationDate(creationDate)
.setReceivedDate(receivedDate)
.setReadDate(readDate)
.setStatus(status)
.setForwarded(forwarded);
if (attachment != null) builder.setAttachment(new Attachment(attachment, attachmentType));
if (attachment != null) builder.setAttachment(new Attachment(attachment, attachmentName, attachmentType));
return builder;
}
@ -282,6 +285,18 @@ public class Message {
*/
public void setAttachmentType(AttachmentType attachmentType) { this.attachmentType = attachmentType; }
/**
* @return the attachmentName
* @since Envoy Server v0.2-beta
*/
public String getAttachmentName() { return attachmentName; }
/**
* @param attachmentName the attachmentName to set
* @since Envoy Server v0.2-beta
*/
public void setAttachmentName(String attachmentName) { this.attachmentName = attachmentName; }
/**
* @return whether this message is a forwarded message
* @since Envoy Server Standalone v0.1-alpha