Merge branch 'master' into enigma2_rel25
[vuplus_dvbapp] / lib / python / Components / FileList.py
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
5
6 from Tools.Directories import SCOPE_SKIN_IMAGE, resolveFilename
7
8 from enigma import RT_HALIGN_LEFT, eListboxPythonMultiContent, \
9         eServiceReference, eServiceCenter, gFont
10 from Tools.LoadPixmap import LoadPixmap
11
12 EXTENSIONS = {
13                 "mp2": "music",
14                 "mp3": "music",
15                 "wav": "music",
16                 "ogg": "music",
17                 "flac": "music",
18                 "jpg": "picture",
19                 "jpeg": "picture",
20                 "png": "picture",
21                 "bmp": "picture",
22                 "ts": "movie",
23                 "avi": "movie",
24                 "divx": "movie",
25                 "mpg": "movie",
26                 "mpeg": "movie",
27                 "mkv": "movie",
28                 "mp4": "movie"
29         }
30
31 def FileEntryComponent(name, absolute = None, isDir = False):
32         res = [ (absolute, isDir) ]
33         res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
34         if isDir:
35                 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
36         else:
37                 extension = name.split('.')
38                 extension = extension[-1].lower()
39                 if EXTENSIONS.has_key(extension):
40                         png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
41                 else:
42                         png = None
43         if png is not None:
44                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
45         
46         return res
47
48 class FileList(MenuList):
49         def __init__(self, directory, showDirectories = True, showFiles = True, showMountpoints = True, matchingPattern = None, useServiceRef = False, inhibitDirs = False, inhibitMounts = False, isTop = False, enableWrapAround = False, additionalExtensions = None):
50                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
51                 self.additional_extensions = additionalExtensions
52                 self.mountpoints = []
53                 self.current_directory = None
54                 self.current_mountpoint = None
55                 self.useServiceRef = useServiceRef
56                 self.showDirectories = showDirectories
57                 self.showMountpoints = showMountpoints
58                 self.showFiles = showFiles
59                 self.isTop = isTop
60                 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
61                 self.matchingPattern = matchingPattern
62                 self.inhibitDirs = inhibitDirs or []
63                 self.inhibitMounts = inhibitMounts or []
64
65                 self.refreshMountpoints()
66                 self.changeDir(directory)
67                 self.l.setFont(0, gFont("Regular", 18))
68                 self.l.setItemHeight(23)
69                 self.serviceHandler = eServiceCenter.getInstance()
70
71         def refreshMountpoints(self):
72                 self.mountpoints = [os_path.join(p.mountpoint, "") for p in harddiskmanager.getMountedPartitions()]
73                 self.mountpoints.sort(reverse = True)
74
75         def getMountpoint(self, file):
76                 file = os_path.join(os_path.realpath(file), "")
77                 for m in self.mountpoints:
78                         if file.startswith(m):
79                                 return m
80                 return False
81
82         def getMountpointLink(self, file):
83                 if os_path.realpath(file) == file:
84                         return self.getMountpoint(file)
85                 else:
86                         if file[-1] == "/":
87                                 file = file[:-1]
88                         mp = self.getMountpoint(file)
89                         last = file
90                         file = os_path.dirname(file)
91                         while last != "/" and mp == self.getMountpoint(file):
92                                 last = file
93                                 file = os_path.dirname(file)
94                         return os_path.join(last, "")
95
96         def getSelection(self):
97                 if self.l.getCurrentSelection() is None:
98                         return None
99                 return self.l.getCurrentSelection()[0]
100
101         def getCurrentEvent(self):
102                 l = self.l.getCurrentSelection()
103                 if not l or l[0][1] == True:
104                         return None
105                 else:
106                         return self.serviceHandler.info(l[0][0]).getEvent(l[0][0])
107
108         def getFileList(self):
109                 return self.list
110
111         def inParentDirs(self, dir, parents):
112                 dir = os_path.realpath(dir)
113                 for p in parents:
114                         if dir.startswith(p):
115                                 return True
116                 return False
117
118         def changeDir(self, directory, select = None):
119                 self.list = []
120
121                 if directory and not os_path.isdir(directory):
122                         directory = 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)
127                         else:
128                                 self.current_mountpoint = None
129                 self.current_directory = directory
130                 directories = []
131                 files = []
132
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))
138                         files = [ ]
139                         directories = [ ]
140                 elif self.useServiceRef:
141                         root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
142                         if self.additional_extensions:
143                                 root.setName(self.additional_extensions)
144                         serviceHandler = eServiceCenter.getInstance()
145                         list = serviceHandler.list(root)
146
147                         while 1:
148                                 s = list.getNext()
149                                 if not s.valid():
150                                         del list
151                                         break
152                                 if s.flags & s.mustDescent:
153                                         directories.append(s.getPath())
154                                 else:
155                                         files.append(s)
156                         directories.sort()
157                         files.sort()
158                 else:
159                         if os_path.exists(directory):
160                                 files = listdir(directory)
161                                 files.sort()
162                                 tmpfiles = files[:]
163                                 for x in tmpfiles:
164                                         if os_path.isdir(directory + x):
165                                                 directories.append(directory + x + "/")
166                                                 files.remove(x)
167
168                 if directory is not None and self.showDirectories and not self.isTop:
169                         if directory == self.current_mountpoint and self.showMountpoints:
170                                 self.list.append(FileEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
171                         elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
172                                 self.list.append(FileEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
173
174                 if self.showDirectories:
175                         for x in directories:
176                                 if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
177                                         name = x.split('/')[-2]
178                                         self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
179
180                 if self.showFiles:
181                         for x in files:
182                                 if self.useServiceRef:
183                                         path = x.getPath()
184                                         name = path.split('/')[-1]
185                                 else:
186                                         path = directory + x
187                                         name = x
188
189                                 if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
190                                         self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
191
192                 self.l.setList(self.list)
193
194                 if select is not None:
195                         i = 0
196                         self.moveToIndex(0)
197                         for x in self.list:
198                                 p = x[0][0]
199                                 
200                                 if isinstance(p, eServiceReference):
201                                         p = p.getPath()
202                                 
203                                 if p == select:
204                                         self.moveToIndex(i)
205                                 i += 1
206
207         def getCurrentDirectory(self):
208                 return self.current_directory
209
210         def canDescent(self):
211                 if self.getSelection() is None:
212                         return False
213                 return self.getSelection()[1]
214
215         def descent(self):
216                 if self.getSelection() is None:
217                         return
218                 self.changeDir(self.getSelection()[0], select = self.current_directory)
219
220         def getFilename(self):
221                 if self.getSelection() is None:
222                         return None
223                 x = self.getSelection()[0]
224                 if isinstance(x, eServiceReference):
225                         x = x.getPath()
226                 return x
227
228         def getServiceRef(self):
229                 if self.getSelection() is None:
230                         return None
231                 x = self.getSelection()[0]
232                 if isinstance(x, eServiceReference):
233                         return x
234                 return None
235
236         def execBegin(self):
237                 harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
238
239         def execEnd(self):
240                 harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
241
242         def refresh(self):
243                 self.changeDir(self.current_directory, self.getFilename())
244
245         def partitionListChanged(self, action, device):
246                 self.refreshMountpoints()
247                 if self.current_directory is None:
248                         self.refresh()