- added iStaticServiceInformation
[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
15 class eStaticServiceFSInformation: public iStaticServiceInformation
16 {
17         DECLARE_REF;
18 public:
19         RESULT getName(const eServiceReference &ref, std::string &name);
20 };
21
22 DEFINE_REF(eStaticServiceFSInformation);
23
24 RESULT eStaticServiceFSInformation::getName(const eServiceReference &ref, std::string &name)
25 {
26         name = ref.path;
27 }
28
29 // eServiceFactoryFS
30
31 eServiceFactoryFS::eServiceFactoryFS()
32 {
33         ePtr<eServiceCenter> sc;
34         
35         eServiceCenter::getInstance(sc);
36         if (sc)
37                 sc->addServiceFactory(eServiceFactoryFS::id, this);
38         
39         m_service_information = new eStaticServiceFSInformation();
40 }
41
42 eServiceFactoryFS::~eServiceFactoryFS()
43 {
44         ePtr<eServiceCenter> sc;
45         
46         eServiceCenter::getInstance(sc);
47         if (sc)
48                 sc->removeServiceFactory(eServiceFactoryFS::id);
49 }
50
51 DEFINE_REF(eServiceFactoryFS)
52
53         // iServiceHandler
54 RESULT eServiceFactoryFS::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
55 {
56         ptr=0;
57         return -1;
58 }
59
60 RESULT eServiceFactoryFS::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
61 {
62         ptr=0;
63         return -1;
64 }
65
66 RESULT eServiceFactoryFS::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
67 {
68         ptr = new eServiceFS(ref.path.c_str());
69         return 0;
70 }
71
72 RESULT eServiceFactoryFS::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
73 {
74         ptr = m_service_information;
75         return 0;
76 }
77
78 // eServiceFS
79
80 DEFINE_REF(eServiceFS);
81
82 eServiceFS::eServiceFS(const char *path): path(path)
83 {
84 }
85
86 eServiceFS::~eServiceFS()
87 {
88 }
89
90 RESULT eServiceFS::getContent(std::list<eServiceReference> &list)
91 {
92         DIR *d=opendir(path.c_str());
93         if (!d)
94                 return -errno;
95         while (dirent *e=readdir(d))
96         {
97                 if (!(strcmp(e->d_name, ".") && strcmp(e->d_name, "..")))
98                         continue;
99                 
100                 std::string filename;
101                 
102                 filename = path;
103                 filename += e->d_name;
104                 
105                 struct stat s;
106                 if (::stat(filename.c_str(), &s) < 0)
107                         continue;
108                 
109                 if (S_ISDIR(s.st_mode))
110                         filename += "/";
111                 
112                 if (S_ISDIR(s.st_mode))
113                 {
114                         eServiceReference service(eServiceFactoryFS::id, 
115                                 eServiceReference::isDirectory|
116                                 eServiceReference::canDescent|eServiceReference::mustDescent|
117                                 eServiceReference::shouldSort|eServiceReference::sort1,
118                                 filename);
119                         service.data[0] = 1;
120                         list.push_back(service);
121                 } else
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] = 0;
129                         list.push_back(service);
130                 }
131         }
132         return 0;
133 }
134
135 eAutoInitPtr<eServiceFactoryFS> init_eServiceFactoryFS(eAutoInitNumbers::service+1, "eServiceFactoryFS");