This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/client/src/main/java/envoy/client/util/UserUtil.java

117 lines
4.0 KiB
Java

package envoy.client.util;
import java.util.logging.*;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import envoy.client.data.Context;
import envoy.client.event.*;
import envoy.client.helper.*;
import envoy.client.ui.SceneContext.SceneInfo;
import envoy.client.ui.controller.ChatScene;
import envoy.data.*;
import envoy.data.User.UserStatus;
import envoy.event.*;
import envoy.event.contact.UserOperation;
import envoy.util.EnvoyLog;
import dev.kske.eventbus.EventBus;
/**
* Contains methods that change something about the currently logged in user.
*
* @author Leon Hofmeister
* @since Envoy Client v0.3-beta
*/
public final class UserUtil {
private static final Context context = Context.getInstance();
private static final Logger logger = EnvoyLog.getLogger(UserUtil.class);
private UserUtil() {}
/**
* Logs the current user out and reopens
* {@link envoy.client.ui.controller.LoginScene}.
*
* @since Envoy Client v0.2-beta
*/
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, () -> {
EnvoyLog.getLogger(ShutdownHelper.class).log(Level.INFO, "A logout was requested");
EventBus.getInstance().dispatch(new EnvoyCloseEvent());
EventBus.getInstance().dispatch(new Logout());
context.getSceneContext().load(SceneInfo.LOGIN_SCENE);
logger.log(Level.INFO, "A logout occurred.");
});
}
/**
* Notifies the application that the status of the currently logged in user has
* changed.
*
* @param newStatus the new status
* @since Envoy Client v0.3-beta
*/
public static void changeStatus(UserStatus newStatus) {
// Sending the already active status is a valid action
if (newStatus.equals(context.getLocalDB().getUser().getStatus())) return;
else {
EventBus.getInstance().dispatch(new OwnStatusChange(newStatus));
if (context.getClient().isOnline()) context.getClient().send(new UserStatusChange(context.getLocalDB().getUser().getID(), newStatus));
logger.log(Level.INFO, "A manual status change occurred.");
}
}
/**
* Removes the given contact.
*
* @param block the contact that should be removed
* @since Envoy Client v0.3-beta
*/
public static void disableContact(Contact block) {
if (!context.getClient().isOnline() || block == null) return;
else {
final var alert = new Alert(AlertType.CONFIRMATION);
alert.setContentText("Are you sure you want to " + (block instanceof User ? "block " : "leave group ") + block.getName() + "?");
AlertHelper.confirmAction(alert, () -> {
final var isUser = block instanceof User;
context.getClient()
.send(isUser ? new UserOperation((User) block, ElementOperation.REMOVE)
: new GroupResize(context.getLocalDB().getUser(), (Group) block, ElementOperation.REMOVE));
if (!isUser) block.getContacts().remove(context.getLocalDB().getUser());
EventBus.getInstance().dispatch(new ContactDisabled(block));
logger.log(Level.INFO, isUser ? "A user was blocked." : "The user left a group.");
});
}
}
/**
* Deletes the given contact with all his messages entirely.
*
* @param delete the contact to delete
* @since Envoy Client v0.3-beta
*/
public static void deleteContact(Contact delete) {
if (delete == null) return;
else {
final var alert = new Alert(AlertType.CONFIRMATION);
alert.setContentText("Are you sure you want to delete " + delete.getName()
+ " entirely? All messages with this contact will be deleted. This action cannot be undone.");
AlertHelper.confirmAction(alert, () -> {
context.getLocalDB().getUsers().remove(delete.getName());
context.getLocalDB().getChats().removeIf(chat -> chat.getRecipient().equals(delete));
if (context.getSceneContext().getController() instanceof ChatScene)
((ChatScene) context.getSceneContext().getController()).resetState();
logger.log(Level.INFO, "A contact with all his messages was deleted.");
});
}
}
}