- Some minor problems fixed
[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 class ConfigSelectionExtended(ConfigSelection):
124         def __init__(self, choices, default = None):
125                 ConfigSelection.__init__(self, choices, default)
126
127         def deleteNotifier(self, notifier):
128                 self.notifiers.remove(notifier)
129
130
131 class __VlcServerConfig():
132         def __init__(self):
133                 self.serverlist = []
134                 config.plugins.vlcplayer = ConfigSubsection()
135                 config.plugins.vlcplayer.servercount = ConfigInteger(0)
136                 config.plugins.vlcplayer.servers = ConfigSubList()
137                 for servernum in range(0, config.plugins.vlcplayer.servercount.value):
138                         self.new()
139
140         # Add a new server or load a configsection if existing
141         def new(self):
142                 newServerConfigSubsection = ConfigSubsection()
143                 config.plugins.vlcplayer.servers.append(newServerConfigSubsection)
144                 newServerConfigSubsection.name = ConfigText("Server " + str(self.__getServerCount()), False)
145                 newServerConfigSubsection.addressType = ConfigSelectionExtended({"FQDN": "FQDN", "IP": "IP-Address"}, "IP")
146                 newServerConfigSubsection.hostip = ConfigMutable({"IP": ConfigIP([192,168,1,1]), "FQDN": ConfigText("fqdname", False)},
147                                                                                         newServerConfigSubsection.addressType.value)
148                 newServerConfigSubsection.httpport = ConfigInteger(8080, (0,65535))
149                 newServerConfigSubsection.basedir = ConfigText("/", False)
150                 newServerConfigSubsection.dvdPath = ConfigText("", False)
151                 newServerConfigSubsection.transcodeVideo = ConfigYesNo()
152                 newServerConfigSubsection.transcodeAudio = ConfigYesNo(True)
153                 newServerConfigSubsection.videocodec = ConfigSelection({"mp1v": "MPEG1", "mp2v": "MPEG2"}, "mp2v")
154                 newServerConfigSubsection.videobitrate = ConfigInteger(1000, (100, 9999))
155                 newServerConfigSubsection.audiocodec = ConfigSelection({"mpga":"MP1", "mp2a": "MP2", "mp3": "MP3"}, "mp2a")
156                 newServerConfigSubsection.audiobitrate = ConfigInteger(128, (64, 320))
157                 newServerConfigSubsection.samplerate = ConfigSelection({"0":"as Input", "44100": "44100", "48000": "48000"}, "0")
158                 newServerConfigSubsection.audiochannels = ConfigInteger(2, (1, 9))
159                 newServerConfigSubsection.videowidth = ConfigSelection(["352", "704", "720", "1280", "1920"])
160                 newServerConfigSubsection.videoheight = ConfigSelection(["288", "576", "720", "1080"])
161                 newServerConfigSubsection.framespersecond = ConfigInteger(25, (1, 99))
162                 newServerConfigSubsection.aspectratio = ConfigSelection(["none", "16:9", "4:3"], "none")
163                 newServerConfigSubsection.soverlay = ConfigYesNo()
164                 newServer = VlcServer(newServerConfigSubsection)
165
166                 self.serverlist.append(newServer)
167
168                 return newServer
169
170         # Add was canceled or existing server should be removed
171         def delete(self, server):
172                 config.plugins.vlcplayer.servers.remove(server.getCfg())
173                 self.serverlist.remove(server)
174                 self.__save()
175
176         # Edit or Add should complete
177         def save(self, server):
178                 server.getCfg().save()
179                 self.__save()
180
181         # Edit has been canceled
182         def cancel(self, server):
183                 for element in server.getCfg().dict().values():
184                         element.cancel()
185
186         def getServerlist(self):
187                 return self.serverlist
188
189         def getServerByName(self, name):
190                 for server in self.serverlist:
191                         if server.getName() == name:
192                                 return server
193                 return None
194
195         def __save(self):
196                 config.plugins.vlcplayer.servercount.value = self.__getServerCount()
197                 config.plugins.vlcplayer.servercount.save()
198
199         def __getServerCount(self):
200                 return len(config.plugins.vlcplayer.servers)
201
202
203 vlcServerConfig = __VlcServerConfig()
204
205 class VlcServerConfigScreen(Screen, ConfigListScreen):
206         skin = """
207                 <screen name="VlcServerConfigScreen" position="80,100" size="560,320" title="Edit VLC Server">
208                         <widget name="config" position="10,10" size="540,250" scrollbarMode="showOnDemand" />
209                         <ePixmap name="red"    position="0,280"   zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
210                         <ePixmap name="green"  position="140,280" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
211                         <ePixmap name="yellow" position="280,280" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
212                         <ePixmap name="blue"   position="420,280" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
213                         <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" />
214                         <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" />
215                         <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" />
216                         <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" />
217                 </screen>"""
218
219         def __init__(self, session, server):
220                 Screen.__init__(self, session)
221                 self.server = server
222                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
223                 {
224                         "green": self.keySave,
225                         "red": self.keyCancel,
226                         "cancel": self.keyCancel
227                 }, -2)
228
229                 self["key_red"] = Button(_("Cancel"))
230                 self["key_green"] = Button(_("OK"))
231                 self["key_yellow"] = Button("")
232                 self["key_blue"] = Button("")
233
234                 cfglist = []
235                 cfglist.append(getConfigListEntry(_("Server Profile Name"), server.name()))
236                 cfglist.append(getConfigListEntry(_("Enter VLC-Server as FQDN or IP-Address"), server.addressType()))
237                 self.hostConfigListEntry = getConfigListEntry(_("Server Address"), server.host())
238                 cfglist.append(self.hostConfigListEntry)
239                 cfglist.append(getConfigListEntry(_("HTTP Port"), server.httpPort()))
240                 cfglist.append(getConfigListEntry(_("Movie Directory"), server.basedir()))
241                 cfglist.append(getConfigListEntry(_("DVD Device (leave empty for default)"), server.dvdPath()))
242
243                 cfglist.append(getConfigListEntry(_("Transcode MPEG/DVD Video"), server.transcodeVideo()))
244                 cfglist.append(getConfigListEntry(_("Video Codec"), server.videoCodec()))
245                 cfglist.append(getConfigListEntry(_("Video Bitrate"), server.videoBitrate()))
246                 cfglist.append(getConfigListEntry(_("Video Width"), server.videoWidth()))
247                 cfglist.append(getConfigListEntry(_("Video Height"), server.videoHeight()))
248                 #cfglist.append(getConfigListEntry(_("Frames per Second"), config.plugins.vlcplayer.fps))
249                 cfglist.append(getConfigListEntry(_("Correct aspect ratio to"), server.aspectRatio()))
250                 cfglist.append(getConfigListEntry(_("Subtitle overlay"), server.sOverlay()))
251
252                 cfglist.append(getConfigListEntry(_("Transcode MPEG/DVD Audio"), server.transcodeAudio()))
253                 cfglist.append(getConfigListEntry(_("Audio Codec"), server.audioCodec()))
254                 cfglist.append(getConfigListEntry(_("Audio Bitrate"), server.audioBitrate()))
255                 cfglist.append(getConfigListEntry(_("Audio Samplerate"), server.samplerate()))
256                 cfglist.append(getConfigListEntry(_("Audio Channels"), server.audioChannels()))
257
258                 ConfigListScreen.__init__(self, cfglist, session)
259
260                 server.addressType().addNotifier(self.switchAddressType, False)
261                 
262                 self.onClose.append(self.__onClose)
263                 
264         def __onClose(self):
265                 self.server.addressType().deleteNotifier(self.switchAddressType)
266
267         def switchAddressType(self, configElement):
268                 self.server.host().setAsCurrent(configElement.value)
269                 self["config"].invalidate(self.hostConfigListEntry)
270
271         def keySave(self):
272                 self.close(True, self.server)
273
274         def keyCancel(self):
275                 self.close(False, self.server)