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