2 #include <lib/dvb/db.h>
3 #include <lib/dvb/dvb.h>
4 #include <lib/dvb/frontend.h>
5 #include <lib/dvb/epgcache.h>
6 #include <lib/base/eerror.h>
7 #include <lib/base/estring.h>
8 #include <dvbsi++/service_description_section.h>
9 #include <dvbsi++/descriptor_tag.h>
10 #include <dvbsi++/service_descriptor.h>
11 #include <dvbsi++/satellite_delivery_system_descriptor.h>
13 DEFINE_REF(eDVBService);
15 RESULT eBouquet::addService(const eServiceReference &ref)
18 std::find(m_services.begin(), m_services.end(), ref);
19 if ( it != m_services.end() )
21 m_services.push_back(ref);
25 RESULT eBouquet::removeService(const eServiceReference &ref)
28 std::find(m_services.begin(), m_services.end(), ref);
29 if ( it == m_services.end() )
35 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
37 if ( pos < 0 || pos >= m_services.size() )
40 list::iterator source=m_services.end();
41 list::iterator dest=m_services.end();
43 for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
45 if (dest == m_services.end() && !--pos)
52 if (dest != m_services.end() && source != m_services.end())
55 if (dest == m_services.end() || source == m_services.end() || source == dest)
57 while (source != dest)
60 std::iter_swap(source++, source);
62 std::iter_swap(source--, source);
67 RESULT eBouquet::flushChanges()
69 FILE *f=fopen((CONFIGDIR"/enigma2/"+m_filename).c_str(), "w");
72 if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
74 for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
76 eServiceReference tmp = *i;
77 std::string str = tmp.path;
78 if ( (i->flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
80 unsigned int p1 = str.find("FROM BOUQUET \"");
81 if (p1 == std::string::npos)
83 eDebug("doof... kaputt");
88 if (p1 == std::string::npos)
90 eDebug("doof2... kaputt");
96 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
98 if ( i->name.length() )
99 if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
106 eDebug("couldn't write file %s", m_filename.c_str());
110 RESULT eBouquet::setListName(const std::string &name)
112 m_bouquet_name = name;
116 eDVBService::eDVBService()
121 eDVBService::~eDVBService()
125 eDVBService &eDVBService::operator=(const eDVBService &s)
127 m_service_name = s.m_service_name;
128 m_service_name_sort = s.m_service_name_sort;
129 m_provider_name = s.m_provider_name;
136 void eDVBService::genSortName()
138 m_service_name_sort = removeDVBChars(m_service_name);
139 makeUpper(m_service_name_sort);
140 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
141 m_service_name_sort.erase(0, 1);
143 /* put unnamed services at the end, not at the beginning. */
144 if (m_service_name_sort.empty())
145 m_service_name_sort = "\xFF";
148 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
150 if (!ref.name.empty())
151 name = ref.name; // use renamed service name..
152 else if (!m_service_name.empty())
153 name = m_service_name;
159 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
161 return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
164 bool eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
166 ePtr<eDVBResourceManager> res_mgr;
167 if ( eDVBResourceManager::getInstance( res_mgr ) )
168 eDebug("isPlayble... no res manager!!");
171 eDVBChannelID chid, chid_ignore;
172 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
173 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
174 return res_mgr->canAllocateChannel(chid, chid_ignore);
179 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
182 switch (query.m_type)
184 case eDVBChannelQuery::tName:
185 res = m_service_name_sort.find(query.m_string) != std::string::npos;
187 case eDVBChannelQuery::tProvider:
188 res = m_provider_name.find(query.m_string) != std::string::npos;
190 case eDVBChannelQuery::tType:
191 res = ref.getServiceType() == query.m_int;
193 case eDVBChannelQuery::tBouquet:
196 case eDVBChannelQuery::tSatellitePosition:
197 res = ((unsigned int)ref.getDVBNamespace().get())>>16 == (unsigned int)query.m_int;
199 case eDVBChannelQuery::tFlags:
200 res = (m_flags & query.m_int) == query.m_int;
202 case eDVBChannelQuery::tChannelID:
205 ref.getChannelID(chid);
206 res = chid == query.m_channelid;
209 case eDVBChannelQuery::tAND:
210 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
212 case eDVBChannelQuery::tOR:
213 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
215 case eDVBChannelQuery::tAny:
226 int eDVBService::getCachePID(cacheID id)
228 std::map<int, int>::iterator it = m_cache.find(id);
229 if ( it != m_cache.end() )
234 void eDVBService::setCachePID(cacheID id, int pid)
244 /* THIS CODE IS BAD. it should be replaced by somethine better. */
245 void eDVBDB::reloadServicelist()
247 eDebug("---- opening lame channel db");
248 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "rt");
252 if ( !stat("lamedb", &s) )
254 if ( !stat(CONFIGDIR"/enigma2", &s) )
256 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
263 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
265 eDebug("not a servicefile");
269 eDebug("reading services");
270 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
272 eDebug("services invalid, no transponders");
277 // clear all transponders
281 if (!fgets(line, 256, f))
283 if (!strcmp(line, "end\n"))
285 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
286 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
287 if (original_network_id == -1)
289 eDVBChannelID channelid = eDVBChannelID(
290 eDVBNamespace(dvb_namespace),
291 eTransportStreamID(transport_stream_id),
292 eOriginalNetworkID(original_network_id));
294 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
298 if (!strcmp(line, "/\n"))
302 eDVBFrontendParametersSatellite sat;
303 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion,
304 system=eDVBFrontendParametersSatellite::System::DVB_S,
305 modulation=eDVBFrontendParametersSatellite::Modulation::QPSK,
306 rolloff=eDVBFrontendParametersSatellite::RollOff::alpha_auto;
307 sscanf(line+2, "%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion, &system, &modulation, &rolloff);
308 sat.frequency = frequency;
309 sat.symbol_rate = symbol_rate;
310 sat.polarisation = polarisation;
312 sat.orbital_position =
313 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
314 sat.inversion = inversion;
316 sat.modulation = modulation;
317 sat.roll_off = rolloff;
318 feparm->setDVBS(sat);
319 } else if (line[1]=='t')
321 eDVBFrontendParametersTerrestrial ter;
322 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
323 sscanf(line+2, "%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &bandwidth, &code_rate_HP, &code_rate_LP, &modulation, &transmission_mode, &guard_interval, &hierarchy, &inversion);
324 ter.frequency = frequency;
325 ter.bandwidth = bandwidth;
326 ter.code_rate_HP = code_rate_HP;
327 ter.code_rate_LP = code_rate_LP;
328 ter.modulation = modulation;
329 ter.transmission_mode = transmission_mode;
330 ter.guard_interval = guard_interval;
331 ter.hierarchy = hierarchy;
332 ter.inversion = inversion;
333 feparm->setDVBT(ter);
334 } else if (line[1]=='c')
336 eDVBFrontendParametersCable cab;
337 int frequency, symbol_rate,
338 inversion=eDVBFrontendParametersCable::Inversion::Unknown,
339 modulation=eDVBFrontendParametersCable::Modulation::Auto,
340 fec_inner=eDVBFrontendParametersCable::FEC::fAuto;
341 sscanf(line+2, "%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner);
342 cab.frequency = frequency;
343 cab.fec_inner = fec_inner;
344 cab.inversion = inversion;
345 cab.symbol_rate = symbol_rate;
346 cab.modulation = modulation;
347 feparm->setDVBC(cab);
350 addChannelToList(channelid, feparm);
353 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
355 eDebug("services invalid, no services");
359 // clear all services
365 if (!fgets(line, 256, f))
367 if (!strcmp(line, "end\n"))
370 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
371 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
372 if (service_number == -1)
374 ePtr<eDVBService> s = new eDVBService;
375 eServiceReferenceDVB ref =
376 eServiceReferenceDVB(
377 eDVBNamespace(dvb_namespace),
378 eTransportStreamID(transport_stream_id),
379 eOriginalNetworkID(original_network_id),
380 eServiceID(service_id),
385 line[strlen(line)-1]=0;
387 s->m_service_name = line;
392 line[strlen(line)-1]=0;
393 std::string str=line;
395 if (str[1]!=':') // old ... (only service_provider)
397 s->m_provider_name=line;
399 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
401 unsigned int c=str.find(',');
404 if (c == std::string::npos)
410 v=str.substr(2, c-2);
413 // eDebug("%c ... %s", p, v.c_str());
415 s->m_provider_name=v;
418 sscanf(v.c_str(), "%x", &s->m_flags);
422 sscanf(v.c_str(), "%02d%04x", &cid, &val);
427 sscanf(v.c_str(), "%04x", &val);
434 eDebug("loaded %d services", count);
439 void eDVBDB::saveServicelist()
441 eDebug("---- saving lame channel db");
442 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "w");
443 int channels=0, services=0;
445 eFatal("couldn't save lame channel db!");
446 fprintf(f, "eDVB services /3/\n");
447 fprintf(f, "transponders\n");
448 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
449 i != m_channels.end(); ++i)
451 const eDVBChannelID &chid = i->first;
452 const channel &ch = i->second;
454 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
455 chid.transport_stream_id.get(), chid.original_network_id.get());
456 eDVBFrontendParametersSatellite sat;
457 eDVBFrontendParametersTerrestrial ter;
458 eDVBFrontendParametersCable cab;
459 if (!ch.m_frontendParameters->getDVBS(sat))
461 if (sat.system == eDVBFrontendParametersSatellite::System::DVB_S2)
463 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
464 sat.frequency, sat.symbol_rate,
465 sat.polarisation, sat.fec,
466 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
474 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
475 sat.frequency, sat.symbol_rate,
476 sat.polarisation, sat.fec,
477 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
481 if (!ch.m_frontendParameters->getDVBT(ter))
483 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
484 ter.frequency, ter.bandwidth, ter.code_rate_HP,
485 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
486 ter.guard_interval, ter.hierarchy, ter.inversion);
488 if (!ch.m_frontendParameters->getDVBC(cab))
490 fprintf(f, "\tc %d:%d:%d:%d:%d\n",
491 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner);
496 fprintf(f, "end\nservices\n");
498 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
499 i != m_services.end(); ++i)
501 const eServiceReferenceDVB &s = i->first;
502 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
503 s.getServiceID().get(), s.getDVBNamespace().get(),
504 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
508 fprintf(f, "%s\n", i->second->m_service_name.c_str());
510 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
513 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
514 ca != i->second->m_cache.end(); ++ca)
515 fprintf(f, ",c:%02d%04x", ca->first, ca->second);
517 // write cached ca pids
518 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
519 ca != i->second->m_ca.end(); ++ca)
520 fprintf(f, ",C:%04x", *ca);
522 if (i->second->m_flags)
523 fprintf(f, ",f:%x", i->second->m_flags);
528 fprintf(f, "end\nHave a lot of bugs!\n");
529 eDebug("saved %d channels and %d services!", channels, services);
533 void eDVBDB::loadBouquet(const char *path)
535 std::string bouquet_name = path;
536 if (!bouquet_name.length())
538 eDebug("Bouquet load failed.. no path given..");
541 unsigned int pos = bouquet_name.rfind('/');
542 if ( pos != std::string::npos )
543 bouquet_name.erase(0, pos+1);
544 if (bouquet_name.empty())
546 eDebug("Bouquet load failed.. no filename given..");
549 eBouquet &bouquet = m_bouquets[bouquet_name];
550 bouquet.m_filename = bouquet_name;
551 std::list<eServiceReference> &list = bouquet.m_services;
554 std::string p = CONFIGDIR"/enigma2/";
556 eDebug("loading bouquet... %s", p.c_str());
557 FILE *fp=fopen(p.c_str(), "rt");
562 if ( !stat(path, &s) )
564 rename(path, p.c_str() );
568 eDebug("failed to open.");
569 if ( strstr(path, "bouquets.tv") )
571 eDebug("recreate bouquets.tv");
572 bouquet.m_bouquet_name="Bouquets (TV)";
573 bouquet.flushChanges();
575 else if ( strstr(path, "bouquets.radio") )
577 eDebug("recreate bouquets.radio");
578 bouquet.m_bouquet_name="Bouquets (Radio)";
579 bouquet.flushChanges();
584 bool read_descr=false;
585 eServiceReference *e = NULL;
588 if (!fgets(line, 256, fp))
590 line[strlen(line)-1]=0;
591 if (strlen(line) && line[strlen(line)-1]=='\r')
592 line[strlen(line)-1]=0;
597 if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
599 int offs = line[8] == ':' ? 10 : 9;
600 eServiceReference tmp(line+offs);
601 if (tmp.type != eServiceReference::idDVB)
603 eDebug("only DVB Bouquets supported");
606 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
608 unsigned int pos = tmp.path.rfind('/');
609 if ( pos != std::string::npos )
610 tmp.path.erase(0, pos+1);
611 if (tmp.path.empty())
613 eDebug("Bouquet load failed.. no filename given..");
616 loadBouquet(tmp.path.c_str());
618 snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], tmp.path.c_str());
626 else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
631 else if (!strncmp(line, "#NAME ", 6))
632 bouquet.m_bouquet_name=line+6;
637 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
640 void eDVBDB::reloadBouquets()
643 loadBouquet("bouquets.tv");
644 loadBouquet("bouquets.radio");
645 // create default bouquets when missing
646 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
648 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
649 b.m_filename = "userbouquet.favourites.tv";
650 b.m_bouquet_name = "Favourites (TV)";
652 eServiceReference ref;
653 memset(ref.data, 0, sizeof(ref.data));
657 ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
658 eBouquet &parent = m_bouquets["bouquets.tv"];
659 parent.m_services.push_back(ref);
660 parent.flushChanges();
662 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
664 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
665 b.m_filename = "userbouquet.favourites.radio";
666 b.m_bouquet_name = "Favourites (Radio)";
668 eServiceReference ref;
669 memset(ref.data, 0, sizeof(ref.data));
673 ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
674 eBouquet &parent = m_bouquets["bouquets.radio"];
675 parent.m_services.push_back(ref);
676 parent.flushChanges();
680 eDVBDB *eDVBDB::instance;
693 RESULT eDVBDB::removeService(const eServiceReference &ref)
695 if (ref.type == eServiceReference::idDVB)
697 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
698 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
699 if (it != m_services.end())
701 m_services.erase(it);
708 RESULT eDVBDB::removeServices(int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
710 return removeServices(eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
713 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
717 eTransportStreamID eTsid;
718 eOriginalNetworkID eOnid;
719 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
720 std::set<eDVBChannelID> removed_chids;
721 while (it != m_channels.end())
723 const eDVBChannelID &ch = it->first;
726 it->second.m_frontendParameters->getSystem(system);
727 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
729 eDVBFrontendParametersSatellite sat;
730 it->second.m_frontendParameters->getDVBS(sat);
731 if ((unsigned int)sat.orbital_position != orbpos)
734 if ( remove && chid.dvbnamespace != eNs && chid.dvbnamespace != ch.dvbnamespace )
736 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
738 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
742 eDebug("remove %08x %04x %04x",
743 ch.dvbnamespace.get(),
744 ch.original_network_id.get(),
745 ch.transport_stream_id.get());
746 removed_chids.insert(it->first);
747 m_channels.erase(it++);
752 if (!removed_chids.empty())
754 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
755 while(service != m_services.end())
758 service->first.getChannelID(chid);
759 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
760 if (it != removed_chids.end())
761 m_services.erase(service++);
770 RESULT eDVBDB::addFlag(const eServiceReference &ref, unsigned int flagmask)
772 if (ref.type == eServiceReference::idDVB)
774 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
775 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
776 if (it != m_services.end())
777 it->second->m_flags |= ~flagmask;
783 RESULT eDVBDB::removeFlag(const eServiceReference &ref, unsigned int flagmask)
785 if (ref.type == eServiceReference::idDVB)
787 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
788 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
789 if (it != m_services.end())
790 it->second->m_flags &= ~flagmask;
796 RESULT eDVBDB::removeFlags(unsigned int flagmask, int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
798 return removeFlags(flagmask, eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
801 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
804 eTransportStreamID eTsid;
805 eOriginalNetworkID eOnid;
806 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
807 std::set<eDVBChannelID> removed_chids;
808 while (it != m_channels.end())
810 const eDVBChannelID &ch = it->first;
813 it->second.m_frontendParameters->getSystem(system);
814 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
816 eDVBFrontendParametersSatellite sat;
817 it->second.m_frontendParameters->getDVBS(sat);
818 if ((unsigned int)sat.orbital_position != orbpos)
821 if ( remove && chid.dvbnamespace != eNs && chid.dvbnamespace != ch.dvbnamespace )
823 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
825 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
828 removed_chids.insert(it->first);
831 if (!removed_chids.empty())
833 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
834 while(service != m_services.end())
837 service->first.getChannelID(chid);
838 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
839 if (it != removed_chids.end())
840 service->second->m_flags &= ~flagmask;
847 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
851 ch.m_frontendParameters = feparm;
852 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
856 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
858 m_channels.erase(id);
862 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
864 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
865 if (i == m_channels.end())
870 parm = i->second.m_frontendParameters;
874 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
876 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
877 if (it == m_services.end())
878 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
882 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
884 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
885 i = m_services.find(reference);
886 if (i == m_services.end())
895 RESULT eDVBDB::flush()
901 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
903 std::string str = ref.path;
906 eDebug("getBouquet failed.. no path given!");
909 unsigned int pos = str.find("FROM BOUQUET \"");
910 if ( pos != std::string::npos )
912 str.erase(0, pos+14);
914 if ( pos != std::string::npos )
921 eDebug("getBouquet failed.. couldn't parse bouquet name");
924 std::map<std::string, eBouquet>::iterator i =
925 m_bouquets.find(str);
926 if (i == m_bouquets.end())
931 bouquet = &i->second;
935 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
937 if ( source.path.find("FROM") != std::string::npos )
939 if ( source.path.find("BOUQUET") != std::string::npos )
940 query = new eDVBDBBouquetQuery(this, source, q);
941 else if ( source.path.find("SATELLITES") != std::string::npos )
942 query = new eDVBDBSatellitesQuery(this, source, q);
943 else if ( source.path.find("PROVIDERS") != std::string::npos )
944 query = new eDVBDBProvidersQuery(this, source, q);
946 eFatal("invalid query %s", source.toString().c_str());
949 query = new eDVBDBQuery(this, source, q);
953 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
956 eTransportStreamID Tsid(tsid);
957 eOriginalNetworkID Onid(onid);
958 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
959 sit != m_services.end(); ++sit)
961 if (sit->first.getTransportStreamID() == Tsid &&
962 sit->first.getOriginalNetworkID() == Onid &&
963 sit->first.getServiceID() == Sid)
966 return eServiceReference();
969 DEFINE_REF(eDVBDBQueryBase);
971 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
972 :m_db(db), m_query(query), m_source(source)
976 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
978 ePtr<eDVBService> a_service, b_service;
980 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
982 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
984 if (m_db->getService(a, a_service))
986 if (m_db->getService(b, b_service))
992 case eDVBChannelQuery::tName:
993 return a_service->m_service_name_sort < b_service->m_service_name_sort;
994 case eDVBChannelQuery::tProvider:
995 return a_service->m_provider_name < b_service->m_provider_name;
996 case eDVBChannelQuery::tType:
997 return a.getServiceType() < b.getServiceType();
998 case eDVBChannelQuery::tBouquet:
1000 case eDVBChannelQuery::tSatellitePosition:
1001 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
1008 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1009 :eDVBDBQueryBase(db, source, query)
1011 m_cursor = m_db->m_services.begin();
1014 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
1016 while (m_cursor != m_db->m_services.end())
1018 ref = m_cursor->first;
1020 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
1028 ref.type = eServiceReference::idInvalid;
1033 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1034 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
1038 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
1040 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1041 std::list<eServiceReference> &list = bouquet.m_services;
1042 while (m_cursor != list.end())
1044 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1046 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1047 m_db->m_services.find(ref);
1049 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
1057 ref.type = eServiceReference::idInvalid;
1062 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1063 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1067 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1069 if (m_cursor != m_list.end())
1074 ref.type = eServiceReference::idInvalid;
1078 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1080 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1082 int x = (a.getDVBNamespace().get() >> 16);
1083 int y = (b.getDVBNamespace().get() >> 16);
1090 return a.name < b.name;
1093 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1094 :eDVBDBListQuery(db, source, query)
1096 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1097 it != m_db->m_services.end(); ++it)
1099 int res = it->second->checkFilter(it->first, *query);
1102 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1104 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
1105 if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
1112 eServiceReferenceDVB ref;
1113 ref.setDVBNamespace(dvbnamespace);
1114 ref.flags=eServiceReference::flagDirectory;
1116 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1118 ref.path=buf+source.path;
1119 unsigned int pos=ref.path.find("FROM");
1120 ref.path.erase(pos);
1121 ref.path+="ORDER BY name";
1122 // eDebug("ref.path now %s", ref.path.c_str());
1123 m_list.push_back(ref);
1125 ref.path=buf+source.path;
1126 pos=ref.path.find("FROM");
1127 ref.path.erase(pos+5);
1128 ref.path+="PROVIDERS ORDER BY name";
1129 // eDebug("ref.path now %s", ref.path.c_str());
1130 m_list.push_back(ref);
1132 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1133 ref.path=buf+source.path;
1134 pos=ref.path.find("FROM");
1135 ref.path.erase(pos);
1136 ref.path+="ORDER BY name";
1137 // eDebug("ref.path now %s", ref.path.c_str());
1138 m_list.push_back(ref);
1142 m_cursor=m_list.begin();
1145 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1146 :eDVBDBListQuery(db, source, query)
1148 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1149 it != m_db->m_services.end(); ++it)
1151 int res = it->second->checkFilter(it->first, *query);
1156 const char *provider_name = it->second->m_provider_name.length() ?
1157 it->second->m_provider_name.c_str() :
1160 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
1161 if (i->name == provider_name)
1168 eServiceReferenceDVB ref;
1170 ref.name=provider_name;
1171 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1172 ref.path=buf+source.path;
1173 unsigned int pos = ref.path.find("FROM");
1174 ref.flags=eServiceReference::flagDirectory;
1175 ref.path.erase(pos);
1176 ref.path+="ORDER BY name";
1177 // eDebug("ref.path now %s", ref.path.c_str());
1178 m_list.push_back(ref);
1182 m_cursor=m_list.begin();
1185 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1187 static int decodeType(const std::string &type)
1190 return eDVBChannelQuery::tName;
1191 else if (type == "provider")
1192 return eDVBChannelQuery::tProvider;
1193 else if (type == "type")
1194 return eDVBChannelQuery::tType;
1195 else if (type == "bouquet")
1196 return eDVBChannelQuery::tBouquet;
1197 else if (type == "satellitePosition")
1198 return eDVBChannelQuery::tSatellitePosition;
1199 else if (type == "channelID")
1200 return eDVBChannelQuery::tChannelID;
1201 else if (type == "flags")
1202 return eDVBChannelQuery::tFlags;
1207 /* never, NEVER write a parser in C++! */
1208 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1210 std::list<std::string>::const_iterator end_of_exp;
1214 eDebug("empty expression!");
1221 while (end_of_exp != end)
1222 if (*end_of_exp == ")")
1227 if (end_of_exp == end)
1229 eDebug("expression parse: end of expression while searching for closing brace");
1234 // begin..end_of_exp
1235 int r = parseExpression(res, begin, end_of_exp);
1240 /* we had only one sub expression */
1241 if (end_of_exp == end)
1243 // eDebug("only one sub expression");
1247 /* otherwise we have an operator here.. */
1249 ePtr<eDVBChannelQuery> r2 = res;
1250 res = new eDVBChannelQuery();
1256 if (*end_of_exp == "||")
1257 res->m_type = eDVBChannelQuery::tOR;
1258 else if (*end_of_exp == "&&")
1259 res->m_type = eDVBChannelQuery::tAND;
1262 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1269 return parseExpression(res->m_p2, end_of_exp, end);
1272 // "begin" <op> "end"
1273 std::string type, op, val;
1275 res = new eDVBChannelQuery();
1279 while (begin != end)
1293 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1302 eDebug("malformed query: missing stuff");
1307 res->m_type = decodeType(type);
1309 if (res->m_type == -1)
1311 eDebug("malformed query: invalid type %s", type.c_str());
1318 else if (op == "!=")
1322 eDebug("invalid operator %s", op.c_str());
1327 res->m_string = val;
1328 res->m_int = atoi(val.c_str());
1329 // res->m_channelid = eDVBChannelID(val);
1334 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1336 std::list<std::string> tokens;
1338 std::string current_token;
1339 std::string bouquet_name;
1341 // eDebug("splitting %s....", query.c_str());
1343 const char *splitchars="()";
1344 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1345 while (i <= query.size())
1347 int c = (i < query.size()) ? query[i] : ' ';
1350 int issplit = !!strchr(splitchars, c);
1351 int isaln = isalnum(c);
1352 int iswhite = c == ' ';
1353 int isquot = c == '\"';
1357 iswhite = issplit = 0;
1361 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1363 if (current_token.size())
1364 tokens.push_back(current_token);
1365 current_token.clear();
1368 if (!(iswhite || isquot))
1372 quotemode = !quotemode;
1373 lastsplit = issplit;
1377 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1379 // printf("%s\n", a->c_str());
1382 int sort = eDVBChannelQuery::tName;
1383 /* check for "ORDER BY ..." */
1385 std::list<std::string>::iterator it = tokens.begin();
1386 while (it != tokens.end())
1391 if (it != tokens.end() && *it == "BY")
1394 sort = decodeType(*it);
1399 else if (*it == "FROM")
1402 if (it != tokens.end() && *it == "BOUQUET")
1408 else if (it != tokens.end() && *it == "SATELLITES")
1410 else if (it != tokens.end() && *it == "PROVIDERS")
1414 eDebug("FROM unknown %s", (*it).c_str());
1424 eWarning("ORDER BY .. string invalid.");
1429 // eDebug("sort by %d", sort);
1431 /* now we recursivly parse that. */
1432 int r = parseExpression(res, tokens.begin(), tokens.end());
1434 /* we have an empty (but valid!) expression */
1437 res = new eDVBChannelQuery();
1439 res->m_type = eDVBChannelQuery::tAny;
1445 res->m_bouquet_name = bouquet_name;
1448 // eDebug("return: %d", r);
1452 DEFINE_REF(eDVBChannelQuery);