Enable original Plex user select dialog

This commit is contained in:
croneter 2018-10-07 12:09:27 +02:00
parent c26e1283db
commit b27a846292
5 changed files with 252 additions and 44 deletions

12
resources/lib/image.py Normal file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
from . import util, path_ops
CACHE_PATH = path_ops.path.join(util.PROFILE, 'avatars', '')
if not path_ops.exists(CACHE_PATH):
path_ops.makedirs(CACHE_PATH)
def getImage(url, ID):
return url, ''

View file

@ -10,6 +10,7 @@ import xbmc
from . import plex, util, backgroundthread from . import plex, util, backgroundthread
from .plexnet import plexapp, threadutils from .plexnet import plexapp, threadutils
from .windows import userselect
from . import utils from . import utils
from . import userclient from . import userclient

View file

@ -45,7 +45,7 @@ MONITOR = UtilityMonitor()
def T(ID, eng=''): def T(ID, eng=''):
return ADDON.getLocalizedString(ID) return ADDON.getLocalizedString(ID) or xbmc.getLocalizedString(ID)
def LOG(msg, level=xbmc.LOGNOTICE): def LOG(msg, level=xbmc.LOGNOTICE):

View file

@ -0,0 +1,202 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
:module: plexkodiconnect.userselect
:synopsis: This module shows a dialog to let one choose between different Plex
(home) users
"""
from __future__ import absolute_import, division, unicode_literals
from logging import getLogger
import xbmc
import xbmcgui
from . import kodigui
from .. import backgroundthread, utils, plex_tv, variables as v
LOG = getLogger('PLEX.' + __name__)
class UserThumbTask(backgroundthread.Task):
def setup(self, users, callback):
self.users = users
self.callback = callback
return self
def run(self):
for user in self.users:
if self.isCanceled():
return
thumb, back = user.thumb, ''
self.callback(user, thumb, back)
class UserSelectWindow(kodigui.BaseWindow):
xmlFile = 'script-plex-user_select.xml'
path = v.ADDON_PATH
theme = 'Main'
res = '1080i'
width = 1920
height = 1080
USER_LIST_ID = 101
PIN_ENTRY_GROUP_ID = 400
HOME_BUTTON_ID = 500
def __init__(self, *args, **kwargs):
self.task = None
self.user = None
self.aborted = False
kodigui.BaseWindow.__init__(self, *args, **kwargs)
def onFirstInit(self):
self.userList = kodigui.ManagedControlList(self, self.USER_LIST_ID, 6)
self.start()
def onAction(self, action):
try:
ID = action.getId()
if 57 < ID < 68:
if not xbmc.getCondVisibility('ControlGroup({0}).HasFocus(0)'.format(self.PIN_ENTRY_GROUP_ID)):
item = self.userList.getSelectedItem()
if not item.dataSource.isProtected:
return
self.setFocusId(self.PIN_ENTRY_GROUP_ID)
self.pinEntryClicked(ID + 142)
return
elif 142 <= ID <= 149: # JumpSMS action
if not xbmc.getCondVisibility('ControlGroup({0}).HasFocus(0)'.format(self.PIN_ENTRY_GROUP_ID)):
item = self.userList.getSelectedItem()
if not item.dataSource.isProtected:
return
self.setFocusId(self.PIN_ENTRY_GROUP_ID)
self.pinEntryClicked(ID + 60)
return
elif ID in (xbmcgui.ACTION_NAV_BACK, xbmcgui.ACTION_BACKSPACE):
if xbmc.getCondVisibility('ControlGroup({0}).HasFocus(0)'.format(self.PIN_ENTRY_GROUP_ID)):
self.pinEntryClicked(211)
return
except:
utils.ERROR()
kodigui.BaseWindow.onAction(self, action)
def onClick(self, controlID):
if controlID == self.USER_LIST_ID:
item = self.userList.getSelectedItem()
if item.dataSource.isProtected:
self.setFocusId(self.PIN_ENTRY_GROUP_ID)
else:
self.userSelected(item)
elif 200 < controlID < 212:
self.pinEntryClicked(controlID)
elif controlID == self.HOME_BUTTON_ID:
self.home_button_clicked()
def onFocus(self, controlID):
if controlID == self.USER_LIST_ID:
item = self.userList.getSelectedItem()
item.setProperty('editing.pin', '')
def userThumbCallback(self, user, thumb, back):
item = self.userList.getListItemByDataSource(user)
if item:
item.setThumbnailImage(thumb)
item.setProperty('back.image', back)
def start(self):
self.setProperty('busy', '1')
try:
users = plex_tv.plex_home_users(utils.settings('plexToken'))
items = []
for user in users:
# thumb, back = image.getImage(user.thumb, user.id)
# mli = kodigui.ManagedListItem(user.title, thumbnailImage=thumb, data_source=user)
mli = kodigui.ManagedListItem(user.title, user.title[0].upper(), data_source=user)
mli.setProperty('pin', user.title)
# mli.setProperty('back.image', back)
mli.setProperty('protected', user.isProtected and '1' or '')
mli.setProperty('admin', user.isAdmin and '1' or '')
items.append(mli)
self.userList.addItems(items)
self.task = UserThumbTask().setup(users, self.userThumbCallback)
backgroundthread.BGThreader.addTask(self.task)
self.setFocusId(self.USER_LIST_ID)
self.setProperty('initialized', '1')
finally:
self.setProperty('busy', '')
def home_button_clicked(self):
"""
Action taken if user clicked the home button
"""
self.user = None
self.aborted = True
self.doClose()
def pinEntryClicked(self, controlID):
item = self.userList.getSelectedItem()
pin = item.getProperty('editing.pin') or ''
if len(pin) > 3:
return
if controlID < 210:
pin += str(controlID - 200)
elif controlID == 210:
pin += '0'
elif controlID == 211:
pin = pin[:-1]
if pin:
item.setProperty('pin', ' '.join(list(u"\u2022" * len(pin))))
item.setProperty('editing.pin', pin)
if len(pin) > 3:
self.userSelected(item, pin)
else:
item.setProperty('pin', item.dataSource.title)
item.setProperty('editing.pin', '')
def userSelected(self, item, pin=None):
self.user = item.dataSource
LOG.info('Home user selected: %s', self.user)
self.user.authToken = plex_tv.switch_home_user(
self.user.id,
pin,
utils.settings('plexToken'),
utils.settings('plex_machineIdentifier'))
if self.user.authToken is None:
self.user = None
item.setProperty('pin', item.dataSource.title)
item.setProperty('editing.pin', '')
# 'Error': 'Login failed with plex.tv for user'
utils.messageDialog(utils.lang(30135),
'%s %s' % (utils.lang(39229),
self.user.username))
return
self.doClose()
def finished(self):
if self.task:
self.task.cancel()
def start():
"""
Hit this function to open a dialog to choose the Plex user
Returns
=======
tuple (user, aborted)
user : HomeUser
Or None if user switch failed or aborted by the user)
aborted : bool
True if the user cancelled the dialog
"""
w = UserSelectWindow.open()
user, aborted = w.user, w.aborted
del w
return user, aborted

View file

@ -6,14 +6,14 @@
(home) users (home) users
""" """
from __future__ import absolute_import, division, unicode_literals from __future__ import absolute_import, division, unicode_literals
from logging import getLogger
import xbmc import xbmc
import xbmcgui import xbmcgui
from . import kodigui from . import kodigui, dropdown
from .. import backgroundthread, utils, plex_tv, variables as v from .. import util, image, backgroundthread
from ..util import T
from ..plexnet import plexapp
LOG = getLogger('PLEX.' + __name__)
class UserThumbTask(backgroundthread.Task): class UserThumbTask(backgroundthread.Task):
@ -26,13 +26,14 @@ class UserThumbTask(backgroundthread.Task):
for user in self.users: for user in self.users:
if self.isCanceled(): if self.isCanceled():
return return
thumb, back = user.thumb, ''
thumb, back = image.getImage(user.thumb, user.id)
self.callback(user, thumb, back) self.callback(user, thumb, back)
class UserSelectWindow(kodigui.BaseWindow): class UserSelectWindow(kodigui.BaseWindow):
xmlFile = 'script-plex-user_select.xml' xmlFile = 'script-plex-user_select.xml'
path = v.ADDON_PATH path = util.ADDON.getAddonInfo('path')
theme = 'Main' theme = 'Main'
res = '1080i' res = '1080i'
width = 1920 width = 1920
@ -44,8 +45,7 @@ class UserSelectWindow(kodigui.BaseWindow):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.task = None self.task = None
self.user = None self.selected = False
self.aborted = False
kodigui.BaseWindow.__init__(self, *args, **kwargs) kodigui.BaseWindow.__init__(self, *args, **kwargs)
def onFirstInit(self): def onFirstInit(self):
@ -77,7 +77,7 @@ class UserSelectWindow(kodigui.BaseWindow):
self.pinEntryClicked(211) self.pinEntryClicked(211)
return return
except: except:
utils.ERROR() util.ERROR()
kodigui.BaseWindow.onAction(self, action) kodigui.BaseWindow.onAction(self, action)
@ -91,7 +91,7 @@ class UserSelectWindow(kodigui.BaseWindow):
elif 200 < controlID < 212: elif 200 < controlID < 212:
self.pinEntryClicked(controlID) self.pinEntryClicked(controlID)
elif controlID == self.HOME_BUTTON_ID: elif controlID == self.HOME_BUTTON_ID:
self.home_button_clicked() self.shutdownClicked()
def onFocus(self, controlID): def onFocus(self, controlID):
if controlID == self.USER_LIST_ID: if controlID == self.USER_LIST_ID:
@ -107,7 +107,7 @@ class UserSelectWindow(kodigui.BaseWindow):
def start(self): def start(self):
self.setProperty('busy', '1') self.setProperty('busy', '1')
try: try:
users = plex_tv.plex_home_users(utils.settings('plexToken')) users = plexapp.ACCOUNT.homeUsers
items = [] items = []
for user in users: for user in users:
@ -133,13 +133,15 @@ class UserSelectWindow(kodigui.BaseWindow):
""" """
Action taken if user clicked the home button Action taken if user clicked the home button
""" """
self.user = None self.selected = False
self.aborted = True
self.doClose() self.doClose()
def pinEntryClicked(self, controlID): def pinEntryClicked(self, controlID):
item = self.userList.getSelectedItem() item = self.userList.getSelectedItem()
pin = item.getProperty('editing.pin') or '' if item.getProperty('editing.pin'):
pin = item.getProperty('editing.pin')
else:
pin = ''
if len(pin) > 3: if len(pin) > 3:
return return
@ -161,22 +163,24 @@ class UserSelectWindow(kodigui.BaseWindow):
item.setProperty('editing.pin', '') item.setProperty('editing.pin', '')
def userSelected(self, item, pin=None): def userSelected(self, item, pin=None):
self.user = item.dataSource user = item.dataSource
LOG.info('Home user selected: %s', self.user) # xbmc.sleep(500)
self.user.authToken = plex_tv.switch_home_user( util.DEBUG_LOG('Home user selected: {0}'.format(user))
self.user.id,
pin, from .. import plex
utils.settings('plexToken'), with plex.CallbackEvent(plexapp.APP, 'account:response') as e:
utils.settings('plex_machineIdentifier')) if plexapp.ACCOUNT.switchHomeUser(user.id, pin) and plexapp.ACCOUNT.switchUser:
if self.user.authToken is None: util.DEBUG_LOG('Waiting for user change...')
self.user = None else:
e.close()
item.setProperty('pin', item.dataSource.title) item.setProperty('pin', item.dataSource.title)
item.setProperty('editing.pin', '') item.setProperty('editing.pin', '')
# 'Error': 'Login failed with plex.tv for user' util.messageDialog(T(30135, 'Error'),
utils.messageDialog(utils.lang(30135), '%s %s' % (T(39229, 'Login failed with plex.tv for user'),
'%s %s' % (utils.lang(39229),
self.user.username)) self.user.username))
return return
self.selected = True
self.doClose() self.doClose()
def finished(self): def finished(self):
@ -185,18 +189,7 @@ class UserSelectWindow(kodigui.BaseWindow):
def start(): def start():
"""
Hit this function to open a dialog to choose the Plex user
Returns
=======
tuple (user, aborted)
user : HomeUser
Or None if user switch failed or aborted by the user)
aborted : bool
True if the user cancelled the dialog
"""
w = UserSelectWindow.open() w = UserSelectWindow.open()
user, aborted = w.user, w.aborted selected = w.selected
del w del w
return user, aborted return selected