Update Description
[enigma2-plugins.git] / vlcplayer / src / VlcFileList.py
1 # -*- coding: ISO-8859-1 -*-
2 #===============================================================================
3 # VLC Player Plugin by A. Lätsch 2007
4 #                   modified by Volker Christian 2008
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2, or (at your option) any later
9 # version.
10 #===============================================================================
11
12
13 import re
14
15 from enigma import eListboxPythonMultiContent, RT_HALIGN_LEFT, gFont
16 from Tools.LoadPixmap import LoadPixmap
17 from Tools.Directories import SCOPE_SKIN_IMAGE, SCOPE_PLUGINS, resolveFilename
18 from Components.MenuList import MenuList
19
20 from pyexpat import ExpatError
21
22 from skin import parseFont
23
24 MEDIA_EXTENSIONS = {
25                 "mp3": "music",
26                 "wav": "music",
27                 "ogg": "music",
28                 "ts": "movie",
29                 "avi": "movie",
30                 "mpg": "movie",
31                 "mpeg": "movie",
32                 "wmv": "movie",
33                 "mov": "movie",
34                 "iso": "movie",
35                 "mkv": "movie",
36                 "flv": "movie"
37         }
38
39
40 PLAYLIST_EXTENSIONS = {
41                 "m3u": "playlist.png",
42                 "pls": "playlist.png",
43                 "xspf": "playlist.png",
44         }
45
46
47 class VlcFileList(MenuList):
48         def __init__(self, getFilesAndDirsCB, baseDir, matchingPattern):
49                 MenuList.__init__(self, list, False, eListboxPythonMultiContent)
50                 self.font = gFont("Regular", 18)
51                 self.l.setFont(0, self.font)
52                 self.l.setItemHeight(23)
53                 self.l.setBuildFunc(self.buildListboxEntry)
54                 self.currentDirectory = baseDir
55                 self.getFilesAndDirsCB = getFilesAndDirsCB
56                 self.changeRegex(matchingPattern)
57
58         def applySkin(self, desktop, parent):
59                 attribs = [ ]
60                 if self.skinAttributes is not None:
61                         for (attrib, value) in self.skinAttributes:
62                                 if attrib == "font":
63                                         self.font = parseFont(value, ((1,1),(1,1)))
64                                         self.l.setFont(0, self.font)
65                                 elif attrib == "itemHeight":
66                                         self.l.setItemHeight(int(value))
67                                 else:
68                                         attribs.append((attrib, value))
69                         self.skinAttributes = attribs
70                 return MenuList.applySkin(self, desktop, parent)
71
72         def buildListboxEntry(self, path, isDir, name):
73                 size = self.l.getItemSize()
74                 height = size.height()
75                 res = [
76                         (path, isDir, name),
77                         (eListboxPythonMultiContent.TYPE_TEXT, height + 15, 0, size.width() - height - 15, height, 0, RT_HALIGN_LEFT, name)
78                 ]
79
80                 if isDir:
81                         png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
82                 else:
83                         extension = name.split('.')
84                         extension = extension[-1].lower()
85                         if MEDIA_EXTENSIONS.has_key(extension):
86                                 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + MEDIA_EXTENSIONS[extension] + ".png"))
87                         elif PLAYLIST_EXTENSIONS.has_key(extension):
88                                 png = LoadPixmap(resolveFilename(SCOPE_PLUGINS, "Extensions/VlcPlayer/") + PLAYLIST_EXTENSIONS[extension])
89                         else:
90                                 png = None
91
92                 if png is not None:
93                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 0, height, height, png))
94
95                 return res
96
97         def update(self):
98                 success = False
99                 filelistEntries = self.getFilesAndDirsCB(self.currentDirectory, self.regex)
100                 fileEntries = []
101                 directoryEntries = []
102                 if filelistEntries is not None:
103                         files, directories = filelistEntries
104                         for file in files:
105                                 name, path = file
106                                 fileEntries.append((path, False, name))
107                         for directory in directories:
108                                 name, path = directory
109                                 directoryEntries.append((path, True, name))
110                         fileEntries.sort(cmp = lambda x, y: cmp(x[0], y[0]))
111                         directoryEntries.sort(cmp = lambda x, y: cmp(x[0], y[0]))
112                         success = True
113                 self.list = directoryEntries + fileEntries
114                 self.l.setList(self.list)
115                 self.moveToIndex(0)
116                 return success
117
118         def isVideoTS(self):
119                 for e in self.list:
120                         if e[1] == True and e[0].upper().endswith("VIDEO_TS"):
121                                 return True
122                 return False
123
124         def changeDirectory(self, directory):
125                 previousDirectory = self.currentDirectory
126                 self.currentDirectory = directory
127                 try:
128                         if self.update():
129                                 if self.isVideoTS():
130                                         ret = "dvdsimple://" + self.currentDirectory + "/VIDEO_TS", self.currentDirectory
131                                         self.currentDirectory = previousDirectory
132                                         self.update()
133                                 else:
134                                         ret = None, self.currentDirectory
135                         else:
136                                 self.currentDirectory = previousDirectory
137                                 ret = None, None
138                 except ExpatError, e:
139                         print e
140                         self.currentDirectory = previousDirectory
141                         self.update()
142                         ret = None, self.currentDirectory
143                 return ret
144
145         def activate(self):
146                 cur = self.getCurrent()
147                 if cur is not None:
148                         if cur[1]:
149                                 ret = self.changeDirectory(cur[0])
150                         else:
151                                 ret = cur[0], cur[2]
152                 else:
153                         ret = None, None
154                 return ret
155
156         def changeRegex(self, matchingPattern):
157                 if matchingPattern is not None:
158                         self.regex = re.compile(matchingPattern)
159                 else:
160                         self.regex = None
161
162         def getNextFile(self):
163                 i = self.getSelectedIndex() + 1
164                 Len = len(self.list)
165                 while i < Len:
166                         cur = self.list[i]
167                         if cur[1] == False:
168                                 self.moveToIndex(i)
169                                 return cur[0], cur[2]
170                         i = i + 1
171                 return None, None
172
173         def getPrevFile(self):
174                 i = self.getSelectedIndex() - 1
175                 while i > -1:
176                         cur = self.list[i]
177                         if cur[1] == False:
178                                 self.moveToIndex(i)
179                                 return cur[0], cur[2]
180                         i = i - 1
181                 return None, None
182