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