diff --git a/resources/lib/main.py b/resources/lib/main.py index 685e1d76..f4c7d2b0 100644 --- a/resources/lib/main.py +++ b/resources/lib/main.py @@ -298,63 +298,84 @@ def main(): LOG.info('Starting PKC %s', util.ADDON.getAddonInfo('version')) LOG.info('User-agent: %s', plex.defaultUserAgent()) + server_select_shown = False + try: + plex.init() + # Check plex.tv sign-in status + if not plexapp.ACCOUNT.authToken: + LOG.info('Not signed in to plex.tv yet') + if utils.settings('myplexlogin') == 'true': + plex.authorize_user() + # Main Loop while not xbmc.abortRequested: - if plex.init(): - while not xbmc.abortRequested: - if ( - not plexapp.ACCOUNT.isOffline and not - plexapp.ACCOUNT.isAuthenticated and - (len(plexapp.ACCOUNT.homeUsers) > 1 or plexapp.ACCOUNT.isProtected) + # Check whether a PMS server has been set + selectedServer = plexapp.SERVERMANAGER.selectedServer + if not selectedServer: + if not server_select_shown: + server_select_shown = True + LOG.debug('No PMS set yet, presenting dialog') + plex.select_server() + else: + util.MONITOR.waitForAbort(1) + continue + # Ping the PMS until we're sure its online + if not selectedServer.isReachable(): + selectedServer.updateReachability(force=True, + allowFallback=True) + continue + while not xbmc.abortRequested: - ): - result = userselect.start() - if not result: - return - elif result == 'signout': - signout() - break - elif result == 'signin': - break - LOG.info('User selected') + if ( + not plexapp.ACCOUNT.isOffline and not + plexapp.ACCOUNT.isAuthenticated and + (len(plexapp.ACCOUNT.homeUsers) > 1 or plexapp.ACCOUNT.isProtected) - try: - selectedServer = plexapp.SERVERMANAGER.selectedServer + ): + result = userselect.start() + if not result: + return + elif result == 'signout': + signout() + break + elif result == 'signin': + break + LOG.info('User selected') - if not selectedServer: - LOG.debug('Waiting for selected server...') - for timeout, skip_preferred, skip_owned in ((10, True, False), (10, True, True)): - plex.CallbackEvent(plexapp.APP, 'change:selectedServer', timeout=timeout).wait() + try: + selectedServer = plexapp.SERVERMANAGER.selectedServer - selectedServer = plexapp.SERVERMANAGER.checkSelectedServerSearch(skip_preferred=skip_preferred, skip_owned=skip_owned) - if selectedServer: - break - else: - LOG.debug('Finished waiting for selected server...') + if not selectedServer: + LOG.debug('Waiting for selected server...') + for timeout, skip_preferred, skip_owned in ((10, True, False), (10, True, True)): + plex.CallbackEvent(plexapp.APP, 'change:selectedServer', timeout=timeout).wait() - LOG.info('Starting with server: %s', selectedServer) + selectedServer = plexapp.SERVERMANAGER.checkSelectedServerSearch(skip_preferred=skip_preferred, skip_owned=skip_owned) + if selectedServer: + break + else: + LOG.debug('Finished waiting for selected server...') - windowutils.HOME = home.HomeWindow.open() - util.CRON.cancelReceiver(windowutils.HOME) + LOG.info('Starting with server: %s', selectedServer) - if not windowutils.HOME.closeOption: - return + windowutils.HOME = home.HomeWindow.open() + util.CRON.cancelReceiver(windowutils.HOME) - closeOption = windowutils.HOME.closeOption + if not windowutils.HOME.closeOption: + return - windowutils.shutdownHome() + closeOption = windowutils.HOME.closeOption - if closeOption == 'signout': - signout() - break - elif closeOption == 'switch': - plexapp.ACCOUNT.isAuthenticated = False - finally: - windowutils.shutdownHome() - gc.collect(2) + windowutils.shutdownHome() - else: - break + if closeOption == 'signout': + signout() + break + elif closeOption == 'switch': + plexapp.ACCOUNT.isAuthenticated = False + finally: + windowutils.shutdownHome() + gc.collect(2) except: util.ERROR() finally: diff --git a/resources/lib/plex.py b/resources/lib/plex.py index 98ab314b..3c356c23 100644 --- a/resources/lib/plex.py +++ b/resources/lib/plex.py @@ -275,33 +275,50 @@ class CallbackEvent(plexapp.CompatEvent): def init(): LOG.info('Initializing') - with CallbackEvent(plexapp.APP, 'init'): plexapp.init() LOG.info('Waiting for account initialization...') + LOG.info('Account initialization done...') - if not plexapp.ACCOUNT.authToken: - from .windows import background - with background.BackgroundContext(function=authorize) as d: - token = d.result - if not token: - LOG.info('Did not get a Plex token') - return False +def authorize_user(): + """ + Display userselect dialog. Returns True if user has been selected + and a valid token has been retrieved, False otherwise + """ + LOG.info('Displaying userselect dialog') + from .windows import background + with background.BackgroundContext(function=authorize) as d: + token = d.result + if not token: + LOG.info('Did not get a Plex token') + return False + with CallbackEvent(plexapp.APP, 'account:response'): + plexapp.ACCOUNT.validateToken(token) + LOG.info('Waiting for account initialization') + return plexapp.ACCOUNT.isSignedIn - with CallbackEvent(plexapp.APP, 'account:response'): - plexapp.ACCOUNT.validateToken(token) - LOG.info('Waiting for account initialization') - return True + +def select_user(): + """ + """ + pass + + +def select_server(): + """ + Displays a window for the user to select a pms. + """ + pass def authorize(): + """ + Shows dialogs to sign in to plex.tv with pin. Returns token or None + """ from .windows import signin, background - background.setSplash(False) - back = signin.Background.create() - try: while True: pinLoginWindow = signin.PinLoginWindow.create()