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