layout fix
[enigma2.git] / lib / service / service.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/base/estring.h>
3 #include <lib/python/python.h>
4 #include <lib/service/service.h>
5 #include <lib/base/init_num.h>
6 #include <lib/base/init.h>
7 #include <Python.h>
8
9 eServiceReference::eServiceReference(const std::string &string)
10 {
11         const char *c=string.c_str();
12         int pathl=0;
13
14         if (!string.length())
15                 type = idInvalid;
16         else if ( sscanf(c, "%d:%d:%x:%x:%x:%x:%x:%x:%x:%x:%n", &type, &flags, &data[0], &data[1], &data[2], &data[3], &data[4], &data[5], &data[6], &data[7], &pathl) < 8 )
17         {
18                 memset( data, 0, sizeof(data) );
19                 eDebug("find old format eServiceReference string");
20                 if ( sscanf(c, "%d:%d:%x:%x:%x:%x:%n", &type, &flags, &data[0], &data[1], &data[2], &data[3], &pathl) < 2 )
21                         type = idInvalid;
22         }
23
24         if (pathl)
25         {
26                 const char *pathstr = c+pathl;
27                 const char *namestr = strchr(pathstr, ':');
28                 if (namestr)
29                 {
30                         if (pathstr != namestr)
31                                 path.assign(pathstr, namestr-pathstr);
32                         if (*(namestr+1))
33                                 name=namestr+1;
34                 }
35                 else
36                         path=pathstr;
37         }
38 }
39
40 std::string eServiceReference::toString() const
41 {
42         std::string ret;
43         ret += getNum(type);
44         ret += ":";
45         ret += getNum(flags);
46         for (unsigned int i=0; i<sizeof(data)/sizeof(*data); ++i)
47                 ret+=":"+ getNum(data[i], 0x10);
48         ret+=":"+path;
49         if (name.length())
50                 ret+=":"+name;
51         return ret;
52 }
53
54 std::string eServiceReference::toCompareString() const
55 {
56         std::string ret;
57         ret += getNum(type);
58         ret += ":0";
59         for (unsigned int i=0; i<sizeof(data)/sizeof(*data); ++i)
60                 ret+=":"+getNum(data[i], 0x10);
61         ret+=":"+path;
62         return ret;
63 }
64
65 eServiceCenter *eServiceCenter::instance;
66
67 eServiceCenter::eServiceCenter()
68 {
69         if (!instance)
70         {
71                 eDebug("settings instance.");
72                 instance = this;
73         }
74 }
75
76 eServiceCenter::~eServiceCenter()
77 {
78         if (instance == this)
79         {
80                 eDebug("clear instance");
81                 instance = 0;
82         }
83 }
84
85 DEFINE_REF(eServiceCenter);
86
87 RESULT eServiceCenter::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
88 {
89         std::map<int,ePtr<iServiceHandler> >::iterator i = handler.find(ref.type);
90         if (i == handler.end())
91         {
92                 ptr = 0;
93                 return -1;
94         }
95         return i->second->play(ref, ptr);
96 }
97
98 RESULT eServiceCenter::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
99 {
100         std::map<int,ePtr<iServiceHandler> >::iterator i = handler.find(ref.type);
101         if (i == handler.end())
102         {
103                 ptr = 0;
104                 return -1;
105         }
106         return i->second->record(ref, ptr);
107 }
108
109 RESULT eServiceCenter::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
110 {
111         std::map<int,ePtr<iServiceHandler> >::iterator i = handler.find(ref.type);
112         if (i == handler.end())
113         {
114                 ptr = 0;
115                 return -1;
116         }
117         return i->second->list(ref, ptr);
118 }
119
120 RESULT eServiceCenter::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
121 {
122         std::map<int,ePtr<iServiceHandler> >::iterator i = handler.find(ref.type);
123         if (i == handler.end())
124         {
125                 ptr = 0;
126                 return -1;
127         }
128         return i->second->info(ref, ptr);
129 }
130
131 RESULT eServiceCenter::offlineOperations(const eServiceReference &ref, ePtr<iServiceOfflineOperations> &ptr)
132 {
133         std::map<int,ePtr<iServiceHandler> >::iterator i = handler.find(ref.type);
134         if (i == handler.end())
135         {
136                 ptr = 0;
137                 return -1;
138         }
139         return i->second->offlineOperations(ref, ptr);
140 }
141
142 RESULT eServiceCenter::addServiceFactory(int id, iServiceHandler *hnd)
143 {
144         handler.insert(std::pair<int,ePtr<iServiceHandler> >(id, hnd));
145         return 0;
146 }
147
148 RESULT eServiceCenter::removeServiceFactory(int id)
149 {
150         handler.erase(id);
151         return 0;
152 }
153
154         /* default handlers */
155 RESULT iServiceHandler::info(const eServiceReference &, ePtr<iStaticServiceInformation> &ptr)
156 {
157         ptr = 0;
158         return -1;
159 }
160
161 #include <lib/service/event.h>
162
163 RESULT iStaticServiceInformation::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &evt, time_t start_time)
164 {
165         evt = 0;
166         return -1;
167 }
168
169 int iStaticServiceInformation::getLength(const eServiceReference &ref)
170 {
171         return -1;
172 }
173
174 bool iStaticServiceInformation::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
175 {
176         return true;
177 }
178
179 RESULT iServiceInformation::getEvent(ePtr<eServiceEvent> &evt, int m_nownext)
180 {
181         evt = 0;
182         return -1;
183 }
184
185 int iStaticServiceInformation::getInfo(const eServiceReference &ref, int w)
186 {
187         return -1;
188 }
189
190 std::string iStaticServiceInformation::getInfoString(const eServiceReference &ref, int w)
191 {
192         return "";
193 }
194
195 int iServiceInformation::getInfo(int w)
196 {
197         return -1;
198 }
199
200 std::string iServiceInformation::getInfoString(int w)
201 {
202         return "";
203 }
204
205 PyObject* iServiceInformation::getInfoObject(int w)
206 {
207         Py_INCREF(Py_None);
208         return Py_None;
209 }
210
211 int iStaticServiceInformation::setInfo(const eServiceReference &ref, int w, int v)
212 {
213         return -1;
214 }
215
216 int iStaticServiceInformation::setInfoString(const eServiceReference &ref, int w, const char *v)
217 {
218         return -1;
219 }
220
221 int iServiceInformation::setInfo(int w, int v)
222 {
223         return -1;
224 }
225
226 int iServiceInformation::setInfoString(int w, const char *v)
227 {
228         return -1;
229 }
230
231 eAutoInitPtr<eServiceCenter> init_eServiceCenter(eAutoInitNumbers::service, "eServiceCenter");