add some extension-icons
[vuplus_dvbapp] / 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 EXTENSIONS = {
21                 "mp3": "music",
22                 "wav": "music",
23                 "jpg": "picture",
24                 "jpeg": "picture",
25                 "png": "picture",
26                 "ts": "movie",
27                 "avi": "movie",
28                 "mpg": "movie",
29                 "mpeg": "movie",
30         }
31
32 def FileEntryComponent(name, absolute, isDir = False):
33         res = [ (absolute, isDir) ]
34         res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 200, 20, 0, RT_HALIGN_LEFT ,name))
35         if isDir:
36                 png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "/extensions/directory.png"))
37         else:
38                 # FIXME: detect file extensions correctly
39                 if EXTENSIONS.has_key(name[-3:]):
40                         png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "/extensions/" + EXTENSIONS[name[-3:]] + ".png"))
41         if png is not None:
42                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
43         
44         return res
45
46 class FileList(HTMLComponent, GUIComponent, MenuList):
47         def __init__(self, directory, showDirectories = True, showFiles = True, matchingPattern = None):
48                 GUIComponent.__init__(self)
49                 self.l = eListboxPythonMultiContent()
50
51                 self.showDirectories = showDirectories
52                 self.showFiles = showFiles
53                 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
54                 self.matchingPattern = matchingPattern
55                 self.changeDir(directory)
56
57                 self.l.setFont(0, gFont("Regular", 18))
58                 
59         def getSelection(self):
60                 return self.l.getCurrentSelection()[0]
61         
62         def changeDir(self, directory):
63                 self.list = []
64                 
65                 directories = os.listdir(directory)
66                 
67                 if directory != "/" and self.showDirectories:
68                         self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
69                 for x in directories:
70                         if os.path.isdir(directory + x):
71                                 if self.showDirectories:
72                                         self.list.append(FileEntryComponent(name = x, absolute = directory + x + "/" , isDir = True))
73                         elif self.showFiles:
74                                 if self.matchingPattern is not None:
75                                         if re.compile(self.matchingPattern).search(x):
76                                                 self.list.append(FileEntryComponent(name = x, absolute = directory + x , isDir = False))
77                                 else:
78                                         self.list.append(FileEntryComponent(name = x, absolute = directory + x , isDir = False))
79                                 
80                 self.l.setList(self.list)
81                                 
82         def GUIcreate(self, parent):
83                 self.instance = eListbox(parent)
84                 self.instance.setContent(self.l)
85                 self.instance.setItemHeight(23)