2020-10-08 16:12:24 +02:00
|
|
|
package envoy.client.util;
|
|
|
|
|
2020-10-13 23:51:02 +02:00
|
|
|
import java.util.logging.*;
|
2020-10-08 16:12:24 +02:00
|
|
|
|
2020-10-19 22:16:18 +02:00
|
|
|
import javafx.scene.control.*;
|
2020-10-08 16:12:24 +02:00
|
|
|
import javafx.scene.control.Alert.AlertType;
|
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
import dev.kske.eventbus.EventBus;
|
|
|
|
|
2020-10-11 23:04:25 +02:00
|
|
|
import envoy.data.*;
|
2020-10-08 16:12:24 +02:00
|
|
|
import envoy.data.User.UserStatus;
|
2020-10-11 23:04:25 +02:00
|
|
|
import envoy.event.*;
|
2020-10-24 12:19:11 +02:00
|
|
|
import envoy.event.contact.UserOperation;
|
2020-10-08 16:12:24 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
import envoy.client.data.Context;
|
|
|
|
import envoy.client.event.*;
|
|
|
|
import envoy.client.helper.*;
|
2020-11-06 08:58:13 +01:00
|
|
|
import envoy.client.ui.SceneInfo;
|
2020-10-19 18:17:51 +02:00
|
|
|
import envoy.client.ui.controller.ChatScene;
|
2020-10-08 16:12:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains methods that change something about the currently logged in user.
|
|
|
|
*
|
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @since Envoy Client v0.3-beta
|
|
|
|
*/
|
|
|
|
public final class UserUtil {
|
|
|
|
|
2020-10-13 23:51:02 +02:00
|
|
|
private static final Context context = Context.getInstance();
|
|
|
|
private static final Logger logger = EnvoyLog.getLogger(UserUtil.class);
|
2020-10-11 23:04:25 +02:00
|
|
|
|
2020-10-08 16:12:24 +02:00
|
|
|
private UserUtil() {}
|
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Logs the current user out and reopens {@link envoy.client.ui.controller.LoginScene}.
|
2020-10-08 16:12:24 +02:00
|
|
|
*
|
|
|
|
* @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());
|
2020-10-11 23:04:25 +02:00
|
|
|
context.getSceneContext().load(SceneInfo.LOGIN_SCENE);
|
2020-10-13 23:51:02 +02:00
|
|
|
logger.log(Level.INFO, "A logout occurred.");
|
2020-10-08 16:12:24 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Notifies the application that the status of the currently logged in user has changed.
|
2020-10-08 16:12:24 +02:00
|
|
|
*
|
|
|
|
* @param newStatus the new status
|
|
|
|
* @since Envoy Client v0.3-beta
|
|
|
|
*/
|
|
|
|
public static void changeStatus(UserStatus newStatus) {
|
2020-10-10 11:10:17 +02:00
|
|
|
|
|
|
|
// Sending the already active status is a valid action
|
2020-10-19 18:17:51 +02:00
|
|
|
if (newStatus.equals(context.getLocalDB().getUser().getStatus()))
|
|
|
|
return;
|
2020-10-10 11:10:17 +02:00
|
|
|
else {
|
|
|
|
EventBus.getInstance().dispatch(new OwnStatusChange(newStatus));
|
2020-10-19 18:17:51 +02:00
|
|
|
if (context.getClient().isOnline())
|
|
|
|
context.getClient()
|
|
|
|
.send(new UserStatusChange(context.getLocalDB().getUser().getID(), newStatus));
|
2020-10-13 23:51:02 +02:00
|
|
|
logger.log(Level.INFO, "A manual status change occurred.");
|
2020-10-11 23:04:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-17 16:40:13 +02:00
|
|
|
* Removes the given contact.
|
2020-10-11 23:04:25 +02:00
|
|
|
*
|
|
|
|
* @param block the contact that should be removed
|
|
|
|
* @since Envoy Client v0.3-beta
|
|
|
|
*/
|
2020-10-17 16:40:13 +02:00
|
|
|
public static void disableContact(Contact block) {
|
2020-10-19 18:17:51 +02:00
|
|
|
if (!context.getClient().isOnline() || block == null)
|
|
|
|
return;
|
2020-10-11 23:04:25 +02:00
|
|
|
else {
|
|
|
|
final var alert = new Alert(AlertType.CONFIRMATION);
|
2020-10-19 18:17:51 +02:00
|
|
|
alert.setContentText("Are you sure you want to "
|
|
|
|
+ (block instanceof User ? "block " : "leave group ") + block.getName() + "?");
|
2020-10-11 23:04:25 +02:00
|
|
|
AlertHelper.confirmAction(alert, () -> {
|
2020-10-17 16:40:13 +02:00
|
|
|
final var isUser = block instanceof User;
|
2020-10-11 23:04:25 +02:00
|
|
|
context.getClient()
|
2020-10-17 16:40:13 +02:00
|
|
|
.send(isUser ? new UserOperation((User) block, ElementOperation.REMOVE)
|
2020-10-19 18:17:51 +02:00
|
|
|
: new GroupResize(context.getLocalDB().getUser(), (Group) block,
|
|
|
|
ElementOperation.REMOVE));
|
|
|
|
if (!isUser)
|
|
|
|
block.getContacts().remove(context.getLocalDB().getUser());
|
2020-10-17 16:40:13 +02:00
|
|
|
EventBus.getInstance().dispatch(new ContactDisabled(block));
|
|
|
|
logger.log(Level.INFO, isUser ? "A user was blocked." : "The user left a group.");
|
2020-10-11 23:04:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2020-10-19 18:17:51 +02:00
|
|
|
if (delete == null)
|
|
|
|
return;
|
2020-10-11 23:04:25 +02:00
|
|
|
else {
|
|
|
|
final var alert = new Alert(AlertType.CONFIRMATION);
|
|
|
|
alert.setContentText("Are you sure you want to delete " + delete.getName()
|
2020-10-19 18:17:51 +02:00
|
|
|
+ " entirely? All messages with this contact will be deleted. This action cannot be undone.");
|
2020-10-11 23:04:25 +02:00
|
|
|
AlertHelper.confirmAction(alert, () -> {
|
|
|
|
context.getLocalDB().getUsers().remove(delete.getName());
|
2020-10-19 18:17:51 +02:00
|
|
|
context.getLocalDB().getChats()
|
|
|
|
.removeIf(chat -> chat.getRecipient().equals(delete));
|
2020-10-11 23:04:25 +02:00
|
|
|
if (context.getSceneContext().getController() instanceof ChatScene)
|
|
|
|
((ChatScene) context.getSceneContext().getController()).resetState();
|
2020-10-13 23:51:02 +02:00
|
|
|
logger.log(Level.INFO, "A contact with all his messages was deleted.");
|
2020-10-11 23:04:25 +02:00
|
|
|
});
|
2020-10-10 11:10:17 +02:00
|
|
|
}
|
2020-10-08 16:12:24 +02:00
|
|
|
}
|
2020-10-19 22:16:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes anything pointing to this user, independent of client or server. Will do nothing if
|
|
|
|
* the client is currently offline.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.3-beta
|
|
|
|
*/
|
|
|
|
public static void deleteAccount() {
|
|
|
|
|
2020-10-24 12:19:11 +02:00
|
|
|
// Show the first wall of defense, if not disabled by the user
|
|
|
|
final var outerAlert = new Alert(AlertType.CONFIRMATION);
|
|
|
|
outerAlert.setContentText(
|
|
|
|
"Are you sure you want to delete your account entirely? This action can seriously not be undone.");
|
|
|
|
outerAlert.setTitle("Delete Account?");
|
|
|
|
AlertHelper.confirmAction(outerAlert, () -> {
|
|
|
|
|
|
|
|
// Show the final wall of defense in every case
|
|
|
|
final var lastAlert = new Alert(AlertType.WARNING,
|
|
|
|
"Do you REALLY want to delete your account? Last Warning. Proceed?",
|
|
|
|
ButtonType.CANCEL, ButtonType.OK);
|
|
|
|
lastAlert.setTitle("Delete Account?");
|
|
|
|
lastAlert.showAndWait().filter(ButtonType.OK::equals).ifPresent(b2 -> {
|
|
|
|
|
|
|
|
// Delete the account
|
|
|
|
// TODO: Notify server of account deletion
|
|
|
|
context.getLocalDB().delete();
|
|
|
|
logger.log(Level.INFO, "The user just deleted his account. Goodbye.");
|
|
|
|
ShutdownHelper.exit(true);
|
2020-10-19 22:16:18 +02:00
|
|
|
});
|
2020-10-24 12:19:11 +02:00
|
|
|
});
|
2020-10-19 22:16:18 +02:00
|
|
|
}
|
2020-10-08 16:12:24 +02:00
|
|
|
}
|