PlexKodiConnect/resources/lib/dialogs/serverconnect.py

140 lines
4 KiB
Python
Raw Normal View History

2016-10-23 02:15:10 +11:00
# -*- coding: utf-8 -*-
2017-07-01 20:32:23 +10:00
###############################################################################
2016-10-23 02:15:10 +11:00
2017-07-01 20:32:23 +10:00
from logging import getLogger
2016-10-23 02:15:10 +11:00
import xbmc
import xbmcgui
2017-07-16 23:22:08 +10:00
import connect.connectionmanager as connectionmanager
2016-10-23 02:15:10 +11:00
from utils import language as lang
2017-07-01 20:32:23 +10:00
###############################################################################
2016-10-23 02:15:10 +11:00
2017-07-01 20:32:23 +10:00
log = getLogger("PLEX."+__name__)
2016-10-23 02:15:10 +11:00
2017-07-16 23:22:08 +10:00
CONN_STATE = connectionmanager.CONNECTIONSTATE
2016-10-23 02:15:10 +11:00
ACTION_PARENT_DIR = 9
ACTION_PREVIOUS_MENU = 10
ACTION_BACK = 92
ACTION_SELECT_ITEM = 7
ACTION_MOUSE_LEFT_CLICK = 100
USER_IMAGE = 150
USER_NAME = 151
LIST = 155
CANCEL = 201
MESSAGE_BOX = 202
MESSAGE = 203
BUSY = 204
2017-07-16 23:22:08 +10:00
PLEX_CONNECT = 205
2016-10-23 02:15:10 +11:00
MANUAL_SERVER = 206
2017-07-01 20:32:23 +10:00
###############################################################################
2016-10-23 02:15:10 +11:00
class ServerConnect(xbmcgui.WindowXMLDialog):
username = ""
user_image = None
servers = []
_selected_server = None
_connect_login = False
_manual_server = False
def set_args(self, **kwargs):
2017-07-16 23:22:08 +10:00
# connect_manager, username, user_image, servers, plex_connect
2016-10-23 02:15:10 +11:00
for key, value in kwargs.iteritems():
setattr(self, key, value)
def is_server_selected(self):
return True if self._selected_server else False
def get_server(self):
return self._selected_server
def is_connect_login(self):
return self._connect_login
def is_manual_server(self):
return self._manual_server
def onInit(self):
self.message = self.getControl(MESSAGE)
self.message_box = self.getControl(MESSAGE_BOX)
self.busy = self.getControl(BUSY)
self.list_ = self.getControl(LIST)
for server in self.servers:
2017-07-28 01:48:14 +10:00
server_type = "wifi" if server.get('local') == '0' else "network"
2016-10-23 02:15:10 +11:00
self.list_.addItem(self._add_listitem(server['Name'], server['Id'], server_type))
2017-07-03 02:04:22 +10:00
self.getControl(USER_NAME).setLabel("%s %s" % (lang(33000), self.username.decode('utf-8')))
2016-10-23 02:15:10 +11:00
if self.user_image is not None:
self.getControl(USER_IMAGE).setImage(self.user_image)
2017-07-16 23:22:08 +10:00
if not self.plex_connect: # Change connect user
self.getControl(PLEX_CONNECT).setLabel("[UPPERCASE][B]"+'plex.tv user change'+"[/B][/UPPERCASE]")
2016-10-23 02:15:10 +11:00
if self.servers:
self.setFocus(self.list_)
@classmethod
def _add_listitem(cls, label, server_id, server_type):
item = xbmcgui.ListItem(label)
item.setProperty('id', server_id)
item.setProperty('server_type', server_type)
return item
def onAction(self, action):
if action in (ACTION_BACK, ACTION_PREVIOUS_MENU, ACTION_PARENT_DIR):
self.close()
if action in (ACTION_SELECT_ITEM, ACTION_MOUSE_LEFT_CLICK):
if self.getFocusId() == LIST:
server = self.list_.getSelectedItem()
selected_id = server.getProperty('id')
log.info('Server Id selected: %s', selected_id)
if self._connect_server(selected_id):
self.message_box.setVisibleCondition('False')
self.close()
def onClick(self, control):
2017-07-16 23:22:08 +10:00
if control == PLEX_CONNECT:
2016-10-23 02:15:10 +11:00
self.connect_manager.clearData()
self._connect_login = True
self.close()
elif control == MANUAL_SERVER:
self._manual_server = True
self.close()
elif control == CANCEL:
self.close()
def _connect_server(self, server_id):
server = self.connect_manager.getServerInfo(server_id)
self.message.setLabel("%s %s..." % (lang(30610), server['Name']))
self.message_box.setVisibleCondition('True')
self.busy.setVisibleCondition('True')
result = self.connect_manager.connectToServer(server)
if result['State'] == CONN_STATE['Unavailable']:
self.busy.setVisibleCondition('False')
self.message.setLabel(lang(30609))
return False
else:
xbmc.sleep(1000)
self._selected_server = result['Servers'][0]
return True