remove unused import
[enigma2-plugins.git] / vlcplayer / src / VlcFileList.py
1 # -*- coding: ISO-8859-1 -*-
2 #===============================================================================
3 # VLC Player Plugin by A. Lätsch 2007
4 #                   modified by Volker Christian 2008
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2, or (at your option) any later
9 # version.
10 #===============================================================================
11
12
13 import re
14
15 from enigma import eListboxPythonMultiContent, RT_HALIGN_LEFT, gFont
16 from Tools.LoadPixmap import LoadPixmap
17 from Tools.Directories import SCOPE_SKIN_IMAGE, SCOPE_PLUGINS, resolveFilename
18 from Components.MenuList import MenuList
19
20 from pyexpat import ExpatError
21
22
23 MEDIA_EXTENSIONS = {
24                 "mp3": "music",
25                 "wav": "music",
26                 "ogg": "music",
27                 "ts": "movie",
28                 "avi": "movie",
29                 "mpg": "movie",
30                 "mpeg": "movie",
31                 "wmv": "movie",
32                 "mov": "movie",
33                 "iso": "movie",
34                 "mkv": "movie",
35                 "flv": "movie"
36         }
37
38
39 PLAYLIST_EXTENSIONS = {
40                 "m3u": "playlist.png",
41                 "pls": "playlist.png",
42                 "xspf": "playlist.png",
43         }
44
45
46 def VlcFileListEntry(name, path, isDir = False):
47         res = [ (path, isDir) ]
48         res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
49         if isDir:
50                 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
51         else:
52                 extension = name.split('.')
53                 extension = extension[-1].lower()
54                 if MEDIA_EXTENSIONS.has_key(extension):
55                         png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + MEDIA_EXTENSIONS[extension] + ".png"))
56                 elif PLAYLIST_EXTENSIONS.has_key(extension):
57                         png = LoadPixmap(resolveFilename(SCOPE_PLUGINS, "Extensions/VlcPlayer/") + PLAYLIST_EXTENSIONS[extension])
58                 else:
59                         png = None
60         if png is not None:
61                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
62
63         return res
64
65
66 class VlcFileList(MenuList):
67         def __init__(self, getFilesAndDirsCB, baseDir, matchingPattern):
68                 MenuList.__init__(self, list, False, eListboxPythonMultiContent)
69                 self.l.setFont(0, gFont("Regular", 18))
70                 self.l.setItemHeight(23)
71                 self.currentDirectory = baseDir
72                 self.getFilesAndDirsCB = getFilesAndDirsCB
73                 self.changeRegex(matchingPattern)
74
75         def update(self):
76                 success = False
77                 filelistEntries = self.getFilesAndDirsCB(self.currentDirectory, self.regex)
78                 fileEntries = []
79                 directoryEntries = []
80                 if filelistEntries is not None:
81                         files, directories = filelistEntries
82                         for file in files:
83                                 [name, path] = file
84                                 fileEntries.append(VlcFileListEntry(name, path))
85                         for directory in directories:
86                                 [name, path] = directory
87                                 directoryEntries.append(VlcFileListEntry(name, path, True))
88                         fileEntries.sort(cmp = lambda x, y: cmp(x[0], y[0]))
89                         directoryEntries.sort(cmp = lambda x, y: cmp(x[0], y[0]))
90                         success = True
91                 self.list = directoryEntries + fileEntries
92                 self.l.setList(self.list)
93                 self.moveToIndex(0)
94                 return success
95
96         def isVideoTS(self):
97                 for e in self.list:
98                         if e[0][1] == True and e[0][0].upper().endswith("VIDEO_TS"):
99                                 return True
100                 return False
101
102         def changeDirectory(self, directory):
103                 previousDirectory = self.currentDirectory
104                 self.currentDirectory = directory
105                 try:
106                         if self.update():
107                                 if self.isVideoTS():
108                                         ret = "dvdsimple://" + self.currentDirectory + "/VIDEO_TS", self.currentDirectory
109                                         self.currentDirectory = previousDirectory
110                                         self.update()
111                                 else:
112                                         ret = None, self.currentDirectory
113                         else:
114                                 self.currentDirectory = previousDirectory
115                                 ret = None, None
116                 except ExpatError, e:
117                         self.currentDirectory = previousDirectory
118                         self.update()
119                         ret = None, self.currentDirectory
120                 return ret
121
122         def activate(self):
123                 if self.getCurrent() is not None:
124                         if self.getCurrent()[0][1]:
125                                 ret = self.changeDirectory(self.getCurrent()[0][0])
126                         else:
127                                 ret = self.getCurrent()[0][0], self.getCurrent()[1][7]
128                 else:
129                         ret = None, None
130                 return ret
131
132         def changeRegex(self, matchingPattern):
133                 if matchingPattern is not None:
134                         self.regex = re.compile(matchingPattern)
135                 else:
136                         self.regex = None
137
138         def getNextFile(self):
139                 i = self.getSelectedIndex() + 1
140                 while i < len(self.list):
141                         if self.list[i][0][1] == False:
142                                 self.moveToIndex(i)
143                                 return self.getCurrent()[0][0], self.getCurrent()[1][7]
144                         i = i + 1
145                 return None, None
146
147         def getPrevFile(self):
148                 i = self.getSelectedIndex() - 1
149                 while i >= 0:
150                         if self.list[i][0][1] == False:
151                                 self.moveToIndex(i)
152                                 return self.getCurrent()[0][0], self.getCurrent()[1][7]
153                         i = i - 1
154                 return None, None