cleanup
[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 Exception:
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                 if param["path"] == "playlist":
55                         # TODO: Fix dummy return if unable to load mp
56                         if not self.tryOpenMP():
57                                 return (("empty", "True", "playlist"),)
58
59                         mp = self.session.mediaplayer
60                         if mp.playlist:
61                                 return [(serviceRef.toString(), "True", "playlist") for serviceRef in mp.playlist.getServiceRefList()]
62                         else:
63                                 return (("empty", "True", "playlist"),)
64
65                 returnList = []
66
67                 matchingPattern = "(?i)^.*\.(mp3|ogg|ts|wav|wave|m3u|pls|e2pls|mpg|vob)" #MediaPlayer-Match
68                 useServiceRef = False
69                 if param["types"] == "audio":
70                         matchingPattern = "(?i)^.*\.(mp3|ogg|wav|wave|m3u|pls|e2pls)"
71                         useServiceRef = True
72                 elif param["types"] == "video":
73                         matchingPattern = "(?i)^.*\.(ts|avi|mpeg|m3u|pls|e2pls|mpg|vob)"
74                         useServiceRef = True
75                 elif param["types"] == "any":
76                         matchingPattern = ".*"
77                 else:
78                         matchingPattern = param["types"]
79
80                 filelist = FileList(param["path"], showDirectories=True, showFiles=True, matchingPattern=matchingPattern, useServiceRef=useServiceRef, isTop=False)
81                 list = filelist.getFileList()
82                 for x in list:
83                         if useServiceRef == True:
84                                 if x[0][1] == False: #isDir
85                                         returnList.append((x[0][0].toString(), x[0][1], param["path"]))
86                                 else:
87                                         returnList.append((x[0][0], x[0][1], param["path"]))
88                         else:
89                                 if x[0][1] == False: #isDir
90                                         returnList.append((param["path"] + x[0][0], x[0][1], param["path"]))
91                                 else:
92                                         returnList.append((x[0][0], x[0][1], param["path"]))
93
94                 return returnList
95
96         def playFile(self, param):
97                 print "playFile: ", param
98                 # TODO: fix error handling
99                 if not self.tryOpenMP():
100                         return
101
102                 root = param["root"]
103                 file = param["file"]
104
105                 mp = self.session.mediaplayer
106                 ref = eServiceReference(file)
107
108                 mp.switchToPlayList()
109
110                 if len(mp.playlist) == 1:
111                                 mp.changeEntry(0)
112
113                 mp.playlist.addFile(ref)
114
115                 #mp.playServiceRefEntry(ref)
116                 sRefList = mp.playlist.getServiceRefList()
117                 Len = len(sRefList)
118                 print "len len(mp.playlist.getServiceRefList()): ", len(mp.playlist.getServiceRefList())
119                 if Len:
120                         lastEntry = Len - 1
121                         currref = sRefList[lastEntry]
122                         if self.session.nav.getCurrentlyPlayingServiceReference() is None or currref != self.session.nav.getCurrentlyPlayingServiceReference():
123                                 self.session.nav.playService(sRefList[lastEntry])
124                                 info = eServiceCenter.getInstance().info(currref)
125                                 description = info and info.getInfoString(currref, iServiceInformation.sDescription) or ""
126                                 mp["title"].setText(description)
127                         mp.unPauseService()
128                         #mp.playEntry(len(self.playlist.getServiceRefList()))
129
130                 mp.playlist.updateList()
131                 mp.infoTimerFire()
132
133         def writePlaylist(self, param):
134                 print "writePlaylist: ", param
135                 filename = "playlist/%s.e2pls" % param
136                 from Tools.Directories import resolveFilename, SCOPE_CONFIG
137
138                 # TODO: fix error handling
139                 if not self.tryOpenMP():
140                         return
141
142                 mp = self.session.mediaplayer
143                 mp.playlistIOInternal.save(resolveFilename(SCOPE_CONFIG, filename))
144
145         def command(self, param):
146                 print "command: ", param
147
148                 # TODO: fix error handling
149                 if not self.tryOpenMP():
150                         return
151
152                 mp = self.session.mediaplayer
153
154                 if param == "previous":
155                         mp.previousEntry()
156                 elif param == "play":
157                         mp.playEntry()
158                 elif param == "pause":
159                         mp.pauseEntry()
160                 elif param == "next":
161                         mp.nextEntry()
162                 elif param == "stop":
163                         mp.stopEntry()
164                 elif param == "exit":
165                         mp.exit()
166
167         list = property(lambda self: self.result)
168         lut = {"ServiceReference": 0
169                         , "IsDirectory": 1
170                         , "Root": 2
171                         }