Merge branch 'develop' into f/display_unread_messages

This commit is contained in:
DieGurke 2020-07-11 10:59:23 +02:00
commit 77911e8f9a
7 changed files with 115 additions and 37 deletions

View File

@ -1,7 +1,8 @@
package envoy.client.data; package envoy.client.data;
import static java.util.function.Function.identity;
import java.io.File; import java.io.File;
import java.util.function.Function;
import java.util.logging.Level; import java.util.logging.Level;
import envoy.client.ui.Startup; import envoy.client.ui.Startup;
@ -34,15 +35,15 @@ public class ClientConfig extends Config {
} }
private ClientConfig() { 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("port", new ConfigItem<>("port", "p", Integer::parseInt, null, true));
items.put("localDB", new ConfigItem<>("localDB", "db", File::new, new File("localDB"), 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("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("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("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.CONFIG, true));
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true)); items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true));
items.put("user", new ConfigItem<>("user", "u", Function.identity())); items.put("user", new ConfigItem<>("user", "u", identity()));
items.put("password", new ConfigItem<>("password", "pw", String::toCharArray)); items.put("password", new ConfigItem<>("password", "pw", identity()));
} }
/** /**
@ -97,7 +98,7 @@ public class ClientConfig extends Config {
* @return the password * @return the password
* @since Envoy Client v0.3-alpha * @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 * @return {@code true} if user name and password are set

View File

@ -2,7 +2,10 @@ package envoy.client.ui.controller;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -12,10 +15,12 @@ import javafx.collections.FXCollections;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import envoy.client.data.*; import envoy.client.data.*;
import envoy.client.data.audio.AudioRecorder; import envoy.client.data.audio.AudioRecorder;
@ -60,6 +65,9 @@ public final class ChatScene implements Restorable {
@FXML @FXML
private Button voiceButton; private Button voiceButton;
@FXML
private Button attachmentButton;
@FXML @FXML
private Button settingsButton; private Button settingsButton;
@ -87,12 +95,15 @@ public final class ChatScene implements Restorable {
private AudioRecorder recorder; private AudioRecorder recorder;
private boolean recording; private boolean recording;
private Attachment pendingAttachment; private Attachment pendingAttachment;
private boolean postingPermanentlyDisabled = false; private boolean postingPermanentlyDisabled;
private static final Settings settings = Settings.getInstance(); private static final Settings settings = Settings.getInstance();
private static final EventBus eventBus = EventBus.getInstance(); private static final EventBus eventBus = EventBus.getInstance();
private static final Logger logger = EnvoyLog.getLogger(ChatScene.class); private static final Logger logger = EnvoyLog.getLogger(ChatScene.class);
private static final Image DEFAULT_ATTACHMENT_VIEW_IMAGE = IconUtil.loadIconThemeSensitive("attachment_present", 20);
private static final int MAX_MESSAGE_LENGTH = 255; private static final int MAX_MESSAGE_LENGTH = 255;
private static final int DEFAULT_ICON_SIZE = 16;
/** /**
* Initializes the appearance of certain visual components. * Initializes the appearance of certain visual components.
@ -106,8 +117,10 @@ public final class ChatScene implements Restorable {
messageList.setCellFactory(MessageListCellFactory::new); messageList.setCellFactory(MessageListCellFactory::new);
userList.setCellFactory(ContactListCellFactory::new); userList.setCellFactory(ContactListCellFactory::new);
settingsButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("settings", 16))); settingsButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("settings", DEFAULT_ICON_SIZE)));
voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", 20))); voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE)));
attachmentButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("attachment", DEFAULT_ICON_SIZE)));
attachmentView.setImage(DEFAULT_ATTACHMENT_VIEW_IMAGE);
// Listen to received messages // Listen to received messages
eventBus.register(MessageCreationEvent.class, e -> { eventBus.register(MessageCreationEvent.class, e -> {
@ -229,7 +242,7 @@ public final class ChatScene implements Restorable {
recording = false; recording = false;
} }
pendingAttachment = null; pendingAttachment = null;
attachmentView.setVisible(false); updateAttachmentView(false);
remainingChars.setVisible(true); remainingChars.setVisible(true);
remainingChars remainingChars
@ -237,6 +250,7 @@ public final class ChatScene implements Restorable {
} }
messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled); messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled);
voiceButton.setDisable(!recorder.isSupported()); voiceButton.setDisable(!recorder.isSupported());
attachmentButton.setDisable(false);
userList.refresh(); userList.refresh();
} }
@ -270,18 +284,17 @@ public final class ChatScene implements Restorable {
recording = true; recording = true;
Platform.runLater(() -> { Platform.runLater(() -> {
voiceButton.setText("Recording"); voiceButton.setText("Recording");
voiceButton.setGraphic(new ImageView(IconUtil.loadIcon("microphone_recording", 24))); voiceButton.setGraphic(new ImageView(IconUtil.loadIcon("microphone_recording", DEFAULT_ICON_SIZE)));
}); });
recorder.start(); recorder.start();
} else { } else {
pendingAttachment = new Attachment(recorder.finish(), AttachmentType.VOICE); pendingAttachment = new Attachment(recorder.finish(), AttachmentType.VOICE);
recording = false; recording = false;
Platform.runLater(() -> { Platform.runLater(() -> {
voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", 20))); voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE)));
voiceButton.setText(null); voiceButton.setText(null);
checkPostConditions(false); checkPostConditions(false);
attachmentView.setImage(IconUtil.loadIconThemeSensitive("attachment_present", 20)); updateAttachmentView(true);
attachmentView.setVisible(true);
}); });
} }
} catch (final EnvoyException e) { } catch (final EnvoyException e) {
@ -291,6 +304,52 @@ public final class ChatScene implements Restorable {
}).start(); }).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", "*.bmp", "*.gif"),
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 {
final var fileBytes = Files.readAllBytes(file.toPath());
pendingAttachment = new Attachment(fileBytes, type);
// Setting the preview image as image of the attachmentView
if (type == AttachmentType.PICTURE)
attachmentView.setImage(new Image(new ByteArrayInputStream(fileBytes), DEFAULT_ICON_SIZE, DEFAULT_ICON_SIZE, true, true));
attachmentView.setVisible(true);
} catch (final IOException e) {
new Alert(AlertType.ERROR, "The selected file could not be loaded!").showAndWait();
}
}
}
/** /**
* Checks the text length of the {@code messageTextArea}, adjusts the * Checks the text length of the {@code messageTextArea}, adjusts the
* {@code remainingChars} label and checks whether to send the message * {@code remainingChars} label and checks whether to send the message
@ -383,7 +442,7 @@ public final class ChatScene implements Restorable {
if (pendingAttachment != null) { if (pendingAttachment != null) {
builder.setAttachment(pendingAttachment); builder.setAttachment(pendingAttachment);
pendingAttachment = null; pendingAttachment = null;
attachmentView.setVisible(false); updateAttachmentView(false);
} }
// Building the final message // Building the final message
final var message = currentChat.getRecipient() instanceof Group ? builder.buildGroupMessage((Group) currentChat.getRecipient()) final var message = currentChat.getRecipient() instanceof Group ? builder.buildGroupMessage((Group) currentChat.getRecipient())
@ -432,6 +491,20 @@ public final class ChatScene implements Restorable {
infoLabel.setVisible(true); infoLabel.setVisible(true);
} }
/**
* Updates the {@code attachmentView} in terms of visibility.<br>
* Additionally resets the shown image to
* {@code DEFAULT_ATTACHMENT_VIEW_IMAGE} if another image is currently
* present.
*
* @param visible whether the {@code attachmentView} should be displayed
* @since Envoy Client v0.1-beta
*/
private void updateAttachmentView(boolean visible) {
if (!attachmentView.getImage().equals(DEFAULT_ATTACHMENT_VIEW_IMAGE)) attachmentView.setImage(DEFAULT_ATTACHMENT_VIEW_IMAGE);
attachmentView.setVisible(visible);
}
// Context menu actions // Context menu actions
@FXML @FXML

