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
34 def FileEntryComponent(name, absolute = None, isDir = False):
35 res = [ (absolute, isDir) ]
36 res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
38 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
40 extension = name.split('.')
41 extension = extension[-1].lower()
42 if EXTENSIONS.has_key(extension):
43 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
47 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
51 class FileList(MenuList):
52 def __init__(self, directory, showDirectories = True, showFiles = True, showMountpoints = True, matchingPattern = None, useServiceRef = False, inhibitDirs = False, inhibitMounts = False, isTop = False, enableWrapAround = False, additionalExtensions = None):
53 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
54 self.additional_extensions = additionalExtensions
56 self.current_directory = None
57 self.current_mountpoint = None
58 self.useServiceRef = useServiceRef
59 self.showDirectories = showDirectories
60 self.showMountpoints = showMountpoints
61 self.showFiles = showFiles
63 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
64 self.matchingPattern = matchingPattern
65 self.inhibitDirs = inhibitDirs or []
66 self.inhibitMounts = inhibitMounts or []
68 self.refreshMountpoints()
69 self.changeDir(directory)
70 self.l.setFont(0, gFont("Regular", 18))
71 self.l.setItemHeight(23)
72 self.serviceHandler = eServiceCenter.getInstance()
74 def refreshMountpoints(self):
75 self.mountpoints = [os_path.join(p.mountpoint, "") for p in harddiskmanager.getMountedPartitions()]
76 self.mountpoints.sort(reverse = True)
78 def getMountpoint(self, file):
79 file = os_path.join(os_path.realpath(file), "")
80 for m in self.mountpoints:
81 if file.startswith(m):
85 def getMountpointLink(self, file):
86 if os_path.realpath(file) == file:
87 return self.getMountpoint(file)
91 mp = self.getMountpoint(file)
93 file = os_path.dirname(file)
94 while last != "/" and mp == self.getMountpoint(file):
96 file = os_path.dirname(file)
97 return os_path.join(last, "")
99 def getSelection(self):
100 if self.l.getCurrentSelection() is None:
102 return self.l.getCurrentSelection()[0]
104 def getCurrentEvent(self):
105 l = self.l.getCurrentSelection()
106 if not l or l[0][1] == True:
109 return self.serviceHandler.info(l[0][0]).getEvent(l[0][0])
111 def getFileList(self):
114 def inParentDirs(self, dir, parents):
115 dir = os_path.realpath(dir)
117 if dir.startswith(p):
121 def changeDir(self, directory, select = None):
124 # if we are just entering from the list of mount points:
125 if self.current_directory is None:
126 if directory and self.showMountpoints:
127 self.current_mountpoint = self.getMountpointLink(directory)
129 self.current_mountpoint = None
130 self.current_directory = directory
134 if directory is None and self.showMountpoints: # present available mountpoints
135 for p in harddiskmanager.getMountedPartitions():
136 path = os_path.join(p.mountpoint, "")
137 if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
138 self.list.append(FileEntryComponent(name = p.description, absolute = path, isDir = True))
141 elif directory is None:
144 elif self.useServiceRef:
145 root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
146 if self.additional_extensions:
147 root.setName(self.additional_extensions)
148 serviceHandler = eServiceCenter.getInstance()
149 list = serviceHandler.list(root)
156 if s.flags & s.mustDescent:
157 directories.append(s.getPath())
163 if fileExists(directory):
165 files = listdir(directory)
171 if os_path.isdir(directory + x):
172 directories.append(directory + x + "/")
175 if directory is not None and self.showDirectories and not self.isTop:
176 if directory == self.current_mountpoint and self.showMountpoints:
177 self.list.append(FileEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
178 elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
179 self.list.append(FileEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
181 if self.showDirectories:
182 for x in directories:
183 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
184 name = x.split('/')[-2]
185 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
189 if self.useServiceRef:
191 name = path.split('/')[-1]
196 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
197 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
199 self.l.setList(self.list)
201 if select is not None:
207 if isinstance(p, eServiceReference):
214 def getCurrentDirectory(self):
215 return self.current_directory
217 def canDescent(self):
218 if self.getSelection() is None:
220 return self.getSelection()[1]
223 if self.getSelection() is None:
225 self.changeDir(self.getSelection()[0], select = self.current_directory)
227 def getFilename(self):
228 if self.getSelection() is None:
230 x = self.getSelection()[0]
231 if isinstance(x, eServiceReference):
235 def getServiceRef(self):
236 if self.getSelection() is None:
238 x = self.getSelection()[0]
239 if isinstance(x, eServiceReference):
244 harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
247 harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
250 self.changeDir(self.current_directory, self.getFilename())
252 def partitionListChanged(self, action, device):
253 self.refreshMountpoints()
254 if self.current_directory is None:
258 def MultiFileSelectEntryComponent(name, absolute = None, isDir = False, selected = False):
259 res = [ (absolute, isDir, selected, name) ]
260 res.append((eListboxPythonMultiContent.TYPE_TEXT, 55, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
262 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
264 extension = name.split('.')
265 extension = extension[-1].lower()
266 if EXTENSIONS.has_key(extension):
267 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
271 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 30, 2, 20, 20, png))
273 if not name.startswith('<'):
274 if selected is False:
275 icon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/lock_off.png"))
276 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 2, 0, 25, 25, icon))
278 icon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/lock_on.png"))
279 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 2, 0, 25, 25, icon))
284 class MultiFileSelectList(FileList):
285 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):
286 self.selectedFiles = preselectedFiles
287 if self.selectedFiles is None:
288 self.selectedFiles = []
289 FileList.__init__(self, directory, showMountpoints = showMountpoints, matchingPattern = matchingPattern, showDirectories = showDirectories, showFiles = showFiles, useServiceRef = useServiceRef, inhibitDirs = inhibitDirs, inhibitMounts = inhibitMounts, isTop = isTop, enableWrapAround = enableWrapAround, additionalExtensions = additionalExtensions)
290 self.changeDir(directory)
291 self.l.setItemHeight(25)
292 self.l.setFont(0, gFont("Regular", 20))
293 self.onSelectionChanged = [ ]
295 def selectionChanged(self):
296 for f in self.onSelectionChanged:
299 def changeSelectionState(self):
300 idx = self.l.getCurrentSelectionIndex()
305 if x[0][3].startswith('<'):
309 realPathname = x[0][0]
311 realPathname = self.current_directory + x[0][0]
314 for entry in self.selectedFiles:
315 if entry == realPathname:
316 self.selectedFiles.remove(entry)
320 alreadyinList = False
321 for entry in self.selectedFiles:
322 if entry == realPathname:
324 if not alreadyinList:
325 self.selectedFiles.append(realPathname)
326 newList.append(MultiFileSelectEntryComponent(name = x[0][3], absolute = x[0][0], isDir = x[0][1], selected = SelectState ))
333 self.l.setList(self.list)
335 def getSelectedList(self):
336 return self.selectedFiles
338 def changeDir(self, directory, select = None):
341 # if we are just entering from the list of mount points:
342 if self.current_directory is None:
343 if directory and self.showMountpoints:
344 self.current_mountpoint = self.getMountpointLink(directory)
346 self.current_mountpoint = None
347 self.current_directory = directory
351 if directory is None and self.showMountpoints: # present available mountpoints
352 for p in harddiskmanager.getMountedPartitions():
353 path = os_path.join(p.mountpoint, "")
354 if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
355 self.list.append(MultiFileSelectEntryComponent(name = p.description, absolute = path, isDir = True))
358 elif directory is None:
361 elif self.useServiceRef:
362 root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
363 if self.additional_extensions:
364 root.setName(self.additional_extensions)
365 serviceHandler = eServiceCenter.getInstance()
366 list = serviceHandler.list(root)
373 if s.flags & s.mustDescent:
374 directories.append(s.getPath())
380 if fileExists(directory):
382 files = listdir(directory)
388 if os_path.isdir(directory + x):
389 directories.append(directory + x + "/")
392 if directory is not None and self.showDirectories and not self.isTop:
393 if directory == self.current_mountpoint and self.showMountpoints:
394 self.list.append(MultiFileSelectEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
395 elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
396 self.list.append(MultiFileSelectEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
398 if self.showDirectories:
399 for x in directories:
400 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
401 name = x.split('/')[-2]
402 alreadySelected = False
403 for entry in self.selectedFiles:
405 alreadySelected = True
407 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x, isDir = True, selected = True))
409 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x, isDir = True, selected = False))
413 if self.useServiceRef:
415 name = path.split('/')[-1]
420 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
421 alreadySelected = False
422 for entry in self.selectedFiles:
423 if os_path.basename(entry) == x:
424 alreadySelected = True
426 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x , isDir = False, selected = True))
428 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x , isDir = False, selected = False))
430 self.l.setList(self.list)
432 if select is not None:
438 if isinstance(p, eServiceReference):