Added ability to save attachments

This commit is contained in:
delvh
2020-07-27 12:00:49 +02:00
parent 1cdad2df0b
commit e216152e6b
5 changed files with 81 additions and 24 deletions

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

@ -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,11 @@ 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.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,6 +40,8 @@ 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);
@ -116,11 +120,31 @@ 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) {
// Show save file dialog
final var fileChooser = new FileChooser();
fileChooser.setInitialFileName(message.getAttachment().getName());
final File file = fileChooser.showSaveDialog(sceneContext.getStage());
// 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.");
} 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; }
}