make setup-layout nicer (thanks to zombi)
[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
31 # for localized messages
32 from . import _
33
34 def initPartnerboxEntryConfig():
35         config.plugins.Partnerbox.Entries.append(ConfigSubsection())
36         i = len(config.plugins.Partnerbox.Entries) -1
37         config.plugins.Partnerbox.Entries[i].name = ConfigText(default = "dreambox", visible_width = 50, fixed_size = False)
38         config.plugins.Partnerbox.Entries[i].ip = ConfigIP(default = [192,168,0,98])
39         config.plugins.Partnerbox.Entries[i].port = ConfigInteger(default=80, limits=(1, 65555))
40         config.plugins.Partnerbox.Entries[i].enigma = ConfigSelection(default="0", choices = [("0", _("Enigma 2")),("1", _("Enigma 1"))])
41         config.plugins.Partnerbox.Entries[i].password = ConfigText(default = "dreambox", visible_width = 50, fixed_size = False)
42         config.plugins.Partnerbox.Entries[i].useinternal = ConfigSelection(default="1", choices = [("0", _("use external")),("1", _("use internal"))])
43         config.plugins.Partnerbox.Entries[i].zaptoservicewhenstreaming = ConfigYesNo(default = True)
44         return config.plugins.Partnerbox.Entries[i]
45
46 def initConfig():
47         count = config.plugins.Partnerbox.entriescount.value
48         if count != 0:
49                 i = 0
50                 while i < count:
51                         initPartnerboxEntryConfig()
52                         i += 1
53
54 class PartnerboxSetup(ConfigListScreen, Screen):
55         skin = """
56                 <screen position="center,center" size="550,400" title="Partnerbox Setup" >
57                         <widget name="config" position="20,10" size="510,330" scrollbarMode="showOnDemand" />
58                         <widget name="key_red" position="0,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
59                         <widget name="key_green" position="140,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
60                         <widget name="key_yellow" position="280,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
61                         <ePixmap name="red" pixmap="skin_default/buttons/red.png" position="0,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
62                         <ePixmap name="green" pixmap="skin_default/buttons/green.png" position="140,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
63                         <ePixmap name="yellow" pixmap="skin_default/buttons/yellow.png" position="280,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
64                 </screen>"""
65
66         def __init__(self, session, args = None):
67                 Screen.__init__(self, session)
68
69                 self["key_red"] = Button(_("Cancel"))
70                 self["key_green"] = Button(_("OK"))
71                 self["key_yellow"] = Button(_("Partnerbox Entries"))
72
73
74                 self.list = [ ]
75                 self.list.append(getConfigListEntry(_("Show 'RemoteTimer' in E-Menu"), config.plugins.Partnerbox.showremotetimerinextensionsmenu))
76                 self.list.append(getConfigListEntry(_("Show 'RemoteTV Player' in E-Menu"), config.plugins.Partnerbox.showremotetvinextensionsmenu))
77                 self.list.append(getConfigListEntry(_("Show 'Stream current Service' in E-Menu"), config.plugins.Partnerbox.showcurrentstreaminextensionsmenu))
78                 self.list.append(getConfigListEntry(_("Enable Partnerbox-Function in TimerEvent"), config.plugins.Partnerbox.enablepartnerboxintimerevent))
79                 self.list.append(getConfigListEntry(_("Enable Partnerbox-Function in EPGList"), config.plugins.Partnerbox.enablepartnerboxepglist))
80                 ConfigListScreen.__init__(self, self.list, session)
81                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
82                 {
83                         "green": self.keySave,
84                         "cancel": self.keyClose,
85                         "ok": self.keySave,
86                         "yellow": self.PartnerboxEntries,
87                 }, -2)
88
89         def keySave(self):
90                 for x in self["config"].list:
91                         x[1].save()
92                 configfile.save()
93                 self.close(self.session, True)
94
95         def keyClose(self):
96                 for x in self["config"].list:
97                         x[1].cancel()
98                 self.close(self.session, False)
99
100         def PartnerboxEntries(self):
101                 self.session.open(PartnerboxEntriesListConfigScreen)
102
103
104 class PartnerboxEntriesListConfigScreen(Screen):
105         skin = """
106                 <screen position="center,center" size="550,400" title="%s" >
107                         <widget name="name" position="5,0" size="200,50" font="Regular;20" halign="left"/>
108                         <widget name="ip" position="215,0" size="140,50" font="Regular;20" halign="left"/>
109                         <widget name="port" position="350,0" size="80,50" font="Regular;20" halign="left"/>
110                         <widget name="type" position="430,0" size="120,50" font="Regular;20" halign="left"/>
111                         <widget name="entrylist" position="0,50" size="550,300" scrollbarMode="showOnDemand"/>
112
113                         <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" />
114                         <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" />
115                         <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" />
116                         <ePixmap name="red" position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
117                         <ePixmap name="yellow" position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
118                         <ePixmap name="blue" position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
119                 </screen>""" % _("Partnerbox: List of Entries")
120
121         def __init__(self, session, what = None):
122                 Screen.__init__(self, session)
123                 self.session = session
124                 self["name"] = Button(_("Name"))
125                 self["ip"] = Button(_("IP"))
126                 self["port"] = Button(_("Port"))
127                 self["type"] = Button(_("Enigma Type"))
128                 self["key_red"] = Button(_("Add"))
129                 self["key_yellow"] = Button(_("Edit"))
130                 self["key_blue"] = Button(_("Delete"))
131                 self["entrylist"] = PartnerboxEntryList([])
132                 self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
133                         {
134                          "ok"   :       self.keyOK,
135                          "back" :       self.keyClose,
136                          "red"  :       self.keyRed,
137                          "yellow":      self.keyYellow,
138                          "blue":        self.keyDelete,
139                          }, -1)
140                 self.what = what
141                 self.updateList()
142
143         def updateList(self):
144                 self["entrylist"].buildList()
145
146         def keyClose(self):
147                 self.close(self.session, self.what, None)
148
149         def keyRed(self):
150                 self.session.openWithCallback(self.updateList,PartnerboxEntryConfigScreen,None)
151
152         def keyOK(self):
153                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
154                 except: sel = None
155                 self.close(self.session, self.what, sel)
156
157         def keyYellow(self):
158                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
159                 except: sel = None
160                 if sel is None:
161                         return
162                 self.session.openWithCallback(self.updateList,PartnerboxEntryConfigScreen,sel)
163
164         def keyDelete(self):
165                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
166                 except: sel = None
167                 if sel is None:
168                         return
169                 self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this Partnerbox Entry?"))
170
171         def deleteConfirm(self, result):
172                 if not result:
173                         return
174                 sel = self["entrylist"].l.getCurrentSelection()[0]
175                 config.plugins.Partnerbox.entriescount.value = config.plugins.Partnerbox.entriescount.value - 1
176                 config.plugins.Partnerbox.entriescount.save()
177                 config.plugins.Partnerbox.Entries.remove(sel)
178                 config.plugins.Partnerbox.Entries.save()
179                 config.plugins.Partnerbox.save()
180                 configfile.save()
181                 self.updateList()
182
183 class PartnerboxEntryList(MenuList):
184         def __init__(self, list, enableWrapAround = True):
185                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
186                 self.l.setFont(0, gFont("Regular", 20))
187                 self.l.setFont(1, gFont("Regular", 18))
188         def postWidgetCreate(self, instance):
189                 MenuList.postWidgetCreate(self, instance)
190                 instance.setItemHeight(20)
191
192         def buildList(self):
193                 self.list=[]
194                 for c in config.plugins.Partnerbox.Entries:
195                         res = [c]
196                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 5, 0, 200, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(c.name.value)))
197                         ip = "%d.%d.%d.%d" % tuple(c.ip.value)
198                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 210, 0, 130, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(ip)))
199                         port = "%d"%(c.port.value)
200                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 350, 0, 80, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(port)))
201                         if int(c.enigma.value) == 0:
202                                 e_type = "Enigma2"
203                         else:
204                                 e_type = "Enigma1"
205                         res.append((eListboxPythonMultiContent.TYPE_TEXT, 430, 0, 120, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, str(e_type)))
206                         self.list.append(res)
207                 self.l.setList(self.list)
208                 self.moveToIndex(0)
209
210 class PartnerboxEntryConfigScreen(ConfigListScreen, Screen):
211         skin = """
212                 <screen name="PartnerboxEntryConfigScreen" position="center,center" size="550,400" title="%s">
213                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
214                         <ePixmap name="red"     position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
215                         <ePixmap name="green" position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
216                         <ePixmap name="blue" position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
217
218                         <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" />
219                         <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" />
220                         <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" />
221                 </screen>""" % _("Partnerbox: Edit Entry")
222
223         def __init__(self, session, entry):
224                 self.session = session
225                 Screen.__init__(self, session)
226
227                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
228                 {
229                         "green": self.keySave,
230                         "red": self.keyCancel,
231                         "blue": self.keyDelete,
232                         "cancel": self.keyCancel
233                 }, -2)
234
235                 self["key_red"] = Button(_("Cancel"))
236                 self["key_green"] = Button(_("OK"))
237                 self["key_blue"] = Button(_("Delete"))
238
239                 if entry is None:
240                         self.newmode = 1
241                         self.current = initPartnerboxEntryConfig()
242                 else:
243                         self.newmode = 0
244                         self.current = entry
245
246                 cfglist = [
247                         getConfigListEntry(_("Name"), self.current.name),
248                         getConfigListEntry(_("IP"), self.current.ip),
249                         getConfigListEntry(_("Port"), self.current.port),
250                         getConfigListEntry(_("Enigma Type"), self.current.enigma),
251                         getConfigListEntry(_("Password"), self.current.password),
252                         getConfigListEntry(_("Servicelists/EPG"), self.current.useinternal),
253                         getConfigListEntry(_("Zap to service when streaming"), self.current.zaptoservicewhenstreaming)
254                 ]
255
256                 ConfigListScreen.__init__(self, cfglist, session)
257
258         def keySave(self):
259                 if self.newmode == 1:
260                         config.plugins.Partnerbox.entriescount.value = config.plugins.Partnerbox.entriescount.value + 1
261                         config.plugins.Partnerbox.entriescount.save()
262                 ConfigListScreen.keySave(self)
263                 config.plugins.Partnerbox.save()
264                 configfile.save()
265                 self.close()
266
267         def keyCancel(self):
268                 if self.newmode == 1:
269                         config.plugins.Partnerbox.Entries.remove(self.current)
270                 ConfigListScreen.cancelConfirm(self, True)
271
272         def keyDelete(self):
273                 if self.newmode == 1:
274                         self.keyCancel()
275                 else:           
276                         self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this Partnerbox Entry?"))
277
278         def deleteConfirm(self, result):
279                 if not result:
280                         return
281                 config.plugins.Partnerbox.entriescount.value = config.plugins.Partnerbox.entriescount.value - 1
282                 config.plugins.Partnerbox.entriescount.save()
283                 config.plugins.Partnerbox.Entries.remove(self.current)
284                 config.plugins.Partnerbox.Entries.save()
285                 config.plugins.Partnerbox.save()
286                 configfile.save()
287                 self.close()