add getSelectionIndex
[enigma2.git] / lib / python / Components / FileList.py
1 from HTMLComponent import *
2 from GUIComponent import *
3 import re
4
5 from os import path as os_path, listdir
6 from MenuList import MenuList
7 from Components.Harddisk import harddiskmanager
8
9 from Tools.Directories import SCOPE_SKIN_IMAGE, resolveFilename
10
11 from enigma import RT_HALIGN_LEFT, loadPNG, eListbox, eListboxPythonMultiContent, \
12         eServiceReference, eServiceCenter, gFont
13
14 EXTENSIONS = {
15                 "mp3": "music",
16                 "wav": "music",
17                 "ogg": "music",
18                 "jpg": "picture",
19                 "jpeg": "picture",
20                 "png": "picture",
21                 "bmp": "picture",
22                 "ts": "movie",
23                 "avi": "movie",
24                 "mpg": "movie",
25                 "mpeg": "movie",
26         }
27
28 def FileEntryComponent(name, absolute = None, isDir = False):
29         res = [ (absolute, isDir) ]
30         res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
31         if isDir:
32                 png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
33         else:
34                 extension = name.split('.')
35                 extension = extension[-1].lower()
36                 if EXTENSIONS.has_key(extension):
37                         png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
38                 else:
39                         png = None
40         if png is not None:
41                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
42         
43         return res
44
45 class FileList(MenuList, HTMLComponent, GUIComponent):
46         def __init__(self, directory, showDirectories = True, showFiles = True, matchingPattern = None, useServiceRef = False, isTop = False):
47                 GUIComponent.__init__(self)
48                 self.l = eListboxPythonMultiContent()
49                 
50                 self.mount_point = None
51                 self.current_directory = None
52                 self.useServiceRef = useServiceRef
53                 self.showDirectories = showDirectories
54                 self.showFiles = showFiles
55                 self.isTop = isTop
56                 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
57                 self.matchingPattern = matchingPattern
58                 self.changeDir(directory)
59
60                 self.l.setFont(0, gFont("Regular", 18))
61                 self.l.setItemHeight(23)
62
63         def getSelection(self):
64                 if self.l.getCurrentSelection() is None:
65                         return None
66                 return self.l.getCurrentSelection()[0]
67         
68         def getSelectionIndex(self):
69                 return self.l.getCurrentSelectionIndex()
70
71         def getFileList(self):
72                 return self.list
73         
74         def changeDir(self, directory, select = None):
75                 self.list = []
76                 
77                 # if we are just entering from the list of mount points:
78                 if self.current_directory is None:
79                         self.mount_point = directory
80                 self.current_directory = directory
81                 directories = []
82                 files = []
83                 
84                 if directory is None: # present available mountpoints
85                         print "listing partitions:"
86                         for p in harddiskmanager.getMountedPartitions():
87                                 if p.mountpoint == "/":
88                                         self.list.append(FileEntryComponent(name = p.description, absolute = p.mountpoint, isDir = True))
89                                 else:
90                                         self.list.append(FileEntryComponent(name = p.description, absolute = p.mountpoint + "/", isDir = True))
91                         files = [ ]
92                         directories = [ ]
93                 elif self.useServiceRef:
94                         root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
95                         serviceHandler = eServiceCenter.getInstance()
96                         list = serviceHandler.list(root)
97                         
98                         while 1:
99                                 s = list.getNext()
100                                 if not s.valid():
101                                         del list
102                                         break
103                                 if s.flags & s.mustDescent:
104                                         directories.append(s.getPath())
105                                 else:
106                                         files.append(s)
107                                 print s.getName(), s.flags
108                         directories.sort()
109                         files.sort()
110                 else:
111                         files = listdir(directory)
112                         files.sort()
113                         tmpfiles = files[:]
114                         for x in tmpfiles:
115                                 if os_path.isdir(directory + x):
116                                         directories.append(directory + x + "/")
117                                         files.remove(x)
118                 
119                 if directory is not None and self.showDirectories and not self.isTop:
120                         if directory == self.mount_point:
121                                 self.list.append(FileEntryComponent(name = ".. (" +_("List of Storage Devices") + ")", absolute = None, isDir = True))
122                         else:
123                                 self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
124
125                 if self.showDirectories:
126                         for x in directories:
127                                 name = x.split('/')[-2]
128                                 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
129
130                 if self.showFiles:
131                         for x in files:
132                                 if self.useServiceRef:
133                                         path = x.getPath()
134                                         name = path.split('/')[-1]
135                                 else:
136                                         path = directory + x
137                                         name = x
138                                 
139                                 if self.matchingPattern is not None:
140                                         if re.compile(self.matchingPattern).search(path):
141                                                 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
142                                 else:
143                                         self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
144
145                 self.l.setList(self.list)
146                 
147                 if select is not None:
148                         i = 0
149                         self.moveToIndex(0)
150                         for x in self.list:
151                                 p = x[0][0]
152                                 
153                                 if isinstance(p, eServiceReference):
154                                         p = p.getPath()
155                                 
156                                 if p == select:
157                                         self.moveToIndex(i)
158                                 i += 1
159
160         def getCurrentDirectory(self):
161                 return self.current_directory
162
163         def canDescent(self):
164                 if self.getSelection() is None:
165                         return False
166                 return self.getSelection()[1]
167         
168         def descent(self):
169                 if self.getSelection() is None:
170                         return
171                 self.changeDir(self.getSelection()[0], select = self.current_directory)
172                 
173         def getFilename(self):
174                 if self.getSelection() is None:
175                         return None
176                 x = self.getSelection()[0]
177                 if isinstance(x, eServiceReference):
178                         x = x.getPath()
179                 return x
180
181         def getServiceRef(self):
182                 if self.getSelection() is None:
183                         return None
184                 x = self.getSelection()[0]
185                 if isinstance(x, eServiceReference):
186                         return x
187                 return None
188
189         GUI_WIDGET = eListbox
190
191         def postWidgetCreate(self, instance):
192                 instance.setContent(self.l)