aboutsummaryrefslogtreecommitdiff
path: root/lib/python/Components/FileList.py
blob: 961eb09ddc28d2c8ab96645d0a5021378bd6f6cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from HTMLComponent import *
from GUIComponent import *
import re

from MenuList import MenuList

from Tools.Directories import *

from enigma import *

RT_HALIGN_LEFT = 0
RT_HALIGN_RIGHT = 1
RT_HALIGN_CENTER = 2
RT_HALIGN_BLOCK = 4

RT_VALIGN_TOP = 0
RT_VALIGN_CENTER = 8
RT_VALIGN_BOTTOM = 16

EXTENSIONS = {
		"mp3": "music",
		"wav": "music",
		"jpg": "picture",
		"jpeg": "picture",
		"png": "picture",
		"ts": "movie",
		"avi": "movie",
		"mpg": "movie",
		"mpeg": "movie",
	}

def FileEntryComponent(name, absolute, isDir = False):
	res = [ (absolute, isDir) ]
	res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 200, 20, 0, RT_HALIGN_LEFT ,name))
	if isDir:
		png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "/extensions/directory.png"))
	else:
		extension = name.split('.')
		extension = extension[-1]
		if EXTENSIONS.has_key(extension):
			png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "/extensions/" + EXTENSIONS[extension] + ".png"))
	if png is not None:
		res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
	
	return res

class FileList(HTMLComponent, GUIComponent, MenuList):
	def __init__(self, directory, showDirectories = True, showFiles = True, matchingPattern = None):
		GUIComponent.__init__(self)
		self.l = eListboxPythonMultiContent()

		self.showDirectories = showDirectories
		self.showFiles = showFiles
		# example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
		self.matchingPattern = matchingPattern
		self.changeDir(directory)

		self.l.setFont(0, gFont("Regular", 18))
		
	def getSelection(self):
		return self.l.getCurrentSelection()[0]
	
	def changeDir(self, directory):
		self.list = []
		
		files = os.listdir(directory)
		files.sort()
		
		if directory != "/" and self.showDirectories:
			self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
			
		directories = []
		for x in files:
			if os.path.isdir(directory + x):
				directories.append(x)
				files.remove(x)

		if self.showDirectories:
			for x in directories:
				self.list.append(FileEntryComponent(name = x, absolute = directory + x + "/" , isDir = True))

					
		if self.showFiles:
			for x in files:
				if self.matchingPattern is not None:
					if re.compile(self.matchingPattern).search(x):
						self.list.append(FileEntryComponent(name = x, absolute = directory + x , isDir = False))
				else:
					self.list.append(FileEntryComponent(name = x, absolute = directory + x , isDir = False))
				
		self.l.setList(self.list)
				
	def GUIcreate(self, parent):
		self.instance = eListbox(parent)
		self.instance.setContent(self.l)
		self.instance.setItemHeight(23)