forked from Regss/script.audio.profiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.py
More file actions
269 lines (227 loc) · 11 KB
/
default.py
File metadata and controls
269 lines (227 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding: utf-8 -*-
import xbmc
import xbmcgui
import xbmcaddon
import xbmcvfs
import json
import os
import sys
ADDON = xbmcaddon.Addon()
ADDON_ID = ADDON.getAddonInfo('id')
ADDON_NAME = ADDON.getAddonInfo('name')
ADDON_ICON = ADDON.getAddonInfo('icon')
ADDON_PATH = xbmc.translatePath(ADDON.getAddonInfo('path'))
ADDON_PATH_DATA = xbmc.translatePath(os.path.join('special://profile/addon_data/', ADDON_ID)).replace('\\', '/') + '/'
ADDON_PATH_LIB = os.path.join(ADDON_PATH, 'resources', 'lib' )
ADDON_LANG = ADDON.getLocalizedString
sys.path.append(ADDON_PATH_LIB)
import dialog
import debug
# set vars
sName = {
1: ADDON.getSetting('name1'),
2: ADDON.getSetting('name2'),
3: ADDON.getSetting('name3'),
4: ADDON.getSetting('name4')
}
sProfile = {
1: ADDON.getSetting('profile1'),
2: ADDON.getSetting('profile2'),
3: ADDON.getSetting('profile3'),
4: ADDON.getSetting('profile4')
}
cecCommands = ['', 'CECActivateSource', 'CECStandby', 'CECToggleState']
xbmc_version = int(xbmc.getInfoLabel('System.BuildVersion')[0:2])
class PROFILES:
def __init__(self):
debug.debug('[SYS.ARGV]: ' + str(sys.argv))
debug.debug('[XBMC VERSION]: ' + str(xbmc_version))
# select xml file depending on xbmc version
version = 'old' if xbmc_version < 17 else 'new'
self.xmlFile = 'script-audio-profiles-menu-{}.xml'.format(version)
# detect mode, check args
if (len(sys.argv) < 2 or len(sys.argv[0]) == 0):
mode = False
else:
mode = str(sys.argv[1])
debug.debug('[MODE]: ' + str(mode))
self.start(mode)
def start(self, mode):
xbmcgui.Window(10000).clearProperty(ADDON_ID + '_autoclose')
# check is profiles is set
if 'true' not in sProfile.values():
debug.notify(ADDON_LANG(32105))
xbmcaddon.Addon(id=ADDON_ID).openSettings()
if mode is False:
self.save()
return
if mode == 'popup':
enabledProfiles = self.getEnabledProfiles()
xbmcgui.Window(10000).setProperty(ADDON_ID + '_autoclose', '1' if 'true' in ADDON.getSetting('player_autoclose') else '0')
ret = dialog.DIALOG().start(self.xmlFile, labels={10071: ADDON_LANG(32106)}, buttons=enabledProfiles[1], list=10070)
if ret is not None:
self.profile(str(enabledProfiles[0][ret]))
return
if mode == '0' or mode == '1' or mode == '2' or mode == '3' or mode == '4':
if self.check(mode) is False:
return
if mode == '0':
self.toggle(mode)
else:
self.profile(mode)
return
debug.error('Wrong arg, use like RunScript("' + ADDON_ID + ',x") x - number of profile')
def getEnabledProfiles(self):
enabledProfileKey = []
enabledProfileName = []
for k, p in sProfile.items():
if 'true' in p:
enabledProfileKey.append(k)
enabledProfileName.append(sName[k])
return [enabledProfileKey, enabledProfileName]
# get audio config and save to file
def save(self):
enabledProfiles = self.getEnabledProfiles()
ret = dialog.DIALOG().start(self.xmlFile, labels={10071: ADDON_LANG(32100)}, buttons=enabledProfiles[1], list=10070)
if ret is None:
return False
else:
button = enabledProfiles[0][ret]
settingsToSave = {}
if xbmc_version < 17:
json_s = [
# get all settings from System / Audio section
'{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"system","category":"audiooutput"}},"id":1}',
# get volume level
'{"jsonrpc": "2.0", "method": "Application.GetProperties", "params": {"properties": ["volume"]}, "id": 1}',
# get all settings from Video / Playback section
'{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"videos","category":"videoplayer"}}, "id":1}',
# get all settings from System / Video section
'{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"system","category":"videoscreen"}}, "id":1}'
]
else:
json_s = [
# get all settings from System / Audio section
'{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"system","category":"audio"}},"id":1}',
# get volume level
'{"jsonrpc": "2.0", "method": "Application.GetProperties", "params": {"properties": ["volume"]}, "id": 1}',
# get all settings from Video / Playback section
'{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"player","category":"videoplayer"}}, "id":1}',
# get all settings from System / Video section
'{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"system","category":"display"}}, "id":1}'
]
# send json requests
for j in json_s:
jsonGet = xbmc.executeJSONRPC(j)
jsonGet = json.loads(unicode(jsonGet, 'utf-8'))
debug.debug('[JSON]: ' + str(jsonGet))
if 'result' in jsonGet:
if 'settings' in jsonGet['result']:
for set in jsonGet['result']['settings']:
if 'value' in set.keys():
if set['value'] == True or set['value'] == False: # lowercase bolean values
settingsToSave[set['id']] = str(set['value']).lower()
else:
if type(set['value']) is int:
settingsToSave[set['id']] = str(set['value'])
else:
settingsToSave[set['id']] = str(set['value']).encode('utf-8')
if 'volume' in jsonGet['result']:
settingsToSave['volume'] = str(jsonGet['result']['volume'])
# prepare JSON string to save to file
jsonToWrite = json.dumps(settingsToSave)
# create dir in addon data if not exist
if not xbmcvfs.exists(ADDON_PATH_DATA):
xbmcvfs.mkdir(ADDON_PATH_DATA)
# save profile file
debug.notice('[SAVING SETTING]: ' + sName[button])
f = xbmcvfs.File(ADDON_PATH_DATA + 'profile' + str(button) + '.json', 'w')
result = f.write(jsonToWrite)
f.close()
debug.notify(ADDON_LANG(32102) + ' ' + str(button) + ' (' + sName[button] + ')')
def check(self, mode):
# check profile config
self.aProfile = []
# stop if selected (mode) profile are disabled
if mode != '0' and 'false' in sProfile[int(mode)]:
debug.notify(ADDON_LANG(32103) + ' (' + sName[int(mode)] + ')')
debug.notice('[CHECK]: This profile is disabled in addon settings - ' + str(mode))
return False
# check if profile have settings file
for key in sProfile:
if 'true' in sProfile[key]:
if not xbmcvfs.exists(ADDON_PATH_DATA + 'profile' + str(key) + '.json'):
debug.notify(ADDON_LANG(32101) + ' ' + str(key) + ' (' + sName[key] + ')')
debug.error('[PROFILE FILE]: not exist for profile - ' + str(key))
return False
self.aProfile.append(str(key))
def toggle(self, mode):
# create profile file
if not xbmcvfs.exists(ADDON_PATH_DATA):
xbmcvfs.mkdir(ADDON_PATH_DATA)
# try read last active profile
try:
f = xbmcvfs.File(ADDON_PATH_DATA + 'profile')
profile = f.read()
f.close()
if (len(self.aProfile) == 1) or (profile not in self.aProfile):
profile = self.aProfile[0]
else:
ip = int(self.aProfile.index(profile))
if len(self.aProfile) == ip:
profile == self.aProfile[0]
else:
profile = self.aProfile[ip+1]
except:
profile = self.aProfile[0]
self.profile(profile)
def profile(self, profile):
# read addon settings
sVolume = ADDON.getSetting('volume')
sPlayer = ADDON.getSetting('player')
sVideo = ADDON.getSetting('video')
sCec = ADDON.getSetting('profile' + profile + '_cec')
# read settings from profile
f = xbmcvfs.File(ADDON_PATH_DATA + 'profile' + profile + '.json', 'r')
result = f.read()
try:
jsonResult = json.loads(result)
f.close()
except:
debug.notify(ADDON_LANG(32104) + ' ' + profile + ' (' + sName[int(profile)] + ')')
debug.error('[LOAD JSON FROM FILE]: Error reading from profile - ' + str(profile))
return False
# settings needed quote for value
quote_needed = [
'audiooutput.audiodevice',
'audiooutput.passthroughdevice',
'locale.audiolanguage',
'lookandfeel.soundskin'
]
# set settings readed from profile file
debug.notice('[RESTORING SETTING]: ' + sName[int(profile)])
for setName, setValue in jsonResult.items():
# skip setting that type is disable to changing
if 'false' in sPlayer and setName.startswith('videoplayer'):
continue
if 'false' in sVideo and setName.startswith('videoscreen'):
continue
debug.debug('[RESTORING SETTING]: ' + setName + ': ' + setValue)
# add quotes
if setName in quote_needed:
setValue = '"' + setValue + '"'
# set setting
if 'true' in sVolume and setName == 'volume':
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "Application.SetVolume", "params": {"volume": ' + jsonResult['volume'] + '}, "id": 1}')
else:
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "Settings.SetSettingValue", "params": {"setting": "' + setName + '", "value": ' + setValue.encode('utf-8') + '}, "id": 1}')
debug.notify(sName[int(profile)].decode('utf-8'))
# write curent profile
f = xbmcvfs.File(ADDON_PATH_DATA + 'profile', 'w')
f.write(profile)
f.close()
# CEC
if sCec != '' and int(sCec) > 0:
debug.notice('[SENDING CEC COMMAND]: ' + cecCommands[int(sCec)])
xbmc.executebuiltin(cecCommands[int(sCec)])
PROFILES()