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