fix navigation in mp and use "Filesystems" to indicate topdir
[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
13         def __init__(self, session, func=LIST):
14                 Source.__init__(self)
15                 self.func = func
16                 self.session = session
17                 error = "unknown command (%s)" % func
18                 if func is self.LIST:
19                         self.result = ((error, error, error),)
20                 else:
21                         self.result = (False, error)
22
23         def handleCommand(self, cmd):
24                 self.cmd = cmd
25                 func = self.func
26                 if func is self.LIST:
27                         self.result = self.getFileList(cmd)
28                 elif func is self.PLAY:
29                         self.result = self.playFile(cmd)
30                 elif func is self.COMMAND:
31                         self.result = self.command(cmd)
32                 elif func is self.WRITEPLAYLIST:
33                         self.result = self.writePlaylist(cmd)
34                 elif func is self.ADD:
35                         self.result = self.addFile(cmd)
36
37         def tryOpenMP(self, noCreate = False):
38                 # check if there is an active link
39                 if hasattr(self.session, "mediaplayer"):
40                         mp = self.session.mediaplayer
41                         try:
42                                 len(mp.playlist)
43                         except Exception, e:
44                                 pass
45                         else:
46                                 return mp
47
48                 # check if we actually have the mp installed
49                 try:
50                         from Plugins.Extensions.MediaPlayer.plugin import MediaPlayer, MyPlayList
51                 # nope, bail out
52                 except ImportError, ie:
53                         return None
54                 else:
55                         # mp installed, see if it's running
56                         if isinstance(self.session.current_dialog, MediaPlayer):
57                                 self.session.mediaplayer = self.session.current_dialog
58                         # start new mp
59                         else:
60                                 # bail out if we don't want to open a new mp
61                                 if noCreate:
62                                         return False
63                                 self.session.mediaplayer = self.session.open(MediaPlayer)
64                         return self.session.mediaplayer
65
66         def getFileList(self, param):
67                 if param["path"] == "playlist":
68                         mp = self.tryOpenMP()
69                         # TODO: Fix dummy return if unable to load mp
70                         if mp is None:
71                                 return (("empty", "True", "playlist"),)
72
73                         if mp.playlist:
74                                 return [(serviceRef.toString(), "True", "playlist") for serviceRef in mp.playlist.getServiceRefList()]
75                         else:
76                                 return (("empty", "True", "playlist"),)
77
78                 matchingPattern = "(?i)^.*\.(mp3|ogg|ts|wav|wave|m3u|pls|e2pls|mpg|vob)" #MediaPlayer-Match
79                 useServiceRef = False
80                 if param["types"] == "audio":
81                         matchingPattern = "(?i)^.*\.(mp3|ogg|wav|wave|m3u|pls|e2pls)"
82                         useServiceRef = True
83                 elif param["types"] == "video":
84                         matchingPattern = "(?i)^.*\.(ts|avi|mpeg|m3u|pls|e2pls|mpg|vob)"
85                         useServiceRef = True
86                 elif param["types"] == "any":
87                         matchingPattern = ".*"
88                 else:
89                         matchingPattern = param["types"]
90
91                 path = param["path"]
92                 if path == "Filesystems":
93                         path = None
94                 elif path is not None and not os_path.exists(path):
95                         # TODO: returning something is better than just dying but is this return sane?
96                         return ((None, True, path),)
97
98                 filelist = FileList(path, showDirectories=True, showFiles=True, matchingPattern=matchingPattern, useServiceRef=useServiceRef, isTop=False)
99                 list = filelist.getFileList()
100                 if useServiceRef is True:
101                         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 ]
102                 else:
103                         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 ]
104
105                 return returnList
106
107         def playFile(self, param):
108                 return self.addFile(param, play=True)
109
110         def addFile(self, param, play=False):
111                 # TODO: fix error handling
112                 mp = self.tryOpenMP()
113                 if mp is None:
114                         return (False, "mediaplayer not installed")
115
116                 # TODO: what's the root for?
117                 root = param["root"]
118                 file = param["file"]
119
120                 if not file:
121                         return (False, "missing or invalid parameter file")
122
123                 ref = eServiceReference(file)
124                 if not ref.valid():
125                         if not os_path.exists(file):
126                                 return (False, "%s is neither a valid reference nor a valid file" % file)
127                         ref = eServiceReference(4097, 0, file)
128
129                 mp.playlist.addFile(ref)
130                 if play:
131                         mp.playServiceRefEntry(ref)
132                         ret = (True, "%s added to playlist and playback started" % file)
133                 else:
134                         ret = (True, "%s added to playlist" % file)
135
136                 mp.playlist.updateList()
137                 return ret
138
139         def writePlaylist(self, param):
140                 filename = "playlist/%s.e2pls" % param
141                 from Tools.Directories import resolveFilename, SCOPE_CONFIG
142
143                 # TODO: fix error handling
144                 mp = self.tryOpenMP()
145                 if mp is None:
146                         return (False, "mediaplayer not installed")
147
148                 fullPath = resolveFilename(SCOPE_CONFIG, filename)
149                 mp.playlistIOInternal.save(resolveFilename(SCOPE_CONFIG, filename))
150                 return (True, "playlist saved to %s" % fullPath)
151
152         def command(self, param):
153                 # TODO: fix error handling
154                 noCreate = True if param == "exit" else False
155                 mp = self.tryOpenMP(noCreate=noCreate)
156                 if mp is None:
157                         return (False, "mediaplayer not installed")
158                 elif mp is False:
159                         return (True, "mediaplayer was not active")
160
161                 if param == "previous":
162                         mp.previousMarkOrEntry()
163                 elif param == "play":
164                         mp.playEntry()
165                 elif param == "pause":
166                         mp.pauseEntry()
167                 elif param == "next":
168                         mp.nextEntry()
169                 elif param == "stop":
170                         mp.stopEntry()
171                 elif param == "exit":
172                         mp.exit()
173                 else:
174                         return (False, "unknown parameter %s" % param)
175                 return (True, "executed %s" % param)
176
177         list = property(lambda self: self.result)
178         lut = {
179                 "ServiceReference": 0,
180                 "IsDirectory": 1,
181                 "Root": 2,
182         }