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