VlcServer.play() now wants a session and not a screen as first argument
[enigma2-plugins.git] / vlcplayer / src / VlcMediaList.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.Label import Label
15 from Components.Button import Button
16 from Components.Pixmap import Pixmap
17 from Screens.MessageBox import MessageBox
18 from Screens.Screen import Screen
19
20 from pyexpat import ExpatError
21
22 from VlcFileList import VlcFileList
23 from VlcPlayList import VlcPlayList
24 from . import _
25
26
27 class VlcMediaListScreen(Screen):
28         skin ="""
29                 <screen size="560,400" position="80,100" name="VlcMediaListScreen" >
30                         <ePixmap name="red"    position="0,355"   zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
31                         <ePixmap name="green"  position="140,355" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
32                         <ePixmap name="yellow" position="280,355" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
33                         <ePixmap name="blue"   position="420,355" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
34                         <widget name="key_red" position="0,355" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
35                         <widget name="key_green" position="140,355" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
36                         <widget name="key_yellow" position="280,355" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
37                         <widget name="key_blue" position="420,355" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
38                         <ePixmap size="551,325" alphatest="on" position="5,30" pixmap="skin_default/border_epg.png" />
39                         <widget size="320,25" alphatest="on" position="5,10" zPosition="1" name="filelist_button_sel" pixmap="skin_default/epg_now.png" />
40                         <widget size="320,25" alphatest="on" position="5,10" zPosition="1" name="playlist_button_sel" pixmap="skin_default/epg_next.png" />
41                         <widget valign="center" transparent="1" size="108,22" backgroundColor="#25062748" position="5,10" zPosition="2" name="filelist_text" halign="center" font="Regular;18" />
42                         <widget valign="center" transparent="1" size="108,22" backgroundColor="#25062748" position="111,10" zPosition="2" name="playlist_text" halign="center" font="Regular;18" />
43                         <widget size="540,320" scrollbarMode="showOnDemand" position="11,35" name="filelist" />
44                         <widget size="540,320" scrollbarMode="showOnDemand" position="11,35" name="playlist" />
45                 </screen>"""
46         
47         defaultFilter = "(?i)\.(avi|mpeg|mpg|divx|xvid|mp4|mov|ts|vob|wmv|mkv|iso|m3u|pls|xspf)$"
48
49         def __init__(self, session, server):
50                 Screen.__init__(self, session)
51                 self.session = session
52                 self.server = server
53                 self["filelist"] = VlcFileList(self.getFilesAndDirsCB, server.getBasedir(), self.defaultFilter)
54                 self["playlist"] = VlcPlayList(self.getPlaylistEntriesCB)
55                 self["key_red"] = Button(_("filter off"))
56                 self["key_green"] = Button(_("refresh"))
57                 self["key_yellow"] = Button(_("Serverlist"))
58                 self["key_blue"] = Button(_("play DVD"))
59                 self["filelist_button_sel"] = Pixmap()
60                 self["playlist_button_sel"] = Pixmap()
61                 self["more_button_sel"] = Pixmap()
62                 self["filelist_text"] = Label(_("Filelist"))
63                 self["playlist_text"] = Label(_("Playlist"))
64                 self["server_name"] = Label(server.getName())
65                 self["current_dir"] = Label()
66                 
67                 self["actions"] = ActionMap(["WizardActions", "MenuActions", "ShortcutActions", "MoviePlayerActions", "EPGSelectActions"],
68                         {
69                          "back":        self.close,
70                          "red":         self.keyToggleFilter,
71                          "green":       self.keyUpdate,
72                          "yellow":      self.keyOpenServerlist,
73                          "blue":        self.keyDvd,
74                          "up":          self.up,
75                          "down":        self.down,
76                          "left":        self.left,
77                          "right":       self.right,
78                          "ok":          self.ok,
79                          "prevBouquet": self.switchToFileList,
80                          "nextBouquet": self.switchToPlayList,
81                          }, -1)
82                 self.currentList = None
83                 self.playlistIds = []
84
85                 self.onClose.append(self.__onClose)
86                 self.onFirstExecBegin.append(self.__onFirstExecBegin)
87
88         def __onFirstExecBegin(self):
89                 self.setTitle("vlc://" + (self.server.getName() or self.server.getHost()) + "/" + self.server.getBasedir())
90                 self["current_dir"].setText(self.server.getBasedir())
91                         
92         def __onClose(self):
93                 try:
94                         for id in self.playlistIds:
95                                 self.server.delete(id)
96                 except Exception, e:
97                         pass
98                         
99         def close(self, proceed = False):
100                 Screen.close(self, proceed)
101
102         def keyUpdate(self):
103                 self.updateFilelist()
104                 self.updatePlaylist()
105                 if self.currentList == self["playlist"]:
106                         self.switchToPlayList()
107                 else:
108                         self.switchToFileList()
109
110         def updatePlaylist(self):
111                 self["playlist"].update()
112
113         def updateFilelist(self):
114                 self["filelist"].update()
115
116         def keyToggleFilter(self):
117                 if self["filelist"].regex is None:
118                         self["filelist"].changeRegex(self.defaultFilter)
119                         self["key_red"].setText(_("filter off"))
120                 else:
121                         self["filelist"].changeRegex(None)
122                         self["key_red"].setText(_("filter on"))
123                 try:
124                         self.updateFilelist()
125                 except Exception, e:
126                         self.session.open(
127                                 MessageBox, _("Error updating filelist from server %s:\n%s" % (
128                                                 self.server.getName(), e)
129                                         ), MessageBox.TYPE_ERROR)
130
131         def keyDvd(self):
132                 self.play("dvdsimple://" + self.server.getDvdPath(), "DVD")
133
134         def keyOpenServerlist(self):
135                 self.close(True)
136
137         def up(self):
138                 self.currentList.up()
139
140         def down(self):
141                 self.currentList.down()
142
143         def left(self):
144                 self.currentList.pageUp()
145
146         def right(self):
147                 self.currentList.pageDown()
148
149         def play(self, media, name):
150                 self.server.play(self.session, media, name, self.currentList)
151
152         def ok(self):
153                 media, name = self.currentList.activate()
154                 if media is not None:
155                         if media.lower().endswith(".m3u") or media.lower().endswith(".pls") or media.lower().endswith(".xspf"):
156                                 try:
157                                         id = self.server.loadPlaylist(media)
158                                         if id is not None:
159                                                 self.playlistIds.append(id)
160                                                 self.updatePlaylist()
161                                                 self.switchToPlayList()
162                                 except Exception, e:
163                                         self.session.open(
164                                                 MessageBox, _("Error loading playlist %s into server %s:\n%s" % (
165                                                                 media, self.server.getName(), e)
166                                                         ), MessageBox.TYPE_ERROR)
167                         elif media.lower().endswith(".iso"):
168                                 self.play("dvdsimple://" + media, "DVD")
169                         else:
170                                 self.play(media, name)
171                 elif name is not None:
172                         self.setTitle("vlc://" + (self.server.getName() or self.server.getHost()) + "/" + name)
173                         self["current_dir"].setText(name)
174
175         def getFilesAndDirsCB(self, currentDirectory, regex):
176                 try:
177                         return self.server.getFilesAndDirs(currentDirectory, regex)
178                 except ExpatError, e:
179                         self.session.open(
180                                 MessageBox, _("Error loading playlist into server %s:\n%s" % (
181                                                 self.server.getName(), e)
182                                         ), MessageBox.TYPE_ERROR)
183                         raise ExpatError, e
184                 except Exception, e:
185                         self.session.open(
186                                 MessageBox, _("Error loading filelist into server %s:\n%s" % (
187                                                 self.server.getName(), e)
188                                         ), MessageBox.TYPE_ERROR)
189                 return None
190
191         def getPlaylistEntriesCB(self):
192                 try:
193                         return self.server.getPlaylistEntries()
194                 except ExpatError, e:
195                         self.session.open(
196                                 MessageBox, _("Error loading playlist into server %s:\n%s" % (
197                                                 self.server.getName(), e)
198                                         ), MessageBox.TYPE_ERROR)
199                 except Exception, e:
200                         self.session.open(
201                                 MessageBox, _("Error loading playlist into server %s:\n%s" % (
202                                                 self.server.getName(), e)
203                                         ), MessageBox.TYPE_ERROR)
204                 return None
205
206         def switchLists(self):
207                 if self.currentList == self["filelist"]:
208                         self.switchToPlayList()
209                 else:
210                         self.switchToFileList()
211
212         def switchToFileList(self):
213                 self["filelist"].selectionEnabled(1)
214                 self["filelist"].show()
215                 self["filelist_button_sel"].show()
216                 self["playlist"].selectionEnabled(0)
217                 self["playlist"].hide()
218                 self["playlist_button_sel"].hide()
219                 self.currentList = self["filelist"]
220
221         def switchToPlayList(self):
222                 self["filelist"].selectionEnabled(0)
223                 self["filelist"].hide()
224                 self["filelist_button_sel"].hide()
225                 self["playlist"].selectionEnabled(1)
226                 self["playlist"].show()
227                 self["playlist_button_sel"].show()
228                 self.currentList = self["playlist"]