Skin-Components support for automaticvolumeadjustment, merlinmusicplayer, partnerbox...
[enigma2-plugins.git] / partnerbox / src / PartnerboxSetup.py
1 #
2 #  Partnerbox E2
3 #
4 #  $Id$
5 #
6 #  Coded by Dr.Best (c) 2009
7 #  Support: www.dreambox-tools.info
8 #
9 #  This program is free software; you can redistribute it and/or
10 #  modify it under the terms of the GNU General Public License
11 #  as published by the Free Software Foundation; either version 2
12 #  of the License, or (at your option) any later version.
13 #
14 #  This program is distributed in the hope that it will be useful,
15 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #  GNU General Public License for more details.
18 #
19
20 from enigma import eListboxPythonMultiContent, eListbox, gFont, \
21         RT_HALIGN_LEFT, RT_VALIGN_CENTER
22 from Screens.Screen import Screen
23 from Screens.MessageBox import MessageBox
24 from Components.MenuList import MenuList
25 from Components.Button import Button
26 from Components.config import config
27 from Components.ActionMap import ActionMap, NumberActionMap
28 from Components.ConfigList import ConfigList, ConfigListScreen
29 from Components.config import ConfigSubsection, ConfigSubList, ConfigIP, ConfigInteger, ConfigSelection, ConfigText, ConfigYesNo, getConfigListEntry, configfile
30 from skin import TemplatedListFonts, componentSizes
31
32 # for localized messages
33 from . import _
34
35 def initPartnerboxEntryConfig():
36         config.plugins.Partnerbox.Entries.append(ConfigSubsection())
37         i = len(config.plugins.Partnerbox.Entries) -1
38         config.plugins.Partnerbox.Entries[i].name = ConfigText(default = "dreambox", visible_width = 50, fixed_size = False)
39         config.plugins.Partnerbox.Entries[i].ip = ConfigIP(default = [192,168,0,98])
40         config.plugins.Partnerbox.Entries[i].port = ConfigInteger(default=80, limits=(1, 65555))
41         config.plugins.Partnerbox.Entries[i].enigma = ConfigSelection(default="0", choices = [("0", _("Enigma 2")),("1", _("Enigma 1"))])
42         config.plugins.Partnerbox.Entries[i].password = ConfigText(default = "dreambox", visible_width = 50, fixed_size = False)
43         config.plugins.Partnerbox.Entries[i].useinternal = ConfigSelection(default="1", choices = [("0", _("use external")),("1", _("use internal"))])
44         config.plugins.Partnerbox.Entries[i].zaptoservicewhenstreaming = ConfigYesNo(default = True)
45         return config.plugins.Partnerbox.Entries[i]
46
47 def initConfig():
48         count = config.plugins.Partnerbox.entriescount.value
49         if count != 0:
50                 i = 0
51                 while i < count:
52                         initPartnerboxEntryConfig()
53                         i += 1
54
55 class PartnerboxSetup(ConfigListScreen, Screen):
56         skin = """
57                 <screen position="center,center" size="550,400" title="Partnerbox Setup" >
58                         <widget name="config" position="20,10" size="510,330" scrollbarMode="showOnDemand" />
59                         <widget name="key_red" position="0,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
60                         <widget name="key_green" position="140,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
61                         <widget name="key_yellow" position="280,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
62                         <ePixmap name="red" pixmap="skin_default/buttons/red.png" position="0,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
63                         <ePixmap name="green" pixmap="skin_default/buttons/green.png" position="140,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
64                         <ePixmap name="yellow" pixmap="skin_default/buttons/yellow.png" position="280,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
65                 </screen>"""
66
67         def __init__(self, session, args = None):
68                 Screen.__init__(self, session)
69
70                 self["key_red"] = Button(_("Cancel"))
71                 self["key_green"] = Button(_("OK"))
72                 self["key_yellow"] = Button(_("Partnerbox Entries"))
73
74
75                 self.list = [ ]
76                 self.list.append(getConfigListEntry(_("Show 'RemoteTimer' in E-Menu"), config.plugins.Partnerbox.showremotetimerinextensionsmenu))
77                 self.list.append(getConfigListEntry(_("Show 'RemoteTV Player' in E-Menu"), config.plugins.Partnerbox.showremotetvinextensionsmenu))
78                 self.list.append(getConfigListEntry(_("Show 'Stream current Service' in E-Menu"), config.plugins.Partnerbox.showcurrentstreaminextensionsmenu))
79                 self.list.append(getConfigListEntry(_("Enable Partnerbox-Function in TimerEvent"), config.plugins.Partnerbox.enablepartnerboxintimerevent))
80                 self.list.append(getConfigListEntry(_("Enable Partnerbox-Function in EPGList"), config.plugins.Partnerbox.enablepartnerboxepglist))
81                 self.list.append(getConfigListEntry(_("Enable first Partnerbox-entry in Timeredit as default"), config.plugins.Partnerbox.enabledefaultpartnerboxintimeredit))
82                 ConfigListScreen.__init__(self, self.list, session)
83                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
84                 {
85                         "green": self.keySave,
86                         "cancel": self.keyClose,
87                         "ok": self.keySave,
88                         "yellow": self.PartnerboxEntries,
89                 }, -2)
90
91         def keySave(self):
92                 for x in self["config"].list:
93                         x[1].save()
94                 configfile.save()
95                 self.close(self.session, True)
96
97         def keyClose(self):
98                 for x in self["config"].list:
99                         x[1].cancel()
100                 self.close(self.session, False)
101
102         def PartnerboxEntries(self):
103                 self.session.open(PartnerboxEntriesListConfigScreen)
104
105
106 class PartnerboxEntriesListConfigScreen(Screen):
107         skin = """
108                 <screen position="center,center" size="550,400" title="%s" >
109                         <widget name="name" position="5,0" size="200,50" font="Regular;20" halign="left"/>
110                         <widget name="ip" position="215,0" size="140,50" font="Regular;20" halign="left"/>
111                         <widget name="port" position="350,0" size="80,50" font="Regular;20" halign="left"/>
112                         <widget name="type" position="430,0" size="120,50" font="Regular;20" halign="left"/>
113                         <widget name="entrylist" position="0,50" size="550,300" scrollbarMode="showOnDemand"/>
114
115                         <widget name="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" />
116                         <widget name="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" />
117                         <widget name="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" />
118                         <ePixmap name="red" position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
119                         <ePixmap name="yellow" position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
120                         <ePixmap name="blue" position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
121                 </screen>""" % _("Partnerbox: List of Entries")
122
123         def __init__(self, session, what = None):
124                 Screen.__init__(self, session)
125                 self.session = session
126                 self["name"] = Button(_("Name"))
127                 self["ip"] = Button(_("IP"))
128                 self["port"] = Button(_("Port"))
129                 self["type"] = Button(_("Enigma Type"))
130                 self["key_red"] = Button(_("Add"))
131                 self["key_yellow"] = Button(_("Edit"))
132                 self["key_blue"] = Button(_("Delete"))
133                 self["entrylist"] = PartnerboxEntryList([])
134                 self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
135                         {
136                          "ok"   :       self.keyOK,
137                          "back" :       self.keyClose,
138                          "red"  :       self.keyRed,
139                          "yellow":      self.keyYellow,
140                          "blue":        self.keyDelete,
141                          }, -1)
142                 self.what = what
143                 self.updateList()
144
145         def updateList(self):
146                 self["entrylist"].buildList()
147
148         def keyClose(self):
149                 self.close(self.session, self.what, None)
150
151         def keyRed(self):
152                 self.session.openWithCallback(self.updateList,PartnerboxEntryConfigScreen,None)
153
154         def keyOK(self):
155                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
156                 except: sel = None
157                 self.close(self.session, self.what, sel)
158
159         def keyYellow(self):
160                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
161                 except: sel = None
162                 if sel is None:
163                         return
164                 self.session.openWithCallback(self.updateList,PartnerboxEntryConfigScreen,sel)
165
166         def keyDelete(self):
167                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
168                 except: sel = None
169                 if sel is None:
170                         return
171                 self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this Partnerbox Entry?"))
172
173         def deleteConfirm(self, result):
174                 if not result:
175                         return
176                 sel = self["entrylist"].l.getCurrentSelection()[0]
177                 config.plugins.Partnerbox.entriescount.value = config.plugins.Partnerbox.entriescount.value - 1
178                 config.plugins.Partnerbox.entriescount.save()
179                 config.plugins.Partnerbox.Entries.remove(sel)
180                 config.plugins.Partnerbox.Entries.save()
181                 config.plugins.Partnerbox.save()
182                 configfile.save()
183                 self.updateList()
184
185 class PartnerboxEntryList(MenuList):
186         SKIN_COMPONENT_KEY = "PartnerboxList"
187         SKIN_COMPONENT_NAME_WIDTH = "nameWidth"
188         SKIN_COMPONENT_IP_WIDTH = "ipWidth"
189         SKIN_COMPONENT_PORT_WIDTH = "portWidth"
190         SKIN_COMPONENT_ENIGMA_WIDTH = "enigmaWidth"
191         
192         def __init__(self, list, enableWrapAround = True):
193                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
194                 tlf = TemplatedListFonts()
195                 self.l.setFont(0, gFont(tlf.face(tlf.SMALL), tlf.size(tlf.SMALL)))
196         def postWidgetCreate(self, instance):
197                 MenuList.postWidgetCreate(self, instance)
198                 instance.setItemHeight(componentSizes.itemHeight(self.SKIN_COMPONENT_KEY, 20))
199
200         def buildList(self):
201                 self.list=[]
202                 
203                 sizes = componentSizes[PartnerboxEntryList.SKIN_COMPONENT_KEY]
204                 configEntryHeight = sizes.get(componentSizes.ITEM_HEIGHT, 20)
205                 nameWidth = sizes.get(PartnerboxEntryList.SKIN_COMPONENT_NAME_WIDTH, 200)
206                 ipWidth = sizes.get(PartnerboxEntryList.SKIN_COMPONENT_IP_WIDTH, 135)
207                 portWidth = sizes.get(PartnerboxEntryList.SKIN_COMPONENT_PORT_WIDTH, 80)
208                 enigmaWidth = sizes.get(PartnerboxEntryList.SKIN_COMPONENT_ENIGMA_WIDTH, 120)
209                                 
210                 for c in config.plugins.Partnerbox.Entries:
211                         res = [c]
212                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 5, 0, nameWidth, configEntryHeight, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.name.value)))
213                         ip = "%d.%d.%d.%d" % tuple(c.ip.value)
214                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 10+nameWidth, 0, ipWidth, configEntryHeight, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(ip)))
215                         port = "%d"%(c.port.value)
216                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 15+nameWidth+ipWidth, 0, portWidth, configEntryHeight, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(port)))
217                         if int(c.enigma.value) == 0:
218                                 e_type = "Enigma2"
219                         else:
220                                 e_type = "Enigma1"
221                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 15+nameWidth+ipWidth+portWidth, 0, enigmaWidth, configEntryHeight, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(e_type)))
222                         self.list.append(res)
223                 self.l.setList(self.list)
224                 self.moveToIndex(0)
225
226 class PartnerboxEntryConfigScreen(ConfigListScreen, Screen):
227         skin = """
228                 <screen name="PartnerboxEntryConfigScreen" position="center,center" size="550,400" title="%s">
229                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
230                         <ePixmap name="red"     position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
231                         <ePixmap name="green" position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
232                         <ePixmap name="blue" position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
233
234                         <widget name="key_red" position="0,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
235                         <widget name="key_green" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
236                         <widget name="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" />
237                 </screen>""" % _("Partnerbox: Edit Entry")
238
239         def __init__(self, session, entry):
240                 self.session = session
241                 Screen.__init__(self, session)
242
243                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
244                 {
245                         "green": self.keySave,
246                         "red": self.keyCancel,
247                         "blue": self.keyDelete,
248                         "cancel": self.keyCancel
249                 }, -2)
250
251                 self["key_red"] = Button(_("Cancel"))
252                 self["key_green"] = Button(_("OK"))
253                 self["key_blue"] = Button(_("Delete"))
254
255                 if entry is None:
256                         self.newmode = 1
257                         self.current = initPartnerboxEntryConfig()
258                 else:
259                         self.newmode = 0
260                         self.current = entry
261
262                 cfglist = [
263                         getConfigListEntry(_("Name"), self.current.name),
264                         getConfigListEntry(_("IP"), self.current.ip),
265                         getConfigListEntry(_("Port"), self.current.port),
266                         getConfigListEntry(_("Enigma Type"), self.current.enigma),
267                         getConfigListEntry(_("Password"), self.current.password),
268                         getConfigListEntry(_("Servicelists/EPG"), self.current.useinternal),
269                         getConfigListEntry(_("Zap to service when streaming"), self.current.zaptoservicewhenstreaming)
270                 ]
271
272                 ConfigListScreen.__init__(self, cfglist, session)
273
274         def keySave(self):
275                 if self.newmode == 1:
276                         config.plugins.Partnerbox.entriescount.value = config.plugins.Partnerbox.entriescount.value + 1
277                         config.plugins.Partnerbox.entriescount.save()
278                 ConfigListScreen.keySave(self)
279                 config.plugins.Partnerbox.save()
280                 configfile.save()
281                 self.close()
282
283         def keyCancel(self):
284                 if self.newmode == 1:
285                         config.plugins.Partnerbox.Entries.remove(self.current)
286                 ConfigListScreen.cancelConfirm(self, True)
287
288         def keyDelete(self):
289                 if self.newmode == 1:
290                         self.keyCancel()
291                 else:           
292                         self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this Partnerbox Entry?"))
293
294         def deleteConfirm(self, result):
295                 if not result:
296                         return
297                 config.plugins.Partnerbox.entriescount.value = config.plugins.Partnerbox.entriescount.value - 1
298                 config.plugins.Partnerbox.entriescount.save()
299                 config.plugins.Partnerbox.Entries.remove(self.current)
300                 config.plugins.Partnerbox.Entries.save()
301                 config.plugins.Partnerbox.save()
302                 configfile.save()
303                 self.close()