2015-07-22 23:16:08 +10:00
# -*- coding: utf-8 -*-
2015-03-14 08:24:59 +11:00
#################################################################################################
# connection manager class
#################################################################################################
2015-07-22 23:16:08 +10:00
import json
import socket
2015-03-14 08:24:59 +11:00
import xbmc
import xbmcgui
import xbmcaddon
2015-04-22 12:31:16 +10:00
import Utils as utils
2015-07-22 23:16:08 +10:00
from ClientInformation import ClientInformation
2015-04-03 21:13:01 +11:00
from DownloadUtils import DownloadUtils
from UserClient import UserClient
2015-03-14 08:24:59 +11:00
class ConnectionManager ( ) :
2015-04-03 21:13:01 +11:00
clientInfo = ClientInformation ( )
2015-07-22 23:16:08 +10:00
user = UserClient ( )
2015-04-03 21:13:01 +11:00
doUtils = DownloadUtils ( )
2015-04-22 12:31:16 +10:00
addonName = clientInfo . getAddonName ( )
addonId = clientInfo . getAddonId ( )
2015-07-22 23:16:08 +10:00
addon = xbmcaddon . Addon ( )
2015-04-22 12:20:02 +10:00
WINDOW = xbmcgui . Window ( 10000 )
2015-04-03 21:13:01 +11:00
2015-04-22 12:31:16 +10:00
def __init__ ( self ) :
2015-04-22 12:17:58 +10:00
2015-04-22 12:31:16 +10:00
self . __language__ = self . addon . getLocalizedString
2015-04-22 12:20:02 +10:00
2015-04-22 12:31:16 +10:00
def logMsg ( self , msg , lvl = 1 ) :
2015-07-22 23:16:08 +10:00
className = self . __class__ . __name__
utils . logMsg ( " %s %s " % ( self . addonName , className ) , msg , int ( lvl ) )
2015-03-14 08:24:59 +11:00
def checkServer ( self ) :
2015-04-03 21:13:01 +11:00
self . WINDOW . setProperty ( " Server_Checked " , " True " )
2015-04-22 12:31:16 +10:00
self . logMsg ( " Connection Manager Called " , 2 )
2015-03-14 08:24:59 +11:00
2015-07-22 23:16:08 +10:00
server = self . user . getServer ( )
2015-04-03 21:13:01 +11:00
2015-07-22 23:16:08 +10:00
if server != " " :
2015-04-03 21:13:01 +11:00
self . logMsg ( " Server already set " , 2 )
2015-03-14 08:24:59 +11:00
return
serverInfo = self . getServerDetails ( )
2015-07-22 23:16:08 +10:00
try :
prefix , ip , port = serverInfo . split ( " : " )
setServer = xbmcgui . Dialog ( ) . yesno ( self . __language__ ( 30167 ) , " Proceed with the following server? " , self . __language__ ( 30169 ) + serverInfo )
except : # serverInfo is None
2015-04-03 21:13:01 +11:00
self . logMsg ( " getServerDetails failed " , 1 )
2015-04-03 23:36:41 +11:00
xbmc . executebuiltin ( ' Addon.OpenSettings( %s ) ' % self . addonId )
2015-03-14 08:24:59 +11:00
return
2015-07-22 23:16:08 +10:00
if setServer == 1 :
2015-04-03 21:13:01 +11:00
self . logMsg ( " Server selected. Saving information. " , 1 )
2015-08-14 19:03:12 +10:00
utils . settings ( " ipaddress " , ip . replace ( " / " , " " ) )
utils . settings ( " port " , port )
2015-07-22 23:16:08 +10:00
# If https, enable the setting
2015-04-03 21:13:01 +11:00
if ( prefix == ' https ' ) :
2015-08-14 19:03:12 +10:00
utils . settings ( ' https ' , " true " )
2015-04-03 21:13:01 +11:00
else :
self . logMsg ( " No server selected. " , 1 )
xbmc . executebuiltin ( ' Addon.OpenSettings( %s ) ' % self . addonId )
return
# Get List of public users
self . logMsg ( " Getting user list " , 1 )
2015-07-22 23:16:08 +10:00
server = " %s : %s " % ( ip . replace ( " / " , " " ) , port )
2015-04-22 12:31:16 +10:00
url = " %s /mediabrowser/Users/Public?format=json " % serverInfo
2015-04-03 21:13:01 +11:00
2015-07-22 23:16:08 +10:00
result = self . doUtils . downloadUrl ( url , authenticate = False )
if result == " " :
self . logMsg ( " Unable to connect to %s . " % server , 1 )
2015-03-14 08:24:59 +11:00
return
2015-04-22 12:31:16 +10:00
2015-07-22 23:16:08 +10:00
self . logMsg ( " Result: %s " % result , 2 )
# Process the list returned
2015-03-14 08:24:59 +11:00
names = [ ]
userList = [ ]
for user in result :
2015-07-22 23:16:08 +10:00
name = user [ ' Name ' ]
2015-03-14 08:24:59 +11:00
userList . append ( name )
2015-04-22 12:31:16 +10:00
2015-07-22 23:16:08 +10:00
if user [ ' HasPassword ' ] :
name = " %s (Secure) " % name
2015-03-14 08:24:59 +11:00
names . append ( name )
2015-07-22 23:16:08 +10:00
self . logMsg ( " User list: %s " % names , 1 )
resp = xbmcgui . Dialog ( ) . select ( self . __language__ ( 30200 ) , names )
if resp > - 1 :
selected_user = userList [ resp ]
2015-04-22 12:31:16 +10:00
self . logMsg ( " Selected User: %s " % selected_user , 1 )
2015-08-14 19:03:12 +10:00
utils . settings ( " username " , selected_user )
2015-03-27 20:58:05 +11:00
else :
2015-04-22 12:31:16 +10:00
self . logMsg ( " No user selected. " , 1 )
2015-04-03 21:13:01 +11:00
xbmc . executebuiltin ( ' Addon.OpenSettings( %s ) ' % self . addonId )
2015-03-27 20:58:05 +11:00
return
2015-08-24 21:44:23 +10:00
musicEnabled = xbmcgui . Dialog ( ) . yesno ( " Music Setting " , " Enable music library? " )
if musicEnabled :
self . logMsg ( " User opted to enable music library. " , 1 )
utils . settings ( ' enableMusicSync ' , " true " )
musicPath = xbmcgui . Dialog ( ) . yesno ( " Music Setting " , " Direct stream the music library? " , " Select this option only if you plan on listening to music outside your network. " )
if musicPath :
self . logMsg ( " User option to direct stream music library. " , 1 )
utils . settings ( ' directstreammusic ' , " true " )
return
2015-03-14 08:24:59 +11:00
def getServerDetails ( self ) :
2015-04-03 21:13:01 +11:00
self . logMsg ( " Getting Server Details from Network " )
2015-03-14 08:24:59 +11:00
MULTI_GROUP = ( " <broadcast> " , 7359 )
2015-04-03 21:13:01 +11:00
MESSAGE = " who is EmbyServer? "
2015-03-14 08:24:59 +11:00
sock = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM )
sock . settimeout ( 6.0 )
2015-04-03 21:13:01 +11:00
2015-03-14 08:24:59 +11:00
sock . setsockopt ( socket . IPPROTO_IP , socket . IP_MULTICAST_TTL , 20 )
sock . setsockopt ( socket . SOL_SOCKET , socket . SO_BROADCAST , 1 )
sock . setsockopt ( socket . SOL_IP , socket . IP_MULTICAST_LOOP , 1 )
sock . setsockopt ( socket . IPPROTO_IP , socket . SO_REUSEADDR , 1 )
2015-04-22 12:31:16 +10:00
self . logMsg ( " MutliGroup : %s " % str ( MULTI_GROUP ) , 2 ) ;
self . logMsg ( " Sending UDP Data: %s " % MESSAGE , 2 ) ;
2015-03-14 08:24:59 +11:00
sock . sendto ( MESSAGE , MULTI_GROUP )
try :
data , addr = sock . recvfrom ( 1024 ) # buffer size is 1024 bytes
2015-04-03 21:13:01 +11:00
self . logMsg ( " Received Response: %s " % data )
# Get the address
data = json . loads ( data )
return data [ ' Address ' ]
2015-03-14 08:24:59 +11:00
except :
2015-04-03 21:13:01 +11:00
self . logMsg ( " No UDP Response " )
2015-03-14 08:24:59 +11:00
pass
2015-04-03 21:13:01 +11:00
return None