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