webif/MP: use SCOPE_PLAYLIST instead of SCOPE_CONFIG as base for playlists
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / MP.py
1 from enigma import eServiceReference
2 from Components.Sources.Source import Source
3 from Components.FileList import FileList
4 from os import path as os_path
5
6 class MP(Source):
7         LIST = 0
8         PLAY = 1
9         COMMAND = 3
10         WRITEPLAYLIST = 4
11         ADD = 5
12         REMOVE = 6
13         CURRENT = 7
14         LOADPLAYLIST = 8
15
16         def __init__(self, session, func=LIST):
17                 Source.__init__(self)
18                 self.func = func
19                 self.session = session
20                 error = "Unknown command (%s)" % func
21                 if func is self.LIST:
22                         self.result = ((error, error, error),)
23                 else:
24                         self.result = (False, error)
25
26         def handleCommand(self, cmd):
27                 self.cmd = cmd
28                 func = self.func
29                 if func is self.LIST:
30                         self.result = self.getFileList(cmd)
31                 elif func is self.PLAY:
32                         self.result = self.playFile(cmd)
33                 elif func is self.COMMAND:
34                         self.result = self.command(cmd)
35                 elif func is self.WRITEPLAYLIST:
36                         self.result = self.writePlaylist(cmd)
37                 elif func is self.LOADPLAYLIST:
38                         self.result = self.loadPlaylist(cmd)
39                 elif func is self.REMOVE:
40                         self.result = self.removeFile(cmd)
41                 elif func is self.ADD:
42                         param = { "file" : cmd, "root" : None}
43                         self.result = self.addFile(param)
44
45         def tryOpenMP(self, noCreate = False):
46                 # check if there is an active link
47                 if hasattr(self.session, "mediaplayer"):
48                         mp = self.session.mediaplayer
49                         try:
50                                 len(mp.playlist)
51                         except Exception, e:
52                                 pass
53                         else:
54                                 return mp
55
56                 # check if we actually have the mp installed
57                 try:
58                         from Plugins.Extensions.MediaPlayer.plugin import MediaPlayer, MyPlayList
59                 # nope, bail out
60                 except ImportError, ie:
61                         return None
62                 else:
63                         # mp installed, see if it's running
64                         if isinstance(self.session.current_dialog, MediaPlayer):
65                                 self.session.mediaplayer = self.session.current_dialog
66                         # start new mp
67                         else:
68                                 # bail out if we don't want to open a new mp
69                                 if noCreate:
70                                         return False
71                                 self.session.mediaplayer = self.session.open(MediaPlayer)
72                         return self.session.mediaplayer
73
74         def getFileList(self, param):
75                 if param["path"] == "playlist":
76                         mp = self.tryOpenMP()
77                         # TODO: Fix dummy return if unable to load mp
78                         if mp is None:
79                                 return (("empty", True, "playlist"),)
80
81                         if mp.playlist:
82                                 return [(serviceRef.getPath(), False, "playlist") for serviceRef in mp.playlist.getServiceRefList()]
83                         else:
84                                 return (("empty", True, "playlist"),)
85
86                 matchingPattern = "(?i)^.*\.(mp2|mp3|ogg|ts|wav|wave|m3u|pls|e2pls|mpg|vob|avi|divx|m4v|mkv|mp4|m4a|dat|flac|mov|m2ts)" #MediaPlayer-Match
87                 useServiceRef = False
88                 if param["types"] == "audio":
89                         matchingPattern = "(?i)^.*\.(mp3|ogg|wav|wave|m3u|pls|e2pls)"
90                         useServiceRef = True
91                 elif param["types"] == "video":
92                         matchingPattern = "(?i)^.*\.(ts|avi|mpeg|m3u|pls|e2pls|mpg|vob)"
93                         useServiceRef = True
94                 elif param["types"] == "any":
95                         matchingPattern = ".*"
96                 elif param["types"]:
97                         matchingPattern = param["types"]
98
99                 path = param["path"]
100                 if path == "Filesystems":
101                         path = None
102                 elif path is not None and not os_path.isdir(path):
103                         # TODO: returning something is better than just dying but is this return sane?
104                         return ((None, True, path),)
105
106                 filelist = FileList(path, showDirectories=True, showFiles=True, matchingPattern=matchingPattern, useServiceRef=useServiceRef, isTop=False)
107                 list = filelist.getFileList()
108                 if useServiceRef is True:
109                         returnList = [ (x[0][0].toString(), x[0][1], path) if x[0][1] is False else (x[0][0], x[0][1], path) for x in list ]
110                 else:
111                         returnList = [ (param["path"] + x[0][0], x[0][1], path) if x[0][1] is False else (x[0][0], x[0][1], path) for x in list ]
112
113                 return returnList
114
115         def playFile(self, param):
116                 return self.addFile(param, doPlay=True)
117
118         def addFile(self, param, doPlay=False):
119                 # TODO: fix error handling
120                 mp = self.tryOpenMP()
121                 if mp is None:
122                         return (False, "Mediaplayer not installed")
123
124                 file = param["file"]
125                 doAdd = False if param["root"] == "playlist" else True
126
127                 if not file:
128                         return (False, "Missing or invalid parameter file")
129
130                 ref = eServiceReference(file)
131                 if not ref.valid():
132                         if not os_path.isfile(file):
133                                 return (False, "'%s' is neither a valid reference nor a valid file" % file)
134                         ref = eServiceReference(4097, 0, file)
135
136                 if doAdd:
137                         mp.playlist.addFile(ref)
138                 if doPlay:
139                         mp.playServiceRefEntry(ref)
140
141                 mp.playlist.updateList()
142                 if doPlay:
143                         return (True, "Playback of '%s' started" % (file))
144                 else:
145                         return (True, "'%s' has been added to playlist" % (file))
146         
147         def removeFile(self, file):
148                 # TODO: fix error handling
149                 mp = self.tryOpenMP()
150                 if mp is None:
151                         return (False, "Mediaplayer not installed")
152
153                 ref = eServiceReference(file)
154                 if not ref.valid():
155                         ref = eServiceReference(4097, 0, file)
156                         if not ref.valid():
157                                 return (False, "'%s' is neither a valid reference nor a valid file" % file)
158
159                 serviceRefList = mp.playlist.getServiceRefList()
160                 i = 0
161                 for mpref in serviceRefList:
162                         if mpref == ref:
163                                 mp.playlist.deleteFile(i)
164                                 mp.playlist.updateList()
165                                 return (True, "'%s' removed from playlist" % file)
166                         i += 1
167
168                 return (False, "'%s' not found in playlist" % file)
169
170         def loadPlaylist(self, param):
171                 from Tools.Directories import resolveFilename, SCOPE_PLAYLIST
172
173                 # TODO: fix error handling
174                 mp = self.tryOpenMP()
175                 if mp is None:
176                         return (False, "Mediaplayer not installed")
177
178                 fullPath = resolveFilename(SCOPE_PLAYLIST, param)
179                 mp.PlaylistSelected((param, fullPath))
180                 return (True, "Playlist loaded from '%s'" % fullPath)
181
182         def writePlaylist(self, param):
183                 filename = "%s.e2pls" % param
184                 from Tools.Directories import resolveFilename, SCOPE_PLAYLIST
185
186                 # TODO: fix error handling
187                 mp = self.tryOpenMP()
188                 if mp is None:
189                         return (False, "Mediaplayer not installed")
190
191                 fullPath = resolveFilename(SCOPE_PLAYLIST, filename)
192                 mp.playlistIOInternal.save(fullPath)
193                 return (True, "Playlist saved to '%s'" % fullPath)
194
195         def command(self, param):
196                 # TODO: fix error handling
197                 noCreate = True if param == "exit" else False
198                 mp = self.tryOpenMP(noCreate=noCreate)
199                 if mp is None:
200                         return (False, "Mediaplayer not installed")
201                 elif mp is False:
202                         return (True, "Mediaplayer was not active")
203
204                 if param == "previous":
205                         mp.previousMarkOrEntry()
206                 elif param == "play":
207                         mp.playEntry()
208                 elif param == "pause":
209                         mp.pauseEntry()
210                 elif param == "next":
211                         mp.nextEntry()
212                 elif param == "stop":
213                         mp.stopEntry()
214                 elif param == "exit":
215                         mp.exit()
216                 elif param == "shuffle":
217                         mp.playlist.PlayListShuffle()
218                 elif param == "clear":
219                         mp.clear_playlist()
220                 else:
221                         return (False, "Unknown parameter %s" % param)
222                 return (True, "Command '%s' executed" % param)
223
224         def getCurrent(self):
225                 mp = self.tryOpenMP()
226                 if mp is None:
227                         msg = "Mediaplayer not installed"
228                         return ((msg, msg, msg, msg, msg, msg),)
229
230                 return ((
231                         mp["artist"].getText(),
232                         mp["title"].getText(),
233                         mp["album"].getText(),
234                         mp["year"].getText(),
235                         mp["genre"].getText(),
236                         mp["coverArt"].coverArtFileName
237                 ),)
238
239         def getList(self):
240                 if self.func is self.CURRENT:
241                         return self.getCurrent()
242                 return self.result
243
244         def getLut(self):
245                 if self.func is self.CURRENT:
246                         return {
247                                 "Artist": 0,
248                                 "Title": 1,
249                                 "Album": 2,
250                                 "Year": 3,
251                                 "Genre": 4,
252                                 "CoverFilename": 5,
253                         }
254                 return  {
255                         "ServiceReference": 0,
256                         "IsDirectory": 1,
257                         "Root": 2,
258                 }
259
260         list = property(getList)
261         lut = property(getLut)