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