1 #include <lib/base/eerror.h>
2 #include <lib/base/object.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>
11 #include <sys/types.h>
15 class eStaticServiceFSInformation: public iStaticServiceInformation
17 DECLARE_REF(eStaticServiceFSInformation);
19 RESULT getName(const eServiceReference &ref, std::string &name);
20 int getLength(const eServiceReference &ref) { return -1; }
23 DEFINE_REF(eStaticServiceFSInformation);
25 RESULT eStaticServiceFSInformation::getName(const eServiceReference &ref, std::string &name)
33 eServiceFactoryFS::eServiceFactoryFS()
35 ePtr<eServiceCenter> sc;
37 eServiceCenter::getPrivInstance(sc);
39 sc->addServiceFactory(eServiceFactoryFS::id, this);
41 m_service_information = new eStaticServiceFSInformation();
44 eServiceFactoryFS::~eServiceFactoryFS()
46 ePtr<eServiceCenter> sc;
48 eServiceCenter::getPrivInstance(sc);
50 sc->removeServiceFactory(eServiceFactoryFS::id);
53 DEFINE_REF(eServiceFactoryFS)
56 RESULT eServiceFactoryFS::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
62 RESULT eServiceFactoryFS::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
68 RESULT eServiceFactoryFS::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
70 ptr = new eServiceFS(ref.path.c_str());
74 RESULT eServiceFactoryFS::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
76 ptr = m_service_information;
80 RESULT eServiceFactoryFS::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
88 DEFINE_REF(eServiceFS);
90 eServiceFS::eServiceFS(const char *path): path(path)
95 eServiceFS::~eServiceFS()
99 RESULT eServiceFS::getContent(std::list<eServiceReference> &list, bool sorted)
101 DIR *d=opendir(path.c_str());
104 while (dirent *e=readdir(d))
106 if (!(strcmp(e->d_name, ".") && strcmp(e->d_name, "..")))
109 std::string filename;
112 filename += e->d_name;
115 if (::stat(filename.c_str(), &s) < 0)
118 if (S_ISDIR(s.st_mode))
121 if (S_ISDIR(s.st_mode))
123 eServiceReference service(eServiceFactoryFS::id,
124 eServiceReference::isDirectory|
125 eServiceReference::canDescent|eServiceReference::mustDescent|
126 eServiceReference::shouldSort|eServiceReference::sort1,
129 list.push_back(service);
132 size_t e = filename.rfind('.');
133 std::string extension = (e != std::string::npos) ? filename.substr(e) : "";
136 if (extension == ".ts")
137 type = eServiceFactoryDVB::id;
138 else if (extension == ".mp3")
140 else if (extension == ".ogg")
142 else if (extension == ".mpg")
144 else if (extension == ".vob")
146 else if (extension == ".wav" || extension == ".wave")
148 else if (extension == ".m3u" || extension == ".pls" || extension == ".e2pls")
153 eServiceReference service(type,
157 list.push_back(service);
164 list.sort(iListableServiceCompare(this));
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
179 // unknown format string chars are returned as python None values !
180 PyObject *eServiceFS::getContent(const char* format, bool sorted)
183 std::list<eServiceReference> tmplist;
186 if (!format || !(retcount=strlen(format)))
187 format = "R"; // just return service reference swig object ...
189 if (!getContent(tmplist, sorted))
191 int services=tmplist.size();
192 ePtr<iStaticServiceInformation> sptr;
193 eServiceCenterPtr service_center;
195 if (strchr(format, 'N'))
196 eServiceCenter::getPrivInstance(service_center);
198 ret = PyList_New(services);
199 std::list<eServiceReference>::iterator it(tmplist.begin());
201 for (int cnt=0; cnt < services; ++cnt)
203 eServiceReference &ref=*it++;
204 PyObject *tuple = retcount > 1 ? PyTuple_New(retcount) : 0;
205 for (int i=0; i < retcount; ++i)
210 case 'R': // service reference (swig)object
211 tmp = New_eServiceReference(ref);
213 case 'S': // service reference string
214 tmp = PyString_FromString(ref.toString().c_str());
216 case 'N': // service name
219 service_center->info(ref, sptr);
223 sptr->getName(ref, name);
225 tmp = PyString_FromString(name.c_str());
229 tmp = PyString_FromString("<n/a>");
242 PyTuple_SET_ITEM(tuple, i, tmp);
244 PyList_SET_ITEM(ret, cnt, tmp);
248 PyList_SET_ITEM(ret, cnt, tuple);
251 return ret ? ret : PyList_New(0);
254 RESULT eServiceFS::getNext(eServiceReference &ptr)
259 int res = getContent(m_list);
266 ptr = eServiceReference();
270 ptr = m_list.front();
275 int eServiceFS::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
277 /* directories first */
278 if ((a.flags & ~b.flags) & eServiceReference::isDirectory)
280 else if ((~a.flags & b.flags) & eServiceReference::isDirectory)
282 /* sort by filename */
284 return a.path < b.path;
287 RESULT eServiceFS::startEdit(ePtr<iMutableServiceList> &res)
293 eAutoInitPtr<eServiceFactoryFS> init_eServiceFactoryFS(eAutoInitNumbers::service+1, "eServiceFactoryFS");