Added key shortcuts and system commands for logout, exit and settings

Additionally added **buggy** logout mechanism: LocalDB is not reset
properly and IndexOutOfBoundsExceptions occur in the UI
This commit is contained in:
Leon Hofmeister 2020-09-23 23:11:32 +02:00
parent 2d9283551a
commit 05d4917bb2
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
8 changed files with 91 additions and 23 deletions

View File

@ -7,7 +7,7 @@ import java.time.Instant;
import java.util.*;
import java.util.logging.Level;
import envoy.client.event.EnvoyCloseEvent;
import envoy.client.event.*;
import envoy.data.*;
import envoy.event.*;
import envoy.exception.EnvoyException;
@ -213,11 +213,19 @@ public final class LocalDB implements EventListener {
private void onNewAuthToken(NewAuthToken evt) { authToken = evt.get(); }
/**
* Deletes the file that stores the "remember me" feature.
* Deletes all associations to the current user.
*
* @since Envoy Client v0.2-beta
*/
public void deleteLoginFile() { lastLoginFile.delete(); }
@Event(eventType = Logout.class, priority = 100)
public void onLogout() {
lastLoginFile.delete();
userFile = null;
user = null;
authToken = null;
chats.clear();
cacheMap = new CacheMap();
}
/**
* @return a {@code Map<String, User>} of all users stored locally with their

View File

@ -0,0 +1,14 @@
package envoy.client.event;
import envoy.event.Event.Valueless;
/**
* Indicates that a logout has been requested.
*
* @author leon
* @since Envoy Client v0.2-beta
*/
public class Logout extends Valueless {
private static final long serialVersionUID = 1L;
}

View File

@ -1,11 +1,11 @@
package envoy.client.helper;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import envoy.client.data.*;
import envoy.client.event.EnvoyCloseEvent;
import envoy.client.event.*;
import envoy.client.ui.SceneContext.SceneInfo;
import dev.kske.eventbus.EventBus;
@ -25,21 +25,31 @@ public class ShutdownHelper {
*
* @since Envoy Client v0.2-beta
*/
public static void exit() { exit(Settings.getInstance().isHideOnClose(), true); }
public static void exit() {
if (Settings.getInstance().isHideOnClose()) Context.getInstance().getStage().setIconified(true);
else {
final var alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Exit?");
alert.setContentText("Are you sure you want to exit Envoy?");
AlertHelper.confirmAction(alert, () -> { EventBus.getInstance().dispatch(new EnvoyCloseEvent()); System.exit(0); });
}
}
/**
* Exits the currently open Envoy application or minimizes it.
* Does not necessarily shutdown VM.
* Logs the current user out and reopens
* {@link envoy.client.ui.controller.LoginScene}.
*
* @param minimize whether Envoy should be minimized instead of closed
* @param shutdownVM whether the JVM should shutdown
* @since Envoy Client v0.2-beta
*/
public static void exit(boolean minimize, boolean shutdownVM) {
if (minimize) Context.getInstance().getStage().setIconified(true);
else {
public static void logout() {
final var alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Logout?");
alert.setContentText("Are you sure you want to log out?");
AlertHelper.confirmAction(alert, () -> {
EventBus.getInstance().dispatch(new EnvoyCloseEvent());
if (shutdownVM) System.exit(0);
}
EventBus.getInstance().dispatch(new Logout());
Context.getInstance().getSceneContext().load(SceneInfo.LOGIN_SCENE);
});
}
}

View File

@ -240,7 +240,10 @@ public final class Client implements EventListener, Closeable {
logger.log(Level.INFO, "Closing connection...");
try {
socket.close();
} catch (final IOException e) {}
online = false;
} catch (final IOException e) {
logger.log(Level.WARNING, "Failed to close socket: ", e);
}
}
}

View File

@ -108,8 +108,16 @@ public final class SceneContext implements EventListener {
sceneStack.push(scene);
stage.setScene(scene);
// Adding the option to exit Linux-like with "Control" + "Q"
// Add the option to exit Linux-like with "Control" + "Q"
scene.getAccelerators().put(new KeyCodeCombination(KeyCode.Q, KeyCombination.CONTROL_DOWN), ShutdownHelper::exit);
// Add the option to logout using "Control"+"Shift"+"L"
scene.getAccelerators()
.put(new KeyCodeCombination(KeyCode.L, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), ShutdownHelper::logout);
// Add the option to open the settings scene with "Control"+"S", if being in
// chat scene
if (sceneInfo.equals(SceneInfo.CHAT_SCENE))
scene.getAccelerators().put(new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN), () -> load(SceneInfo.SETTINGS_SCENE));
// The LoginScene is the only scene not intended to be resized
// As strange as it seems, this is needed as otherwise the LoginScene won't be

View File

@ -56,6 +56,8 @@ public final class Startup extends Application {
*/
@Override
public void start(Stage stage) throws Exception {
// Initialize config and logger
try {
config.loadAll(Startup.class, "client.properties", getParameters().getRaw().toArray(new String[0]));
EnvoyLog.initialize(config);
@ -68,7 +70,7 @@ public final class Startup extends Application {
// Initialize the local database
try {
var localDBFile = new File(config.getHomeDirectory(), config.getServer());
final var localDBFile = new File(config.getHomeDirectory(), config.getServer());
logger.info("Initializing LocalDB at " + localDBFile);
localDB = new LocalDB(localDBFile);
} catch (IOException | EnvoyException e) {

View File

@ -28,8 +28,10 @@ import envoy.client.data.*;
import envoy.client.data.audio.AudioRecorder;
import envoy.client.data.commands.*;
import envoy.client.event.*;
import envoy.client.helper.ShutdownHelper;
import envoy.client.net.*;
import envoy.client.ui.*;
import envoy.client.ui.SceneContext.SceneInfo;
import envoy.client.ui.custom.TextInputContextMenu;
import envoy.client.ui.listcell.*;
import envoy.client.util.ReflectionUtil;
@ -338,6 +340,19 @@ public final class ChatScene implements EventListener, Restorable {
.setDescription("See for yourself :)")
.setNumberOfArguments(2)
.build("dabr");
// Logout initialization
builder.setAction(text -> ShutdownHelper.logout()).setNumberOfArguments(0).setDescription("Logs you out.").build("logout");
// Exit initialization
builder.setAction(text -> ShutdownHelper.exit()).setNumberOfArguments(0).setDescription("Exits the program").build("exit", false);
builder.build("q");
// Open settings scene initialization
builder.setAction(text -> sceneContext.load(SceneInfo.SETTINGS_SCENE))
.setNumberOfArguments(0)
.setDescription("Opens the settings screen")
.build("settings");
}
@Override

View File

@ -3,7 +3,8 @@ package envoy.client.ui.settings;
import javafx.scene.control.*;
import envoy.client.data.SettingsItem;
import envoy.client.event.*;
import envoy.client.event.ThemeChangeEvent;
import envoy.client.helper.ShutdownHelper;
import envoy.data.User.UserStatus;
import dev.kske.eventbus.EventBus;
@ -61,5 +62,12 @@ public final class GeneralSettingsPane extends SettingsPane {
// TODO add action when value is changed
statusComboBox.setOnAction(e -> {});
getChildren().add(statusComboBox);
final var logoutButton = new Button("Logout");
logoutButton.setOnAction(e -> ShutdownHelper.logout());
final var logoutTooltip = new Tooltip("Brings you back to the login screen and removes \"remember me\" status from this account");
logoutTooltip.setWrapText(true);
logoutButton.setTooltip(logoutTooltip);
getChildren().add(logoutButton);
}
}