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
8 from enigma import RT_HALIGN_LEFT, eListboxPythonMultiContent, \
9 eServiceReference, eServiceCenter, gFont
10 from Tools.LoadPixmap import LoadPixmap
32 def FileEntryComponent(name, absolute = None, isDir = False):
33 res = [ (absolute, isDir) ]
34 res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
36 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
38 extension = name.split('.')
39 extension = extension[-1].lower()
40 if EXTENSIONS.has_key(extension):
41 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
45 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
49 class FileList(MenuList):
50 def __init__(self, directory, showDirectories = True, showFiles = True, showMountpoints = True, matchingPattern = None, useServiceRef = False, inhibitDirs = False, inhibitMounts = False, isTop = False, enableWrapAround = False, additionalExtensions = None):
51 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
52 self.additional_extensions = additionalExtensions
54 self.current_directory = None
55 self.current_mountpoint = None
56 self.useServiceRef = useServiceRef
57 self.showDirectories = showDirectories
58 self.showMountpoints = showMountpoints
59 self.showFiles = showFiles
61 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
62 self.matchingPattern = matchingPattern
63 self.inhibitDirs = inhibitDirs or []
64 self.inhibitMounts = inhibitMounts or []
66 self.refreshMountpoints()
67 self.changeDir(directory)
68 self.l.setFont(0, gFont("Regular", 18))
69 self.l.setItemHeight(23)
70 self.serviceHandler = eServiceCenter.getInstance()
72 def refreshMountpoints(self):
73 self.mountpoints = [os_path.join(p.mountpoint, "") for p in harddiskmanager.getMountedPartitions()]
74 self.mountpoints.sort(reverse = True)
76 def getMountpoint(self, file):
77 file = os_path.join(os_path.realpath(file), "")
78 for m in self.mountpoints:
79 if file.startswith(m):
83 def getMountpointLink(self, file):
84 if os_path.realpath(file) == file:
85 return self.getMountpoint(file)
89 mp = self.getMountpoint(file)
91 file = os_path.dirname(file)
92 while last != "/" and mp == self.getMountpoint(file):
94 file = os_path.dirname(file)
95 return os_path.join(last, "")
97 def getSelection(self):
98 if self.l.getCurrentSelection() is None:
100 return self.l.getCurrentSelection()[0]
102 def getCurrentEvent(self):
103 l = self.l.getCurrentSelection()
104 if not l or l[0][1] == True:
107 return self.serviceHandler.info(l[0][0]).getEvent(l[0][0])
109 def getFileList(self):
112 def inParentDirs(self, dir, parents):
113 dir = os_path.realpath(dir)
115 if dir.startswith(p):
119 def changeDir(self, directory, select = None):
122 if directory and not os_path.isdir(directory):
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 self.useServiceRef:
142 root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
143 if self.additional_extensions:
144 root.setName(self.additional_extensions)
145 serviceHandler = eServiceCenter.getInstance()
146 list = serviceHandler.list(root)
153 if s.flags & s.mustDescent:
154 directories.append(s.getPath())
160 if os_path.exists(directory):
161 files = listdir(directory)
165 if os_path.isdir(directory + x):
166 directories.append(directory + x + "/")
169 if directory is not None and self.showDirectories and not self.isTop:
170 if directory == self.current_mountpoint and self.showMountpoints:
171 self.list.append(FileEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
172 elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
173 self.list.append(FileEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
175 if self.showDirectories:
176 for x in directories:
177 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
178 name = x.split('/')[-2]
179 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
183 if self.useServiceRef:
185 name = path.split('/')[-1]
190 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
191 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
193 self.l.setList(self.list)
195 if select is not None:
201 if isinstance(p, eServiceReference):
208 def getCurrentDirectory(self):
209 return self.current_directory
211 def canDescent(self):
212 if self.getSelection() is None:
214 return self.getSelection()[1]
217 if self.getSelection() is None:
219 self.changeDir(self.getSelection()[0], select = self.current_directory)
221 def getFilename(self):
222 if self.getSelection() is None:
224 x = self.getSelection()[0]
225 if isinstance(x, eServiceReference):
229 def getServiceRef(self):
230 if self.getSelection() is None:
232 x = self.getSelection()[0]
233 if isinstance(x, eServiceReference):
238 harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
241 harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
244 self.changeDir(self.current_directory, self.getFilename())
246 def partitionListChanged(self, action, device):
247 self.refreshMountpoints()
248 if self.current_directory is None: