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
44 def FileEntryComponent(name, absolute = None, isDir = False):
45 res = [ (absolute, isDir) ]
46 x, y, w, h = skin.parameters.get("FileListName",(35, 1, 470, 20))
47 res.append((eListboxPythonMultiContent.TYPE_TEXT, x, y, w, h, 0, RT_HALIGN_LEFT, name))
49 png = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "extensions/directory.png"))
51 extension = name.split('.')
52 extension = extension[-1].lower()
53 if EXTENSIONS.has_key(extension):
54 png = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "extensions/" + EXTENSIONS[extension] + ".png"))
58 x, y, w, h = skin.parameters.get("FileListIcon",(10, 2, 20, 20))
59 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, x, y, w, h, png))
63 class FileList(MenuList):
64 def __init__(self, directory, showDirectories = True, showFiles = True, showMountpoints = True, matchingPattern = None, useServiceRef = False, inhibitDirs = False, inhibitMounts = False, isTop = False, enableWrapAround = False, additionalExtensions = None):
65 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
66 self.additional_extensions = additionalExtensions
68 self.current_directory = None
69 self.current_mountpoint = None
70 self.useServiceRef = useServiceRef
71 self.showDirectories = showDirectories
72 self.showMountpoints = showMountpoints
73 self.showFiles = showFiles
75 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
76 self.matchingPattern = matchingPattern
77 self.inhibitDirs = inhibitDirs or []
78 self.inhibitMounts = inhibitMounts or []
80 self.refreshMountpoints()
81 self.changeDir(directory)
82 font = skin.fonts.get("FileList", ("Regular", 18, 23))
83 self.l.setFont(0, gFont(font[0], font[1]))
84 self.l.setItemHeight(font[2])
85 self.serviceHandler = eServiceCenter.getInstance()
87 def refreshMountpoints(self):
88 self.mountpoints = [os_path.join(p.mountpoint, "") for p in harddiskmanager.getMountedPartitions()]
89 self.mountpoints.sort(reverse = True)
91 def getMountpoint(self, file):
92 file = os_path.join(os_path.realpath(file), "")
93 for m in self.mountpoints:
94 if file.startswith(m):
98 def getMountpointLink(self, file):
99 if os_path.realpath(file) == file:
100 return self.getMountpoint(file)
104 mp = self.getMountpoint(file)
106 file = os_path.dirname(file)
107 while last != "/" and mp == self.getMountpoint(file):
109 file = os_path.dirname(file)
110 return os_path.join(last, "")
112 def getSelection(self):
113 if self.l.getCurrentSelection() is None:
115 return self.l.getCurrentSelection()[0]
117 def getCurrentEvent(self):
118 l = self.l.getCurrentSelection()
119 if not l or l[0][1] == True:
122 return self.serviceHandler.info(l[0][0]).getEvent(l[0][0])
124 def getFileList(self):
127 def inParentDirs(self, dir, parents):
128 dir = os_path.realpath(dir)
130 if dir.startswith(p):
134 def changeDir(self, directory, select = None):
137 # if we are just entering from the list of mount points:
138 if self.current_directory is None:
139 if directory and self.showMountpoints:
140 self.current_mountpoint = self.getMountpointLink(directory)
142 self.current_mountpoint = None
143 self.current_directory = directory
147 if directory is None and self.showMountpoints: # present available mountpoints
148 for p in harddiskmanager.getMountedPartitions():
149 path = os_path.join(p.mountpoint, "")
150 if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
151 self.list.append(FileEntryComponent(name = p.description, absolute = path, isDir = True))
154 elif directory is None:
157 elif self.useServiceRef:
158 root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
159 if self.additional_extensions:
160 root.setName(self.additional_extensions)
161 serviceHandler = eServiceCenter.getInstance()
162 list = serviceHandler.list(root)
169 if s.flags & s.mustDescent:
170 directories.append(s.getPath())
176 if fileExists(directory):
178 files = listdir(directory)
184 if os_path.isdir(directory + x):
185 directories.append(directory + x + "/")
188 if directory is not None and self.showDirectories and not self.isTop:
189 if directory == self.current_mountpoint and self.showMountpoints:
190 self.list.append(FileEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
191 elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
192 self.list.append(FileEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
194 if self.showDirectories:
195 for x in directories:
196 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
197 name = x.split('/')[-2]
198 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
202 if self.useServiceRef:
204 name = path.split('/')[-1]
209 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
210 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
212 if self.showMountpoints and len(self.list) == 0:
213 self.list.append(FileEntryComponent(name = _("nothing connected"), absolute = None, isDir = False))
215 self.l.setList(self.list)
217 if select is not None:
223 if isinstance(p, eServiceReference):
230 def getCurrentDirectory(self):
231 return self.current_directory
233 def canDescent(self):
234 if self.getSelection() is None:
236 return self.getSelection()[1]
239 if self.getSelection() is None:
241 self.changeDir(self.getSelection()[0], select = self.current_directory)
243 def getFilename(self):
244 if self.getSelection() is None:
246 x = self.getSelection()[0]
247 if isinstance(x, eServiceReference):
251 def getServiceRef(self):
252 if self.getSelection() is None:
254 x = self.getSelection()[0]
255 if isinstance(x, eServiceReference):
260 harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
263 harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
266 self.changeDir(self.current_directory, self.getFilename())
268 def partitionListChanged(self, action, device):
269 self.refreshMountpoints()
270 if self.current_directory is None:
274 def MultiFileSelectEntryComponent(name, absolute = None, isDir = False, selected = False):
275 res = [ (absolute, isDir, selected, name) ]
276 x, y, w, h = skin.parameters.get("FileListMultiName",(55, 0, 470, 25))
277 res.append((eListboxPythonMultiContent.TYPE_TEXT, x, y, w, h, 0, RT_HALIGN_LEFT, name))
279 png = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "extensions/directory.png"))
281 extension = name.split('.')
282 extension = extension[-1].lower()
283 if EXTENSIONS.has_key(extension):
284 png = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "extensions/" + EXTENSIONS[extension] + ".png"))
288 x, y, w, h = skin.parameters.get("FileListMultiIcon",(30, 2, 20, 20))
289 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, x, y, w, h, png))
291 if not name.startswith('<'):
292 if selected is False:
293 icon = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/lock_off.png"))
294 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 2, 0, 25, 25, icon))
296 icon = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/lock_on.png"))
297 x, y, w, h = skin.parameters.get("FileListMultiLock",(2, 0, 25, 25))
298 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, x, y, w, h, icon))
303 class MultiFileSelectList(FileList):
304 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):
305 self.selectedFiles = preselectedFiles
306 if self.selectedFiles is None:
307 self.selectedFiles = []
308 FileList.__init__(self, directory, showMountpoints = showMountpoints, matchingPattern = matchingPattern, showDirectories = showDirectories, showFiles = showFiles, useServiceRef = useServiceRef, inhibitDirs = inhibitDirs, inhibitMounts = inhibitMounts, isTop = isTop, enableWrapAround = enableWrapAround, additionalExtensions = additionalExtensions)
309 self.changeDir(directory)
310 font = skin.fonts.get("FileListMulti", ("Regular", 20, 25))
311 self.l.setFont(0, gFont(font[0], font[1]))
312 self.l.setItemHeight(font[2])
313 self.onSelectionChanged = [ ]
315 def selectionChanged(self):
316 for f in self.onSelectionChanged:
319 def changeSelectionState(self):
320 idx = self.l.getCurrentSelectionIndex()
325 if x[0][3].startswith('<'):
329 realPathname = x[0][0]
331 realPathname = self.current_directory + x[0][0]
334 for entry in self.selectedFiles:
335 if entry == realPathname:
336 self.selectedFiles.remove(entry)
340 alreadyinList = False
341 for entry in self.selectedFiles:
342 if entry == realPathname:
344 if not alreadyinList:
345 self.selectedFiles.append(realPathname)
346 newList.append(MultiFileSelectEntryComponent(name = x[0][3], absolute = x[0][0], isDir = x[0][1], selected = SelectState ))
353 self.l.setList(self.list)
355 def getSelectedList(self):
356 return self.selectedFiles
358 def changeDir(self, directory, select = None):
361 # if we are just entering from the list of mount points:
362 if self.current_directory is None:
363 if directory and self.showMountpoints:
364 self.current_mountpoint = self.getMountpointLink(directory)
366 self.current_mountpoint = None
367 self.current_directory = directory
371 if directory is None and self.showMountpoints: # present available mountpoints
372 for p in harddiskmanager.getMountedPartitions():
373 path = os_path.join(p.mountpoint, "")
374 if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
375 self.list.append(MultiFileSelectEntryComponent(name = p.description, absolute = path, isDir = True))
378 elif directory is None:
381 elif self.useServiceRef:
382 root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
383 if self.additional_extensions:
384 root.setName(self.additional_extensions)
385 serviceHandler = eServiceCenter.getInstance()
386 list = serviceHandler.list(root)
393 if s.flags & s.mustDescent:
394 directories.append(s.getPath())
400 if fileExists(directory):
402 files = listdir(directory)
408 if os_path.isdir(directory + x):
409 directories.append(directory + x + "/")
412 if directory is not None and self.showDirectories and not self.isTop:
413 if directory == self.current_mountpoint and self.showMountpoints:
414 self.list.append(MultiFileSelectEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
415 elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
416 self.list.append(MultiFileSelectEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
418 if self.showDirectories:
419 for x in directories:
420 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
421 name = x.split('/')[-2]
422 alreadySelected = False
423 for entry in self.selectedFiles:
425 alreadySelected = True
427 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x, isDir = True, selected = True))
429 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x, isDir = True, selected = False))
433 if self.useServiceRef:
435 name = path.split('/')[-1]
440 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
441 alreadySelected = False
442 for entry in self.selectedFiles:
443 if os_path.basename(entry) == x:
444 alreadySelected = True
446 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x , isDir = False, selected = True))
448 self.list.append(MultiFileSelectEntryComponent(name = name, absolute = x , isDir = False, selected = False))
450 self.l.setList(self.list)
452 if select is not None:
458 if isinstance(p, eServiceReference):