select directory on .. (and first entry otherewise) in Filelist, add getCurrentDirec...
[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, 470, 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].lower()
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.current_directory = None
56                 self.useServiceRef = useServiceRef
57                 self.showDirectories = showDirectories
58                 self.showFiles = showFiles
59                 self.isTop = isTop
60                 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
61                 self.matchingPattern = matchingPattern
62                 self.changeDir(directory)
63
64                 self.l.setFont(0, gFont("Regular", 18))
65                 
66         def getSelection(self):
67                 return self.l.getCurrentSelection()[0]
68         
69         def getFileList(self):
70                 return self.list
71         
72         def changeDir(self, directory, select = None):
73                 self.list = []
74                 
75                 self.current_directory = directory
76                 directories = []
77                 files = []
78                 
79                 if self.useServiceRef:
80                         root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
81                         serviceHandler = eServiceCenter.getInstance()
82                         list = serviceHandler.list(root)
83                         
84                         while 1:
85                                 s = list.getNext()
86                                 if not s.valid():
87                                         del list
88                                         break
89                                 if s.flags & s.mustDescent:
90                                         directories.append(s.getPath())
91                                 else:
92                                         files.append(s)
93                                 print s.getName(), s.flags
94                         directories.sort()
95                         files.sort()
96                 else:
97                         files = os.listdir(directory)
98                         files.sort()
99                         tmpfiles = files[:]
100                         for x in tmpfiles:
101                                 if os.path.isdir(directory + x):
102                                         directories.append(directory + x + "/")
103                                         files.remove(x)
104                 
105                 if directory != "/" and self.showDirectories and not self.isTop:
106                         self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
107
108                 if self.showDirectories:
109                         for x in directories:
110                                 name = x.split('/')[-2]
111                                 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
112
113                 if self.showFiles:
114                         for x in files:
115                                 if self.useServiceRef:
116                                         path = x.getPath()
117                                         name = path.split('/')[-1]
118                                 else:
119                                         path = directory + x
120                                         name = x
121                                 
122                                 if self.matchingPattern is not None:
123                                         if re.compile(self.matchingPattern).search(path):
124                                                 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
125                                 else:
126                                         self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
127
128                 self.l.setList(self.list)
129                 
130                 if select is not None:
131                         i = 0
132                         self.moveToIndex(0)
133                         for x in self.list:
134                                 p = x[0][0]
135                                 
136                                 if isinstance(p, eServiceReference):
137                                         p = p.getPath()
138                                 
139                                 if p == select:
140                                         self.moveToIndex(i)
141                                 i += 1
142
143         def getCurrentDirectory(self):
144                 return self.current_directory
145
146         def canDescent(self):
147                 return self.getSelection()[1]
148         
149         def descent(self):
150                 self.changeDir(self.getSelection()[0], select = self.current_directory)
151                 
152         def getFilename(self):
153                 x = self.getSelection()[0]
154                 if isinstance(x, eServiceReference):
155                         x = x.getPath()
156                 return x
157
158         def getServiceRef(self):
159                 x = self.getSelection()[0]
160                 if isinstance(x, eServiceReference):
161                         return x
162                 return None
163
164         GUI_WIDGET = eListbox
165
166         def postWidgetCreate(self, instance):
167                 instance.setContent(self.l)
168                 instance.setItemHeight(23)