mediaplayer can play wave files (package gst-plugin-wavparse needed)
[enigma2.git] / lib / service / servicefs.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/base/object.h>
3 #include <string>
4 #include <errno.h>
5 #include <lib/service/servicefs.h>
6 #include <lib/service/service.h>
7 #include <lib/service/servicedvb.h>
8 #include <lib/base/init_num.h>
9 #include <lib/base/init.h>
10 #include <dirent.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14
15 class eStaticServiceFSInformation: public iStaticServiceInformation
16 {
17         DECLARE_REF(eStaticServiceFSInformation);
18 public:
19         RESULT getName(const eServiceReference &ref, std::string &name);
20         int getLength(const eServiceReference &ref) { return -1; }
21 };
22
23 DEFINE_REF(eStaticServiceFSInformation);
24
25 RESULT eStaticServiceFSInformation::getName(const eServiceReference &ref, std::string &name)
26 {
27         name = ref.path;
28         return 0;
29 }
30
31 // eServiceFactoryFS
32
33 eServiceFactoryFS::eServiceFactoryFS()
34 {
35         ePtr<eServiceCenter> sc;
36         
37         eServiceCenter::getPrivInstance(sc);
38         if (sc)
39                 sc->addServiceFactory(eServiceFactoryFS::id, this);
40         
41         m_service_information = new eStaticServiceFSInformation();
42 }
43
44 eServiceFactoryFS::~eServiceFactoryFS()
45 {
46         ePtr<eServiceCenter> sc;
47         
48         eServiceCenter::getPrivInstance(sc);
49         if (sc)
50                 sc->removeServiceFactory(eServiceFactoryFS::id);
51 }
52
53 DEFINE_REF(eServiceFactoryFS)
54
55         // iServiceHandler
56 RESULT eServiceFactoryFS::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
57 {
58         ptr=0;
59         return -1;
60 }
61
62 RESULT eServiceFactoryFS::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
63 {
64         ptr=0;
65         return -1;
66 }
67
68 RESULT eServiceFactoryFS::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
69 {
70         ptr = new eServiceFS(ref.path.c_str());
71         return 0;
72 }
73
74 RESULT eServiceFactoryFS::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
75 {
76         ptr = m_service_information;
77         return 0;
78 }
79
80 RESULT eServiceFactoryFS::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
81 {
82         ptr = 0;
83         return -1;
84 }
85
86 // eServiceFS
87
88 DEFINE_REF(eServiceFS);
89
90 eServiceFS::eServiceFS(const char *path): path(path)
91 {
92         m_list_valid = 0;
93 }
94
95 eServiceFS::~eServiceFS()
96 {
97 }
98
99 RESULT eServiceFS::getContent(std::list<eServiceReference> &list, bool sorted)
100 {
101         DIR *d=opendir(path.c_str());
102         if (!d)
103                 return -errno;
104         while (dirent *e=readdir(d))
105         {
106                 if (!(strcmp(e->d_name, ".") && strcmp(e->d_name, "..")))
107                         continue;
108                 
109                 std::string filename;
110                 
111                 filename = path;
112                 filename += e->d_name;
113                 
114                 struct stat s;
115                 if (::stat(filename.c_str(), &s) < 0)
116                         continue;
117                 
118                 if (S_ISDIR(s.st_mode))
119                         filename += "/";
120                 
121                 if (S_ISDIR(s.st_mode))
122                 {
123                         eServiceReference service(eServiceFactoryFS::id, 
124                                 eServiceReference::isDirectory|
125                                 eServiceReference::canDescent|eServiceReference::mustDescent|
126                                 eServiceReference::shouldSort|eServiceReference::sort1,
127                                 filename);
128                         service.data[0] = 1;
129                         list.push_back(service);
130                 } else
131                 {
132                         size_t e = filename.rfind('.');
133                         std::string extension = (e != std::string::npos) ? filename.substr(e) : "";
134                         int type = -1;
135                         
136                         if (extension == ".ts")
137                                 type = eServiceFactoryDVB::id;
138                         else if (extension == ".mp3")
139                                 type = 4097;
140                         else if (extension == ".ogg")
141                                 type = 4097;
142                         else if (extension == ".mpg")
143                                 type = 4097;
144                         else if (extension == ".vob")
145                                 type = 4097;
146                         else if (extension == ".wav" || extension == ".wave")
147                                 type = 4097;
148                         
149                         if (type != -1)
150                         {
151                                 eServiceReference service(type,
152                                         0,
153                                         filename);
154                                 service.data[0] = 0;
155                                 list.push_back(service);
156                         }
157                 }
158         }
159         closedir(d);
160
161         if (sorted)
162                 list.sort(iListableServiceCompare(this));
163
164         return 0;
165 }
166
167 RESULT eServiceFS::getContent(PyObject *list, bool sorted)
168 {
169         if (!list || !PyList_Check(list))
170                 return -1;
171
172         std::list<eServiceReference> tmplist;
173
174         getContent(tmplist, sorted);
175
176         if (sorted)
177                 tmplist.sort(iListableServiceCompare(this));
178
179         for (std::list<eServiceReference>::iterator it(tmplist.begin());
180                 it != tmplist.end(); ++it)
181         {
182                 PyObject *refobj = New_eServiceReference(*it);
183                 PyList_Append(list, refobj);
184                 Py_DECREF(refobj);
185         }
186
187         return 0;
188 }
189
190 RESULT eServiceFS::getNext(eServiceReference &ptr)
191 {
192         if (!m_list_valid)
193         {
194                 m_list_valid = 1;
195                 int res = getContent(m_list);
196                 if (res)
197                         return res;
198         }
199         
200         if (!m_list.size())
201         {
202                 ptr = eServiceReference();
203                 return -ERANGE;
204         }
205         
206         ptr = m_list.front();
207         m_list.pop_front();
208         return 0;
209 }
210
211 int eServiceFS::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
212 {
213                 /* directories first */
214         if ((a.flags & ~b.flags) & eServiceReference::isDirectory)
215                 return 1;
216         else if ((~a.flags & b.flags) & eServiceReference::isDirectory)
217                 return 0;
218                 /* sort by filename */
219         else
220                 return a.path < b.path;
221 }
222
223 RESULT eServiceFS::startEdit(ePtr<iMutableServiceList> &res)
224 {
225         res = 0;
226         return -1;
227 }
228
229 eAutoInitPtr<eServiceFactoryFS> init_eServiceFactoryFS(eAutoInitNumbers::service+1, "eServiceFactoryFS");