store and re-use previous pip position and size
[enigma2.git] / lib / python / Components / FileList.py
1 from HTMLComponent import *
2 from GUIComponent import *
3 import re
4
5 from MenuList import MenuList
6
7 from Tools.Directories import *
8
9 from enigma import *
10
11 RT_HALIGN_LEFT = 0
12 RT_HALIGN_RIGHT = 1
13 RT_HALIGN_CENTER = 2
14 RT_HALIGN_BLOCK = 4
15
16 RT_VALIGN_TOP = 0
17 RT_VALIGN_CENTER = 8
18 RT_VALIGN_BOTTOM = 16
19
20 EXTENSIONS = {
21                 "mp3": "music",
22                 "wav": "music",
23                 "ogg": "music",
24                 "jpg": "picture",
25                 "jpeg": "picture",
26                 "png": "picture",
27                 "ts": "movie",
28                 "avi": "movie",
29                 "mpg": "movie",
30                 "mpeg": "movie",
31         }
32
33 def FileEntryComponent(name, absolute = None, isDir = False):
34         res = [ (absolute, isDir) ]
35         res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
36         if isDir:
37                 png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
38         else:
39                 extension = name.split('.')
40                 extension = extension[-1].lower()
41                 if EXTENSIONS.has_key(extension):
42                         png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
43                 else:
44                         png = None
45         if png is not None:
46                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
47         
48         return res
49
50 class FileList(MenuList, HTMLComponent, GUIComponent):
51         def __init__(self, directory, showDirectories = True, showFiles = True, matchingPattern = None, useServiceRef = False, isTop = False):
52                 GUIComponent.__init__(self)
53                 self.l = eListboxPythonMultiContent()
54                 
55                 self.useServiceRef = useServiceRef
56                 self.showDirectories = showDirectories
57                 self.showFiles = showFiles
58                 self.isTop = isTop
59                 # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
60                 self.matchingPattern = matchingPattern
61                 self.changeDir(directory)
62
63                 self.l.setFont(0, gFont("Regular", 18))
64                 
65         def getSelection(self):
66                 return self.l.getCurrentSelection()[0]
67         
68         def getFileList(self):
69                 return self.list
70         
71         def changeDir(self, directory):
72                 self.list = []
73                 
74                 directories = []
75                 files = []
76                 
77                 if self.useServiceRef:
78                         root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
79                         serviceHandler = eServiceCenter.getInstance()
80                         list = serviceHandler.list(root)
81                         
82                         while 1:
83                                 s = list.getNext()
84                                 if not s.valid():
85                                         del list
86                                         break
87                                 if s.flags & s.mustDescent:
88                                         directories.append(s.getPath())
89                                 else:
90                                         files.append(s)
91                                 print s.getName(), s.flags
92                         directories.sort()
93                         files.sort()
94                 else:
95                         files = os.listdir(directory)
96                         files.sort()
97                         tmpfiles = files[:]
98                         for x in tmpfiles:
99                                 if os.path.isdir(directory + x):
100                                         directories.append(directory + x + "/")
101                                         files.remove(x)
102                 
103                 if directory != "/" and self.showDirectories and not self.isTop:
104                         self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
105
106                 if self.showDirectories:
107                         for x in directories:
108                                 name = x.split('/')[-2]
109                                 self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
110
111                 if self.showFiles:
112                         for x in files:
113                                 if self.useServiceRef:
114                                         path = x.getPath()
115                                         name = path.split('/')[-1]
116                                 else:
117                                         path = directory + x
118                                         name = x
119                                 
120                                 if self.matchingPattern is not None:
121                                         if re.compile(self.matchingPattern).search(path):
122                                                 self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
123                                 else:
124                                         self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
125                                 
126                 self.l.setList(self.list)
127                 
128         def canDescent(self):
129                 return self.getSelection()[1]
130         
131         def descent(self):
132                 self.changeDir(self.getSelection()[0])
133                 
134         def getFilename(self):
135                 return self.getSelection()[0].getPath()
136
137         def getServiceRef(self):
138                 return self.getSelection()[0]
139
140         GUI_WIDGET = eListbox
141
142         def postWidgetCreate(self, instance):
143                 instance.setContent(self.l)
144                 instance.setItemHeight(23)