99199aa9dd99c3d4d69e60936575aeb27165be55
[enigma2.git] / lib / service / service.cpp
1 #include <lib/service/service.h>
2 #include <lib/base/init_num.h>
3 #include <lib/base/init.h>
4
5 eServiceReference::eServiceReference(const eString &string)
6 {
7         const char *c=string.c_str();
8         int pathl=-1;
9         
10         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 )
11         {
12                 memset( data, 0, sizeof(data) );
13                 eDebug("find old format eServiceReference string");
14                 sscanf(c, "%d:%d:%x:%x:%x:%x:%n", &type, &flags, &data[0], &data[1], &data[2], &data[3], &pathl);
15         }
16
17         if (pathl)
18                 path=c+pathl;
19 }
20
21 eString eServiceReference::toString() const
22 {
23         eString ret;
24         ret+=eString().sprintf("%d:", type);
25         ret+=eString().sprintf("%d", flags);
26         for (unsigned int i=0; i<sizeof(data)/sizeof(*data); ++i)
27                 ret+=":"+eString().sprintf("%x", data[i]);
28         ret+=":"+path;
29         return ret;
30 }
31
32
33 eServiceCenter *eServiceCenter::instance;
34
35 eServiceCenter::eServiceCenter()
36 {
37         if (!instance)
38                 instance = this;
39 }
40
41 eServiceCenter::~eServiceCenter()
42 {
43         if (instance == this)
44                 instance = 0;
45 }
46
47 DEFINE_REF(eServiceCenter);
48
49 RESULT eServiceCenter::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
50 {
51         std::map<int,ePtr<iServiceHandler> >::iterator i = handler.find(ref.type);
52         if (i == handler.end())
53         {
54                 ptr = 0;
55                 return -1;
56         }
57         return i->second->play(ref, ptr);
58 }
59
60 RESULT eServiceCenter::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
61 {
62         std::map<int,ePtr<iServiceHandler> >::iterator i = handler.find(ref.type);
63         if (i == handler.end())
64         {
65                 ptr = 0;
66                 return -1;
67         }
68         return i->second->record(ref, ptr);
69 }
70
71 RESULT eServiceCenter::list(const eServiceReference &ref, ePtr<iListableService> &ptr)
72 {
73         std::map<int,ePtr<iServiceHandler> >::iterator i = handler.find(ref.type);
74         if (i == handler.end())
75         {
76                 ptr = 0;
77                 return -1;
78         }
79         return i->second->list(ref, ptr);
80 }
81
82 RESULT eServiceCenter::addServiceFactory(int id, iServiceHandler *hnd)
83 {
84         handler.insert(std::pair<int,ePtr<iServiceHandler> >(id, hnd));
85         return 0;
86 }
87
88 RESULT eServiceCenter::removeServiceFactory(int id)
89 {
90         handler.erase(id);
91         return 0;
92 }
93
94 eAutoInitP0<eServiceCenter> init_eServiceCenter(eAutoInitNumbers::service, "eServiceCenter");