small change
[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                 
62         def getSelection(self):
63                 if self.l.getCurrentSelection() is None:
64                         return None
65                 return self.l.getCurrentSelection()[0]
66         
67         def getFileList(self):
68                 return self.list
69         
70         def changeDir(self, directory, select = None):
71                 self.list = []
72                 
73                 # if we are just entering from the list of mount points:
74                 if self.current_directory is None:
75                         self.mount_point = directory
76                 self.current_directory = directory
77                 directories = []
78                 files = []
79                 
80                 if directory is None: # present available mountpoints
81                         print "listing partitions:"
82                         for p in harddiskmanager.getMountedPartitions():
83                                 if p.mountpoint == "/":
84                                         self.list.append(FileEntryComponent(name = p.description, absolute = p.mountpoint, isDir = True))
85                                 else:
86                                         self.list.append(FileEntryComponent(name = p.description, absolute = p.mountpoint + "/", isDir = True))
87                         files = [ ]
88                         directories = [ ]
89                 elif self.useServiceRef:
90                         root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
91                         serviceHandler = eServiceCenter.getInstance()
92                         list = serviceHandler.list(root)
93                         
94                         while 1:
95                                 s = list.getNext()
96                                 if not s.valid():
97                                         del list
98                                         break
99                                 if s.flags & s.mustDescent:
100                                         directories.append(s.getPath())
101                                 else:
102                                         files.append(s)
103                                 print s.getName(), s.flags
104                         directories.sort()
105                         files.sort()
106                 else:
107                         files = listdir(directory)
108                         files.sort()
109                         tmpfiles = files[:]
110                         for x in tmpfiles:
111                                 if os_path.isdir(directory + x):
112                                         directories.append(directory + x + "/")
113                                         files.remove(x)
114                 
115                 if directory is not None and self.showDirectories and not self.isTop:
116                         if directory == self.mount_point:
117                                 self.list.append(FileEntryComponent(name = ".. (" +_("List of Storage Devices") + ")", absolute = None, isDir = True))
118                         else:
119                                 self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
120
121                 if self.showDirectories:
122                         for x in directories:
123                                 name = x.split('/')[-2]
124                                 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
125
126                 if self.showFiles:
127                         for x in files:
128                                 if self.useServiceRef:
129                                         path = x.getPath()
130                                         name = path.split('/')[-1]
131                                 else:
132                                         path = directory + x
133                                         name = x
134                                 
135                                 if self.matchingPattern is not None:
136                                         if re.compile(self.matchingPattern).search(path):
137                                                 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
138                                 else:
139                                         self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
140
141                 self.l.setList(self.list)
142                 
143                 if select is not None:
144                         i = 0
145                         self.moveToIndex(0)
146                         for x in self.list:
147                                 p = x[0][0]
148                                 
149                                 if isinstance(p, eServiceReference):
150                                         p = p.getPath()
151                                 
152                                 if p == select:
153                                         self.moveToIndex(i)
154                                 i += 1
155
156         def getCurrentDirectory(self):
157                 return self.current_directory
158
159         def canDescent(self):
160                 if self.getSelection() is None:
161                         return False
162                 return self.getSelection()[1]
163         
164         def descent(self):
165                 if self.getSelection() is None:
166                         return
167                 self.changeDir(self.getSelection()[0], select = self.current_directory)
168                 
169         def getFilename(self):
170                 if self.getSelection() is None:
171                         return None
172                 x = self.getSelection()[0]
173                 if isinstance(x, eServiceReference):
174                         x = x.getPath()
175                 return x
176
177         def getServiceRef(self):
178                 if self.getSelection() is None:
179                         return None
180                 x = self.getSelection()[0]
181                 if isinstance(x, eServiceReference):
182                         return x
183                 return None
184
185         GUI_WIDGET = eListbox
186
187         def postWidgetCreate(self, instance):
188                 instance.setContent(self.l)
189                 instance.setItemHeight(23)