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_CURRENT_SKIN, 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(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "extensions/directory.png"))
40 extension = name.split('.')
41 extension = extension[-1].lower()
42 if EXTENSIONS.has_key(extension):
43 png = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "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 if self.showMountpoints and len(self.list) == 0:
200 self.list.append(FileEntryComponent(name = _("nothing connected"), absolute = None, isDir = False))
202 self.l.setList(self.list)
204 if select is not None:
210 if isinstance(p, eServiceReference):
217 def getCurrentDirectory(self):
218 return self.current_directory
220 def canDescent(self):
221 if self.getSelection() is None:
223 return self.getSelection()[1]
226 if self.getSelection() is None:
228 self.changeDir(self.getSelection()[0], select = self.current_directory)
230 def getFilename(self):
231 if self.getSelection() is None:
233 x = self.getSelection()[0]
234 if isinstance(x, eServiceReference):
238 def getServiceRef(self):
239 if self.getSelection() is None:
241 x = self.getSelection()[0]
242 if isinstance(x, eServiceReference):
247 harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
250 harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
253 self.changeDir(self.current_directory, self.getFilename())
255 def partitionListChanged(self, action, device):
256 self.refreshMountpoints()
257 if self.current_directory is None:
261 def MultiFileSelectEntryComponent(name, absolute = None, isDir = False, selected = False):
262 res = [ (absolute, isDir, selected, name) ]
263 res.append((eListboxPythonMultiContent.TYPE_TEXT, 55, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
265 png = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "extensions/directory.png"))
267 extension = name.split('.')
268 extension = extension[-1].lower()
269 if EXTENSIONS.has_key(extension):
270 png = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "extensions/" + EXTENSIONS[extension] + ".png"))
274 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 30, 2, 20, 20, png))
276 if not name.startswith('<'):
277 if selected is False:
278 icon = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/lock_off.png"))
279 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 2, 0, 25, 25, icon))
281 icon = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/lock_on.png"))
282 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 2, 0, 25, 25, icon))
287 class MultiFileSelectList(FileList):
288 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):
289 self.selectedFiles = preselectedFiles
290 if self.selectedFiles is None:
291 self.selectedFiles = []
292 FileList.__init__(self, directory, showMountpoints = showMountpoints, matchingPattern = matchingPattern, showDirectories = showDirectories, showFiles = showFiles, useServiceRef = useServiceRef, inhibitDirs = inhibitDirs, inhibitMounts = inhibitMounts, isTop = isTop, enableWrapAround = enableWrapAround, additionalExtensions = additionalExtensions)
293 self.changeDir(directory)
294 self.l.setItemHeight(25)
295 self.l.setFont(0, gFont("Regular", 20))
296 self.onSelectionChanged = [ ]
298 def selectionChanged(self):
299 for f in self.onSelectionChanged:
302 def changeSelectionState(self):
303 idx = self.l.getCurrentSelectionIndex()
308 if x[0][3].startswith('<'):
312 realPathname = x[0][0]
314 realPathname = self.current_directory + x[0][0]
317 for entry in self.selectedFiles:
318 if entry == realPathname:
319 self.selectedFiles.remove(entry)
323 alreadyinList = False
324 for entry in self.selectedFiles:
325 if entry == realPathname:
327 if not alreadyinList:
328 self.selectedFiles.append(realPathname)
329 newList.append(MultiFileSelectEntryComponent(name = x[0][3], absolute = x[0][0], isDir = x[0][1], selected = SelectState ))
336 self.l.setList(self.list)
338 def getSelectedList(self):
339 return self.selectedFiles
341 def changeDir(self, directory, select = None):
344 # if we are just entering from the list of mount points:
345 if self.current_directory is None:
346 if directory and self.showMountpoints:
347 self.current_mountpoint = self.getMountpointLink(directory)
349 self.current_mountpoint = None
350 self.current_directory = directory
354 if directory is None and self.showMountpoints: # present available mountpoints
355 for p in harddiskmanager.getMountedPartitions():
356 path = os_path.join(p.mountpoint, "")
357 if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
358 self.list.append(MultiFileSelectEntryComponent(name = p.description, absolute = path, isDir = True))
361 elif directory is None:
364 elif self.useServiceRef:
365 root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
366 if self.additional_extensions:
367 root.setName(self.additional_extensions)
368 serviceHandler = eServiceCenter.getInstance()
369 list = serviceHandler.list(root)
376 if s.flags & s.mustDescent:
377 directories.append(s.getPath())
383 if fileExists(directory):
385 files = listdir(directory)
391 if os_path.isdir(directory + x):
392 directories.append(directory + x + "/")
395 if directory is not None and self.showDirectories and not self.isTop:
396 if directory == self.current_mountpoint and self.showMountpoints:
397 self.list.append(MultiFileSelectEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
398 elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
399 self.list.append(MultiFileSelectEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
401 if self.showDirectories:
402 for x in directories:
403 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
404 name = x.split('/')[-2]
405 alreadySelected = False
406 for entry in self.selectedFiles:
408 alreadySelected = True
410 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x, isDir = True, selected = True))
412 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x, isDir = True, selected = False))
416 if self.useServiceRef:
418 name = path.split('/')[-1]
423 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
424 alreadySelected = False
425 for entry in self.selectedFiles:
426 if os_path.basename(entry) == x:
427 alreadySelected = True
429 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x , isDir = False, selected = True))
431 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x , isDir = False, selected = False))
433 self.l.setList(self.list)
435 if select is not None:
441 if isinstance(p, eServiceReference):