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 std::string extension = filename.substr(filename.rfind('.'));
135 if (extension == ".ts")
136 type = eServiceFactoryDVB::id;
137 else if (extension == ".mp3")
142 eServiceReference service(type,
146 list.push_back(service);
153 list.sort(iListableServiceCompare(this));
158 RESULT eServiceFS::getContent(PyObject *list, bool sorted)
160 if (!list || !PyList_Check(list))
163 std::list<eServiceReference> tmplist;
165 getContent(tmplist, sorted);
168 tmplist.sort(iListableServiceCompare(this));
170 for (std::list<eServiceReference>::iterator it(tmplist.begin());
171 it != tmplist.end(); ++it)
173 PyObject *refobj = New_eServiceReference(*it);
174 PyList_Append(list, refobj);
181 RESULT eServiceFS::getNext(eServiceReference &ptr)
186 int res = getContent(m_list);
193 ptr = eServiceReference();
197 ptr = m_list.front();
202 int eServiceFS::compareLessEqual(const eServiceReference &a, const eServiceReference &b)
204 /* directories first */
205 if ((a.flags & ~b.flags) & eServiceReference::isDirectory)
207 else if ((~a.flags & b.flags) & eServiceReference::isDirectory)
209 /* sort by filename */
211 return a.path < b.path;
214 RESULT eServiceFS::startEdit(ePtr<iMutableServiceList> &res)
220 eAutoInitPtr<eServiceFactoryFS> init_eServiceFactoryFS(eAutoInitNumbers::service+1, "eServiceFactoryFS");