From e96e727c7c8083128c1df74516ae02c241ca2a19 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 1 Oct 2022 17:40:26 -0400 Subject: [PATCH 1/2] Move quickconnect code into user selection list --- resources/lib/dialogs.py | 40 -------- resources/lib/server_detect.py | 175 +++++++++++++++------------------ 2 files changed, 78 insertions(+), 137 deletions(-) diff --git a/resources/lib/dialogs.py b/resources/lib/dialogs.py index 4ad448a..5c150dd 100644 --- a/resources/lib/dialogs.py +++ b/resources/lib/dialogs.py @@ -204,43 +204,3 @@ class PlayNextDialog(xbmcgui.WindowXMLDialog): def get_play_called(self): return self.play_called - - -class QuickConnectDialog(xbmcgui.WindowXMLDialog): - connect_method = -1 - - def __init__(self, *args, **kwargs): - xbmcgui.WindowXMLDialog.__init__(self, *args, **kwargs) - log.debug("QuickConnectDialog INITIALISED") - - self.code = '' - - def onInit(self): - self.action_exitkeys_id = [10, 13] - - message_control = self.getControl(3) - message_control.setText(self.code) - - message_control = self.getControl(4) - message_control.setLabel(translate_string(30443)) - - self.getControl(3010).setLabel(translate_string(30444)) - self.getControl(3011).setLabel(translate_string(30365)) - - def onFocus(self, controlId): - pass - - def doAction(self, actionID): - pass - - def onClick(self, controlID): - - if controlID == 3010: - self.connect_method = 1 - self.close() - if controlID == 3011: - self.connect_method = 0 - self.close() - - def getConnectMethod(self): - return self.connect_method diff --git a/resources/lib/server_detect.py b/resources/lib/server_detect.py index 1d3f28b..306223b 100644 --- a/resources/lib/server_detect.py +++ b/resources/lib/server_detect.py @@ -14,7 +14,6 @@ from .kodi_utils import HomeWindow from .jellyfin import API from .lazylogger import LazyLogger from .utils import datetime_from_string, translate_string, save_user_details, load_user_details, get_current_datetime, get_saved_users -from .dialogs import QuickConnectDialog log = LazyLogger(__name__) @@ -212,59 +211,79 @@ def check_server(force=False, change_user=False, notify=False): # stop playback when switching users xbmc.Player().stop() - auth = quick_connect(api) - - if auth: - users = [] - user_selection = -1 - selected_user_name = auth.get('User', {}).get('Name') - something_changed = True - else: - users, user_selection = user_select(api, current_username) - - if not auth and user_selection > -1: + # Check if quick connect is active on the server, initiate connection + quick = api.get('/QuickConnect/Initiate') + code = quick.get('Code') + secret = quick.get('Secret') + users, user_selection = user_select(api, current_username, code) + if user_selection > -1: + # The user made a selection in the dialog something_changed = True selected_user = users[user_selection] - selected_user_name = selected_user.getLabel() - secured = selected_user.getProperty("secure") == "true" - manual = selected_user.getProperty("manual") == "true" + quick_connect = selected_user.getProperty("quickconnect") == "true" + count = 0 + if quick_connect: + # Try to authenticate to server with secret code 10 times + while count < 10: + log.debug('Checking for quick connect auth: attempt {}'.format(count)) + check = api.get('/QuickConnect/Connect?secret={}'.format(secret)) + if check.get('Authenticated'): + break + count += 1 + xbmc.sleep(1000) - # If using a manual login, ask for username - if manual: - kb = xbmc.Keyboard() - kb.setHeading(translate_string(30005)) - if current_username: - kb.setDefault(current_username) - kb.doModal() - if kb.isConfirmed(): - selected_user_name = kb.getText() - log.debug("Manual entered username: {0}".format(selected_user_name)) + auth = api.post('/Users/AuthenticateWithQuickConnect', + {'secret': secret}) + + # If authentication was successful, save the username + if auth: + selected_user_name = auth['User'].get('Name') else: - return - - home_window.set_property('user_name', selected_user_name) - settings.setSetting('username', selected_user_name) - user_details = load_user_details() - - if not user_details: - # Ask for password if user has one - password = '' - if secured and not user_details.get('token'): - kb = xbmc.Keyboard() - kb.setHeading(translate_string(30006)) - kb.setHiddenInput(True) - kb.doModal() - if kb.isConfirmed(): - password = kb.getText() - - auth_payload = {'username': selected_user_name, 'pw': password} - auth = api.authenticate(auth_payload) - if not auth: # Login failed, we don't want to change anything something_changed = False - log.info('There was an error logging in with user {}'.format(selected_user_name)) - xbmcgui.Dialog().ok(__addon_name__, translate_string(30446)) + log.info("There was an error logging in with quick connect") + + else: + selected_user_name = selected_user.getLabel() + secured = selected_user.getProperty("secure") == "true" + manual = selected_user.getProperty("manual") == "true" + + # If using a manual login, ask for username + if manual: + kb = xbmc.Keyboard() + kb.setHeading(translate_string(30005)) + if current_username: + kb.setDefault(current_username) + kb.doModal() + if kb.isConfirmed(): + selected_user_name = kb.getText() + log.debug("Manual entered username: {0}".format(selected_user_name)) + else: + return + + home_window.set_property('user_name', selected_user_name) + settings.setSetting('username', selected_user_name) + user_details = load_user_details() + + if not user_details: + # Ask for password if user has one + password = '' + if secured and not user_details.get('token'): + kb = xbmc.Keyboard() + kb.setHeading(translate_string(30006)) + kb.setHiddenInput(True) + kb.doModal() + if kb.isConfirmed(): + password = kb.getText() + + auth_payload = {'username': selected_user_name, 'pw': password} + auth = api.authenticate(auth_payload) + if not auth: + # Login failed, we don't want to change anything + something_changed = False + log.info('There was an error logging in with user {}'.format(selected_user_name)) + xbmcgui.Dialog().ok(__addon_name__, translate_string(30446)) if something_changed: @@ -283,55 +302,7 @@ def check_server(force=False, change_user=False, notify=False): xbmc.executebuiltin("ReloadSkin()") -def quick_connect(api): - ''' - Log in using quick connect funcion - ''' - settings = xbmcaddon.Addon() - addon_path = settings.getAddonInfo('path') - - result = api.get('/QuickConnect/Initiate') - - if not isinstance(result, dict) or not result: - log.debug('Quick connect is disabled on the server') - return {} - - code = result.get('Code') - secret = result.get('Secret') - - # Open Quick Connect dialog, ask to proceed - qc_dialog = QuickConnectDialog("QuickConnectDialog.xml", addon_path, "default", "720p") - qc_dialog.code = code - qc_dialog.doModal() - connect_method = qc_dialog.getConnectMethod() - del qc_dialog - - if connect_method < 1: - # User backed out or selected manual login - return {} - - count = 0 - while count < 15: - # Check the server to see if the auth request has been completed - log.debug('Checking for quick connect auth: attempt {}'.format(count)) - check = api.get('/QuickConnect/Connect?secret={}'.format(secret)) - if check.get('Authenticated'): - break - count += 1 - xbmc.sleep(1000) - - if not check.get('Authenticated'): - log.info('Quick connect not authorized in 15 seconds, defaulting to manual authentication') - return {} - - # Retrieve authentication information - auth = api.post('/Users/AuthenticateWithQuickConnect', - {'secret': secret}) - - return auth - - -def user_select(api, current_username): +def user_select(api, current_username, code): ''' Display user selection screen ''' @@ -352,6 +323,16 @@ def user_select(api, current_username): # Build user display selected_id = -1 users = [] + # If quick connect is active, make it the first entry + if code: + user_item = xbmcgui.ListItem(code) + user_image = "DefaultUser.png" + art = {"Thumb": user_image} + user_item.setArt(art) + user_item.setLabel2(translate_string(30443)) + user_item.setProperty('quickconnect', "true") + users.append(user_item) + for user in public: user_item = create_user_listitem(api.server, user) if user_item: @@ -380,7 +361,7 @@ def user_select(api, current_username): selection_title, users, preselect=selected_id, - autoclose=20000, + autoclose=60000, useDetails=True) return (users, user_selection) From f7a427b8092a09592450e15271c5c49386114020 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 1 Oct 2022 18:17:53 -0400 Subject: [PATCH 2/2] Remove unneeded dialog template --- .../skins/default/720p/QuickConnectDialog.xml | 68 ------------------- 1 file changed, 68 deletions(-) delete mode 100644 resources/skins/default/720p/QuickConnectDialog.xml diff --git a/resources/skins/default/720p/QuickConnectDialog.xml b/resources/skins/default/720p/QuickConnectDialog.xml deleted file mode 100644 index 402739a..0000000 --- a/resources/skins/default/720p/QuickConnectDialog.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - 3010 - 2 - - 1 - 450 - 200 - - - - - 0 - 0 - 400 - 270 - bg.png - - - - 20 - 5 - 360 - 50 - - font45_title - center - - - - 20 - 65 - 360 - 50 - font45 - center - - - - white.png - white.png - 20 - 135 - 360 - 40 - - - 3011 - font14 - center - - - - white.png - white.png - 20 - 195 - 360 - 40 - - 3010 - 3012 - font14 - center - - - -