From b6840a99ce5247f5ecf9eb64fbe1db959a974fd0 Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 9 Jul 2020 09:13:26 +0200 Subject: [PATCH 1/5] Adjust to new LoginCredentials API --- pom.xml | 2 +- .../java/envoy/client/data/ClientConfig.java | 11 ++++--- .../client/ui/controller/LoginScene.java | 31 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 92cf7df..2b7c323 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + f~password_in_login_credentials-SNAPSHOT org.openjfx diff --git a/src/main/java/envoy/client/data/ClientConfig.java b/src/main/java/envoy/client/data/ClientConfig.java index a30dffb..49e7892 100644 --- a/src/main/java/envoy/client/data/ClientConfig.java +++ b/src/main/java/envoy/client/data/ClientConfig.java @@ -1,7 +1,8 @@ package envoy.client.data; +import static java.util.function.Function.identity; + import java.io.File; -import java.util.function.Function; import java.util.logging.Level; import envoy.client.ui.Startup; @@ -34,15 +35,15 @@ public class ClientConfig extends Config { } private ClientConfig() { - items.put("server", new ConfigItem<>("server", "s", Function.identity(), null, true)); + items.put("server", new ConfigItem<>("server", "s", identity(), null, true)); items.put("port", new ConfigItem<>("port", "p", Integer::parseInt, null, true)); items.put("localDB", new ConfigItem<>("localDB", "db", File::new, new File("localDB"), true)); items.put("ignoreLocalDB", new ConfigItem<>("ignoreLocalDB", "nodb", Boolean::parseBoolean, false, false)); items.put("homeDirectory", new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy"), true)); items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.CONFIG, true)); items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true)); - items.put("user", new ConfigItem<>("user", "u", Function.identity())); - items.put("password", new ConfigItem<>("password", "pw", String::toCharArray)); + items.put("user", new ConfigItem<>("user", "u", identity())); + items.put("password", new ConfigItem<>("password", "pw", identity())); } /** @@ -97,7 +98,7 @@ public class ClientConfig extends Config { * @return the password * @since Envoy Client v0.3-alpha */ - public char[] getPassword() { return (char[]) items.get("password").get(); } + public String getPassword() { return (String) items.get("password").get(); } /** * @return {@code true} if user name and password are set diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 897cd08..cb60d02 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -52,13 +52,13 @@ public final class LoginScene { @FXML private Label connectionLabel; - private Client client; - private LocalDB localDB; - private Cache receivedMessageCache; - private Cache receivedGroupMessageCache; - private Cache receivedMessageStatusChangeCache; + private Client client; + private LocalDB localDB; + private Cache receivedMessageCache; + private Cache receivedGroupMessageCache; + private Cache receivedMessageStatusChangeCache; private Cache receivedGroupMessageStatusChangeCache; - private SceneContext sceneContext; + private SceneContext sceneContext; private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); private static final EventBus eventBus = EventBus.getInstance(); @@ -103,13 +103,13 @@ public final class LoginScene { public void initializeData(Client client, LocalDB localDB, Cache receivedMessageCache, Cache receivedGroupMessageCache, Cache receivedMessageStatusChangeCache, Cache receivedGroupMessageStatusChangeCache, SceneContext sceneContext) { - this.client = client; - this.localDB = localDB; - this.receivedMessageCache = receivedMessageCache; - this.receivedGroupMessageCache = receivedGroupMessageCache; - this.receivedMessageStatusChangeCache = receivedMessageStatusChangeCache; + this.client = client; + this.localDB = localDB; + this.receivedMessageCache = receivedMessageCache; + this.receivedGroupMessageCache = receivedGroupMessageCache; + this.receivedMessageStatusChangeCache = receivedMessageStatusChangeCache; this.receivedGroupMessageStatusChangeCache = receivedGroupMessageStatusChangeCache; - this.sceneContext = sceneContext; + this.sceneContext = sceneContext; // Prepare handshake localDB.loadIDGenerator(); @@ -131,14 +131,13 @@ public final class LoginScene { } else if (!Bounds.isValidContactName(userTextField.getTextField().getText())) { new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); userTextField.getTextField().clear(); - } else performHandshake(new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText().toCharArray(), - registerCheckBox.isSelected(), Startup.VERSION)); + } else performHandshake(new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText(), registerCheckBox.isSelected(), + Startup.VERSION)); } @FXML private void offlineModeButtonPressed() { - attemptOfflineMode( - new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText().toCharArray(), false, Startup.VERSION)); + attemptOfflineMode(new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText(), false, Startup.VERSION)); } @FXML From e67665f0bd952f768d7b22496577ceb51141409e Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 9 Jul 2020 16:05:09 +0200 Subject: [PATCH 2/5] Add attachment creation support, display picture messages --- .../envoy/client/ui/controller/ChatScene.java | 63 +++++++++++++++++-- .../client/ui/listcell/MessageControl.java | 2 + src/main/resources/fxml/ChatScene.fxml | 3 + 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 2cf116b..09cbdfd 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -2,7 +2,9 @@ package envoy.client.ui.controller; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -16,6 +18,7 @@ import javafx.scene.image.ImageView; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; +import javafx.stage.FileChooser; import envoy.client.data.*; import envoy.client.data.audio.AudioRecorder; @@ -60,6 +63,9 @@ public final class ChatScene implements Restorable { @FXML private Button voiceButton; + @FXML + private Button attachmentButton; + @FXML private Button settingsButton; @@ -234,6 +240,7 @@ public final class ChatScene implements Restorable { } messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled); voiceButton.setDisable(!recorder.isSupported()); + attachmentButton.setDisable(false); } /** @@ -287,6 +294,48 @@ public final class ChatScene implements Restorable { }).start(); } + @FXML + private void attachmentButtonClicked() { + + // Display file chooser + final var fileChooser = new FileChooser(); + fileChooser.setTitle("Add Attachment"); + fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); + fileChooser.getExtensionFilters() + .addAll(new FileChooser.ExtensionFilter("Pictures", "*.png", "*.jpg"), + new FileChooser.ExtensionFilter("Videos", "*.mp4"), + new FileChooser.ExtensionFilter("All Files", "*.*")); + final var file = fileChooser.showOpenDialog(sceneContext.getStage()); + + if(file != null) { + + // Check max file size + if (file.length() > 16E6) { + new Alert(AlertType.WARNING, "The selected file exceeds the size limit of 16MB!").showAndWait(); + return; + } + + // Get attachment type (default is document) + AttachmentType type = AttachmentType.DOCUMENT; + switch (fileChooser.getSelectedExtensionFilter().getDescription()) { + case "Pictures": + type = AttachmentType.PICTURE; + break; + case "Videos": + type = AttachmentType.VIDEO; + break; + } + + // Create the pending attachment + try { + pendingAttachment = new Attachment(Files.readAllBytes(file.toPath()), type); + attachmentView.setVisible(true); + } catch (IOException e) { + new Alert(AlertType.ERROR, "The selected file could not be loaded!").showAndWait(); + } + } + } + /** * Checks the text length of the {@code messageTextArea}, adjusts the * {@code remainingChars} label and checks whether to send the message @@ -373,16 +422,18 @@ public final class ChatScene implements Restorable { final var text = messageTextArea.getText().strip(); try { // Creating the message and its metadata - final var builder = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator()) + final var builder = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB + .getIDGenerator()) .setText(text); - // Setting an attachment, if present - if (pendingAttachment != null) { + // Setting an attachment, if present + if (pendingAttachment != null) { builder.setAttachment(pendingAttachment); pendingAttachment = null; attachmentView.setVisible(false); } - // Building the final message - final var message = currentChat.getRecipient() instanceof Group ? builder.buildGroupMessage((Group) currentChat.getRecipient()) + // Building the final message + final var message = currentChat.getRecipient() instanceof Group ? builder.buildGroupMessage((Group) currentChat + .getRecipient()) : builder.build(); // Send message @@ -429,7 +480,7 @@ public final class ChatScene implements Restorable { } // Context menu actions - + @FXML private void deleteContact() { try {} catch (final NullPointerException e) {} } diff --git a/src/main/java/envoy/client/ui/listcell/MessageControl.java b/src/main/java/envoy/client/ui/listcell/MessageControl.java index 55be558..03cb937 100644 --- a/src/main/java/envoy/client/ui/listcell/MessageControl.java +++ b/src/main/java/envoy/client/ui/listcell/MessageControl.java @@ -2,6 +2,7 @@ package envoy.client.ui.listcell; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; +import java.io.ByteArrayInputStream; import java.time.format.DateTimeFormatter; import java.util.Map; import java.util.logging.Level; @@ -67,6 +68,7 @@ public class MessageControl extends Label { if (message.hasAttachment()) { switch (message.getAttachment().getType()) { case PICTURE: + vbox.getChildren().add(new ImageView(new Image(new ByteArrayInputStream(message.getAttachment().getData())))); break; case VIDEO: break; diff --git a/src/main/resources/fxml/ChatScene.fxml b/src/main/resources/fxml/ChatScene.fxml index 4a6ac3a..21ded83 100644 --- a/src/main/resources/fxml/ChatScene.fxml +++ b/src/main/resources/fxml/ChatScene.fxml @@ -100,6 +100,9 @@ + - - + - @@ -101,7 +100,11 @@