2014-10-31 17:40:47 +01:00
|
|
|
import utils as utils
|
|
|
|
from xml.dom import minidom
|
|
|
|
from xml.parsers.expat import ExpatError
|
|
|
|
import json
|
|
|
|
import xbmc,xbmcvfs
|
|
|
|
|
|
|
|
|
|
|
|
class GuiSettingsManager:
|
2014-11-10 19:33:55 +01:00
|
|
|
settingsFile = None
|
2014-10-31 17:40:47 +01:00
|
|
|
doc = None
|
|
|
|
|
|
|
|
def __init__(self,settingsFile):
|
|
|
|
self._readFile(xbmc.translatePath(settingsFile))
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
#get a list of all the settings we can manipulate via json
|
|
|
|
json_response = json.loads(xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.GetSettings","params":{"level":"advanced"}}'))
|
|
|
|
|
|
|
|
settings = json_response['result']['settings']
|
2019-09-10 22:36:52 +02:00
|
|
|
settingsAllowed = []
|
|
|
|
|
2014-10-31 17:40:47 +01:00
|
|
|
for aSetting in settings:
|
2019-09-10 22:36:52 +02:00
|
|
|
settingsAllowed.append(aSetting['id'])
|
2014-10-31 17:40:47 +01:00
|
|
|
|
2019-09-10 22:36:52 +02:00
|
|
|
#parse the existing xml file and get all the settings we need to update
|
|
|
|
updateSettings = self.__parseNodes(self.doc.getElementsByTagName('setting'))
|
2014-10-31 17:40:47 +01:00
|
|
|
|
|
|
|
#go through all the found settings and update them
|
2019-09-10 22:36:52 +02:00
|
|
|
for aSetting in updateSettings:
|
|
|
|
if(aSetting.name in settingsAllowed):
|
|
|
|
utils.log("updating: " + aSetting.name + ", value: " + aSetting.value)
|
2014-10-31 17:40:47 +01:00
|
|
|
|
2019-09-10 22:36:52 +02:00
|
|
|
#check for boolean and numeric values
|
|
|
|
if(aSetting.value.isdigit() or (aSetting.value == 'true' or aSetting.value == 'false')):
|
|
|
|
xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.SetSettingValue","params":{"setting":"' + aSetting.name + '","value":' + aSetting.value + '}}')
|
|
|
|
else:
|
|
|
|
xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.SetSettingValue","params":{"setting":"' + aSetting.name + '","value":"' + utils.encode(aSetting.value) + '"}}')
|
2014-11-10 19:33:55 +01:00
|
|
|
|
|
|
|
#make a copy of the guisettings file to make user based restores easier
|
|
|
|
xbmcvfs.copy(self.settingsFile, xbmc.translatePath("special://home/userdata/guisettings.xml.restored"))
|
2014-10-31 17:40:47 +01:00
|
|
|
|
|
|
|
def __parseNodes(self,nodeList):
|
|
|
|
result = []
|
|
|
|
|
2019-09-10 22:36:52 +02:00
|
|
|
for node in nodeList:
|
|
|
|
#only add if it's not a default setting
|
|
|
|
if('default' not in node.attributes.keys()):
|
|
|
|
aSetting = SettingNode(node.getAttribute('id'),node.firstChild.nodeValue)
|
|
|
|
aSetting.isDefault = False
|
2014-10-31 17:40:47 +01:00
|
|
|
|
|
|
|
result.append(aSetting)
|
2019-09-10 22:36:52 +02:00
|
|
|
|
2014-10-31 17:40:47 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
def _readFile(self,fileLoc):
|
|
|
|
|
|
|
|
if(xbmcvfs.exists(fileLoc)):
|
|
|
|
try:
|
|
|
|
self.doc = minidom.parse(fileLoc)
|
2014-11-10 19:33:55 +01:00
|
|
|
self.settingsFile = fileLoc
|
2014-10-31 17:40:47 +01:00
|
|
|
except ExpatError:
|
|
|
|
utils.log("Can't read " + fileLoc)
|
|
|
|
|
|
|
|
class SettingNode:
|
|
|
|
name = ''
|
|
|
|
value = ''
|
|
|
|
isDefault = True
|
|
|
|
|
2019-09-10 22:36:52 +02:00
|
|
|
def __init__(self,name,value):
|
2014-10-31 17:40:47 +01:00
|
|
|
self.name = name
|
2019-09-10 22:36:52 +02:00
|
|
|
self.value= value
|
2014-10-31 17:40:47 +01:00
|
|
|
|
2019-09-10 22:36:52 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "%s : %s" % (self.name,self.value)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.__str__()
|
2014-10-31 17:40:47 +01:00
|
|
|
|
2017-01-23 18:34:35 +01:00
|
|
|
|