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