fix init of components.filelist
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / MP.py
1 from enigma import eServiceReference, iServiceInformation, eServiceCenter
2 from Components.Sources.Source import Source
3 from ServiceReference import ServiceReference
4 from Components.FileList import FileList
5 from os import path as os_path
6
7 class MP( Source):
8     LIST = 0
9     PLAY = 1
10     COMMAND = 3
11     WRITEPLAYLIST = 4
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         self.result = [[error,error,error]]
19     
20     def handleCommand(self,cmd):       
21         self.cmd = cmd
22         if self.func is self.LIST:
23             self.result = self.getFileList(cmd)
24         elif self.func is self.PLAY:
25             self.result = self.playFile(cmd)
26         elif self.func is self.COMMAND:
27             self.result = self.command(cmd)
28         elif self.func is self.WRITEPLAYLIST:
29             self.result = self.writePlaylist(cmd)
30
31     def tryOpenMP(self):
32         # See is the Link is still active
33         if self.session.mediaplayer is not None:
34             try:
35                 test = len(self.session.mediaplayer.playlist)
36                 return True
37             except:
38                 pass
39         
40         # Link inactive, instantiate new MP
41         try:
42             from Plugins.Extensions.MediaPlayer.plugin import MediaPlayer, MyPlayList
43             self.session.mediaplayer = self.session.open(MediaPlayer)
44             self.session.mediaplayer.playlist = MyPlayList()
45             return True
46         
47         # No MP installed
48         except ImportError, ie:
49             return False
50            
51     def getFileList(self,param):
52         print "getFileList:",param
53         
54         returnList = []
55         
56         if param["path"] == "playlist":
57             # TODO: Fix dummy return if unable to load mp
58             if not self.tryOpenMP():
59                 returnList.append(["empty","True","playlist"])
60                 return returnList
61             
62             mp = self.session.mediaplayer
63             if len(mp.playlist) != 0:
64                 serviceRefList = mp.playlist.getServiceRefList()
65                 for count in range(len(serviceRefList)):
66                     returnList.append([serviceRefList[count].toString(),"True","playlist"])
67             else:
68                 returnList.append(["empty","True","playlist"])
69             
70             return returnList
71
72         matchingPattern = "(?i)^.*\.(mp3|ogg|ts|wav|wave|m3u|pls|e2pls|mpg|vob)" #MediaPlayer-Match
73         useServiceRef = False
74         if param["types"] == "audio":
75             matchingPattern = "(?i)^.*\.(mp3|ogg|wav|wave|m3u|pls|e2pls)"
76             useServiceRef = True
77         elif param["types"] == "video":
78             matchingPattern = "(?i)^.*\.(ts|avi|mpeg|m3u|pls|e2pls|mpg|vob)"
79             useServiceRef = True
80         elif param["types"] == "any":
81             matchingPattern = ".*"
82         else:
83             matchingPattern = param["types"]
84
85         filelist = FileList(param["path"], showDirectories = True, showFiles = True, matchingPattern = matchingPattern, useServiceRef = useServiceRef, isTop = False)
86         list = filelist.getFileList()
87         for x in list:
88             if useServiceRef == True:
89                 if x[0][1] == False: #isDir
90                     returnList.append([x[0][0].toString(),x[0][1],param["path"]])
91                 else:
92                     returnList.append([x[0][0],x[0][1],param["path"]])
93             else:
94                 if x[0][1] == False: #isDir
95                     returnList.append([param["path"]+x[0][0],x[0][1],param["path"]])
96                 else:
97                     returnList.append([x[0][0],x[0][1],param["path"]])
98
99         return returnList
100
101     def playFile(self,param):
102         print "playFile: ",param
103         # TODO: fix error handling
104         if not self.tryOpenMP():
105             return
106
107         root = param["root"]
108         file = param["file"]
109
110         mp = self.session.mediaplayer
111         ref = eServiceReference(file)
112         
113         mp.switchToPlayList()
114         
115         if len(mp.playlist) == 1:
116                 mp.changeEntry(0)
117         
118         mp.playlist.addFile(ref)
119
120         #mp.playServiceRefEntry(ref)
121         print "len len(mp.playlist.getServiceRefList()): ",len(mp.playlist.getServiceRefList())
122         if len(mp.playlist.getServiceRefList()):
123             lastEntry = len(mp.playlist.getServiceRefList()) -1
124             currref = mp.playlist.getServiceRefList()[lastEntry]
125             if self.session.nav.getCurrentlyPlayingServiceReference() is None or currref != self.session.nav.getCurrentlyPlayingServiceReference():
126                 self.session.nav.playService(mp.playlist.getServiceRefList()[lastEntry])
127                 info = eServiceCenter.getInstance().info(currref)
128                 description = info and info.getInfoString(currref, iServiceInformation.sDescription) or ""
129                 mp["title"].setText(description)
130             mp.unPauseService()
131             #mp.playEntry(len(self.playlist.getServiceRefList()))
132         
133         mp.playlist.updateList()
134         mp.infoTimerFire()
135         return
136     #
137     def writePlaylist(self,param):
138         print "writePlaylist: ",param
139         filename = "playlist/%s.e2pls" % param
140         from Tools.Directories import resolveFilename, SCOPE_CONFIG
141         
142         # TODO: fix error handling
143         if not self.tryOpenMP():
144             return
145         
146         mp = self.session.mediaplayer
147         mp.playlistIOInternal.save(resolveFilename(SCOPE_CONFIG, filename))
148         
149     def command(self,param):
150         print "command: ",param
151         param = int(param)
152         
153         # TODO: fix error handling
154         if not self.tryOpenMP():
155             return
156         
157         mp = self.session.mediaplayer
158         
159         if param == 0:
160             mp.previousEntry()
161         elif param == 1:
162             mp.playEntry()
163         elif param == 2:
164             mp.pauseEntry()
165         elif param == 3:
166             mp.nextEntry()
167         elif param == 4:
168             mp.stopEntry()
169         elif param == 5:
170             mp.exit()
171         
172         return
173     
174     def getList(self):
175         return self.result
176     
177     list = property(getList)
178     lut = {"ServiceReference": 0
179            ,"IsDirectory": 1
180            ,"Root": 2
181            }