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