ad40f0af9ff9020e59668a0e0863ebb7a5576a48
[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/base/init_num.h>
8 #include <lib/base/init.h>
9 #include <dirent.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13
14 // eServiceFactoryFS
15
16 eServiceFactoryFS::eServiceFactoryFS(): ref(0)
17 {
18         ePtr<eServiceCenter> sc;
19         
20         eServiceCenter::getInstance(sc);
21         if (sc)
22                 sc->addServiceFactory(eServiceFactoryFS::id, this);
23 }
24
25 eServiceFactoryFS::~eServiceFactoryFS()
26 {
27         ePtr<eServiceCenter> sc;
28         
29         eServiceCenter::getInstance(sc);
30         if (sc)
31                 sc->removeServiceFactory(eServiceFactoryFS::id);
32 }
33
34 DEFINE_REF(eServiceFactoryFS)
35
36         // iServiceHandler
37 RESULT eServiceFactoryFS::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
38 {
39         ptr=0;
40         return -1;
41 }
42
43 RESULT eServiceFactoryFS::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
44 {
45         ptr=0;
46         return -1;
47 }
48
49 RESULT eServiceFactoryFS::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
50 {
51         ptr = new eServiceFS(ref.path.c_str());
52         return 0;
53 }
54
55 // eServiceFS
56
57 DEFINE_REF(eServiceFS);
58
59 eServiceFS::eServiceFS(const char *path): ref(0), path(path)
60 {
61 }
62
63 eServiceFS::~eServiceFS()
64 {
65 }
66
67 RESULT eServiceFS::getContent(std::list<eServiceReference> &list)
68 {
69         DIR *d=opendir(path.c_str());
70         if (!d)
71                 return -errno;
72         while (dirent *e=readdir(d))
73         {
74                 if (!(strcmp(e->d_name, ".") && strcmp(e->d_name, "..")))
75                         continue;
76                 
77                 eString filename;
78                 
79                 filename = path;
80                 filename += e->d_name;
81                 
82                 struct stat s;
83                 if (::stat(filename.c_str(), &s) < 0)
84                         continue;
85                 
86                 if (S_ISDIR(s.st_mode))
87                         filename += "/";
88                 
89                 if (S_ISDIR(s.st_mode))
90                 {
91                         eServiceReference service(eServiceFactoryFS::id, 
92                                 eServiceReference::isDirectory|
93                                 eServiceReference::canDescent|eServiceReference::mustDescent|
94                                 eServiceReference::shouldSort|eServiceReference::sort1,
95                                 filename);
96                         service.data[0] = 1;
97                         list.push_back(service);
98                 } else
99                 {
100                         eServiceReference service(eServiceFactoryFS::id, 
101                                 eServiceReference::isDirectory|
102                                 eServiceReference::canDescent|eServiceReference::mustDescent|
103                                 eServiceReference::shouldSort|eServiceReference::sort1,
104                                 filename);
105                         service.data[0] = 0;
106                         list.push_back(service);
107                 }
108         }
109         return 0;
110 }
111
112 eAutoInitP0<eServiceFactoryFS> init_eServiceFactoryFS(eAutoInitNumbers::service+1, "eServiceFactoryFS");