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