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/eenv.h>
7 #include <lib/base/eerror.h>
8 #include <lib/base/estring.h>
9 #include <xmlccwrap/xmlccwrap.h>
10 #include <dvbsi++/service_description_section.h>
11 #include <dvbsi++/descriptor_tag.h>
12 #include <dvbsi++/service_descriptor.h>
13 #include <dvbsi++/satellite_delivery_system_descriptor.h>
15 DEFINE_REF(eDVBService);
17 RESULT eBouquet::addService(const eServiceReference &ref, eServiceReference before)
20 std::find(m_services.begin(), m_services.end(), ref);
21 if ( it != m_services.end() )
25 it = std::find(m_services.begin(), m_services.end(), before);
26 m_services.insert(it, ref);
29 m_services.push_back(ref);
33 RESULT eBouquet::removeService(const eServiceReference &ref)
36 std::find(m_services.begin(), m_services.end(), ref);
37 if ( it == m_services.end() )
43 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
45 if ( pos < 0 || pos >= m_services.size() )
48 list::iterator source=m_services.end();
49 list::iterator dest=m_services.end();
51 for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
53 if (dest == m_services.end() && !--pos)
60 if (dest != m_services.end() && source != m_services.end())
63 if (dest == m_services.end() || source == m_services.end() || source == dest)
65 while (source != dest)
68 std::iter_swap(source++, source);
70 std::iter_swap(source--, source);
75 RESULT eBouquet::flushChanges()
77 FILE *f=fopen(eEnv::resolve("${sysconfdir}/enigma2/" + m_filename).c_str(), "w");
80 if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
82 for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
84 eServiceReference tmp = *i;
85 std::string str = tmp.path;
86 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
88 if ( i->name.length() )
89 if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
96 eDebug("couldn't write file %s", m_filename.c_str());
100 RESULT eBouquet::setListName(const std::string &name)
102 m_bouquet_name = name;
106 eDVBService::eDVBService()
107 :m_cache(0), m_flags(0)
111 eDVBService::~eDVBService()
116 eDVBService &eDVBService::operator=(const eDVBService &s)
118 m_service_name = s.m_service_name;
119 m_service_name_sort = s.m_service_name_sort;
120 m_provider_name = s.m_provider_name;
123 copyCache(s.m_cache);
127 void eDVBService::genSortName()
129 m_service_name_sort = removeDVBChars(m_service_name);
130 makeUpper(m_service_name_sort);
131 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
132 m_service_name_sort.erase(0, 1);
134 /* put unnamed services at the end, not at the beginning. */
135 if (m_service_name_sort.empty())
136 m_service_name_sort = "\xFF";
139 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
141 if (!ref.name.empty())
142 name = ref.name; // use renamed service name..
143 else if (!m_service_name.empty())
144 name = m_service_name;
150 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
152 return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
155 int eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore, bool simulate)
157 ePtr<eDVBResourceManager> res_mgr;
158 if ( eDVBResourceManager::getInstance( res_mgr ) )
159 eDebug("isPlayble... no res manager!!");
162 eDVBChannelID chid, chid_ignore;
163 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
164 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
165 return res_mgr->canAllocateChannel(chid, chid_ignore, simulate);
170 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
173 switch (query.m_type)
175 case eDVBChannelQuery::tName:
176 res = m_service_name_sort == query.m_string;
178 case eDVBChannelQuery::tProvider:
179 if (query.m_string == "Unknown" && m_provider_name.empty())
182 res = m_provider_name == query.m_string;
184 case eDVBChannelQuery::tType:
186 int service_type = ref.getServiceType();
187 if (query.m_int == 1) // TV Service
189 // Hack for dish network
190 int onid = ref.getOriginalNetworkID().get();
191 if (onid >= 0x1001 && onid <= 0x100b)
193 static int dish_tv_types[] = { 128, 133, 137, 140, 144, 145, 150, 154, 160, 163, 164, 165, 166, 167, 168, 173, 174 };
194 static size_t dish_tv_num_types = sizeof(dish_tv_types) / sizeof(int);
195 if (std::binary_search(dish_tv_types, dish_tv_types + dish_tv_num_types, service_type))
199 res = service_type == query.m_int;
202 case eDVBChannelQuery::tBouquet:
205 case eDVBChannelQuery::tSatellitePosition:
206 res = ((unsigned int)ref.getDVBNamespace().get())>>16 == (unsigned int)query.m_int;
208 case eDVBChannelQuery::tFlags:
209 res = (m_flags & query.m_int) == query.m_int;
211 case eDVBChannelQuery::tChannelID:
214 ref.getChannelID(chid);
215 res = chid == query.m_channelid;
218 case eDVBChannelQuery::tAND:
219 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
221 case eDVBChannelQuery::tOR:
222 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
224 case eDVBChannelQuery::tAny:
235 bool eDVBService::cacheEmpty()
238 for (int i=0; i < cacheMax; ++i)
239 if (m_cache[i] != -1)
244 void eDVBService::initCache()
246 m_cache = new int[cacheMax];
247 memset(m_cache, -1, sizeof(int) * cacheMax);
250 void eDVBService::copyCache(int *source)
255 m_cache = new int[cacheMax];
256 memcpy(m_cache, source, cacheMax * sizeof(int));
265 int eDVBService::getCacheEntry(cacheID id)
267 if (id >= cacheMax || !m_cache)
272 void eDVBService::setCacheEntry(cacheID id, int pid)
282 void eDVBDB::reloadServicelist()
284 loadServicelist(eEnv::resolve("${sysconfdir}/enigma2/lamedb").c_str());
287 void eDVBDB::parseServiceData(ePtr<eDVBService> s, std::string str)
289 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
291 size_t c=str.find(',');
294 if (c == std::string::npos)
300 v=str.substr(2, c-2);
303 // eDebug("%c ... %s", p, v.c_str());
305 s->m_provider_name=v;
308 sscanf(v.c_str(), "%x", &s->m_flags);
312 sscanf(v.c_str(), "%02d%x", &cid, &val);
313 s->setCacheEntry((eDVBService::cacheID)cid,val);
317 sscanf(v.c_str(), "%04x", &val);
318 s->m_ca.push_front((uint16_t)val);
323 /* THIS CODE IS BAD. it should be replaced by somethine better. */
324 void eDVBDB::loadServicelist(const char *file)
326 eDebug("---- opening lame channel db");
327 FILE *f=fopen(file, "rt");
328 if (!f && strcmp(file, eEnv::resolve("${sysconfdir}/enigma2/lamedb").c_str()) == 0)
331 if ( !stat("lamedb", &s) )
333 if ( !stat(eEnv::resolve("${sysconfdir}/enigma2").c_str(), &s) )
335 rename("lamedb", eEnv::resolve("${sysconfdir}/enigma2/lamedb").c_str());
343 if ((!fgets(line, 256, f)) || sscanf(line, "eDVB services /%d/", &version) != 1)
345 eDebug("not a valid servicefile");
349 eDebug("reading services (version %d)", version);
350 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
352 eDebug("services invalid, no transponders");
357 // clear all transponders
361 if (!fgets(line, 256, f))
363 if (!strcmp(line, "end\n"))
365 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
366 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
367 if (original_network_id == -1)
369 eDVBChannelID channelid = eDVBChannelID(
370 eDVBNamespace(dvb_namespace),
371 eTransportStreamID(transport_stream_id),
372 eOriginalNetworkID(original_network_id));
374 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
378 if (!strcmp(line, "/\n"))
382 eDVBFrontendParametersSatellite sat;
383 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion,
385 system=eDVBFrontendParametersSatellite::System_DVB_S,
386 modulation=eDVBFrontendParametersSatellite::Modulation_QPSK,
387 rolloff=eDVBFrontendParametersSatellite::RollOff_alpha_0_35,
388 pilot=eDVBFrontendParametersSatellite::Pilot_Unknown;
390 sscanf(line+3, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion, &system, &modulation, &rolloff, &pilot);
392 sscanf(line+3, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion, &flags, &system, &modulation, &rolloff, &pilot);
393 sat.frequency = frequency;
394 sat.symbol_rate = symbol_rate;
395 sat.polarisation = polarisation;
397 sat.orbital_position =
398 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
399 sat.inversion = inversion;
401 sat.modulation = modulation;
402 sat.rolloff = rolloff;
404 feparm->setDVBS(sat);
405 feparm->setFlags(flags);
406 } else if (line[1]=='t')
408 eDVBFrontendParametersTerrestrial ter;
409 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion, flags=0;
410 sscanf(line+3, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &bandwidth, &code_rate_HP, &code_rate_LP, &modulation, &transmission_mode, &guard_interval, &hierarchy, &inversion, &flags);
411 ter.frequency = frequency;
412 ter.bandwidth = bandwidth;
413 ter.code_rate_HP = code_rate_HP;
414 ter.code_rate_LP = code_rate_LP;
415 ter.modulation = modulation;
416 ter.transmission_mode = transmission_mode;
417 ter.guard_interval = guard_interval;
418 ter.hierarchy = hierarchy;
419 ter.inversion = inversion;
420 feparm->setDVBT(ter);
421 feparm->setFlags(flags);
422 } else if (line[1]=='c')
424 eDVBFrontendParametersCable cab;
425 int frequency, symbol_rate,
426 inversion=eDVBFrontendParametersCable::Inversion_Unknown,
427 modulation=eDVBFrontendParametersCable::Modulation_Auto,
428 fec_inner=eDVBFrontendParametersCable::FEC_Auto,
430 sscanf(line+3, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner, &flags);
431 cab.frequency = frequency;
432 cab.fec_inner = fec_inner;
433 cab.inversion = inversion;
434 cab.symbol_rate = symbol_rate;
435 cab.modulation = modulation;
436 feparm->setDVBC(cab);
437 feparm->setFlags(flags);
440 addChannelToList(channelid, feparm);
443 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
445 eDebug("services invalid, no services");
449 // clear all services
455 if (!fgets(line, 256, f))
457 if (!strcmp(line, "end\n"))
460 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
461 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
462 if (service_number == -1)
464 ePtr<eDVBService> s = new eDVBService;
465 eServiceReferenceDVB ref =
466 eServiceReferenceDVB(
467 eDVBNamespace(dvb_namespace),
468 eTransportStreamID(transport_stream_id),
469 eOriginalNetworkID(original_network_id),
470 eServiceID(service_id),
475 line[strlen(line)-1]=0;
477 s->m_service_name = line;
482 line[strlen(line)-1]=0;
483 if (line[1]!=':') // old ... (only service_provider)
484 s->m_provider_name=line;
486 parseServiceData(s, line);
490 eDebug("loaded %d services", count);
495 void eDVBDB::saveServicelist(const char *file)
497 eDebug("---- saving lame channel db");
498 FILE *f=fopen(file, "w");
499 int channels=0, services=0;
501 eFatal("couldn't save lame channel db!");
502 fprintf(f, "eDVB services /4/\n");
503 fprintf(f, "transponders\n");
504 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
505 i != m_channels.end(); ++i)
507 const eDVBChannelID &chid = i->first;
508 const channel &ch = i->second;
510 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
511 chid.transport_stream_id.get(), chid.original_network_id.get());
512 eDVBFrontendParametersSatellite sat;
513 eDVBFrontendParametersTerrestrial ter;
514 eDVBFrontendParametersCable cab;
515 unsigned int flags; // flagOnlyFree yet..
516 ch.m_frontendParameters->getFlags(flags);
517 if (!ch.m_frontendParameters->getDVBS(sat))
519 if (sat.system == eDVBFrontendParametersSatellite::System_DVB_S2)
521 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d\n",
522 sat.frequency, sat.symbol_rate,
523 sat.polarisation, sat.fec,
524 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
534 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d\n",
535 sat.frequency, sat.symbol_rate,
536 sat.polarisation, sat.fec,
537 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
538 sat.inversion, flags);
541 else if (!ch.m_frontendParameters->getDVBT(ter))
543 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d:%d\n",
544 ter.frequency, ter.bandwidth, ter.code_rate_HP,
545 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
546 ter.guard_interval, ter.hierarchy, ter.inversion, flags);
548 else if (!ch.m_frontendParameters->getDVBC(cab))
550 fprintf(f, "\tc %d:%d:%d:%d:%d:%d\n",
551 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner, flags);
556 fprintf(f, "end\nservices\n");
558 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
559 i != m_services.end(); ++i)
561 const eServiceReferenceDVB &s = i->first;
562 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
563 s.getServiceID().get(), s.getDVBNamespace().get(),
564 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
568 fprintf(f, "%s\n", i->second->m_service_name.c_str());
570 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
573 for (int x=0; x < eDVBService::cacheMax; ++x)
575 int entry = i->second->getCacheEntry((eDVBService::cacheID)x);
577 fprintf(f, ",c:%02d%04x", x, entry);
580 // write cached ca pids
581 for (CAID_LIST::const_iterator ca(i->second->m_ca.begin());
582 ca != i->second->m_ca.end(); ++ca)
583 fprintf(f, ",C:%04x", *ca);
585 if (i->second->m_flags)
586 fprintf(f, ",f:%x", i->second->m_flags);
591 fprintf(f, "end\nHave a lot of bugs!\n");
592 eDebug("saved %d channels and %d services!", channels, services);
596 void eDVBDB::saveServicelist()
598 saveServicelist(eEnv::resolve("${sysconfdir}/enigma2/lamedb").c_str());
601 void eDVBDB::loadBouquet(const char *path)
603 std::string bouquet_name = path;
604 if (!bouquet_name.length())
606 eDebug("Bouquet load failed.. no path given..");
609 size_t pos = bouquet_name.rfind('/');
610 if ( pos != std::string::npos )
611 bouquet_name.erase(0, pos+1);
612 if (bouquet_name.empty())
614 eDebug("Bouquet load failed.. no filename given..");
617 eBouquet &bouquet = m_bouquets[bouquet_name];
618 bouquet.m_filename = bouquet_name;
619 std::list<eServiceReference> &list = bouquet.m_services;
622 std::string p = eEnv::resolve("${sysconfdir}/enigma2/");
624 eDebug("loading bouquet... %s", p.c_str());
625 FILE *fp=fopen(p.c_str(), "rt");
630 if ( !stat(path, &s) )
632 rename(path, p.c_str() );
636 eDebug("failed to open.");
637 if ( strstr(path, "bouquets.tv") )
639 eDebug("recreate bouquets.tv");
640 bouquet.m_bouquet_name="Bouquets (TV)";
641 bouquet.flushChanges();
643 else if ( strstr(path, "bouquets.radio") )
645 eDebug("recreate bouquets.radio");
646 bouquet.m_bouquet_name="Bouquets (Radio)";
647 bouquet.flushChanges();
652 bool read_descr=false;
653 eServiceReference *e = NULL;
656 if (!fgets(line, 256, fp))
658 line[strlen(line)-1]=0;
659 if (strlen(line) && line[strlen(line)-1]=='\r')
660 line[strlen(line)-1]=0;
665 if (!strncmp(line, "#SERVICE", 8))
667 int offs = line[8] == ':' ? 10 : 9;
668 eServiceReference tmp(line+offs);
669 if (tmp.type != eServiceReference::idDVB)
671 eDebug("only DVB Bouquets supported");
674 if ( tmp.flags&eServiceReference::canDescent )
676 size_t pos = tmp.path.rfind('/');
678 std::string path = tmp.path;
679 if ( pos != std::string::npos )
680 path.erase(0, pos+1);
683 eDebug("Bouquet load failed.. no filename given..");
686 pos = path.find("FROM BOUQUET ");
687 if (pos != std::string::npos)
689 char endchr = path[pos+13];
692 eDebug("ignore invalid bouquet '%s' (only \" are allowed)",
693 tmp.toString().c_str());
696 char *beg = &path[pos+14];
697 char *end = strchr(beg, endchr);
698 path.assign(beg, end - beg);
702 snprintf(buf, 256, "FROM BOUQUET \"%s\" ORDER BY bouquet", path.c_str());
705 loadBouquet(path.c_str());
712 else if (read_descr && !strncmp(line, "#DESCRIPTION", 12))
714 int offs = line[12] == ':' ? 14 : 13;
718 else if (!strncmp(line, "#NAME ", 6))
719 bouquet.m_bouquet_name=line+6;
724 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
727 void eDVBDB::reloadBouquets()
730 loadBouquet("bouquets.tv");
731 loadBouquet("bouquets.radio");
732 // create default bouquets when missing
733 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
735 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
736 b.m_filename = "userbouquet.favourites.tv";
737 b.m_bouquet_name = "Favourites (TV)";
739 eServiceReference ref;
740 memset(ref.data, 0, sizeof(ref.data));
744 ref.path="FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
745 eBouquet &parent = m_bouquets["bouquets.tv"];
746 parent.m_services.push_back(ref);
747 parent.flushChanges();
749 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
751 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
752 b.m_filename = "userbouquet.favourites.radio";
753 b.m_bouquet_name = "Favourites (Radio)";
755 eServiceReference ref;
756 memset(ref.data, 0, sizeof(ref.data));
760 ref.path="FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
761 eBouquet &parent = m_bouquets["bouquets.radio"];
762 parent.m_services.push_back(ref);
763 parent.flushChanges();
767 eDVBDB *eDVBDB::instance;
769 using namespace xmlcc;
777 PyObject *eDVBDB::readSatellites(ePyObject sat_list, ePyObject sat_dict, ePyObject tp_dict)
779 if (!PyDict_Check(tp_dict)) {
780 PyErr_SetString(PyExc_StandardError,
782 eDebug("arg 2 is not a python dict");
785 else if (!PyDict_Check(sat_dict))
787 PyErr_SetString(PyExc_StandardError,
789 eDebug("arg 1 is not a python dict");
792 else if (!PyList_Check(sat_list))
794 PyErr_SetString(PyExc_StandardError,
796 eDebug("arg 0 is not a python list");
800 tree.setFilename("/etc/tuxbox/satellites.xml");
802 Element *root = tree.getRoot();
805 eDebug("couldn't open /etc/tuxbox/satellites.xml!!");
809 int tmp, *dest = NULL,
810 modulation, system, freq, sr, pol, fec, inv, pilot, rolloff, tsid, onid;
814 const ElementList &root_elements = root->getElementList();
815 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
817 // eDebug("element: %s", (*it)->name().c_str());
818 const Element *el = *it;
819 const ElementList &sat_elements = el->getElementList();
820 const AttributeList &sat_attributes = el->getAttributeList();
824 for (AttributeConstIterator it(sat_attributes.begin()); it != sat_attributes.end(); ++it)
826 // eDebug("\tattr: %s", at->name().c_str());
830 sat_name = PyString_FromString(at->value().c_str());
831 else if (name == "flags")
833 tmp = strtol(at->value().c_str(), &end_ptr, 10);
835 sat_flags = PyInt_FromLong(tmp);
837 else if (name == "position")
839 tmp = strtol(at->value().c_str(), &end_ptr, 10);
844 sat_pos = PyInt_FromLong(tmp);
848 if (sat_pos && sat_name)
850 ePyObject tplist = PyList_New(0);
851 ePyObject tuple = PyTuple_New(3);
853 sat_flags = PyInt_FromLong(0);
854 PyTuple_SET_ITEM(tuple, 0, sat_pos);
855 PyTuple_SET_ITEM(tuple, 1, sat_name);
856 PyTuple_SET_ITEM(tuple, 2, sat_flags);
857 PyList_Append(sat_list, tuple);
859 PyDict_SetItem(sat_dict, sat_pos, sat_name);
860 PyDict_SetItem(tp_dict, sat_pos, tplist);
861 for (ElementConstIterator it(sat_elements.begin()); it != sat_elements.end(); ++it)
863 //eDebug("\telement: %s", (*it)->name().c_str());
864 const AttributeList &tp_attributes = (*it)->getAttributeList();
865 AttributeConstIterator end = tp_attributes.end();
866 modulation = eDVBFrontendParametersSatellite::Modulation_QPSK;
867 system = eDVBFrontendParametersSatellite::System_DVB_S;
871 fec = eDVBFrontendParametersSatellite::FEC_Auto;
872 inv = eDVBFrontendParametersSatellite::Inversion_Unknown;
873 pilot = eDVBFrontendParametersSatellite::Pilot_Unknown;
874 rolloff = eDVBFrontendParametersSatellite::RollOff_alpha_0_35;
878 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
881 //eDebug("\t\tattr: %s", at->name().c_str());
883 if (name == "modulation") dest = &modulation;
884 else if (name == "system") dest = &system;
885 else if (name == "frequency") dest = &freq;
886 else if (name == "symbol_rate") dest = &sr;
887 else if (name == "polarization") dest = &pol;
888 else if (name == "fec_inner") dest = &fec;
889 else if (name == "inversion") dest = &inv;
890 else if (name == "rolloff") dest = &rolloff;
891 else if (name == "pilot") dest = &pilot;
892 else if (name == "tsid") dest = &tsid;
893 else if (name == "onid") dest = &onid;
897 //eDebug("\t\t\tvalue: %s", at->value().c_str());
898 tmp = strtol(at->value().c_str(), &end_ptr, 10);
903 if (freq && sr && pol != -1)
905 tuple = PyTuple_New(12);
906 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(0));
907 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
908 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
909 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(pol));
910 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
911 PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(system));
912 PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(modulation));
913 PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(inv));
914 PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(rolloff));
915 PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(pilot));
916 PyTuple_SET_ITEM(tuple, 10, PyInt_FromLong(tsid));
917 PyTuple_SET_ITEM(tuple, 11, PyInt_FromLong(onid));
918 PyList_Append(tplist, tuple);
931 Py_DECREF(sat_flags);
938 PyObject *eDVBDB::readCables(ePyObject cab_list, ePyObject tp_dict)
940 if (!PyDict_Check(tp_dict)) {
941 PyErr_SetString(PyExc_StandardError,
943 eDebug("arg 1 is not a python dict");
946 else if (!PyList_Check(cab_list))
948 PyErr_SetString(PyExc_StandardError,
950 eDebug("arg 0 is not a python list");
954 tree.setFilename("/etc/tuxbox/cables.xml");
956 Element *root = tree.getRoot();
959 eDebug("couldn't open /etc/tuxbox/cables.xml!!");
965 modulation, fec, freq, sr;
968 const ElementList &root_elements = root->getElementList();
969 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
971 // eDebug("element: %s", el->name().c_str());
972 const Element *el = *it;
973 const ElementList &cab_elements = el->getElementList();
974 const AttributeList &cab_attributes = el->getAttributeList();
977 for (AttributeConstIterator it(cab_attributes.begin()); it != cab_attributes.end(); ++it)
979 // eDebug("\tattr: %s", at->name().c_str());
983 cab_name = PyString_FromString(at->value().c_str());
984 else if (name == "flags")
986 tmp = strtol(at->value().c_str(), &end_ptr, 10);
988 cab_flags = PyInt_FromLong(tmp);
993 ePyObject tplist = PyList_New(0);
994 ePyObject tuple = PyTuple_New(2);
996 cab_flags = PyInt_FromLong(0);
997 PyTuple_SET_ITEM(tuple, 0, cab_name);
998 PyTuple_SET_ITEM(tuple, 1, cab_flags);
999 PyList_Append(cab_list, tuple);
1001 PyDict_SetItem(tp_dict, cab_name, tplist);
1002 for (ElementConstIterator it(cab_elements.begin()); it != cab_elements.end(); ++it)
1004 // eDebug("\telement: %s", (*it)->name().c_str());
1005 const AttributeList &tp_attributes = (*it)->getAttributeList();
1006 AttributeConstIterator end = tp_attributes.end();
1007 modulation = eDVBFrontendParametersCable::Modulation_QAM64;
1008 fec = eDVBFrontendParametersCable::FEC_Auto;
1011 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
1013 // eDebug("\t\tattr: %s", at->name().c_str());
1017 if (name == "modulation") dest = &modulation;
1018 else if (name == "frequency") dest = &freq;
1019 else if (name == "symbol_rate") dest = &sr;
1020 else if (name == "fec_inner") dest = &fec;
1024 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1031 while (freq > 999999)
1033 tuple = PyTuple_New(5);
1034 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(1));
1035 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
1036 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
1037 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(modulation));
1038 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
1039 PyList_Append(tplist, tuple);
1046 Py_DECREF(cab_flags);
1052 PyObject *eDVBDB::readTerrestrials(ePyObject ter_list, ePyObject tp_dict)
1054 if (!PyDict_Check(tp_dict)) {
1055 PyErr_SetString(PyExc_StandardError,
1057 eDebug("arg 1 is not a python dict");
1060 else if (!PyList_Check(ter_list))
1062 PyErr_SetString(PyExc_StandardError,
1064 eDebug("arg 0 is not a python list");
1068 tree.setFilename("/etc/tuxbox/terrestrial.xml");
1070 Element *root = tree.getRoot();
1073 eDebug("couldn't open /etc/tuxbox/terrestrial.xml!!");
1074 Py_INCREF(Py_False);
1077 const Attribute *at;
1080 freq, bw, constellation, crh = 5, crl = 5, guard = 4, transm, hierarchy, inv = 2;
1082 const ElementList &root_elements = root->getElementList();
1083 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
1085 // eDebug("element: %s", el->name().c_str());
1086 const Element *el = *it;
1087 const ElementList &ter_elements = el->getElementList();
1088 const AttributeList &ter_attributes = el->getAttributeList();
1090 ePyObject ter_flags;
1091 for (AttributeConstIterator it(ter_attributes.begin()); it != ter_attributes.end(); ++it)
1093 // eDebug("\tattr: %s", at->name().c_str());
1097 ter_name = PyString_FromString(at->value().c_str());
1098 else if (name == "flags")
1100 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1102 ter_flags = PyInt_FromLong(tmp);
1107 ePyObject tplist = PyList_New(0);
1108 ePyObject tuple = PyTuple_New(2);
1110 ter_flags = PyInt_FromLong(0);
1111 PyTuple_SET_ITEM(tuple, 0, ter_name);
1112 PyTuple_SET_ITEM(tuple, 1, ter_flags);
1113 PyList_Append(ter_list, tuple);
1115 PyDict_SetItem(tp_dict, ter_name, tplist);
1116 for (ElementConstIterator it(ter_elements.begin()); it != ter_elements.end(); ++it)
1118 // eDebug("\telement: %s", (*it)->name().c_str());
1119 const AttributeList &tp_attributes = (*it)->getAttributeList();
1120 AttributeConstIterator end = tp_attributes.end();
1122 bw = eDVBFrontendParametersTerrestrial::Bandwidth_Auto;
1123 constellation = eDVBFrontendParametersTerrestrial::Modulation_Auto;
1124 crh = eDVBFrontendParametersTerrestrial::FEC_Auto;
1125 crl = eDVBFrontendParametersTerrestrial::FEC_Auto;
1126 guard = eDVBFrontendParametersTerrestrial::GuardInterval_Auto;
1127 transm = eDVBFrontendParametersTerrestrial::TransmissionMode_Auto;
1128 hierarchy = eDVBFrontendParametersTerrestrial::Hierarchy_Auto;
1129 inv = eDVBFrontendParametersTerrestrial::Inversion_Unknown;
1130 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
1132 // eDebug("\t\tattr: %s", at->name().c_str());
1136 if (name == "centre_frequency") dest = &freq;
1137 else if (name == "bandwidth") dest = &bw;
1138 else if (name == "constellation") dest = &constellation;
1139 else if (name == "code_rate_hp") dest = &crh;
1140 else if (name == "code_rate_lp") dest = &crl;
1141 else if (name == "guard_interval") dest = &guard;
1142 else if (name == "transmission_mode") dest = &transm;
1143 else if (name == "hierarchy_information") dest = &hierarchy;
1144 else if (name == "inversion") dest = &inv;
1148 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1155 if (crh > 5) // our terrestrial.xml is buggy... 6 for AUTO
1157 if (crl > 5) // our terrestrial.xml is buggy... 6 for AUTO
1159 tuple = PyTuple_New(10);
1160 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1161 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
1162 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(bw));
1163 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(constellation));
1164 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(crh));
1165 PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(crl));
1166 PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(guard));
1167 PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(transm));
1168 PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(hierarchy));
1169 PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(inv));
1170 PyList_Append(tplist, tuple);
1177 Py_DECREF(ter_flags);
1188 RESULT eDVBDB::removeService(const eServiceReference &ref)
1190 if (ref.type == eServiceReference::idDVB)
1192 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1193 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1194 if (it != m_services.end())
1196 m_services.erase(it);
1203 RESULT eDVBDB::removeServices(int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1205 return removeServices(eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1208 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
1212 eTransportStreamID eTsid;
1213 eOriginalNetworkID eOnid;
1214 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1215 std::set<eDVBChannelID> removed_chids;
1216 while (it != m_channels.end())
1218 const eDVBChannelID &ch = it->first;
1221 it->second.m_frontendParameters->getSystem(system);
1222 if ( system == iDVBFrontend::feSatellite )
1224 eDVBFrontendParametersSatellite sat;
1225 it->second.m_frontendParameters->getDVBS(sat);
1226 if ((unsigned int)sat.orbital_position != orbpos)
1229 if ( remove && chid.dvbnamespace != eNs ) // namespace given?
1231 if ( system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000 )
1233 else if ( system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000 )
1235 else if ( chid.dvbnamespace != ch.dvbnamespace )
1238 else if ( system == iDVBFrontend::feCable || system == iDVBFrontend::feTerrestrial )
1240 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1242 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1246 eDebug("remove %08x %04x %04x",
1247 ch.dvbnamespace.get(),
1248 ch.original_network_id.get(),
1249 ch.transport_stream_id.get());
1250 removed_chids.insert(it->first);
1251 m_channels.erase(it++);
1256 if (!removed_chids.empty())
1258 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1259 while(service != m_services.end())
1262 service->first.getChannelID(chid);
1263 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1264 if (it != removed_chids.end())
1265 m_services.erase(service++);
1274 RESULT eDVBDB::removeServices(iDVBFrontendParameters *feparm)
1277 std::set<eDVBChannelID> removed_chids;
1278 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1279 while (it != m_channels.end())
1282 if (!feparm->calculateDifference(&(*it->second.m_frontendParameters), diff, false))
1286 removed_chids.insert(it->first);
1287 m_channels.erase(it++);
1295 if (!removed_chids.empty())
1297 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1298 while(service != m_services.end())
1301 service->first.getChannelID(chid);
1302 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1303 if (it != removed_chids.end())
1304 m_services.erase(service++);
1313 RESULT eDVBDB::addFlag(const eServiceReference &ref, unsigned int flagmask)
1315 if (ref.type == eServiceReference::idDVB)
1317 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1318 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1319 if (it != m_services.end())
1320 it->second->m_flags |= ~flagmask;
1326 RESULT eDVBDB::removeFlag(const eServiceReference &ref, unsigned int flagmask)
1328 if (ref.type == eServiceReference::idDVB)
1330 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1331 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1332 if (it != m_services.end())
1333 it->second->m_flags &= ~flagmask;
1339 RESULT eDVBDB::removeFlags(unsigned int flagmask, int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1341 return removeFlags(flagmask, eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1344 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
1347 eTransportStreamID eTsid;
1348 eOriginalNetworkID eOnid;
1349 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1350 std::set<eDVBChannelID> removed_chids;
1351 while (it != m_channels.end())
1353 const eDVBChannelID &ch = it->first;
1356 it->second.m_frontendParameters->getSystem(system);
1357 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
1359 eDVBFrontendParametersSatellite sat;
1360 it->second.m_frontendParameters->getDVBS(sat);
1361 if ((unsigned int)sat.orbital_position != orbpos)
1364 if ( remove && chid.dvbnamespace != eNs )
1366 if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1368 else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1370 else if ( chid.dvbnamespace != ch.dvbnamespace )
1373 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1375 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1378 removed_chids.insert(it->first);
1381 if (!removed_chids.empty())
1383 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1384 while(service != m_services.end())
1387 service->first.getChannelID(chid);
1388 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1389 if (it != removed_chids.end())
1390 service->second->m_flags &= ~flagmask;
1397 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
1400 std::map<eDVBChannelID, channel>::iterator it = m_channels.find(id);
1402 ch.m_frontendParameters = feparm;
1403 if (it != m_channels.end())
1406 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
1410 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
1412 m_channels.erase(id);
1416 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
1418 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
1419 if (i == m_channels.end())
1424 parm = i->second.m_frontendParameters;
1428 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
1430 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
1431 if (it == m_services.end())
1432 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
1436 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
1438 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
1439 i = m_services.find(reference);
1440 if (i == m_services.end())
1445 service = i->second;
1449 RESULT eDVBDB::flush()
1455 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
1457 std::string str = ref.path;
1460 eDebug("getBouquet failed.. no path given!");
1463 size_t pos = str.find("FROM BOUQUET \"");
1464 if ( pos != std::string::npos )
1466 str.erase(0, pos+14);
1467 pos = str.find('"');
1468 if ( pos != std::string::npos )
1475 eDebug("getBouquet failed.. couldn't parse bouquet name");
1478 std::map<std::string, eBouquet>::iterator i =
1479 m_bouquets.find(str);
1480 if (i == m_bouquets.end())
1485 bouquet = &i->second;
1489 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
1491 if ( source.path.find("FROM") != std::string::npos )
1493 if ( source.path.find("BOUQUET") != std::string::npos )
1494 query = new eDVBDBBouquetQuery(this, source, q);
1495 else if ( source.path.find("SATELLITES") != std::string::npos )
1496 query = new eDVBDBSatellitesQuery(this, source, q);
1497 else if ( source.path.find("PROVIDERS") != std::string::npos )
1498 query = new eDVBDBProvidersQuery(this, source, q);
1500 eFatal("invalid query %s", source.toString().c_str());
1503 query = new eDVBDBQuery(this, source, q);
1507 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
1509 eServiceID Sid(sid);
1510 eTransportStreamID Tsid(tsid);
1511 eOriginalNetworkID Onid(onid);
1512 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
1513 sit != m_services.end(); ++sit)
1515 if (sit->first.getTransportStreamID() == Tsid &&
1516 sit->first.getOriginalNetworkID() == Onid &&
1517 sit->first.getServiceID() == Sid)
1520 return eServiceReference();
1523 DEFINE_REF(eDVBDBQueryBase);
1525 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1526 :m_db(db), m_query(query), m_source(source)
1530 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1532 ePtr<eDVBService> a_service, b_service;
1533 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
1535 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
1537 if (a.name.empty() && m_db->getService(a, a_service))
1539 if (b.name.empty() && m_db->getService(b, b_service))
1545 case eDVBChannelQuery::tName:
1549 return a_service->m_service_name_sort < b_service->m_service_name_sort;
1552 std::string str = b.name;
1554 return a_service->m_service_name_sort < str;
1559 std::string str = a.name;
1561 return str < b_service->m_service_name_sort;
1565 std::string aa = a.name, bb = b.name;
1570 case eDVBChannelQuery::tProvider:
1571 return a_service->m_provider_name < b_service->m_provider_name;
1572 case eDVBChannelQuery::tType:
1573 return a.getServiceType() < b.getServiceType();
1574 case eDVBChannelQuery::tBouquet:
1576 case eDVBChannelQuery::tSatellitePosition:
1577 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
1584 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1585 :eDVBDBQueryBase(db, source, query)
1587 m_cursor = m_db->m_services.begin();
1590 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
1592 while (m_cursor != m_db->m_services.end())
1594 ePtr<eDVBService> service = m_cursor->second;
1595 if (service->isHidden())
1599 ref = m_cursor->first;
1601 int res = (!m_query) || service->checkFilter(ref, *m_query);
1610 ref.type = eServiceReference::idInvalid;
1615 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1616 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
1620 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
1622 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1623 std::list<eServiceReference> &list = bouquet.m_services;
1624 while (m_cursor != list.end())
1626 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1628 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1629 m_db->m_services.find(ref);
1631 int res = (!m_query) || it == m_db->m_services.end() || !(it->second->isHidden() && it->second->checkFilter(ref, *m_query));
1639 ref.type = eServiceReference::idInvalid;
1644 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1645 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1649 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1651 if (m_cursor != m_list.end())
1656 ref.type = eServiceReference::idInvalid;
1660 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1662 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1664 int x = (a.getDVBNamespace().get() >> 16);
1665 int y = (b.getDVBNamespace().get() >> 16);
1672 std::string aa = a.name, bb = b.name;
1678 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1679 :eDVBDBListQuery(db, source, query)
1681 std::set<unsigned int> found;
1682 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1683 it != m_db->m_services.end(); ++it)
1685 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1688 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1689 if (found.find(dvbnamespace) == found.end())
1691 found.insert(dvbnamespace);
1692 eServiceReferenceDVB ref;
1693 ref.setDVBNamespace(dvbnamespace);
1694 ref.flags=eServiceReference::flagDirectory;
1696 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1698 ref.path=buf+source.path;
1699 unsigned int pos=ref.path.find("FROM");
1700 ref.path.erase(pos);
1701 ref.path+="ORDER BY name";
1702 // eDebug("ref.path now %s", ref.path.c_str());
1703 m_list.push_back(ref);
1705 ref.path=buf+source.path;
1706 pos=ref.path.find("FROM");
1707 ref.path.erase(pos+5);
1708 ref.path+="PROVIDERS ORDER BY name";
1709 // eDebug("ref.path now %s", ref.path.c_str());
1710 m_list.push_back(ref);
1712 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1713 ref.path=buf+source.path;
1714 pos=ref.path.find("FROM");
1715 ref.path.erase(pos);
1716 ref.path+="ORDER BY name";
1717 // eDebug("ref.path now %s", ref.path.c_str());
1718 m_list.push_back(ref);
1722 m_cursor=m_list.begin();
1725 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1726 :eDVBDBListQuery(db, source, query)
1728 std::set<std::string> found;
1729 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1730 it != m_db->m_services.end(); ++it)
1732 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1735 const char *provider_name = it->second->m_provider_name.length() ?
1736 it->second->m_provider_name.c_str() :
1738 if (found.find(std::string(provider_name)) == found.end())
1740 found.insert(std::string(provider_name));
1741 eServiceReferenceDVB ref;
1743 ref.name=provider_name;
1744 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1745 ref.path=buf+source.path;
1746 unsigned int pos = ref.path.find("FROM");
1747 ref.flags=eServiceReference::flagDirectory;
1748 ref.path.erase(pos);
1749 ref.path+="ORDER BY name";
1750 // eDebug("ref.path now %s", ref.path.c_str());
1751 m_list.push_back(ref);
1755 m_cursor=m_list.begin();
1758 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1760 static int decodeType(const std::string &type)
1763 return eDVBChannelQuery::tName;
1764 else if (type == "provider")
1765 return eDVBChannelQuery::tProvider;
1766 else if (type == "type")
1767 return eDVBChannelQuery::tType;
1768 else if (type == "bouquet")
1769 return eDVBChannelQuery::tBouquet;
1770 else if (type == "satellitePosition")
1771 return eDVBChannelQuery::tSatellitePosition;
1772 else if (type == "channelID")
1773 return eDVBChannelQuery::tChannelID;
1774 else if (type == "flags")
1775 return eDVBChannelQuery::tFlags;
1780 /* never, NEVER write a parser in C++! */
1781 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1783 std::list<std::string>::const_iterator end_of_exp;
1791 while (end_of_exp != end)
1792 if (*end_of_exp == ")")
1797 if (end_of_exp == end)
1799 eDebug("expression parse: end of expression while searching for closing brace");
1804 // begin..end_of_exp
1805 int r = parseExpression(res, begin, end_of_exp);
1810 /* we had only one sub expression */
1811 if (end_of_exp == end)
1813 // eDebug("only one sub expression");
1817 /* otherwise we have an operator here.. */
1819 ePtr<eDVBChannelQuery> r2 = res;
1820 res = new eDVBChannelQuery();
1826 if (*end_of_exp == "||")
1827 res->m_type = eDVBChannelQuery::tOR;
1828 else if (*end_of_exp == "&&")
1829 res->m_type = eDVBChannelQuery::tAND;
1832 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1839 return parseExpression(res->m_p2, end_of_exp, end);
1842 // "begin" <op> "end"
1843 std::string type, op, val;
1845 res = new eDVBChannelQuery();
1849 while (begin != end)
1863 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1872 eDebug("malformed query: missing stuff");
1877 res->m_type = decodeType(type);
1879 if (res->m_type == -1)
1881 eDebug("malformed query: invalid type %s", type.c_str());
1888 else if (op == "!=")
1892 eDebug("invalid operator %s", op.c_str());
1897 res->m_string = val;
1899 if (res->m_type == eDVBChannelQuery::tChannelID)
1902 if (sscanf(val.c_str(), "%08x%04x%04x", &ns, &tsid, &onid) == 3)
1903 res->m_channelid = eDVBChannelID(eDVBNamespace(ns), eTransportStreamID(tsid), eOriginalNetworkID(onid));
1905 eDebug("couldn't parse channelid !! format should be hex NNNNNNNNTTTTOOOO (namespace, tsid, onid)");
1908 res->m_int = atoi(val.c_str());
1913 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1915 std::list<std::string> tokens;
1917 std::string current_token;
1918 std::string bouquet_name;
1920 // eDebug("splitting %s....", query.c_str());
1922 const char *splitchars="()";
1923 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1924 while (i <= query.size())
1926 int c = (i < query.size()) ? query[i] : ' ';
1929 int issplit = !!strchr(splitchars, c);
1930 int isaln = isalnum(c);
1931 int iswhite = c == ' ';
1932 int isquot = c == '\"';
1936 iswhite = issplit = 0;
1940 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1942 if (current_token.size())
1943 tokens.push_back(current_token);
1944 current_token.clear();
1947 if (!(iswhite || isquot))
1951 quotemode = !quotemode;
1952 lastsplit = issplit;
1956 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1958 // printf("%s\n", a->c_str());
1961 int sort = eDVBChannelQuery::tName;
1962 /* check for "ORDER BY ..." */
1964 std::list<std::string>::iterator it = tokens.begin();
1965 while (it != tokens.end())
1970 if (it != tokens.end() && *it == "BY")
1973 sort = decodeType(*it);
1978 else if (*it == "FROM")
1981 if (it != tokens.end() && *it == "BOUQUET")
1987 else if (it != tokens.end() && *it == "SATELLITES")
1989 else if (it != tokens.end() && *it == "PROVIDERS")
1993 eDebug("FROM unknown %s", (*it).c_str());
2003 eWarning("ORDER BY .. string invalid.");
2008 // eDebug("sort by %d", sort);
2010 /* now we recursivly parse that. */
2011 int r = parseExpression(res, tokens.begin(), tokens.end());
2013 /* we have an empty (but valid!) expression */
2016 res = new eDVBChannelQuery();
2018 res->m_type = eDVBChannelQuery::tAny;
2024 res->m_bouquet_name = bouquet_name;
2027 // eDebug("return: %d", r);
2031 DEFINE_REF(eDVBChannelQuery);