ce1f236f4246af6f019e27daaecec840769e7ecd
[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                 "ogg": "music",
24                 "jpg": "picture",
25                 "jpeg": "picture",
26                 "png": "picture",
27                 "ts": "movie",
28                 "avi": "movie",
29                 "mpg": "movie",
30                 "mpeg": "movie",
31         }
32
33 def FileEntryComponent(name, absolute = None, isDir = False):
34         res = [ (absolute, isDir) ]
35         res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 200, 20, 0, RT_HALIGN_LEFT, name))
36         if isDir:
37                 png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
38         else:
39                 extension = name.split('.')
40                 extension = extension[-1]
41                 if EXTENSIONS.has_key(extension):
42                         png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
43                 else:
44                         png = None
45         if png is not None:
46                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
47         
48         return res
49
50 class FileList(MenuList, HTMLComponent, GUIComponent):
51         def __init__(self, directory, showDirectories = True, showFiles = True, matchingPattern = None, useServiceRef = False, isTop = False):
52                 GUIComponent.__init__(self)
53                 self.l = eListboxPythonMultiContent()
54                 
55                 self.useServiceRef = useServiceRef
56                 self.showDirectories = showDirectories
57                 self.showFiles = showFiles
58                 self.isTop = isTop
59                 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
60                 self.matchingPattern = matchingPattern
61                 self.changeDir(directory)
62
63                 self.l.setFont(0, gFont("Regular", 18))
64                 
65         def getSelection(self):
66                 return self.l.getCurrentSelection()[0]
67         
68         def getFileList(self):
69                 return self.list
70         
71         def changeDir(self, directory):
72                 self.list = []
73                 
74                 directories = []
75                 files = []
76                 
77                 if self.useServiceRef:
78                         root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
79                         serviceHandler = eServiceCenter.getInstance()
80                         list = serviceHandler.list(root)
81                         
82                         while 1:
83                                 s = list.getNext()
84                                 if not s.valid():
85                                         del list
86                                         break
87                                 if s.flags & s.mustDescent:
88                                         directories.append(s.getPath())
89                                 else:
90                                         files.append(s)
91                                 print s.getName(), s.flags
92                 else:
93                         files = os.listdir(directory)
94                         files.sort()
95                         tmpfiles = files[:]
96                         for x in tmpfiles:
97                                 if os.path.isdir(directory + x):
98                                         directories.append(x)
99                                         files.remove(x)
100                 
101                 if directory != "/" and self.showDirectories and not self.isTop:
102                         self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
103
104                 if self.showDirectories:
105                         for x in directories:
106                                 name = x.split('/')[-2]
107                                 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
108
109                 if self.showFiles:
110                         for x in files:
111                                 if self.useServiceRef:
112                                         path = x.getPath()
113                                         name = path.split('/')[-1]
114                                 else:
115                                         path = directory + x
116                                         name = x
117                                 
118                                 if self.matchingPattern is not None:
119                                         if re.compile(self.matchingPattern).search(path):
120                                                 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
121                                 else:
122                                         self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
123                                 
124                 self.l.setList(self.list)
125                 
126         def canDescent(self):
127                 return self.getSelection()[1]
128         
129         def descent(self):
130                 self.changeDir(self.getSelection()[0])
131                 
132         def getFilename(self):
133                 return self.getSelection()[0].getPath()
134
135         def getServiceRef(self):
136                 return self.getSelection()[0]
137
138         GUI_WIDGET = eListbox
139
140         def postWidgetCreate(self, instance):
141                 instance.setContent(self.l)
142                 instance.setItemHeight(23)