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