MP.py: update matchingPattern
[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                 return (True, "%s added to playlist and/or playback started" % (file))
143
144         def removeFile(self, file):
145                 # TODO: fix error handling
146                 mp = self.tryOpenMP()
147                 if mp is None:
148                         return (False, "mediaplayer not installed")
149
150                 ref = eServiceReference(file)
151                 if not ref.valid():
152                         ref = eServiceReference(4097, 0, file)
153                         if not ref.valid():
154                                 return (False, "%s is neither a valid reference nor a valid file" % file)
155
156                 serviceRefList = mp.playlist.getServiceRefList()
157                 i = 0
158                 for mpref in serviceRefList:
159                         if mpref == ref:
160                                 mp.playlist.deleteFile(i)
161                                 mp.playlist.updateList()
162                                 return (True, "%s removed from playlist" % file)
163                         i += 1
164
165                 return (False, "%s not found in playlist" % file)
166
167         def loadPlaylist(self, param):
168                 filename = "playlist/%s" % param
169                 from Tools.Directories import resolveFilename, SCOPE_CONFIG
170
171                 # TODO: fix error handling
172                 mp = self.tryOpenMP()
173                 if mp is None:
174                         return (False, "mediaplayer not installed")
175
176                 fullPath = resolveFilename(SCOPE_CONFIG, filename)
177                 mp.PlaylistSelected(fullPath)
178                 return (True, "playlist loaded from %s" % fullPath)
179
180         def writePlaylist(self, param):
181                 filename = "playlist/%s.e2pls" % param
182                 from Tools.Directories import resolveFilename, SCOPE_CONFIG
183
184                 # TODO: fix error handling
185                 mp = self.tryOpenMP()
186                 if mp is None:
187                         return (False, "mediaplayer not installed")
188
189                 fullPath = resolveFilename(SCOPE_CONFIG, filename)
190                 mp.playlistIOInternal.save(fullPath)
191                 return (True, "playlist saved to %s" % fullPath)
192
193         def command(self, param):
194                 # TODO: fix error handling
195                 noCreate = True if param == "exit" else False
196                 mp = self.tryOpenMP(noCreate=noCreate)
197                 if mp is None:
198                         return (False, "mediaplayer not installed")
199                 elif mp is False:
200                         return (True, "mediaplayer was not active")
201
202                 if param == "previous":
203                         mp.previousMarkOrEntry()
204                 elif param == "play":
205                         mp.playEntry()
206                 elif param == "pause":
207                         mp.pauseEntry()
208                 elif param == "next":
209                         mp.nextEntry()
210                 elif param == "stop":
211                         mp.stopEntry()
212                 elif param == "exit":
213                         mp.exit()
214                 elif param == "shuffle":
215                         mp.playlist.PlayListShuffle()
216                 elif param == "clear":
217                         mp.clear_playlist()
218                 else:
219                         return (False, "unknown parameter %s" % param)
220                 return (True, "executed %s" % param)
221
222         def getCurrent(self):
223                 mp = self.tryOpenMP()
224                 if mp is None:
225                         msg = "mediaplayer not installed"
226                         return ((msg, msg, msg, msg, msg, msg),)
227
228                 return ((
229                         mp["artist"].getText(),
230                         mp["title"].getText(),
231                         mp["album"].getText(),
232                         mp["year"].getText(),
233                         mp["genre"].getText(),
234                         mp["coverArt"].coverArtFileName
235                 ),)
236
237         def getList(self):
238                 if self.func is self.CURRENT:
239                         return self.getCurrent()
240                 return self.result
241
242         def getLut(self):
243                 if self.func is self.CURRENT:
244                         return {
245                                 "Artist": 0,
246                                 "Title": 1,
247                                 "Album": 2,
248                                 "Year": 3,
249                                 "Genre": 4,
250                                 "CoverFilename": 5,
251                         }
252                 return  {
253                         "ServiceReference": 0,
254                         "IsDirectory": 1,
255                         "Root": 2,
256                 }
257
258         list = property(getList)
259         lut = property(getLut)