4c1e5bd558c7c47e7cf836e9f91f5f418dd20b99
[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                         else if (extension == ".m3u" || extension == ".pls" || extension == ".e2")
149                                 type = 4098;
150                         
151                         if (type != -1)
152                         {
153                                 eServiceReference service(type,
154                                         0,
155                                         filename);
156                                 service.data[0] = 0;
157                                 list.push_back(service);
158                         }
159                 }
160         }
161         closedir(d);
162
163         if (sorted)
164                 list.sort(iListableServiceCompare(this));
165
166         return 0;
167 }
168
169 //   The first argument of this function is a format string to specify the order and
170 //   the content of the returned list
171 //   useable format options are
172 //   R = Service Reference (as swig object .. this is very slow)
173 //   S = Service Reference (as python string object .. same as ref.toString())
174 //   N = Service Name (as python string object)
175 //   when exactly one return value per service is selected in the format string,
176 //   then each value is directly a list entry
177 //   when more than one value is returned per service, then the list is a list of
178 //   python tuples
179 //   unknown format string chars are returned as python None values !
180 PyObject *eServiceFS::getContent(const char* format, bool sorted)
181 {
182         PyObject *ret=0;
183         std::list<eServiceReference> tmplist;
184         int retcount=1;
185
186         if (!format || !(retcount=strlen(format)))
187                 format = "R"; // just return service reference swig object ...
188
189         if (!getContent(tmplist, sorted))
190         {
191                 int services=tmplist.size();
192                 ePtr<iStaticServiceInformation> sptr;
193                 eServiceCenterPtr service_center;
194
195                 if (strchr(format, 'N'))
196                         eServiceCenter::getPrivInstance(service_center);
197
198                 ret = PyList_New(services);
199                 std::list<eServiceReference>::iterator it(tmplist.begin());
200
201                 for (int cnt=0; cnt < services; ++cnt)
202                 {
203                         eServiceReference &ref=*it++;
204                         PyObject *tuple = retcount > 1 ? PyTuple_New(retcount) : 0;
205                         for (int i=0; i < retcount; ++i)
206                         {
207                                 PyObject *tmp=0;
208                                 switch(format[i])
209                                 {
210                                 case 'R':  // service reference (swig)object
211                                         tmp = New_eServiceReference(ref);
212                                         break;
213                                 case 'S':  // service reference string
214                                         tmp = PyString_FromString(ref.toString().c_str());
215                                         break;
216                                 case 'N':  // service name
217                                         if (service_center)
218                                         {
219                                                 service_center->info(ref, sptr);
220                                                 if (sptr)
221                                                 {
222                                                         std::string name;
223                                                         sptr->getName(ref, name);
224                                                         if (name.length())
225                                                                 tmp = PyString_FromString(name.c_str());
226                                                 }
227                                         }
228                                         if (!tmp)
229                                                 tmp = PyString_FromString("<n/a>");
230                                         break;
231                                 default:
232                                         if (tuple)
233                                         {
234                                                 tmp = Py_None;
235                                                 Py_INCREF(Py_None);
236                                         }
237                                         break;
238                                 }
239                                 if (tmp)
240                                 {
241                                         if (tuple)
242                                                 PyTuple_SET_ITEM(tuple, i, tmp);
243                                         else
244                                                 PyList_SET_ITEM(ret, cnt, tmp);
245                                 }
246                         }
247                         if (tuple)
248                                 PyList_SET_ITEM(ret, cnt, tuple);
249                 }
250         }
251         return ret ? ret : PyList_New(0);
252 }
253
254 RESULT eServiceFS::getNext(eServiceReference &ptr)
255 {
256         if (!m_list_valid)
257         {
258                 m_list_valid = 1;
259                 int res = getContent(m_list);
260                 if (res)
261                         return res;
262         }
263         
264         if (!m_list.size())
265         {
266                 ptr = eServiceReference();
267                 return -ERANGE;
268         }
269         
270         ptr = m_list.front();
271         m_list.pop_front();
272         return 0;
273 }
274
275 int eServiceFS::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
276 {
277                 /* directories first */
278         if ((a.flags & ~b.flags) & eServiceReference::isDirectory)
279                 return 1;
280         else if ((~a.flags & b.flags) & eServiceReference::isDirectory)
281                 return 0;
282                 /* sort by filename */
283         else
284                 return a.path < b.path;
285 }
286
287 RESULT eServiceFS::startEdit(ePtr<iMutableServiceList> &res)
288 {
289         res = 0;
290         return -1;
291 }
292
293 eAutoInitPtr<eServiceFactoryFS> init_eServiceFactoryFS(eAutoInitNumbers::service+1, "eServiceFactoryFS");