Version 1.4:
[enigma2-plugins.git] / vlcplayer / src / VlcFileList.py
1 # -*- coding: ISO-8859-1 -*-
2 #===============================================================================
3 # VLC Player Plugin by A. Lätsch 2007
4 #
5 # This is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 2, or (at your option) any later
8 # version.
9 #===============================================================================
10
11 from Components.FileList import FileEntryComponent
12 from Components.FileList import FileList
13 from Components.config import config
14 from urllib import urlencode
15 from urllib import urlopen
16 import posixpath
17 import re
18 import xml.sax
19
20 def normpath(path):
21         if path is None:
22                 return None
23         path = path.replace("\\","/").replace("//", "/")
24         if path == "/..":
25                 return ""
26         if len(path) > 0 and path[0] != '/': 
27                 path = posixpath.normpath('/'+path)[1:]
28         else:
29                 path = posixpath.normpath(path)
30
31         if len(path) == 0 or path == "//":
32                 return "/"
33         elif path == ".":
34                 return None
35         return path
36
37 class vlcBrowseXmlHandler(xml.sax.ContentHandler):
38         
39         def __init__(self, host, regex = None):
40                 self.host = host
41                 self.regex = regex
42                 self.files = []
43                 self.directories = []
44         
45         def startElement(self, name, attrs):
46                 if name == "element" and attrs is not None:
47                         type = attrs.getValue("type")
48                         name = attrs.getValue("name").encode("utf8")
49                         path = "%s:%s" % (self.host, normpath(attrs.getValue("path").encode("utf8")))
50                         if type == "directory":
51                                 self.directories.append(FileEntryComponent(name, path, True))
52                         elif len(path) > 0:
53                                 if self.regex is None or self.regex.search(path):
54                                         self.files.append(FileEntryComponent(name, path, False))
55         
56
57 class VlcFileList(FileList):
58         def __init__(self, pMatchingPattern=None):
59                 FileList.__init__(self, None, matchingPattern = pMatchingPattern, useServiceRef = False)
60                 self.current_directory = None
61                 self.current_server = None
62                 self.current_path = None
63         
64         def __getFilesAndDirs(self, servernum, path, regex = None):
65                 cfg = config.plugins.vlcplayer.servers[servernum]
66                 url = "http://%s:%d/requests/browse.xml?%s" % (cfg.host.value, cfg.httpport.value, urlencode({'dir': path}))
67                 print "[VLC] __getFilesAndDirs", url
68                 req = urlopen(url)
69                 if req is None:
70                         raise IOError, "No response from server"
71                 handler = vlcBrowseXmlHandler(str(servernum), regex)
72                 xml.sax.parse(req, handler)
73                 return (handler.files, handler.directories)
74         
75         def initServerlist(self):
76                 self.list = []
77                 for i in range(0, config.plugins.vlcplayer.servercount.value):
78                         cfg = config.plugins.vlcplayer.servers[i]
79                         self.list.append(FileEntryComponent(cfg.host.value, str(i)+":"+cfg.basedir.value, True))
80                 self.list.append(FileEntryComponent(_("<add server>"), None, False))
81                 self.l.setList(self.list)
82                 self.current_directory = None
83                 self.current_server = None
84                 self.current_path = None
85                 
86         def changeDir(self, directory, select = None):
87                 print "[VLC] changeDir ", directory, select
88                 if directory is None:
89                         self.initServerlist()
90                         return
91
92                 i = directory.find(":")
93                 servernum = int(directory[0:i])
94                 path = normpath(directory[i+1:])
95                 if path is None:
96                         self.initServerlist()
97                         return
98
99                 if self.matchingPattern is not None:
100                         regex = re.compile(self.matchingPattern)
101                 else:
102                         regex = None
103
104                 files, directories = self.__getFilesAndDirs(servernum, path, regex)
105
106                 directories.sort(cmp=lambda x, y: cmp(x[0], y[0]));
107                 files.sort(cmp=lambda x, y: cmp(x[0], y[0]));
108                 self.list = directories + files
109                 self.l.setList(self.list)
110                 self.current_directory = str(servernum) + ":" + path
111                 self.current_server = servernum
112                 self.current_path = path
113                 
114                 if select is not None:
115                         self.setSelection(select)
116                 else:
117                         self.moveToIndex(0)
118
119         def setSelection(self, select):
120                 i = 0
121                 self.moveToIndex(0)
122                 for x in self.list:
123                         p = x[0][0]
124                         if p == select:
125                                 self.moveToIndex(i)
126                         i += 1
127
128         def getNextFile(self):
129                 i = self.getSelectedIndex() + 1
130                 while i < len(self.list):
131                         if self.list[i][0][1]==False:
132                                 self.moveToIndex(i)
133                                 return self.list[i][0][0]
134                         i = i+1
135                 return None
136         
137         def getPrevFile(self):
138                 i = self.getSelectedIndex() - 1
139                 while i >= 0:
140                         if self.list[i][0][1]==False:
141                                 self.moveToIndex(i)
142                                 return self.list[i][0][0]
143                         i = i-1
144                 return None