54 lines
1.8 KiB
Java
54 lines
1.8 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, @Nullable MicrosoftAccount account) {
|
|
super(
|
|
parent,
|
|
flags(parent != null),
|
|
MessageType.QUESTION,
|
|
ButtonsType.CLOSE,
|
|
I18n.get("auth.description")
|
|
);
|
|
title = I18n.get("auth.title");
|
|
onResponse(responseId -> {
|
|
switch (ResponseType.of(responseId)) {
|
|
case CLOSE, CANCEL -> this.close();
|
|
case DELETE_EVENT -> 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);
|
|
}
|
|
});
|
|
|
|
var server = new MicrosoftAuthServer(account);
|
|
try {
|
|
server.start();
|
|
} catch (Exception e) {
|
|
Utils.LOGGER.error("Could not start mc login server", e);
|
|
}
|
|
onClose(server::close);
|
|
}
|
|
}
|