add .mp2 as supported filetype
[vuplus_dvbapp] / lib / python / Components / FileList.py
1 from re import compile as re_compile
2 from os import path as os_path, listdir
3 from MenuList import MenuList
4 from Components.Harddisk import harddiskmanager
5
6 from Tools.Directories import SCOPE_SKIN_IMAGE, resolveFilename
7
8 from enigma import RT_HALIGN_LEFT, eListboxPythonMultiContent, \
9         eServiceReference, eServiceCenter, gFont
10 from Tools.LoadPixmap import LoadPixmap
11
12 EXTENSIONS = {
13                 "mp2": "music",
14                 "mp3": "music",
15                 "wav": "music",
16                 "ogg": "music",
17                 "flac": "music",
18                 "jpg": "picture",
19                 "jpeg": "picture",
20                 "png": "picture",
21                 "bmp": "picture",
22                 "ts": "movie",
23                 "avi": "movie",
24                 "mpg": "movie",
25                 "mpeg": "movie",
26                 "mkv": "movie",
27                 "mp4": "movie"
28         }
29
30 def FileEntryComponent(name, absolute = None, isDir = False):
31         res = [ (absolute, isDir) ]
32         res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
33         if isDir:
34                 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
35         else:
36                 extension = name.split('.')
37                 extension = extension[-1].lower()
38                 if EXTENSIONS.has_key(extension):
39                         png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
40                 else:
41                         png = None
42         if png is not None:
43                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
44         
45         return res
46
47 class FileList(MenuList):
48         def __init__(self, directory, showDirectories = True, showFiles = True, showMountpoints = True, matchingPattern = None, useServiceRef = False, inhibitDirs = False, inhibitMounts = False, isTop = False, enableWrapAround = False, additionalExtensions = None):
49                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
50                 self.additional_extensions = additionalExtensions
51                 self.mountpoints = []
52                 self.current_directory = None
53                 self.current_mountpoint = None
54                 self.useServiceRef = useServiceRef
55                 self.showDirectories = showDirectories
56                 self.showMountpoints = showMountpoints
57                 self.showFiles = showFiles
58                 self.isTop = isTop
59                 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
60                 self.matchingPattern = matchingPattern
61                 self.inhibitDirs = inhibitDirs or []
62                 self.inhibitMounts = inhibitMounts or []
63
64                 self.refreshMountpoints()
65                 self.changeDir(directory)
66                 self.l.setFont(0, gFont("Regular", 18))
67                 self.l.setItemHeight(23)
68                 self.serviceHandler = eServiceCenter.getInstance()
69
70         def refreshMountpoints(self):
71                 self.mountpoints = [os_path.join(p.mountpoint, "") for p in harddiskmanager.getMountedPartitions()]
72                 self.mountpoints.sort(reverse = True)
73
74         def getMountpoint(self, file):
75                 file = os_path.join(os_path.realpath(file), "")
76                 for m in self.mountpoints:
77                         if file.startswith(m):
78                                 return m
79                 return False
80
81         def getMountpointLink(self, file):
82                 if os_path.realpath(file) == file:
83                         return self.getMountpoint(file)
84                 else:
85                         if file[-1] == "/":
86                                 file = file[:-1]
87                         mp = self.getMountpoint(file)
88                         last = file
89                         file = os_path.dirname(file)
90                         while last != "/" and mp == self.getMountpoint(file):
91                                 last = file
92                                 file = os_path.dirname(file)
93                         return os_path.join(last, "")
94
95         def getSelection(self):
96                 if self.l.getCurrentSelection() is None:
97                         return None
98                 return self.l.getCurrentSelection()[0]
99
100         def getCurrentEvent(self):
101                 l = self.l.getCurrentSelection()
102                 if not l or l[0][1] == True:
103                         return None
104                 else:
105                         return self.serviceHandler.info(l[0][0]).getEvent(l[0][0])
106
107         def getFileList(self):
108                 return self.list
109
110         def inParentDirs(self, dir, parents):
111                 dir = os_path.realpath(dir)
112                 for p in parents:
113                         if dir.startswith(p):
114                                 return True
115                 return False
116
117         def changeDir(self, directory, select = None):
118                 self.list = []
119
120                 if directory and not os_path.isdir(directory):
121                         directory = None
122                 # if we are just entering from the list of mount points:
123                 if self.current_directory is None:
124                         if directory and self.showMountpoints:
125                                 self.current_mountpoint = self.getMountpointLink(directory)
126                         else:
127                                 self.current_mountpoint = None
128                 self.current_directory = directory
129                 directories = []
130                 files = []
131
132                 if directory is None and self.showMountpoints: # present available mountpoints
133                         for p in harddiskmanager.getMountedPartitions():
134                                 path = os_path.join(p.mountpoint, "")
135                                 if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
136                                         self.list.append(FileEntryComponent(name = p.description, absolute = path, isDir = True))
137                         files = [ ]
138                         directories = [ ]
139                 elif self.useServiceRef:
140                         root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
141                         if self.additional_extensions:
142                                 root.setName(self.additional_extensions)
143                         serviceHandler = eServiceCenter.getInstance()
144                         list = serviceHandler.list(root)
145
146                         while 1:
147                                 s = list.getNext()
148                                 if not s.valid():
149                                         del list
150                                         break
151                                 if s.flags & s.mustDescent:
152                                         directories.append(s.getPath())
153                                 else:
154                                         files.append(s)
155                         directories.sort()
156                         files.sort()
157                 else:
158                         if os_path.exists(directory):
159                                 files = listdir(directory)
160                                 files.sort()
161                                 tmpfiles = files[:]
162                                 for x in tmpfiles:
163                                         if os_path.isdir(directory + x):
164                                                 directories.append(directory + x + "/")
165                                                 files.remove(x)
166
167                 if directory is not None and self.showDirectories and not self.isTop:
168                         if directory == self.current_mountpoint and self.showMountpoints:
169                                 self.list.append(FileEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
170                         elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
171                                 self.list.append(FileEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
172
173                 if self.showDirectories:
174                         for x in directories:
175                                 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
176                                         name = x.split('/')[-2]
177                                         self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
178
179                 if self.showFiles:
180                         for x in files:
181                                 if self.useServiceRef:
182                                         path = x.getPath()
183                                         name = path.split('/')[-1]
184                                 else:
185                                         path = directory + x
186                                         name = x
187
188                                 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
189                                         self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
190
191                 self.l.setList(self.list)
192
193                 if select is not None:
194                         i = 0
195                         self.moveToIndex(0)
196                         for x in self.list:
197                                 p = x[0][0]
198                                 
199                                 if isinstance(p, eServiceReference):
200                                         p = p.getPath()
201                                 
202                                 if p == select:
203                                         self.moveToIndex(i)
204                                 i += 1
205
206         def getCurrentDirectory(self):
207                 return self.current_directory
208
209         def canDescent(self):
210                 if self.getSelection() is None:
211                         return False
212                 return self.getSelection()[1]
213
214         def descent(self):
215                 if self.getSelection() is None:
216                         return
217                 self.changeDir(self.getSelection()[0], select = self.current_directory)
218
219         def getFilename(self):
220                 if self.getSelection() is None:
221                         return None
222                 x = self.getSelection()[0]
223                 if isinstance(x, eServiceReference):
224                         x = x.getPath()
225                 return x
226
227         def getServiceRef(self):
228                 if self.getSelection() is None:
229                         return None
230                 x = self.getSelection()[0]
231                 if isinstance(x, eServiceReference):
232                         return x
233                 return None
234
235         def execBegin(self):
236                 harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
237
238         def execEnd(self):
239                 harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
240
241         def refresh(self):
242                 self.changeDir(self.current_directory, self.getFilename())
243
244         def partitionListChanged(self, action, device):
245                 self.refreshMountpoints()
246                 if self.current_directory is None:
247                         self.refresh()