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