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
6 from Tools.Directories import SCOPE_SKIN_IMAGE, resolveFilename, fileExists
8 from enigma import RT_HALIGN_LEFT, eListboxPythonMultiContent, \
9 eServiceReference, eServiceCenter, gFont
10 from Tools.LoadPixmap import LoadPixmap
33 def FileEntryComponent(name, absolute = None, isDir = False):
34 res = [ (absolute, isDir) ]
35 res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
37 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
39 extension = name.split('.')
40 extension = extension[-1].lower()
41 if EXTENSIONS.has_key(extension):
42 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
46 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
50 class FileList(MenuList):
51 def __init__(self, directory, showDirectories = True, showFiles = True, showMountpoints = True, matchingPattern = None, useServiceRef = False, inhibitDirs = False, inhibitMounts = False, isTop = False, enableWrapAround = False, additionalExtensions = None):
52 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
53 self.additional_extensions = additionalExtensions
55 self.current_directory = None
56 self.current_mountpoint = None
57 self.useServiceRef = useServiceRef
58 self.showDirectories = showDirectories
59 self.showMountpoints = showMountpoints
60 self.showFiles = showFiles
62 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
63 self.matchingPattern = matchingPattern
64 self.inhibitDirs = inhibitDirs or []
65 self.inhibitMounts = inhibitMounts or []
67 self.refreshMountpoints()
68 self.changeDir(directory)
69 self.l.setFont(0, gFont("Regular", 18))
70 self.l.setItemHeight(23)
71 self.serviceHandler = eServiceCenter.getInstance()
73 def refreshMountpoints(self):
74 self.mountpoints = [os_path.join(p.mountpoint, "") for p in harddiskmanager.getMountedPartitions()]
75 self.mountpoints.sort(reverse = True)
77 def getMountpoint(self, file):
78 file = os_path.join(os_path.realpath(file), "")
79 for m in self.mountpoints:
80 if file.startswith(m):
84 def getMountpointLink(self, file):
85 if os_path.realpath(file) == file:
86 return self.getMountpoint(file)
90 mp = self.getMountpoint(file)
92 file = os_path.dirname(file)
93 while last != "/" and mp == self.getMountpoint(file):
95 file = os_path.dirname(file)
96 return os_path.join(last, "")
98 def getSelection(self):
99 if self.l.getCurrentSelection() is None:
101 return self.l.getCurrentSelection()[0]
103 def getCurrentEvent(self):
104 l = self.l.getCurrentSelection()
105 if not l or l[0][1] == True:
108 return self.serviceHandler.info(l[0][0]).getEvent(l[0][0])
110 def getFileList(self):
113 def inParentDirs(self, dir, parents):
114 dir = os_path.realpath(dir)
116 if dir.startswith(p):
120 def changeDir(self, directory, select = None):
123 # if we are just entering from the list of mount points:
124 if self.current_directory is None:
125 if directory and self.showMountpoints:
126 self.current_mountpoint = self.getMountpointLink(directory)
128 self.current_mountpoint = None
129 self.current_directory = directory
133 if directory is None and self.showMountpoints: # present available mountpoints
134 for p in harddiskmanager.getMountedPartitions():
135 path = os_path.join(p.mountpoint, "")
136 if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
137 self.list.append(FileEntryComponent(name = p.description, absolute = path, isDir = True))
140 elif directory is None:
143 elif self.useServiceRef:
144 root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
145 if self.additional_extensions:
146 root.setName(self.additional_extensions)
147 serviceHandler = eServiceCenter.getInstance()
148 list = serviceHandler.list(root)
155 if s.flags & s.mustDescent:
156 directories.append(s.getPath())
162 if fileExists(directory):
164 files = listdir(directory)
170 if os_path.isdir(directory + x):
171 directories.append(directory + x + "/")
174 if directory is not None and self.showDirectories and not self.isTop:
175 if directory == self.current_mountpoint and self.showMountpoints:
176 self.list.append(FileEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
177 elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
178 self.list.append(FileEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
180 if self.showDirectories:
181 for x in directories:
182 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
183 name = x.split('/')[-2]
184 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
188 if self.useServiceRef:
190 name = path.split('/')[-1]
195 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
196 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
198 self.l.setList(self.list)
200 if select is not None:
206 if isinstance(p, eServiceReference):
213 def getCurrentDirectory(self):
214 return self.current_directory
216 def canDescent(self):
217 if self.getSelection() is None:
219 return self.getSelection()[1]
222 if self.getSelection() is None:
224 self.changeDir(self.getSelection()[0], select = self.current_directory)
226 def getFilename(self):
227 if self.getSelection() is None:
229 x = self.getSelection()[0]
230 if isinstance(x, eServiceReference):
234 def getServiceRef(self):
235 if self.getSelection() is None:
237 x = self.getSelection()[0]
238 if isinstance(x, eServiceReference):
243 harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
246 harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
249 self.changeDir(self.current_directory, self.getFilename())
251 def partitionListChanged(self, action, device):
252 self.refreshMountpoints()
253 if self.current_directory is None:
257 def MultiFileSelectEntryComponent(name, absolute = None, isDir = False, selected = False):
258 res = [ (absolute, isDir, selected, name) ]
259 res.append((eListboxPythonMultiContent.TYPE_TEXT, 55, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
261 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
263 extension = name.split('.')
264 extension = extension[-1].lower()
265 if EXTENSIONS.has_key(extension):
266 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
270 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 30, 2, 20, 20, png))
272 if not name.startswith('<'):
273 if selected is False:
274 icon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/lock_off.png"))
275 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 2, 0, 25, 25, icon))
277 icon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/lock_on.png"))
278 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 2, 0, 25, 25, icon))
283 class MultiFileSelectList(FileList):
284 def __init__(self, preselectedFiles, directory, showMountpoints = False, matchingPattern = None, showDirectories = True, showFiles = True, useServiceRef = False, inhibitDirs = False, inhibitMounts = False, isTop = False, enableWrapAround = False, additionalExtensions = None):
285 self.selectedFiles = preselectedFiles
286 if self.selectedFiles is None:
287 self.selectedFiles = []
288 FileList.__init__(self, directory, showMountpoints = showMountpoints, matchingPattern = matchingPattern, showDirectories = showDirectories, showFiles = showFiles, useServiceRef = useServiceRef, inhibitDirs = inhibitDirs, inhibitMounts = inhibitMounts, isTop = isTop, enableWrapAround = enableWrapAround, additionalExtensions = additionalExtensions)
289 self.changeDir(directory)
290 self.l.setItemHeight(25)
291 self.l.setFont(0, gFont("Regular", 20))
292 self.onSelectionChanged = [ ]
294 def selectionChanged(self):
295 for f in self.onSelectionChanged:
298 def changeSelectionState(self):
299 idx = self.l.getCurrentSelectionIndex()
304 if x[0][3].startswith('<'):
308 realPathname = x[0][0]
310 realPathname = self.current_directory + x[0][0]
313 for entry in self.selectedFiles:
314 if entry == realPathname:
315 self.selectedFiles.remove(entry)
319 alreadyinList = False
320 for entry in self.selectedFiles:
321 if entry == realPathname:
323 if not alreadyinList:
324 self.selectedFiles.append(realPathname)
325 newList.append(MultiFileSelectEntryComponent(name = x[0][3], absolute = x[0][0], isDir = x[0][1], selected = SelectState ))
332 self.l.setList(self.list)
334 def getSelectedList(self):
335 return self.selectedFiles
337 def changeDir(self, directory, select = None):
340 # if we are just entering from the list of mount points:
341 if self.current_directory is None:
342 if directory and self.showMountpoints:
343 self.current_mountpoint = self.getMountpointLink(directory)
345 self.current_mountpoint = None
346 self.current_directory = directory
350 if directory is None and self.showMountpoints: # present available mountpoints
351 for p in harddiskmanager.getMountedPartitions():
352 path = os_path.join(p.mountpoint, "")
353 if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
354 self.list.append(MultiFileSelectEntryComponent(name = p.description, absolute = path, isDir = True))
357 elif directory is None:
360 elif self.useServiceRef:
361 root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
362 if self.additional_extensions:
363 root.setName(self.additional_extensions)
364 serviceHandler = eServiceCenter.getInstance()
365 list = serviceHandler.list(root)
372 if s.flags & s.mustDescent:
373 directories.append(s.getPath())
379 if fileExists(directory):
381 files = listdir(directory)
387 if os_path.isdir(directory + x):
388 directories.append(directory + x + "/")
391 if directory is not None and self.showDirectories and not self.isTop:
392 if directory == self.current_mountpoint and self.showMountpoints:
393 self.list.append(MultiFileSelectEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
394 elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
395 self.list.append(MultiFileSelectEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
397 if self.showDirectories:
398 for x in directories:
399 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
400 name = x.split('/')[-2]
401 alreadySelected = False
402 for entry in self.selectedFiles:
404 alreadySelected = True
406 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x, isDir = True, selected = True))
408 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x, isDir = True, selected = False))
412 if self.useServiceRef:
414 name = path.split('/')[-1]
419 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
420 alreadySelected = False
421 for entry in self.selectedFiles:
422 if os_path.basename(entry) == x:
423 alreadySelected = True
425 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x , isDir = False, selected = True))
427 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x , isDir = False, selected = False))
429 self.l.setList(self.list)
431 if select is not None:
437 if isinstance(p, eServiceReference):