add ability to use regular expressions to filter files to display in the FileList
[enigma2.git] / lib / python / Components / FileList.py
1 from HTMLComponent import *
2 from GUIComponent import *
3 import re
4
5 from MenuList import MenuList
6
7 from Tools.Directories import *
8
9 from enigma import *
10
11 RT_HALIGN_LEFT = 0
12 RT_HALIGN_RIGHT = 1
13 RT_HALIGN_CENTER = 2
14 RT_HALIGN_BLOCK = 4
15
16 RT_VALIGN_TOP = 0
17 RT_VALIGN_CENTER = 8
18 RT_VALIGN_BOTTOM = 16
19
20 def FileEntryComponent(name, absolute, isDir = False):
21         res = [ (absolute, isDir) ]
22         res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 200, 20, 0, RT_HALIGN_LEFT ,name))
23         if isDir:
24                 png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "/extensions/directory.png"))
25         else:
26                 # FIXME: detect file extensions correctly
27                 png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "/extensions/" + name[-3:] + ".png"))
28         if png is not None:
29                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
30         
31         return res
32
33 class FileList(HTMLComponent, GUIComponent, MenuList):
34         def __init__(self, directory, showDirectories = True, showFiles = True, matchingPattern = None):
35                 GUIComponent.__init__(self)
36                 self.l = eListboxPythonMultiContent()
37
38                 self.showDirectories = showDirectories
39                 self.showFiles = showFiles
40                 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
41                 self.matchingPattern = matchingPattern
42                 self.changeDir(directory)
43
44                 self.l.setFont(0, gFont("Regular", 18))
45                 
46         def getSelection(self):
47                 return self.l.getCurrentSelection()[0]
48         
49         def changeDir(self, directory):
50                 self.list = []
51                 
52                 directories = os.listdir(directory)
53                 
54                 if directory != "/" and self.showDirectories:
55                         self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
56                 for x in directories:
57                         if os.path.isdir(directory + x):
58                                 if self.showDirectories:
59                                         self.list.append(FileEntryComponent(name = x, absolute = directory + x + "/" , isDir = True))
60                         elif self.showFiles:
61                                 if self.matchingPattern is not None:
62                                         if re.compile(self.matchingPattern).search(x):
63                                                 self.list.append(FileEntryComponent(name = x, absolute = directory + x , isDir = False))
64                                 else:
65                                         self.list.append(FileEntryComponent(name = x, absolute = directory + x , isDir = False))
66                                 
67                 self.l.setList(self.list)
68                                 
69         def GUIcreate(self, parent):
70                 self.instance = eListbox(parent)
71                 self.instance.setContent(self.l)
72                 self.instance.setItemHeight(23)