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