Inceptum/launcher-gtk/src/main/java/io/gitlab/jfronny/inceptum/gtk/window/dialog/MicrosoftLoginDialog.java

82 lines
2.6 KiB
Java

package io.gitlab.jfronny.inceptum.gtk.window.dialog;
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.gtk.util.I18n;
import io.gitlab.jfronny.inceptum.launcher.api.account.*;
import org.gtk.gtk.*;
import org.jetbrains.annotations.Nullable;
import java.net.URI;
import java.net.URISyntaxException;
public class MicrosoftLoginDialog extends MessageDialog {
private static DialogFlags flags(boolean modal) {
DialogFlags flags = DialogFlags.DESTROY_WITH_PARENT;
if (modal) flags = flags.or(DialogFlags.MODAL);
return flags;
}
public MicrosoftLoginDialog(@Nullable Window parent) {
this(parent, null, null);
}
public MicrosoftLoginDialog(@Nullable Window parent, @Nullable MicrosoftAccount account) {
this(parent, account, null);
}
public MicrosoftLoginDialog(@Nullable Window parent, @Nullable Runnable onClose) {
this(parent, null, onClose);
}
public MicrosoftLoginDialog(@Nullable Window parent, @Nullable MicrosoftAccount account, @Nullable Runnable onClose) {
super(
parent,
flags(parent != null),
MessageType.QUESTION,
ButtonsType.CLOSE,
I18n.get("auth.description")
);
title = I18n.get("auth.title");
var server = new MicrosoftAuthServer(account);
try {
server.start();
} catch (Exception e) {
Utils.LOGGER.error("Could not start mc login server", e);
}
Runnable finalize = () -> {
server.close();
if (onClose != null) onClose.run();
};
onResponse(responseId -> {
switch (ResponseType.of(responseId)) {
case CLOSE, CANCEL -> {
finalize.run();
this.close();
}
case DELETE_EVENT -> {
finalize.run();
this.destroy();
}
default -> Utils.LOGGER.error("Unexpected response type: " + responseId);
}
});
var btn = Button.newWithLabel(I18n.get("auth.open-browser"));
((Box) messageArea).append(btn);
btn.onClicked(() -> {
try {
Utils.openWebBrowser(new URI(MicrosoftAuthAPI.MICROSOFT_LOGIN_URL));
} catch (URISyntaxException e) {
Utils.LOGGER.error("Could not open browser", e);
}
});
onCloseRequest(() -> {
finalize.run();
return false;
});
}
}