add InfoBarTimeshiftState, fix timeshift after zap, fix seekactions
[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 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                 extension = name.split('.')
39                 extension = extension[-1]
40                 if EXTENSIONS.has_key(extension):
41                         png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
42         if png is not None:
43                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
44         
45         return res
46
47 class FileList(HTMLComponent, GUIComponent, MenuList):
48         def __init__(self, directory, showDirectories = True, showFiles = True, matchingPattern = None):
49                 GUIComponent.__init__(self)
50                 self.l = eListboxPythonMultiContent()
51
52                 self.showDirectories = showDirectories
53                 self.showFiles = showFiles
54                 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
55                 self.matchingPattern = matchingPattern
56                 self.changeDir(directory)
57
58                 self.l.setFont(0, gFont("Regular", 18))
59                 
60         def getSelection(self):
61                 return self.l.getCurrentSelection()[0]
62         
63         def changeDir(self, directory):
64                 self.list = []
65                 
66                 files = os.listdir(directory)
67                 files.sort()
68                 
69                 if directory != "/" and self.showDirectories:
70                         self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
71                         
72                 directories = []
73                 for x in files:
74                         if os.path.isdir(directory + x):
75                                 directories.append(x)
76                                 files.remove(x)
77
78                 if self.showDirectories:
79                         for x in directories:
80                                 self.list.append(FileEntryComponent(name = x, absolute = directory + x + "/" , isDir = True))
81
82                                         
83                 if self.showFiles:
84                         for x in files:
85                                 if self.matchingPattern is not None:
86                                         if re.compile(self.matchingPattern).search(x):
87                                                 self.list.append(FileEntryComponent(name = x, absolute = directory + x , isDir = False))
88                                 else:
89                                         self.list.append(FileEntryComponent(name = x, absolute = directory + x , isDir = False))
90                                 
91                 self.l.setList(self.list)
92                                 
93         def GUIcreate(self, parent):
94                 self.instance = eListbox(parent)
95                 self.instance.setContent(self.l)
96                 self.instance.setItemHeight(23)