allow AudioSelection screens for services without ChannelSelection interface (needed...
[enigma2.git] / lib / components / scan.cpp
1 #include <lib/dvb/dvb.h>
2 #include <lib/dvb/db.h>
3 #include <lib/components/scan.h>
4 #include <lib/base/eerror.h>
5 #include <lib/dvb/scan.h>
6
7 DEFINE_REF(eComponentScan);
8
9 void eComponentScan::scanEvent(int evt)
10 {
11 //      eDebug("scan event %d!", evt);
12         
13         switch(evt)
14         {
15                 case eDVBScan::evtFinish:
16                 {
17                         m_done = 1;
18                         ePtr<iDVBChannelList> db;
19                         ePtr<eDVBResourceManager> res;
20                         
21                         int err;
22                         if ((err = eDVBResourceManager::getInstance(res)) != 0)
23                         {
24                                 eDebug("no resource manager");
25                                 m_failed = 2;
26                         } else if ((err = res->getChannelList(db)) != 0)
27                         {
28                                 m_failed = 3;
29                                 eDebug("no channel list");
30                         } else
31                         {
32                                 m_scan->insertInto(db);
33                                 db->flush();
34                                 eDebug("scan done!");
35                         }
36                         break;
37                 }
38                 case eDVBScan::evtNewService:
39                         newService();
40                         return;
41                 case eDVBScan::evtFail:
42                         eDebug("scan failed.");
43                         m_failed = 1;
44                         m_done = 1;
45                         break;
46                 case eDVBScan::evtUpdate:
47                         break;
48         }
49         statusChanged();
50 }
51
52 eComponentScan::eComponentScan(): m_done(-1), m_failed(0)
53 {
54 }
55
56 eComponentScan::~eComponentScan()
57 {
58 }
59
60 void eComponentScan::clear()
61 {
62         m_initial.clear();
63 }
64
65 void eComponentScan::addInitial(const eDVBFrontendParametersSatellite &p)
66 {
67         ePtr<eDVBFrontendParameters> parm = new eDVBFrontendParameters();
68         parm->setDVBS(p);
69         m_initial.push_back(parm);
70 }
71
72 void eComponentScan::addInitial(const eDVBFrontendParametersCable &p)
73 {
74         ePtr<eDVBFrontendParameters> parm = new eDVBFrontendParameters();
75         parm->setDVBC(p);
76         m_initial.push_back(parm);
77 }
78
79 void eComponentScan::addInitial(const eDVBFrontendParametersTerrestrial &p)
80 {
81         ePtr<eDVBFrontendParameters> parm = new eDVBFrontendParameters();
82         parm->setDVBT(p);
83         m_initial.push_back(parm);
84 }
85
86
87 int eComponentScan::start(int feid, int flags)
88 {
89         if (m_initial.empty())
90                 return -2;
91
92         if (m_done != -1)
93                 return -1;
94         
95         m_done = 0;
96         ePtr<eDVBResourceManager> mgr;
97         
98         eDVBResourceManager::getInstance(mgr);
99
100         eUsePtr<iDVBChannel> channel;
101
102         if (mgr->allocateRawChannel(channel, feid))
103         {
104                 eDebug("scan: allocating raw channel (on frontend %d) failed!", feid);
105                 return -1;
106         }
107
108         std::list<ePtr<iDVBFrontendParameters> > list;
109         m_scan = new eDVBScan(channel);
110         m_scan->connectEvent(slot(*this, &eComponentScan::scanEvent), m_scan_event_connection);
111
112         if (!(flags & scanRemoveServices))
113         {
114                 ePtr<iDVBChannelList> db;
115                 ePtr<eDVBResourceManager> res;
116                 int err;
117                 if ((err = eDVBResourceManager::getInstance(res)) != 0)
118                         eDebug("no resource manager");
119                 else if ((err = res->getChannelList(db)) != 0)
120                         eDebug("no channel list");
121                 else
122                 {
123                         if (m_initial.size() > 1)
124                         {
125                                 ePtr<iDVBFrontendParameters> tp = m_initial.first();
126                                 int type;
127                                 if (tp && !tp->getSystem(type))
128                                 {
129                                         switch(type)
130                                         {
131                                                 case iDVBFrontend::feSatellite:
132                                                 {
133                                                         eDVBFrontendParametersSatellite parm;
134                                                         tp->getDVBS(parm);
135                                                         db->removeFlags(eDVBService::dxNewFound, -1, -1, -1, parm.orbital_position);
136                                                         break;
137                                                 }
138                                                 case iDVBFrontend::feCable:
139                                                         db->removeFlags(eDVBService::dxNewFound, 0xFFFF0000, -1, -1, -1);
140                                                         break;
141                                                 case iDVBFrontend::feTerrestrial:
142                                                         db->removeFlags(eDVBService::dxNewFound, 0xEEEE0000, -1, -1, -1);
143                                                         break;
144                                         }
145                                 }
146                         }
147                 }
148         }
149         m_scan->start(m_initial, flags);
150
151         return 0;
152 }
153
154 int eComponentScan::getProgress()
155 {
156         if (!m_scan)
157                 return 0;
158         int done, total, services;
159         m_scan->getStats(done, total, services);
160         if (!total)
161                 return 0;
162         return done * 100 / total;
163 }
164
165 int eComponentScan::getNumServices()
166 {
167         if (!m_scan)
168                 return 0;
169         int done, total, services;
170         m_scan->getStats(done, total, services);
171         return services;
172 }
173
174 int eComponentScan::isDone()
175 {
176         return m_done;
177 }
178
179 int eComponentScan::getError()
180 {
181         return m_failed;
182 }
183
184 void eComponentScan::getLastServiceName(std::string &string)
185 {
186         if (!m_scan)
187                 return;
188         m_scan->getLastServiceName(string);
189 }
190
191 RESULT eComponentScan::getFrontend(ePtr<iDVBFrontend> &fe)
192 {
193         if (m_scan)
194                 return m_scan->getFrontend(fe);
195         fe = 0;
196         return -1;
197 }
198
199 RESULT eComponentScan::getCurrentTransponder(ePtr<iDVBFrontendParameters> &tp)
200 {
201         if (m_scan)
202                 return m_scan->getCurrentTransponder(tp);
203         tp = 0;
204         return -1;
205 }
206