2020-10-08 16:12:24 +02:00
|
|
|
package envoy.client.util;
|
|
|
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
|
|
|
import javafx.scene.control.Alert;
|
|
|
|
import javafx.scene.control.Alert.AlertType;
|
|
|
|
|
2020-10-11 23:04:25 +02:00
|
|
|
import envoy.client.data.*;
|
2020-10-08 16:12:24 +02:00
|
|
|
import envoy.client.event.*;
|
|
|
|
import envoy.client.helper.*;
|
|
|
|
import envoy.client.ui.SceneContext.SceneInfo;
|
2020-10-11 23:04:25 +02:00
|
|
|
import envoy.client.ui.controller.ChatScene;
|
|
|
|
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.*;
|
|
|
|
import envoy.event.contact.UserOperation;
|
2020-10-08 16:12:24 +02:00
|
|
|
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 {
|
|
|
|
|
2020-10-11 23:04:25 +02:00
|
|
|
private static final Context context = Context.getInstance();
|
|
|
|
|
2020-10-08 16:12:24 +02:00
|
|
|
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());
|
2020-10-11 23:04:25 +02:00
|
|
|
context.getSceneContext().load(SceneInfo.LOGIN_SCENE);
|
2020-10-08 16:12:24 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the application that the status of the currently logged in user has
|
2020-10-10 11:10:17 +02:00
|
|
|
* 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-11 23:04:25 +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-11 23:04:25 +02:00
|
|
|
if (context.getClient().isOnline()) context.getClient().send(new UserStatusChange(context.getLocalDB().getUser().getID(), newStatus));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the given contact. Should not be confused with the method that is
|
|
|
|
* called when the server reports that a contact has been deleted while the user
|
|
|
|
* was offline.
|
|
|
|
*
|
|
|
|
* @param block the contact that should be removed
|
|
|
|
* @since Envoy Client v0.3-beta
|
|
|
|
* @see LocalDB#setUserAndMergeContacts(envoy.data.User)
|
|
|
|
*/
|
|
|
|
public static void blockContact(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 " + block.getName() + "?");
|
|
|
|
AlertHelper.confirmAction(alert, () -> {
|
|
|
|
context.getClient()
|
|
|
|
.send(block instanceof User ? new UserOperation((User) block, ElementOperation.REMOVE)
|
|
|
|
: new GroupResize(context.getLocalDB().getUser(), (Group) block, ElementOperation.REMOVE));
|
|
|
|
context.getLocalDB().getChat(block.getID()).ifPresent(chat -> chat.setDisabled(true));
|
|
|
|
final var controller = context.getSceneContext().getController();
|
|
|
|
if (controller instanceof ChatScene) ((ChatScene) controller).disableChat(block, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 (!context.getClient().isOnline() || 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();
|
|
|
|
});
|
2020-10-10 11:10:17 +02:00
|
|
|
}
|
2020-10-08 16:12:24 +02:00
|
|
|
}
|
|
|
|
}
|