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