initial version
[enigma2-plugins.git] / vlcplayer / src / VlcFileList.py
1 from Components.FileList import FileEntryComponent\r
2 from Components.FileList import FileList\r
3 from Components.config import config\r
4 from urllib import urlencode\r
5 from urllib import urlopen\r
6 import re\r
7 import xml.sax\r
8 import os.path\r
9 \r
10 class vlcBrowseXmlHandler(xml.sax.ContentHandler):\r
11         \r
12         def __init__(self, host, regex = None):\r
13                 self.host = host\r
14                 self.regex = regex\r
15                 self.files = []\r
16                 self.directories = []\r
17         \r
18         def startElement(self, name, attrs):\r
19                 if name == "element" and attrs is not None:\r
20                         type = attrs.getValue("type")\r
21                         name = attrs.getValue("name").encode("latin_1", "replace")\r
22                         path = self.host + ":" + attrs.getValue("path").encode("latin_1")\r
23                         if type == "directory":\r
24                                 self.directories.append(FileEntryComponent(name, path, True))\r
25                         elif len(path) > 0:\r
26                                 if self.regex is None or self.regex.search(path):\r
27                                         self.files.append(FileEntryComponent(name, path, False))\r
28         \r
29 \r
30 class VlcFileList(FileList):\r
31         def __init__(self, pMatchingPattern=None):\r
32                 FileList.__init__(self, None, matchingPattern = pMatchingPattern, useServiceRef = False)\r
33                 self.current_directory = None\r
34                 self.current_server = None\r
35                 self.current_path = None\r
36         \r
37 #       def _getFilesAndDirs_old(self, host, path, regex = None):\r
38 #               files = []\r
39 #               directories = []\r
40 #               result = vlcHttpCommand_old(host, path)\r
41 #               entries = result.split("\n")\r
42 #               for e in entries:\r
43 #                       if e[0:4] == "DIR:":\r
44 #                               e = e[4:]\r
45 #                               directories.append(FileEntryComponent(os.path.basename(e), host+":"+e, True))\r
46 #                       elif len(e) > 0:\r
47 #                               if regex is None or regex.search(e):\r
48 #                                       files.append(FileEntryComponent(os.path.basename(e), host+":"+e, False))\r
49 #               return (files,directories)\r
50 \r
51         def _getFilesAndDirs(self, servernum, path, regex = None):\r
52                 cfg = config.plugins.vlcplayer.servers[servernum]\r
53                 url = "http://%s:%d/requests/browse.xml?%s" % (cfg.host.value, cfg.httpport.value, urlencode({'dir': path}))\r
54                 print "[VLC] _getFilesAndDirs", url\r
55                 req = urlopen(url)\r
56                 if req is None:\r
57                         raise IOError, "No response from server"\r
58                 handler = vlcBrowseXmlHandler(str(servernum), regex)\r
59                 xml.sax.parse(req, handler)\r
60                 return (handler.files, handler.directories)\r
61         \r
62         def initServerlist(self):\r
63                 self.list = []\r
64                 for i in range(0, config.plugins.vlcplayer.servercount.value):\r
65                         cfg = config.plugins.vlcplayer.servers[i]\r
66                         self.list.append(FileEntryComponent(cfg.host.value, str(i)+":"+cfg.basedir.value, True))\r
67                 self.list.append(FileEntryComponent(_("<add server>"), None, False))\r
68                 self.l.setList(self.list)\r
69                 self.current_directory = None\r
70                 self.current_server = None\r
71                 self.current_path = None\r
72 \r
73         def changeDir(self, directory, select = None):\r
74                 print "[VLC] changeDir ", directory\r
75                 if directory is None:\r
76                         self.initServerlist()\r
77                         return\r
78 \r
79                 directory = directory.replace("\\","/")\r
80                 directory = os.path.normpath(directory)\r
81                 if directory == ".":\r
82                         self.initServerlist()\r
83                         return\r
84 \r
85                 i = directory.find(":")\r
86                 servernum = int(directory[0:i])\r
87                 path = directory[i+1:]\r
88                 if len(path) == 0:\r
89                         path = "/"\r
90 \r
91                 if self.matchingPattern is not None:\r
92                         regex = re.compile(self.matchingPattern)\r
93                 else:\r
94                         regex = None\r
95 \r
96                 files, directories = self._getFilesAndDirs(servernum, path, regex)\r
97 \r
98                 directories.sort(cmp=lambda x, y: cmp(x[0], y[0]));\r
99                 files.sort(cmp=lambda x, y: cmp(x[0], y[0]));\r
100                 self.list = directories + files\r
101                 self.l.setList(self.list)\r
102                 self.current_directory = str(servernum) + ":" + path\r
103                 self.current_server = servernum\r
104                 self.current_path = path\r
105                 \r
106                 if select is not None:\r
107                         self.setSelection(select)\r
108 \r
109         def setSelection(self, select):\r
110                 i = 0\r
111                 self.moveToIndex(0)\r
112                 for x in self.list:\r
113                         p = x[0][0]\r
114                         if p == select:\r
115                                 self.moveToIndex(i)\r
116                         i += 1\r