add Link to WebTv to Standard Webinterface (which gives a bigger base of Betatesters)
[enigma2-plugins.git] / webinterface / src / WebIfConfig.py
1 Version = '$Header$';
2
3 from __init__ import _
4
5 from enigma import eListboxPythonMultiContent, gFont
6 from Screens.Screen import Screen
7 from Screens.MessageBox import MessageBox
8
9 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigInteger,ConfigYesNo,ConfigText,ConfigSelection
10 from Components.ConfigList import ConfigListScreen
11 from Components.Label import Label
12 from Components.Button import Button
13 from Components.MenuList import MenuList
14 from Components.MultiContent import MultiContentEntryText
15
16 from Components.ActionMap import ActionMap
17
18 from Components.Network import iNetwork
19 def initInterfaceConfig(i = None, new = False):
20         choices = getConfiguredIPs()
21
22         if i is None and new is True:
23                 i = config.plugins.Webinterface.interfacecount.value
24         elif i is None:
25                 i = config.plugins.Webinterface.interfacecount.value - 1
26
27         print "[WebIfConfig.initInterfaceConfig] i is %s" %i
28         config.plugins.Webinterface.interfaces.append(ConfigSubsection())
29         config.plugins.Webinterface.interfaces[i].disabled = ConfigYesNo(default = False)
30         config.plugins.Webinterface.interfaces[i].address = ConfigSelection(choices, default=choices[0])
31         config.plugins.Webinterface.interfaces[i].port = ConfigInteger(80, (0, 65535))
32         config.plugins.Webinterface.interfaces[i].useauth = ConfigYesNo(default = False)
33         config.plugins.Webinterface.interfaces[i].usessl = ConfigYesNo(default = False)
34
35         config.plugins.Webinterface.interfacecount.value = i+1
36
37         return i
38
39 def updateConfig():
40         choices = getConfiguredIPs()
41         default = choices[0]
42         for c in config.plugins.Webinterface.interfaces:
43                 c.address.setChoices(choices, default = default)
44                 c.address.load()
45
46 def getConfiguredIPs():
47         choices = [
48                 '0.0.0.0',
49                 '127.0.0.1'
50         ]
51         for adaptername in iNetwork.ifaces:
52                 extip = iNetwork.ifaces[adaptername]['ip']
53                 if iNetwork.ifaces[adaptername]['up'] is True:
54                         extip = "%i.%i.%i.%i"%(extip[0],extip[1],extip[2],extip[3])
55                         choices.append(extip)
56         return choices
57
58 def initConfig():
59         interfacecount = config.plugins.Webinterface.interfacecount.value
60         if interfacecount == 0:
61                 # setting default interface
62                 # 0.0.0.0:80 auth=False
63                 config.plugins.Webinterface.interfaces.append(ConfigSubsection())
64                 config.plugins.Webinterface.interfaces[0].disabled = ConfigYesNo(default = False)
65
66                 #needs to be refreshed before each call, because ifaces can be changed since e2 boot
67                 config.plugins.Webinterface.interfaces[0].address = ConfigSelection(getConfiguredIPs(),default='0.0.0.0')
68
69                 config.plugins.Webinterface.interfaces[0].port = ConfigInteger(80, (0,65535))
70                 config.plugins.Webinterface.interfaces[0].useauth = ConfigYesNo(default = False)
71                 config.plugins.Webinterface.interfaces[0].usessl = ConfigYesNo(default = False)
72                 config.plugins.Webinterface.interfaces[0].save()
73
74                 config.plugins.Webinterface.interfacecount.value = 1
75                 config.plugins.Webinterface.interfacecount.save()
76         else:
77                 i = 0
78                 while i < interfacecount:
79                         print "[WebIfConfig.initConfig] i is %s" %i
80                         initInterfaceConfig(i)
81                         i += 1
82
83 class WebIfConfigScreen(ConfigListScreen,Screen):
84         skin = """
85                 <screen position="100,100" size="550,400" title="%s">
86                         <widget name="config" position="5,5" size="540,360" scrollbarMode="showOnDemand" zPosition="1"/>
87
88                         <widget name="key_red" position="0,360" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
89                         <widget name="key_green" position="140,360" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
90                         <widget name="key_yellow" position="280,360" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
91
92                         <ePixmap name="red" pixmap="skin_default/buttons/red.png" position="0,360" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
93                         <ePixmap name="green" pixmap="skin_default/buttons/green.png" position="140,360" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
94                         <ePixmap name="yellow" pixmap="skin_default/buttons/yellow.png" position="280,360" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
95                 </screen>""" % _("Webinterface: Main Setup")
96
97         def __init__(self, session, args = 0):
98                 Screen.__init__(self, session)
99                 l = [
100                         getConfigListEntry(_("Start Webinterface"), config.plugins.Webinterface.enable),
101                         getConfigListEntry(_("Enable /media"), config.plugins.Webinterface.includemedia),
102                         getConfigListEntry(_("Allow zapping via Webinterface"), config.plugins.Webinterface.allowzapping),
103                         getConfigListEntry(_("Autowrite timer"), config.plugins.Webinterface.autowritetimer),
104                         getConfigListEntry(_("Load movie-length"), config.plugins.Webinterface.loadmovielength)
105                 ]
106
107                 ConfigListScreen.__init__(self, l)
108                 self["key_red"] = Button(_("Cancel"))
109                 self["key_green"] = Button(_("OK"))
110                 self["key_yellow"] = Button(_("Interfaces"))
111                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
112                 {
113                         "red": self.cancel,
114                         "green": self.save,
115                         "yellow": self.openIfacesConfig,
116                         "save": self.save,
117                         "cancel": self.cancel,
118                         "ok": self.save,
119                 }, -2)
120
121         def openIfacesConfig(self):
122                 print "yellow"
123                 self.session.open(WebIfInterfaceListConfigScreen)
124
125         def save(self):
126                 print "saving"
127                 for x in self["config"].list:
128                         x[1].save()
129                 self.close(True,self.session)
130
131         def cancel(self):
132                 print "cancel"
133                 for x in self["config"].list:
134                         x[1].cancel()
135                 self.close(False,self.session)
136
137 class WebIfInterfaceListConfigScreen(Screen):
138         skin = """
139                 <screen position="100,100" size="550,400" title="%s" >
140                         <widget name="address" position="5,0" size="150,50" font="Regular;20" halign="left"/>
141                         <widget name="port" position="120,0" size="50,50" font="Regular;20" halign="left"/>
142                         <widget name="ssl" position="170,0" size="50,50" font="Regular;20" halign="left"/>
143                         <widget name="auth" position="230,0" size="170,50" font="Regular;20" halign="left"/>
144                         <widget name="disabled" position="400,0" size="160,50" font="Regular;20" halign="left"/>
145                         <widget name="ifacelist" position="0,50" size="550,300" scrollbarMode="showOnDemand"/>
146
147                         <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" />
148                         <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" />
149                         <ePixmap name="red" position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
150                         <ePixmap name="yellow" position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
151                 </screen>""" % _("Webinterface: List of configured Interfaces")
152
153         def __init__(self, session):
154                 Screen.__init__(self, session)
155                 self["address"] = Button(_("Address"))
156                 self["port"] = Button(_("Port"))
157                 self["auth"] = Button(_("Authorization"))
158                 self["ssl"] = Button(_("SSL"))
159                 self["disabled"] = Button(_("Disabled"))
160                 self["key_red"] = Button(_("Add"))
161                 self["key_yellow"] = Button(_("Change"))
162                 self["ifacelist"] = WebIfInterfaceList([])
163                 self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
164                         {
165                          "ok"   :       self.keyGreen,
166                          "back" :       self.close,
167                          "red"  :       self.keyRed,
168                          "green":       self.keyGreen,
169                          "yellow":      self.keyYellow,
170                          "up"   :       self.up,
171                          "down" :       self.down,
172                          "left" :       self.left,
173                          "right":       self.right,
174                          }, -1)
175                 self.updateList()
176
177         def updateList(self):
178                 ifaceguilist = []
179                 i = 0
180                 for c in config.plugins.Webinterface.interfaces:
181                         res = [
182                                 i, #550,400
183                                 MultiContentEntryText(pos=(5, 0), size=(150, 25), font=0, text=c.address.value),
184                                 MultiContentEntryText(pos=(120, 0),size=(50, 25), font=0,text=str(c.port.value))
185                         ]
186
187                         if c.usessl.value:
188                                 res.append(MultiContentEntryText(pos=(170, 0),size=(200, 25), font=0,text=_("yes"),color=0x0000FF00))
189                         else:
190                                 res.append(MultiContentEntryText(pos=(170, 0),size=(200, 25), font=0,text=_("no"),color=0x00FF0000))
191
192                         if c.useauth.value:
193                                 res.append(MultiContentEntryText(pos=(230, 0),size=(170, 25), font=0,text=_("yes"),color=0x0000FF00))
194                         else:
195                                 res.append(MultiContentEntryText(pos=(230, 0),size=(170, 25), font=0,text=_("no"),color=0x00FF0000))
196
197                         if c.disabled.value:
198                                 res.append(MultiContentEntryText(pos=(400, 0),size=(160, 25), font=0,text=_("yes"),color=0x0000FF00))
199                         else:
200                                 res.append(MultiContentEntryText(pos=(400, 0),size=(160, 25), font=0,text=_("no"),color=0x00FF0000))
201                         ifaceguilist.append(res)
202                         i += 1
203
204                 ifaceguilist.sort()
205                 self["ifacelist"].l.setList(ifaceguilist)
206
207         def keyRed(self):
208                 print "KEYRED"
209                 self.session.openWithCallback(self.updateList,WebIfInterfaceConfigScreen,None)
210
211         def keyGreen(self):
212                 print "KEYGREEN"
213
214         def keyYellow(self):
215                 x = self["ifacelist"].getCurrent()[0]
216                 print "current list index",x
217                 self.session.openWithCallback(self.updateList,WebIfInterfaceConfigScreen,int(x))
218
219         def up(self):
220                 self["ifacelist"].up()
221
222         def down(self):
223                 self["ifacelist"].down()
224
225         def left(self):
226                 self["ifacelist"].pageUp()
227
228         def right(self):
229                 self["ifacelist"].pageDown()
230
231 class WebIfInterfaceList(MenuList):
232         def __init__(self, list, enableWrapAround = False):
233                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
234                 self.l.setFont(0, gFont("Regular", 20))
235                 #self.l.setFont(1, gFont("Regular", 25))
236
237         def postWidgetCreate(self, instance):
238                 MenuList.postWidgetCreate(self, instance)
239                 instance.setItemHeight(25)
240
241 class WebIfInterfaceConfigScreen(Screen, ConfigListScreen):
242         skin = """
243                 <screen name="Interface Config" position="80,148" size="560,280" title="%s">
244                         <widget name="config" position="10,10" size="520,210" scrollbarMode="showOnDemand" />
245                         <ePixmap name="red"     position="0,240" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
246                         <ePixmap name="green" position="140,240" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
247                         <ePixmap name="blue" position="420,240" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
248
249                         <widget name="key_red" position="0,240" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
250                         <widget name="key_green" position="140,240" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
251                         <widget name="key_blue" position="420,240" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
252                 </screen>""" % _("Webinterface: Edit Interface")
253
254         def __init__(self, session, ifacenum):
255                 Screen.__init__(self, session)
256                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
257                 {
258                         "green": self.keySave,
259                         "red": self.keyCancel,
260                         "blue": self.keyDelete,
261                         "cancel": self.keyCancel
262                 }, -2)
263
264                 self["key_red"] = Button(_("Cancel"))
265                 self["key_green"] = Button(_("OK"))
266                 #self["key_yellow"] = Button("")
267                 self["key_blue"] = Button(_("Delete"))
268
269                 if ifacenum is None:
270                         i = initInterfaceConfig(None, True)
271                 else:
272                         i = ifacenum
273
274                 try:
275                         current = config.plugins.Webinterface.interfaces[i]
276                 except IndexError,e:
277                         print "[WebIf] iface config %i not found, adding it and setting default values"%i
278                         initInterfaceConfig()
279                         current = config.plugins.Webinterface.interfaces[ifacenum]
280
281                 #reloading current network devices
282                 current.address = ConfigSelection(getConfiguredIPs(), default=current.address.value)
283
284                 cfglist = [
285                         getConfigListEntry(_("Disabled"), current.disabled),
286                         getConfigListEntry(_("Address"), current.address),
287                         getConfigListEntry(_("Port"), current.port),
288                         getConfigListEntry(_("Require Authorization"), current.useauth),
289                         getConfigListEntry(_("SSL Encryption"), current.usessl)
290                 ]
291                 ConfigListScreen.__init__(self, cfglist, session)
292                 self.ifacenum = i
293
294         def keySave(self):
295                 config.plugins.Webinterface.interfacecount.save()
296                 for x in self["config"].list:
297                         if isinstance(x[1].value, str):
298                                 x[1].value = x[1].value.strip()
299                         x[1].save()
300                 config.plugins.Webinterface.save()
301                 self.close()
302
303         def cancelConfirm(self, result):
304                 if result:
305                         config.plugins.Webinterface.interfacecount.cancel()
306                 self.callback = None
307                 ConfigListScreen.cancelConfirm(self, result)
308
309         def keyDelete(self):
310                 self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Really delete this Interface?"))
311
312         def deleteConfirm(self, result):
313                 if not result:
314                         return
315                 del(config.plugins.Webinterface.interfaces[self.ifacenum])
316                 config.plugins.Webinterface.interfaces.save()
317                 config.plugins.Webinterface.interfacecount.value = config.plugins.Webinterface.interfacecount.value - 1;
318                 config.plugins.Webinterface.interfacecount.save()
319                 config.plugins.Webinterface.save()
320                 self.close()
321