Fixed a potential crash
[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 ConfigText
23 from Components.config import ConfigYesNo
24 from Components.config import config
25 from Components.config import getConfigListEntry
26 from Screens.Screen import Screen
27 from VlcServer import VlcServer
28 from . import _
29
30 class ConfigMutable(ConfigElement):
31         def __init__(self, configElementDict, defaultKey):
32                 ConfigElement.__init__(self)
33                 self.configElementDict = configElementDict
34                 if self.configElementDict.has_key(defaultKey):
35                         self.currentConfig = self.configElementDict[defaultKey]
36                         self.currentKey = defaultKey
37                         self.defaultKey = self.currentKey
38
39         def addConfigElement(self, key, configElement):
40                 self.elements[key] = configElement
41
42         def setAsCurrent(self, key):
43                 if self.configElementDict.has_key(key):
44                         self.currentConfig = self.configElementDict[key]
45                         self.currentKey = key
46                         self.saved_value = self.currentConfig.saved_value
47
48         def setValue(self, val):
49                 self.currentConfig.value = val
50                 self.changed()
51
52         def set_Value(self, val):
53                 self.currentConfig._value = val
54                 self.changed()
55
56         def getValue(self):
57                 return self.currentConfig.value
58
59         def get_Value(self):
60                 return self.currentConfig._value
61
62         _value = property(get_Value, set_Value)
63
64         def fromstring(self, value):
65                 return self.currentConfig.fromstring(value)
66
67         def load(self):
68                 self.currentConfig.saved_value = self.saved_value
69                 self.currentConfig.load()
70
71         def tostring(self, value):
72                 return self.currentConfig.tostring(value)
73
74         def save(self):
75                 self.currentConfig.save()
76                 self.defaultKey = self.currentKey
77                 self.saved_value = self.currentConfig.saved_value
78
79         def cancel(self):
80                 self.setAsCurrent(self.defaultKey)
81                 self.load()
82
83         def isChanged(self):
84                 return self.currentConfig.isChanged()
85
86         def changed(self):
87                 for x in self.notifiers:
88                         x(self)
89
90         def addNotifier(self, notifier, initial_call = True):
91                 assert callable(notifier), "notifiers must be callable"
92                 self.notifiers.append(notifier)
93                 if initial_call:
94                         notifier(self)
95
96         def disableSave(self):
97                 self.currentConfig.disableSave()
98
99         def __call__(self, selected):
100                 return self.currentConfig(selected)
101
102         def onSelect(self, session):
103                 self.currentConfig.onSelect(session)
104
105         def onDeselect(self, session):
106                 self.currentConfig.onDeselect(session)
107
108         def handleKey(self, key):
109                 self.currentConfig.handleKey(key)
110
111         def getHTML(self, id):
112                 return self.currentConfig.getHTML(id)
113
114         def genText(self):
115                 return self.currentConfig.genText()
116
117         def getText(self):
118                 return self.currentConfig.getText()
119
120         def getMulti(self, selected):
121                 return self.currentConfig.getMulti(selected)
122
123
124 class ConfigSelectionExtended(ConfigSelection):
125         def __init__(self, choices, default = None):
126                 ConfigSelection.__init__(self, choices, default)
127
128         def deleteNotifier(self, notifier):
129                 self.notifiers.remove(notifier)
130
131
132 class __VlcServerConfig():
133         def __init__(self):
134                 self.serverlist = []
135                 config.plugins.vlcplayer = ConfigSubsection()
136                 config.plugins.vlcplayer.servercount = ConfigInteger(0)
137                 config.plugins.vlcplayer.servers = ConfigSubList()
138                 config.plugins.vlcplayer.defaultserver = ConfigText("", False)
139                 for servernum in range(0, config.plugins.vlcplayer.servercount.value):
140                         self.new()
141
142         # Add a new server or load a configsection if existing
143         def new(self):
144                 newServerConfigSubsection = ConfigSubsection()
145                 config.plugins.vlcplayer.servers.append(newServerConfigSubsection)
146                 newServerConfigSubsection.name = ConfigText("Server " + str(self.__getServerCount()), False)
147                 newServerConfigSubsection.addressType = ConfigSelectionExtended({"FQDN": "FQDN", "IP": "IP-Address"}, "IP")
148                 newServerConfigSubsection.hostip = ConfigMutable({"IP": ConfigIP([192,168,1,1]), "FQDN": ConfigText("fqdname", False)},
149                                                                                         newServerConfigSubsection.addressType.value)
150                 newServerConfigSubsection.httpport = ConfigInteger(8080, (0,65535))
151                 newServerConfigSubsection.basedir = ConfigText("/", False)
152                 newServerConfigSubsection.dvdPath = ConfigText("", False)
153                 newServerConfigSubsection.transcodeVideo = ConfigYesNo()
154                 newServerConfigSubsection.transcodeAudio = ConfigYesNo(True)
155                 newServerConfigSubsection.videocodec = ConfigSelection({"mp1v": "MPEG1", "mp2v": "MPEG2"}, "mp2v")
156                 newServerConfigSubsection.videobitrate = ConfigInteger(1000, (100, 9999))
157                 newServerConfigSubsection.audiocodec = ConfigSelection({"mpga":"MP1", "mp2a": "MP2", "mp3": "MP3"}, "mp2a")
158                 newServerConfigSubsection.audiobitrate = ConfigInteger(128, (64, 320))
159                 newServerConfigSubsection.samplerate = ConfigSelection({"0":"as Input", "44100": "44100", "48000": "48000"}, "0")
160                 newServerConfigSubsection.audiochannels = ConfigInteger(2, (1, 9))
161                 newServerConfigSubsection.videowidth = ConfigSelection(["352", "704", "720", "1280", "1920"])
162                 newServerConfigSubsection.videoheight = ConfigSelection(["288", "576", "720", "1080"])
163                 newServerConfigSubsection.framespersecond = ConfigInteger(25, (1, 99))
164                 newServerConfigSubsection.aspectratio = ConfigSelection(["none", "16:9", "4:3"], "none")
165                 newServerConfigSubsection.soverlay = ConfigYesNo()
166                 newServer = VlcServer(newServerConfigSubsection)
167
168                 self.serverlist.append(newServer)
169
170                 return newServer
171
172         # Add was canceled or existing server should be removed
173         def delete(self, server):
174                 config.plugins.vlcplayer.servers.remove(server.getCfg())
175                 self.serverlist.remove(server)
176                 self.__save()
177
178         # Edit or Add should complete
179         def save(self, server):
180                 server.getCfg().save()
181                 self.__save()
182
183         # Edit has been canceled
184         def cancel(self, server):
185                 for element in server.getCfg().dict().values():
186                         element.cancel()
187
188         def getServerlist(self):
189                 return self.serverlist
190
191         def getServerByName(self, name):
192                 for server in self.serverlist:
193                         if server.getName() == name:
194                                 return server
195                 return None
196
197         def getDefaultServer(self):
198                 return self.getServerByName(config.plugins.vlcplayer.defaultserver.value)
199
200         def setAsDefault(self, defaultServer):
201                 if defaultServer is not None:
202                         config.plugins.vlcplayer.defaultserver.value = defaultServer.getName()
203                         config.plugins.vlcplayer.defaultserver.save()
204
205         def __save(self):
206                 config.plugins.vlcplayer.servercount.value = self.__getServerCount()
207                 config.plugins.vlcplayer.servercount.save()
208
209         def __getServerCount(self):
210                 return len(config.plugins.vlcplayer.servers)
211
212
213 vlcServerConfig = __VlcServerConfig()
214
215
216 class VlcServerConfigScreen(Screen, ConfigListScreen):
217         skin = """
218                 <screen name="VlcServerConfigScreen" position="80,100" size="560,320" title="Edit VLC Server">
219                         <widget name="config" position="10,10" size="540,250" scrollbarMode="showOnDemand" />
220                         <ePixmap name="red"    position="0,280"   zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
221                         <ePixmap name="green"  position="140,280" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
222                         <ePixmap name="yellow" position="280,280" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
223                         <ePixmap name="blue"   position="420,280" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
224                         <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" />
225                         <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" />
226                         <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" />
227                         <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" />
228                 </screen>"""
229
230         def __init__(self, session, server):
231                 Screen.__init__(self, session)
232                 self.server = server
233                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
234                 {
235                         "green": self.keySave,
236                         "red": self.keyCancel,
237                         "cancel": self.keyCancel
238                 }, -2)
239
240                 self["key_red"] = Button(_("Cancel"))
241                 self["key_green"] = Button(_("OK"))
242                 self["key_yellow"] = Button("")
243                 self["key_blue"] = Button("")
244
245                 cfglist = []
246                 cfglist.append(getConfigListEntry(_("Server Profile Name"), server.name()))
247                 cfglist.append(getConfigListEntry(_("Enter VLC-Server as FQDN or IP-Address"), server.addressType()))
248                 self.hostConfigListEntry = getConfigListEntry(_("Server Address"), server.host())
249                 cfglist.append(self.hostConfigListEntry)
250                 cfglist.append(getConfigListEntry(_("HTTP Port"), server.httpPort()))
251                 cfglist.append(getConfigListEntry(_("Movie Directory"), server.basedir()))
252                 cfglist.append(getConfigListEntry(_("DVD Device (leave empty for default)"), server.dvdPath()))
253
254                 cfglist.append(getConfigListEntry(_("Transcode MPEG/DVD Video"), server.transcodeVideo()))
255                 cfglist.append(getConfigListEntry(_("Video Codec"), server.videoCodec()))
256                 cfglist.append(getConfigListEntry(_("Video Bitrate"), server.videoBitrate()))
257                 cfglist.append(getConfigListEntry(_("Video Width"), server.videoWidth()))
258                 cfglist.append(getConfigListEntry(_("Video Height"), server.videoHeight()))
259                 #cfglist.append(getConfigListEntry(_("Frames per Second"), config.plugins.vlcplayer.fps))
260                 cfglist.append(getConfigListEntry(_("Correct aspect ratio to"), server.aspectRatio()))
261                 cfglist.append(getConfigListEntry(_("Subtitle overlay"), server.sOverlay()))
262
263                 cfglist.append(getConfigListEntry(_("Transcode MPEG/DVD Audio"), server.transcodeAudio()))
264                 cfglist.append(getConfigListEntry(_("Audio Codec"), server.audioCodec()))
265                 cfglist.append(getConfigListEntry(_("Audio Bitrate"), server.audioBitrate()))
266                 cfglist.append(getConfigListEntry(_("Audio Samplerate"), server.samplerate()))
267                 cfglist.append(getConfigListEntry(_("Audio Channels"), server.audioChannels()))
268
269                 ConfigListScreen.__init__(self, cfglist, session)
270
271                 server.addressType().addNotifier(self.switchAddressType, False)
272
273                 self.onClose.append(self.__onClose)
274
275         def __onClose(self):
276                 self.server.addressType().deleteNotifier(self.switchAddressType)
277
278         def switchAddressType(self, configElement):
279                 self.server.host().setAsCurrent(configElement.value)
280                 self["config"].invalidate(self.hostConfigListEntry)
281
282         def keySave(self):
283                 self.close(True, self.server)
284
285         def keyCancel(self):
286                 self.close(False, self.server)