package ctbrec.ui.sites.jasmin; import java.io.IOException; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ctbrec.sites.ConfigUI; import ctbrec.sites.jasmin.LiveJasmin; import ctbrec.sites.jasmin.LiveJasminHttpClient; import ctbrec.ui.SiteUI; import ctbrec.ui.TabProvider; import ctbrec.ui.controls.Dialogs; public class LiveJasminSiteUi implements SiteUI { private static final transient Logger LOG = LoggerFactory.getLogger(LiveJasminSiteUi.class); private LiveJasmin liveJasmin; private LiveJasminTabProvider tabProvider; private LiveJasminConfigUi configUi; public LiveJasminSiteUi(LiveJasmin liveJasmin) { this.liveJasmin = liveJasmin; tabProvider = new LiveJasminTabProvider(liveJasmin); configUi = new LiveJasminConfigUi(liveJasmin); } @Override public TabProvider getTabProvider() { return tabProvider; } @Override public ConfigUI getConfigUI() { return configUi; } @Override public synchronized boolean login() throws IOException { boolean automaticLogin = liveJasmin.login(); if(automaticLogin) { return true; } else { BlockingQueue queue = new LinkedBlockingQueue<>(); new Thread (() -> { // login with external browser window try { //LiveJasminElectronLoginDialog dialog = new LiveJasminElectronLoginDialog(liveJasmin.getHttpClient().getCookieJar()); } catch (Exception e1) { LOG.error("Error logging in with external browser", e1); Dialogs.showError("Login error", "Couldn't login to " + liveJasmin.getName(), e1); } try { queue.put(true); } catch (InterruptedException e) { LOG.error("Error while signaling termination", e); } }).start(); try { queue.take(); } catch (InterruptedException e) { LOG.error("Error while waiting for login dialog to close", e); throw new IOException(e); } LiveJasminHttpClient httpClient = (LiveJasminHttpClient)liveJasmin.getHttpClient(); boolean loggedIn = httpClient.checkLoginSuccess(); if(loggedIn) { LOG.info("Logged in"); } else { LOG.info("Login failed"); } return loggedIn; } } }