Just source-cleanups
[enigma2-plugins.git] / vlcplayer / src / VlcServerConfig.py
1 # -*- coding: ISO-8859-1 -*-
2 #===============================================================================
3 # VLC Player Plugin by A. Lätsch 2007
4 #                   modified by Volker Christian 2008
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2, or (at your option) any later
9 # version.
10 #===============================================================================
11
12
13 from Components.ActionMap import ActionMap
14 from Components.Button import Button
15 from Components.ConfigList import ConfigListScreen
16 from Components.config import ConfigElement
17 from Components.config import ConfigIP
18 from Components.config import ConfigInteger
19 from Components.config import ConfigSelection
20 from Components.config import ConfigSubList
21 from Components.config import ConfigSubsection
22 from Components.config import ConfigSlider
23 from Components.config import ConfigText
24 from Components.config import ConfigYesNo
25 from Components.config import config
26 from Components.config import getConfigListEntry
27 from Screens.Screen import Screen
28 from VlcServer import VlcServer
29 from . import _
30
31 class ConfigMutable(ConfigElement):
32         def __init__(self, configElementDict, defaultKey):
33                 ConfigElement.__init__(self)
34                 self.configElementDict = configElementDict
35                 if self.configElementDict.has_key(defaultKey):
36                         self.currentConfig = self.configElementDict[defaultKey]
37                         self.currentKey = defaultKey
38                         self.defaultKey = self.currentKey
39
40         def addConfigElement(self, key, configElement):
41                 self.elements[key] = configElement
42
43         def setAsCurrent(self, key):
44                 if self.configElementDict.has_key(key):
45                         self.currentConfig = self.configElementDict[key]
46                         self.currentKey = key
47                         self.saved_value = self.currentConfig.saved_value
48                         
49         def setValue(self, val):
50                 self.currentConfig.value = val
51                 self.changed()
52
53         def set_Value(self, val):
54                 self.currentConfig._value = val
55                 self.changed()
56
57         def getValue(self):
58                 return self.currentConfig.value
59         
60         def get_Value(self):
61                 return self.currentConfig._value
62
63         _value = property(get_Value, set_Value)
64         
65         def fromstring(self, value):
66                 return self.currentConfig.fromstring(value)
67
68         def load(self):
69                 self.currentConfig.saved_value = self.saved_value
70                 self.currentConfig.load()
71
72         def tostring(self, value):
73                 return self.currentConfig.tostring(value)
74
75         def save(self):
76                 self.currentConfig.save()
77                 self.defaultKey = self.currentKey
78                 self.saved_value = self.currentConfig.saved_value
79
80         def cancel(self):
81                 self.setAsCurrent(self.defaultKey)
82                 self.load()
83                 
84         def isChanged(self):
85                 return self.currentConfig.isChanged()
86
87         def changed(self):
88                 for x in self.notifiers:
89                         x(self)
90                         
91         def addNotifier(self, notifier, initial_call = True):
92                 assert callable(notifier), "notifiers must be callable"
93                 self.notifiers.append(notifier)
94                 if initial_call:
95                         notifier(self)
96
97         def disableSave(self):
98                 self.currentConfig.disableSave()
99
100         def __call__(self, selected):
101                 return self.currentConfig(selected)
102
103         def onSelect(self, session):
104                 self.currentConfig.onSelect(session)
105
106         def onDeselect(self, session):
107                 self.currentConfig.onDeselect(session)
108
109         def handleKey(self, key):
110                 self.currentConfig.handleKey(key)
111
112         def getHTML(self, id):
113                 return self.currentConfig.getHTML(id)
114
115         def genText(self):
116                 return self.currentConfig.genText()
117
118         def getText(self):
119                 return self.currentConfig.getText()
120
121         def getMulti(self, selected):
122                 return self.currentConfig.getMulti(selected)
123
124
125 class ConfigSelectionExtended(ConfigSelection):
126         def __init__(self, choices, default = None):
127                 ConfigSelection.__init__(self, choices, default)
128
129         def deleteNotifier(self, notifier):
130                 self.notifiers.remove(notifier)
131
132
133 class __VlcServerConfig():
134         def __init__(self):
135                 self.serverlist = []
136                 config.plugins.vlcplayer = ConfigSubsection()
137                 config.plugins.vlcplayer.servercount = ConfigInteger(0)
138                 config.plugins.vlcplayer.servers = ConfigSubList()
139                 config.plugins.vlcplayer.defaultserver = ConfigText("", False)
140                 for servernum in range(0, config.plugins.vlcplayer.servercount.value):
141                         self.new()
142
143         # Add a new server or load a configsection if existing
144         def new(self):
145                 newServerConfigSubsection = ConfigSubsection()
146                 config.plugins.vlcplayer.servers.append(newServerConfigSubsection)
147                 newServerConfigSubsection.name = ConfigText("Server " + str(self.__getServerCount()), False)
148                 if newServerConfigSubsection.name.value == newServerConfigSubsection.name.default:
149                         newServerConfigSubsection.name.default = ""
150                 newServerConfigSubsection.addressType = ConfigSelectionExtended(
151                                 [("FQDN", "FQDN"),
152                                  ("IP", "IP-Address")
153                                 ], "IP")
154                 newServerConfigSubsection.hostip = ConfigMutable(
155                                 {"IP": ConfigIP([192,168,1,1]),
156                                  "FQDN": ConfigText("fqdname", False)
157                                 }, newServerConfigSubsection.addressType.value)
158                 newServerConfigSubsection.httpport = ConfigInteger(8080, (0,65535))
159                 newServerConfigSubsection.basedir = ConfigText("/", False)
160                 newServerConfigSubsection.dvdPath = ConfigText("", False)
161                 newServerConfigSubsection.transcodeVideo = ConfigYesNo()
162                 newServerConfigSubsection.transcodeAudio = ConfigYesNo(True)
163                 newServerConfigSubsection.videocodec = ConfigSelection(
164                                 [("mp1v", "MPEG1"),
165                                  ("mp2v", "MPEG2")
166                                 ], "mp2v")
167                 newServerConfigSubsection.videobitrate = ConfigInteger(2000, (100, 9999))
168                 newServerConfigSubsection.audiocodec = ConfigSelection(
169                                 [("mpga", "MPEG Layer 1 (mpga)"),
170                                  ("mp2a", "MPEG Layer 2 (mp2a)"),
171                                  ("mp3", "MPEG Layer 3 (mp3)")
172                                 ], "mp2a")
173                 newServerConfigSubsection.audiobitrate = ConfigInteger(128, (64, 320))
174                 newServerConfigSubsection.samplerate = ConfigSelection(
175                                 [("32000", "32000"),
176                                  ("44100", "44100"),
177                                  ("48000", "48000")
178                                 ], "44100")
179                 newServerConfigSubsection.audiochannels = ConfigInteger(2, (1, 9))
180                 newServerConfigSubsection.videonorm = ConfigSelection(
181                                 [("720,576,4:3,25,i", "720 x 576 (4:3) @ 25fps (PAL)"),
182                                  ("720,576,16:9,25,i", "720 x 576 (16:9) @ 25fps (PAL)"),
183                                  ("704,576,4:3,25,i", "704 x 576 (4:3) @ 25fps (PAL)"),
184                                  ("704,576,16:9,25,i", "704 x 576 (16:9) @ 25fps (PAL)"),
185                                  ("544,576,4:3,25,i", "544 x 576 (4:3) @ 25fps (PAL)"),
186                                  ("544,576,16:9,25,i", "544 x 576 (16:9) @ 25fps (PAL)"),
187                                  ("480,576,4:3,25,i", "480 x 576 (4:3) @ 25fps (PAL)"),
188                                  ("480,576,16:9,25,i", "480 x 576 (16:9) @ 25fps (PAL)"),
189                                  ("480,288,4:3,25,i", "480 x 288 (4:3) @ 25fps (PAL)"),
190                                  ("480,288,16:9,25,i", "480 x 288 (16:9) @ 25fps (PAL)"),
191                                  ("352,576,4:3,25,i", "352 x 576 (4:3) @ 25fps (PAL)"),
192                                  ("352,576,16:9,25,i", "352 x 576 (16:9) @ 25fps (PAL)"),
193                                  ("352,288,4:3,25,i", "352 x 288 (4:3) @ 25fps (PAL)"),
194                                  ("352,288,16:9,25,i", "352 x 288 (16:9) @ 25fps (PAL)"),
195                                  ("720,480,4:3,30,i", "720 x 480 (4:3) @ 30fps (NTSC)"),
196                                  ("720,480,16:9,30,i", "720 x 480 (16:9) @ 30fps (NTSC)"),
197                                  ("640,480,4:3,30,i", "640 x 480 (4:3) @ 30fps (NTSC)"),
198                                  ("640,480,16:9,30,i", "640 x 480 (16:9) @ 30fps (NTSC)"),
199                                  ("544,480,4:3,30,i", "544 x 480 (4:3) @ 30fps (NTSC)"),
200                                  ("544,480,16:9,30,i", "544 x 480 (16:9) @ 30fps (NTSC)"),
201                                  ("480,480,4:3,30,i", "480 x 480 (4:3) @ 30fps (NTSC)"),
202                                  ("480,480,16:9,30,i", "480 x 480 (16:9) @ 30fps (NTSC)"),
203                                  ("480,240,4:3,30,i", "480 x 240 (4:3) @ 30fps (NTSC)"),
204                                  ("480,240,16:9,30,i", "480 x 240 (16:9) @ 30fps (NTSC)"),
205                                  ("353,480,4:3,30,i", "353 x 480 (4:3) @ 30fps (NTSC)"),
206                                  ("353,480,16:9,30,i", "353 x 480 (16:9) @ 30fps (NTSC)"),
207                                  ("352,240,4:3,30,i", "352 x 240 (4:3) @ 30fps (NTSC)"),
208                                  ("352,240,16:9,30,i", "352 x 240 (16:9) @ 30fps (NTSC)"),
209                                  ("1920,1080,16:9,50,p", "1920 x 1080 (16:9) @ 50p (HTDV)"),
210                                  ("1920,1080,16:9,25,p", "1920 x 1080 (16:9) @ 25p (HTDV)"),
211                                  ("1920,1080,16:9,25,i", "1920 x 1080 (16:9) @ 25i (HTDV)"),
212                                  ("1440,1080,16:9,25,p", "1440 x 1080 (16:9) @ 25p (HTDV)"),
213                                  ("1440,1080,16:9,25,i", "1440 x 1080 (16:9) @ 25i (HTDV)"),
214                                  ("1280,720,16:9,50,p", "1280 x 720 (16:9) @ 50p (HDTV)"),
215                                  ("1280,720,16:9,25,p", "1280 x 720 (16:9) @ 25p (HDTV)"),
216                                  ("720,576,16:9,50,p", "720 x 576 (16:9) @ 50p (HDTV)")
217                                 ], "352,288,4:3,25,i")
218                 newServerConfigSubsection.overscancorrection = ConfigInteger(0, (0, 100))
219                 newServerConfigSubsection.soverlay = ConfigYesNo()
220                 newServer = VlcServer(newServerConfigSubsection)
221
222                 self.serverlist.append(newServer)
223
224                 return newServer
225
226         # Add was canceled or existing server should be removed
227         def delete(self, server):
228                 config.plugins.vlcplayer.servers.remove(server.getCfg())
229                 self.serverlist.remove(server)
230                 self.__save()
231
232         # Edit or Add should complete
233         def save(self, server):
234                 server.getCfg().save()
235                 self.__save()
236
237         # Edit has been canceled
238         def cancel(self, server):
239                 for element in server.getCfg().dict().values():
240                         element.cancel()
241
242         def getServerlist(self):
243                 return self.serverlist
244
245         def getServerByName(self, name):
246                 for server in self.serverlist:
247                         if server.getName() == name:
248                                 return server
249                 return None
250
251         def getDefaultServer(self):
252                 return self.getServerByName(config.plugins.vlcplayer.defaultserver.value)
253
254         def setAsDefault(self, defaultServer):
255                 if defaultServer is not None:
256                         config.plugins.vlcplayer.defaultserver.value = defaultServer.getName()
257                         config.plugins.vlcplayer.defaultserver.save()
258
259         def __save(self):
260                 config.plugins.vlcplayer.servercount.value = self.__getServerCount()
261                 config.plugins.vlcplayer.servercount.save()
262
263         def __getServerCount(self):
264                 return len(config.plugins.vlcplayer.servers)
265
266
267 vlcServerConfig = __VlcServerConfig()
268
269
270 class VlcServerConfigScreen(Screen, ConfigListScreen):
271         skin = """
272                 <screen name="VlcServerConfigScreen" position="80,100" size="560,320" title="Edit VLC Server">
273                         <widget name="config" position="10,10" size="540,250" scrollbarMode="showOnDemand" />
274                         <ePixmap name="red"    position="0,280"   zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
275                         <ePixmap name="green"  position="140,280" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
276                         <ePixmap name="yellow" position="280,280" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
277                         <ePixmap name="blue"   position="420,280" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
278                         <widget name="key_red" position="0,280" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
279                         <widget name="key_green" position="140,280" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
280                         <widget name="key_yellow" position="280,280" zPosition="5" size="140,40" valign="center" halign="center"  font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
281                         <widget name="key_blue" position="420,280" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
282                 </screen>"""
283
284         def __init__(self, session, server):
285                 Screen.__init__(self, session)
286                 self.server = server
287                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
288                 {
289                         "green": self.keySave,
290                         "red": self.keyCancel,
291                         "cancel": self.keyCancel
292                 }, -2)
293
294                 self["key_red"] = Button(_("Cancel"))
295                 self["key_green"] = Button(_("OK"))
296                 self["key_yellow"] = Button("")
297                 self["key_blue"] = Button("")
298
299                 cfglist = []
300                 cfglist.append(getConfigListEntry(_("Server Profile Name"), server.name()))
301                 cfglist.append(getConfigListEntry(_("Enter VLC-Server as FQDN or IP-Address"), server.addressType()))
302                 self.hostConfigListEntry = getConfigListEntry(_("Server Address"), server.host())
303                 cfglist.append(self.hostConfigListEntry)
304                 cfglist.append(getConfigListEntry(_("HTTP Port"), server.httpPort()))
305                 cfglist.append(getConfigListEntry(_("Movie Directory"), server.basedir()))
306                 cfglist.append(getConfigListEntry(_("DVD Device (leave empty for default)"), server.dvdPath()))
307
308                 cfglist.append(getConfigListEntry(_("Transcode MPEG/DVD Video"), server.transcodeVideo()))
309                 cfglist.append(getConfigListEntry(_("Video Codec"), server.videoCodec()))
310                 cfglist.append(getConfigListEntry(_("Video Bitrate"), server.videoBitrate()))
311                 cfglist.append(getConfigListEntry(_("Video Norm"), server.videoNorm()))
312                 cfglist.append(getConfigListEntry(_("Overscan Correction [in %(percentsign)s of Video width]") % { "percentsign" : "%"}, server.overscanCorrection()))
313
314                 cfglist.append(getConfigListEntry(_("Subtitle overlay"), server.sOverlay()))
315
316                 cfglist.append(getConfigListEntry(_("Transcode MPEG/DVD Audio"), server.transcodeAudio()))
317                 cfglist.append(getConfigListEntry(_("Audio Codec"), server.audioCodec()))
318                 cfglist.append(getConfigListEntry(_("Audio Bitrate"), server.audioBitrate()))
319                 cfglist.append(getConfigListEntry(_("Audio Samplerate"), server.samplerate()))
320                 cfglist.append(getConfigListEntry(_("Audio Channels"), server.audioChannels()))
321
322                 ConfigListScreen.__init__(self, cfglist, session)
323
324                 server.addressType().addNotifier(self.switchAddressType, False)
325                 
326                 self.onClose.append(self.__onClose)
327                 
328         def __onClose(self):
329                 self.server.addressType().deleteNotifier(self.switchAddressType)
330
331         def switchAddressType(self, configElement):
332                 self.server.host().setAsCurrent(configElement.value)
333                 self["config"].invalidate(self.hostConfigListEntry)
334
335         def keySave(self):
336                 self.close(True, self.server)
337
338         def keyCancel(self):
339                 self.close(False, self.server)