initial checkin Automatic Volume Adjustment Plugin
[vuplus_dvbapp-plugin] / automaticvolumeadjustment / src / setup.py
1 # -*- coding: utf-8 -*-
2 #
3 #  AutomaticVolumeAdjustment E2
4 #
5 #  $Id$
6 #
7 #  Coded by Dr.Best (c) 2009
8 #  Support: www.dreambox-tools.info
9 #
10 #  This program is free software; you can redistribute it and/or
11 #  modify it under the terms of the GNU General Public License
12 #  as published by the Free Software Foundation; either version 2
13 #  of the License, or (at your option) any later version.
14 #
15 #  This program is distributed in the hope that it will be useful,
16 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #  GNU General Public License for more details.
19 #
20
21 from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_LEFT, \
22         RT_VALIGN_CENTER
23 from Screens.Screen import Screen
24 from Screens.MessageBox import MessageBox
25 from Components.MenuList import MenuList
26 from Components.Sources.StaticText import StaticText
27 from Components.ActionMap import ActionMap
28 from Components.ConfigList import ConfigList, ConfigListScreen
29 from Components.config import ConfigSubsection, ConfigText, \
30         getConfigListEntry, config, ConfigInteger, Config, ConfigSubList, ConfigDirectory, NoSave, ConfigYesNo
31 from os import path as os_path, open as os_open, close as os_close, O_RDWR as os_O_RDWR, O_CREAT  as os_O_CREAT 
32 from Screens.ChannelSelection import SimpleChannelSelection
33 from enigma import eServiceReference
34 from ServiceReference import ServiceReference
35
36 import plugin as AutomaticVolumeAdjustmentPlugin # import to submit config-values
37         
38 class AutomaticVolumeAdjustmentConfig():
39         def __init__(self):
40                 self.CONFIG_FILE = '/usr/lib/enigma2/python/Plugins/SystemPlugins/AutomaticVolumeAdjustment/config'
41                 # load config file
42                 self.loadConfigFile()
43
44         # load config file and initialize 
45         def loadConfigFile(self):
46                 print "[AutomaticVolumeAdjustmentConfig] Loading config file..."
47                 self.config = Config()
48                 if not os_path.exists(self.CONFIG_FILE):
49                         fd = os_open( self.CONFIG_FILE, os_O_RDWR|os_O_CREAT)
50                         os_close( fd )
51                 self.config.loadFromFile(self.CONFIG_FILE)
52                 self.config.entriescount =  ConfigInteger(0)
53                 self.config.Entries = ConfigSubList()
54                 self.config.enable = ConfigYesNo(default = False)
55                 self.config.adustvalue = ConfigInteger(default=25, limits=(0,50))
56                 self.initConfig()
57
58         def initConfig(self):
59                 count = self.config.entriescount.value
60                 if count != 0:
61                         i = 0
62                         while i < count:
63                                 self.initEntryConfig()
64                                 i += 1
65                 print "[AutomaticVolumeAdjustmentConfig] Loaded %s entries from config file..." % count
66
67         def initEntryConfig(self):
68                 self.config.Entries.append(ConfigSubsection())
69                 i = len(self.config.Entries) - 1
70                 self.config.Entries[i].servicereference = ConfigText(default = "")
71                 self.config.Entries[i].name = NoSave(ConfigDirectory(default = _("Press OK to select a service")))
72                 self.config.Entries[i].adjustvalue = ConfigInteger(default=25, limits=(5,50))
73                 return self.config.Entries[i]
74         
75         def remove(self, configItem):
76                 self.config.entriescount.value = self.config.entriescount.value - 1
77                 self.config.entriescount.save()
78                 self.config.Entries.remove(configItem)
79                 self.config.Entries.save()
80                 self.save()
81         
82         def save(self):
83                 print "[AutomaticVolumeAdjustmentConfig] saving config file..."
84                 self.config.saveToFile(self.CONFIG_FILE)
85                 
86 class AutomaticVolumeAdjustmentConfigScreen(ConfigListScreen, Screen):
87         skin = """
88                 <screen name="AutomaticVolumeAdjustmentConfigScreen" position="center,center" size="550,400">
89                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
90                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
91                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
92                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
93                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
94
95                         <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" />
96                         <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" />
97                         <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" />
98                 </screen>"""
99
100         def __init__(self, session):
101                 Screen.__init__(self, session)
102                 self.title = _("Automatic Volume Adjustment - Config")
103                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
104                 {
105                         "green": self.keySave,
106                         "red": self.keyCancel,
107                         "blue": self.blue,
108                         "cancel": self.keyCancel,
109                 }, -2)
110                 self["key_red"] = StaticText(_("Cancel"))
111                 self["key_green"] = StaticText(_("OK"))
112                 self["key_blue"] = StaticText(_("Services"))
113                 self.configVA = AutomaticVolumeAdjustmentConfig()
114                 self.list = []
115                 self.list.append(getConfigListEntry(_("Enable"), self.configVA.config.enable))
116                 self.list.append(getConfigListEntry(_("Default volume adjustment value for AC3/DTS"), self.configVA.config.adustvalue))
117                 ConfigListScreen.__init__(self, self.list, session)
118                 
119         def blue(self):
120                 self.session.open(AutomaticVolumeAdjustmentEntriesListConfigScreen, self.configVA)
121
122         def keySave(self):
123                 for x in self["config"].list:
124                         x[1].save()
125                 self.configVA.save()
126                 if AutomaticVolumeAdjustmentPlugin.automaticvolumeadjustment is not None:
127                         AutomaticVolumeAdjustmentPlugin.automaticvolumeadjustment.initializeConfigValues(self.configVA, True) # submit config values
128                 self.close()
129
130         def keyCancel(self):
131                 ConfigListScreen.cancelConfirm(self, True)
132                 
133
134 class AutomaticVolumeAdjustmentEntriesListConfigScreen(Screen):
135         skin = """
136                 <screen position="center,center" size="550,400">
137                         <widget render="Label" source="name" position="5,0" size="350,50" font="Regular;20" halign="left"/>
138                         <widget render="Label" source="adjustvalue" position="355,0" size="200,50" font="Regular;20" halign="left"/>
139                         <widget name="entrylist" position="0,50" size="550,300" scrollbarMode="showOnDemand"/>
140                         <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" />
141                         <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" />
142                         <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" />
143                         <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" />
144                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
145                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
146                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
147                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
148                 </screen>"""
149
150         def __init__(self, session, configVA):
151                 Screen.__init__(self, session)
152                 self.title = _("Automatic Volume Adjustment - Service Config")
153                 self["name"] = StaticText(_("Servicename"))
154                 self["adjustvalue"] = StaticText(_("Adjustment value"))
155                 self["key_red"] = StaticText(_("Add"))
156                 self["key_green"] = StaticText(_("OK"))
157                 self["key_yellow"] = StaticText(_("Edit"))
158                 self["key_blue"] = StaticText(_("Delete"))
159                 self["entrylist"] = AutomaticVolumeAdjustmentEntryList([])
160                 self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
161                         {
162                          "ok"   :       self.keyOK,
163                          "back" :       self.keyClose,
164                          "red"  :       self.keyRed,
165                          "green":       self.keyClose,
166                          "yellow":      self.keyYellow,
167                          "blue":        self.keyDelete,
168                          }, -1)
169                 self["entrylist"].setConfig(configVA)
170                 self.updateList()
171
172         def updateList(self):
173                 self["entrylist"].buildList()
174
175         def keyClose(self):
176                 self.close(-1, None)
177
178         def keyRed(self):
179                 self.session.openWithCallback(self.updateList,AutomaticVolumeAdjustmentEntryConfigScreen,None, self["entrylist"].configVA)
180
181         def keyOK(self):
182                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
183                 except: sel = None
184                 self.close(self["entrylist"].getCurrentIndex(), sel)
185
186         def keyYellow(self):
187                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
188                 except: sel = None
189                 if sel is None:
190                         return
191                 self.session.openWithCallback(self.updateList,AutomaticVolumeAdjustmentEntryConfigScreen,sel, self["entrylist"].configVA)
192
193         def keyDelete(self):
194                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
195                 except: sel = None
196                 if sel is None:
197                         return
198                 self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Do you really want to delete this entry?"))
199
200         def deleteConfirm(self, result):
201                 if not result:
202                         return
203                 sel = self["entrylist"].l.getCurrentSelection()[0]
204                 self["entrylist"].configVA.remove(sel)
205                 if AutomaticVolumeAdjustmentPlugin.automaticvolumeadjustment is not None:
206                         AutomaticVolumeAdjustmentPlugin.automaticvolumeadjustment.initializeConfigValues(self["entrylist"].configVA, True) # submit config values
207                 self.updateList()
208
209 class AutomaticVolumeAdjustmentEntryList(MenuList):
210         def __init__(self, list, enableWrapAround = True):
211                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
212                 self.l.setFont(0, gFont("Regular", 20))
213                 self.l.setFont(1, gFont("Regular", 18))
214                 self.configVA = None
215
216         def postWidgetCreate(self, instance):
217                 MenuList.postWidgetCreate(self, instance)
218                 instance.setItemHeight(20)
219
220         def getCurrentIndex(self):
221                 return self.instance.getCurrentIndex()
222                 
223         def setConfig(self, configVA):
224                 self.configVA = configVA
225                 
226         def buildList(self):
227                 list = []
228                 for c in self.configVA.config.Entries:
229                         c.name.value = ServiceReference(eServiceReference(c.servicereference.value)).getServiceName()
230                         res = [
231                                 c,
232                                 (eListboxPythonMultiContent.TYPE_TEXT, 5, 0, 350, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, c.name.value),
233                                 (eListboxPythonMultiContent.TYPE_TEXT, 355, 0,200, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.adjustvalue.value)),
234                         ]
235                         list.append(res)
236                 self.list = list
237                 self.l.setList(list)
238                 self.moveToIndex(0)
239
240 class AutomaticVolumeAdjustmentEntryConfigScreen(ConfigListScreen, Screen):
241         skin = """
242                 <screen name="AutomaticVolumeAdjustmentEntryConfigScreen" position="center,center" size="550,400">
243                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
244                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
245                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
246                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
247                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
248
249                         <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" />
250                         <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" />
251                 </screen>"""
252
253         def __init__(self, session, entry, configVA):
254                 Screen.__init__(self, session)
255                 self.title = _("Automatic Volume Adjustment - Entry Config")
256                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
257                 {
258                         "green": self.keySave,
259                         "red": self.keyCancel,
260                         "cancel": self.keyCancel,
261                         "ok": self.keySelect,
262                 }, -2)
263                 self["key_red"] = StaticText(_("Cancel"))
264                 self["key_green"] = StaticText(_("OK"))
265                 self.configVA = configVA
266                 if entry is None:
267                         self.newmode = 1
268                         self.current = self.configVA.initEntryConfig()
269                 else:
270                         self.newmode = 0
271                         self.current = entry
272                 self.list = [ ]
273                 self.service = getConfigListEntry(_("Servicename"), self.current.name)
274                 self.list.append(self.service)
275                 self.list.append(getConfigListEntry(_("Adjustment value"), self.current.adjustvalue))
276                 ConfigListScreen.__init__(self, self.list, session)
277                 
278         def keySelect(self):
279                 cur = self["config"].getCurrent()
280                 if cur == self.service:
281                         self.session.openWithCallback(self.channelSelected, ConfigChannelSelection)
282                         
283         def channelSelected(self, ref = None):
284                 if ref:
285                         self.current.name.value = ServiceReference(ref).getServiceName()
286                         self.current.servicereference.value = ref.toString()
287                         self.current.save()
288
289         def keySave(self):
290                 if self.current.servicereference.value:
291                         if self.newmode == 1:
292                                 self.configVA.config.entriescount.value = self.configVA.config.entriescount.value + 1
293                                 self.configVA.config.entriescount.save()
294                         for x in self["config"].list:
295                                 x[1].save()
296                         self.configVA.save()
297                         if AutomaticVolumeAdjustmentPlugin.automaticvolumeadjustment is not None:
298                                 AutomaticVolumeAdjustmentPlugin.automaticvolumeadjustment.initializeConfigValues(self.configVA, True) # submit config values
299                         self.close()
300                 else:
301                         self.session.open(MessageBox, _("You must select a valid service!"), type = MessageBox.TYPE_INFO)
302
303         def keyCancel(self):
304                 if self.newmode == 1:
305                         self.configVA.remove(self.current)
306                 ConfigListScreen.cancelConfirm(self, True)
307
308 class ConfigChannelSelection(SimpleChannelSelection):
309         def __init__(self, session):
310                 SimpleChannelSelection.__init__(self, session, _("Channel Selection"))
311                 self.skinName = ["SimpleChannelSelection"]
312                 self["ChannelSelectEPGActions"] = ActionMap(["ChannelSelectEPGActions"],
313                 {
314                                 "showEPGList": self.channelSelected
315                 })
316
317         def channelSelected(self):
318                 ref = self.getCurrentSelection()
319                 if (ref.flags & 7) == 7:
320                         self.enterPath(ref)
321                 elif not (ref.flags & eServiceReference.isMarker):
322                         self.close(ref)