fix for Attribute Error in Setup
[enigma2-plugins.git] / automaticvolumeadjustment / src / AutomaticVolumeAdjustmentSetup.py
1 # -*- coding: utf-8 -*-
2 #
3 #  AutomaticVolumeAdjustment E2
4 #
5 #  $Id$
6 #
7 #  Coded by Dr.Best (c) 2010
8 #  Support: www.dreambox-tools.info
9 #
10 #  This plugin is licensed under the Creative Commons 
11 #  Attribution-NonCommercial-ShareAlike 3.0 Unported 
12 #  License. To view a copy of this license, visit
13 #  http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative
14 #  Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
15 #
16 #  Alternatively, this plugin may be distributed and executed on hardware which
17 #  is licensed by Dream Multimedia GmbH.
18
19 #  This plugin is NOT free software. It is open source, you are allowed to
20 #  modify it (if you keep the license), but it may not be commercially 
21 #  distributed other than under the conditions noted above.
22 #
23 from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_LEFT, RT_VALIGN_CENTER, eServiceReference
24 from Screens.Screen import Screen
25 from Screens.MessageBox import MessageBox
26 from Screens.ChannelSelection import SimpleChannelSelection
27 from Components.MenuList import MenuList
28 from Components.Sources.StaticText import StaticText
29 from Components.ActionMap import ActionMap
30 from Components.ConfigList import ConfigListScreen
31 from Components.config import getConfigListEntry, config
32 from ServiceReference import ServiceReference
33 from AutomaticVolumeAdjustment import AutomaticVolumeAdjustment
34 from AutomaticVolumeAdjustmentConfig import AutomaticVolumeAdjustmentConfig
35
36                 
37 class AutomaticVolumeAdjustmentConfigScreen(ConfigListScreen, Screen):
38         skin = """
39                 <screen name="AutomaticVolumeAdjustmentConfigScreen" position="center,center" size="550,400">
40                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
41                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
42                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
43                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
44                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
45
46                         <widget source="key_red" render="Label" position="0,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
47                         <widget source="key_green" render="Label" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
48                         <widget render="Label" source="key_blue" position="420,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
49                 </screen>"""
50
51         def __init__(self, session):
52                 Screen.__init__(self, session)
53                 self.title = _("Automatic Volume Adjustment - Config")
54                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
55                 {
56                         "green": self.keySave,
57                         "red": self.keyCancel,
58                         "blue": self.blue,
59                         "cancel": self.keyCancel,
60                 }, -2)
61                 self["key_red"] = StaticText(_("Cancel"))
62                 self["key_green"] = StaticText(_("OK"))
63                 self["key_blue"] = StaticText()
64                 self.configVA = AutomaticVolumeAdjustmentConfig()
65                 self.automaticVolumeAdjustmentInstance = AutomaticVolumeAdjustment.instance
66                 self.list = []
67                 ConfigListScreen.__init__(self, self.list, session = session)
68                 self.createSetup("config")
69                 
70         def createSetup(self, widget):
71                 self.list = []
72                 self.config_enable = getConfigListEntry(_("Enable"), self.configVA.config.enable)
73                 self.list.append(self.config_enable)
74                 if self.configVA.config.enable.value:
75                         self.config_modus = getConfigListEntry(_("Modus"), self.configVA.config.modus)
76                         self.list.append(self.config_modus)
77                         if self.configVA.config.modus.value == "0":
78                                 self.list.append(getConfigListEntry(_("Default volume adjustment value for AC3/DTS"), self.configVA.config.adustvalue))
79                                 self.list.append(getConfigListEntry(_("Max. volume for mpeg audio"), self.configVA.config.mpeg_max_volume))
80                                 self["key_blue"].text = _("Services")
81                         else:
82                                 self["key_blue"].text = ""
83                         self.list.append(getConfigListEntry(_("Show volumebar when volume-value was changed"), self.configVA.config.show_volumebar))
84                 else:
85                         self.config_modus = None
86                 self[widget].list = self.list
87                 self[widget].l.setList(self.list)
88         
89         def newConfig(self):
90                 if self["config"].getCurrent() in (self.config_enable, self.config_modus):
91                         self.createSetup("config")
92
93         def keyLeft(self):
94                         ConfigListScreen.keyLeft(self)
95                         self.newConfig()
96
97         def keyRight(self):
98                         ConfigListScreen.keyRight(self)
99                         self.newConfig()
100                 
101         def blue(self):
102                 if self.configVA.config.modus.value == "0":
103                         self.session.open(AutomaticVolumeAdjustmentEntriesListConfigScreen, self.configVA)
104
105         def keySave(self):
106                 for x in self["config"].list:
107                         x[1].save()
108                 self.configVA.save()
109                 if self.automaticVolumeAdjustmentInstance is not None:
110                         self.automaticVolumeAdjustmentInstance.initializeConfigValues(self.configVA, True) # submit config values
111                 self.close()
112
113         def keyCancel(self):
114                 ConfigListScreen.cancelConfirm(self, True)
115                 
116
117 class AutomaticVolumeAdjustmentEntriesListConfigScreen(Screen):
118         skin = """
119                 <screen position="center,center" size="550,400">
120                         <widget render="Label" source="name" position="5,0" size="350,50" font="Regular;20" halign="left"/>
121                         <widget render="Label" source="adjustvalue" position="355,0" size="200,50" font="Regular;20" halign="left"/>
122                         <widget name="entrylist" position="0,50" size="550,300" scrollbarMode="showOnDemand"/>
123                         <widget render="Label" source="key_red" position="0,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
124                         <widget source="key_green" render="Label" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
125                         <widget render="Label" source="key_yellow" position="280,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="yellow" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
126                         <widget render="Label" source="key_blue" position="420,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
127                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
128                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
129                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
130                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
131                 </screen>"""
132
133         def __init__(self, session, configVA):
134                 Screen.__init__(self, session)
135                 self.title = _("Automatic Volume Adjustment - Service Config")
136                 self["name"] = StaticText(_("Servicename"))
137                 self["adjustvalue"] = StaticText(_("Adjustment value"))
138                 self["key_red"] = StaticText(_("Add"))
139                 self["key_green"] = StaticText(_("OK"))
140                 self["key_yellow"] = StaticText(_("Edit"))
141                 self["key_blue"] = StaticText(_("Delete"))
142                 self["entrylist"] = AutomaticVolumeAdjustmentEntryList([])
143                 self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
144                         {
145                          "ok"   :       self.keyOK,
146                          "back" :       self.keyClose,
147                          "red"  :       self.keyRed,
148                          "green":       self.keyClose,
149                          "yellow":      self.keyYellow,
150                          "blue":        self.keyDelete,
151                          }, -1)
152                 self.automaticVolumeAdjustmentInstance = AutomaticVolumeAdjustment.instance
153                 self["entrylist"].setConfig(configVA)
154                 self.updateList()
155
156         def updateList(self):
157                 self["entrylist"].buildList()
158
159         def keyClose(self):
160                 self.close(-1, None)
161
162         def keyRed(self):
163                 self.session.openWithCallback(self.updateList,AutomaticVolumeAdjustmentEntryConfigScreen,None, self["entrylist"].configVA)
164
165         def keyOK(self):
166                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
167                 except: sel = None
168                 self.close(self["entrylist"].getCurrentIndex(), sel)
169
170         def keyYellow(self):
171                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
172                 except: sel = None
173                 if sel is None:
174                         return
175                 self.session.openWithCallback(self.updateList,AutomaticVolumeAdjustmentEntryConfigScreen,sel, self["entrylist"].configVA)
176
177         def keyDelete(self):
178                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
179                 except: sel = None
180                 if sel is None:
181                         return
182                 self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Do you really want to delete this entry?"))
183
184         def deleteConfirm(self, result):
185                 if not result:
186                         return
187                 sel = self["entrylist"].l.getCurrentSelection()[0]
188                 self["entrylist"].configVA.remove(sel)
189                 if self.automaticVolumeAdjustmentInstance is not None:
190                         self.automaticVolumeAdjustmentInstance.initializeConfigValues(self["entrylist"].configVA, True) # submit config values
191                 self.updateList()
192
193 class AutomaticVolumeAdjustmentEntryList(MenuList):
194         def __init__(self, list, enableWrapAround = True):
195                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
196                 self.l.setFont(0, gFont("Regular", 20))
197                 self.l.setFont(1, gFont("Regular", 18))
198                 self.configVA = None
199
200         def postWidgetCreate(self, instance):
201                 MenuList.postWidgetCreate(self, instance)
202                 instance.setItemHeight(20)
203
204         def getCurrentIndex(self):
205                 return self.instance.getCurrentIndex()
206                 
207         def setConfig(self, configVA):
208                 self.configVA = configVA
209                 
210         def buildList(self):
211                 list = []
212                 for c in self.configVA.config.Entries:
213                         c.name.value = ServiceReference(eServiceReference(c.servicereference.value)).getServiceName()
214                         res = [
215                                 c,
216                                 (eListboxPythonMultiContent.TYPE_TEXT, 5, 0, 350, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, c.name.value),
217                                 (eListboxPythonMultiContent.TYPE_TEXT, 355, 0,200, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.adjustvalue.value)),
218                         ]
219                         list.append(res)
220                 self.list = list
221                 self.l.setList(list)
222                 self.moveToIndex(0)
223
224 class AutomaticVolumeAdjustmentEntryConfigScreen(ConfigListScreen, Screen):
225         skin = """
226                 <screen name="AutomaticVolumeAdjustmentEntryConfigScreen" position="center,center" size="550,400">
227                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
228                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
229                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
230                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
231                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
232
233                         <widget source="key_red" render="Label" position="0,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
234                         <widget source="key_green" render="Label" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
235                 </screen>"""
236
237         def __init__(self, session, entry, configVA):   
238                 self.session = session
239                 Screen.__init__(self, session)
240                 self.title = _("Automatic Volume Adjustment - Entry Config")
241                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
242                 {
243                         "green": self.keySave,
244                         "red": self.keyCancel,
245                         "cancel": self.keyCancel,
246                         "ok": self.keySelect,
247                 }, -2)
248                 self["key_red"] = StaticText(_("Cancel"))
249                 self["key_green"] = StaticText(_("OK"))
250                 self.configVA = configVA
251                 if entry is None:
252                         self.newmode = 1
253                         self.current = self.configVA.initEntryConfig()
254                         self.currentvalue = self.current.adjustvalue.value
255                 else:
256                         self.newmode = 0
257                         self.current = entry
258                         self.currentref = entry.servicereference.value
259                         self.currentvalue = entry.adjustvalue.value
260                 self.list = [ ]
261                 self.service = getConfigListEntry(_("Servicename"), self.current.name)
262                 self.list.append(self.service)
263                 self.adjustValue = getConfigListEntry(_("Adjustment value"), self.current.adjustvalue)
264                 self.list.append(self.adjustValue)
265                 ConfigListScreen.__init__(self, self.list, session)
266                 self.automaticVolumeAdjustmentInstance = AutomaticVolumeAdjustment.instance
267                 
268         def keySelect(self):
269                 cur = self["config"].getCurrent()
270                 if cur == self.service:
271                         self.session.openWithCallback(self.channelSelected, SimpleChannelSelection, _("Channel Selection"))
272                         
273         def channelSelected(self, ref = None):
274                 if ref:
275                         self.current.name.value = ServiceReference(ref).getServiceName()
276                         self.current.servicereference.value = ref.toString()
277                         self.current.save()
278
279         def keySave(self):
280                 if self.current.servicereference.value:
281                         if self.newmode == 1:
282                                 self.configVA.config.entriescount.value = self.configVA.config.entriescount.value + 1
283                                 self.configVA.config.entriescount.save()
284                         for x in self["config"].list:
285                                 x[1].save()
286                         self.configVA.save()
287                         if self.automaticVolumeAdjustmentInstance is not None:
288                                 self.automaticVolumeAdjustmentInstance.initializeConfigValues(self.configVA, True) # submit config values
289                         self.close()
290                 else:
291                         self.session.open(MessageBox, _("You must select a valid service!"), type = MessageBox.TYPE_INFO)
292
293         def keyCancel(self):
294                 if self.newmode == 1:
295                         self.configVA.config.Entries.remove(self.current)
296                         self.configVA.config.Entries.save()
297                 else:
298                         self.current.servicereference.value = self.currentref
299                         self.current.adjustvalue.value = self.currentvalue
300                         self.current.save()
301                 ConfigListScreen.cancelConfirm(self, True)