View File

@ -107,14 +107,13 @@ public final class LoginScene {
} else if (!Bounds.isValidContactName(userTextField.getTextField().getText())) { } else if (!Bounds.isValidContactName(userTextField.getTextField().getText())) {
new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait();
userTextField.getTextField().clear(); userTextField.getTextField().clear();
} else performHandshake(new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText().toCharArray(), } else performHandshake(new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText(), registerCheckBox.isSelected(),
registerCheckBox.isSelected(), Startup.VERSION)); Startup.VERSION));
} }
@FXML @FXML
private void offlineModeButtonPressed() { private void offlineModeButtonPressed() {
attemptOfflineMode( attemptOfflineMode(new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText(), false, Startup.VERSION));
new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText().toCharArray(), false, Startup.VERSION));
} }
@FXML @FXML

View File

@ -2,6 +2,7 @@ package envoy.client.ui.listcell;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.io.ByteArrayInputStream;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@ -67,6 +68,7 @@ public class MessageControl extends Label {
if (message.hasAttachment()) { if (message.hasAttachment()) {
switch (message.getAttachment().getType()) { switch (message.getAttachment().getType()) {
case PICTURE: case PICTURE:
vbox.getChildren().add(new ImageView(new Image(new ByteArrayInputStream(message.getAttachment().getData()), 256, 256, true, true)));
break; break;
case VIDEO: case VIDEO:
break; break;
@ -90,9 +92,7 @@ public class MessageControl extends Label {
statusIcon.setPreserveRatio(true); statusIcon.setPreserveRatio(true);
vbox.getChildren().add(statusIcon); vbox.getChildren().add(statusIcon);
getStyleClass().add("own-message"); getStyleClass().add("own-message");
} else { } else getStyleClass().add("received-message");
getStyleClass().add("received-message");
}
// Adjusting height and weight of the cell to the corresponding ListView // Adjusting height and weight of the cell to the corresponding ListView
paddingProperty().setValue(new Insets(5, 20, 5, 20)); paddingProperty().setValue(new Insets(5, 20, 5, 20));
setContextMenu(contextMenu); setContextMenu(contextMenu);

View File

@ -51,7 +51,7 @@
<Insets bottom="5.0" left="10.0" /> <Insets bottom="5.0" left="10.0" />
</GridPane.margin> </GridPane.margin>
<padding> <padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="2.0" top="5.0" />
</padding> </padding>
<contextMenu> <contextMenu>
<ContextMenu anchorLocation="CONTENT_TOP_LEFT"> <ContextMenu anchorLocation="CONTENT_TOP_LEFT">
@ -82,24 +82,29 @@
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding> </padding>
</Button> </Button>
<ListView fx:id="messageList" prefHeight="257.0" <ListView fx:id="messageList" GridPane.columnIndex="1"
prefWidth="465.0" GridPane.columnIndex="1"
GridPane.columnSpan="2147483647" GridPane.rowIndex="1" GridPane.columnSpan="2147483647" GridPane.rowIndex="1"
GridPane.rowSpan="2"> GridPane.rowSpan="2">
<GridPane.margin> <GridPane.margin>
<Insets left="5.0" right="10.0" /> <Insets left="5.0" right="10.0" />
</GridPane.margin> </GridPane.margin>
<padding> <padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="2.0" top="5.0" />
</padding> </padding>
</ListView> </ListView>
<ButtonBar prefWidth="436.0" GridPane.columnIndex="1" <ButtonBar buttonMinWidth="40.0" GridPane.columnIndex="1"
GridPane.columnSpan="2147483647" GridPane.halignment="CENTER" GridPane.columnSpan="2147483647" GridPane.halignment="CENTER"
GridPane.rowIndex="5" GridPane.valignment="BOTTOM"> GridPane.rowIndex="5" GridPane.valignment="BOTTOM">
<GridPane.margin> <GridPane.margin>
<Insets right="10.0" /> <Insets right="10.0" />
</GridPane.margin> </GridPane.margin>
<buttons> <buttons>
<Button fx:id="attachmentButton" disable="true"
mnemonicParsing="false" onAction="#attachmentButtonClicked">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</Button>
<Button fx:id="voiceButton" disable="true" <Button fx:id="voiceButton" disable="true"
onAction="#voiceButtonClicked"> onAction="#voiceButtonClicked">
<padding> <padding>
@ -175,7 +180,7 @@
</Label> </Label>
<Label fx:id="infoLabel" text="Something happened" <Label fx:id="infoLabel" text="Something happened"
textFill="#faa007" visible="false" wrapText="true" textFill="#faa007" visible="false" wrapText="true"
GridPane.columnIndex="1" GridPane.rowIndex="1"> GridPane.columnIndex="1">
<GridPane.margin> <GridPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</GridPane.margin> </GridPane.margin>

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB