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 <xmlccwrap/xmlccwrap.h>
9 #include <dvbsi++/service_description_section.h>
10 #include <dvbsi++/descriptor_tag.h>
11 #include <dvbsi++/service_descriptor.h>
12 #include <dvbsi++/satellite_delivery_system_descriptor.h>
14 DEFINE_REF(eDVBService);
16 RESULT eBouquet::addService(const eServiceReference &ref, eServiceReference before)
19 std::find(m_services.begin(), m_services.end(), ref);
20 if ( it != m_services.end() )
24 it = std::find(m_services.begin(), m_services.end(), before);
25 m_services.insert(it, ref);
28 m_services.push_back(ref);
32 RESULT eBouquet::removeService(const eServiceReference &ref)
35 std::find(m_services.begin(), m_services.end(), ref);
36 if ( it == m_services.end() )
42 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
44 if ( pos < 0 || pos >= m_services.size() )
47 list::iterator source=m_services.end();
48 list::iterator dest=m_services.end();
50 for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
52 if (dest == m_services.end() && !--pos)
59 if (dest != m_services.end() && source != m_services.end())
62 if (dest == m_services.end() || source == m_services.end() || source == dest)
64 while (source != dest)
67 std::iter_swap(source++, source);
69 std::iter_swap(source--, source);
74 RESULT eBouquet::flushChanges()
76 FILE *f=fopen((CONFIGDIR"/enigma2/"+m_filename).c_str(), "w");
79 if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
81 for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
83 eServiceReference tmp = *i;
84 std::string str = tmp.path;
85 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
87 if ( i->name.length() )
88 if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
95 eDebug("couldn't write file %s", m_filename.c_str());
99 RESULT eBouquet::setListName(const std::string &name)
101 m_bouquet_name = name;
105 eDVBService::eDVBService()
106 :m_cache(0), m_flags(0)
110 eDVBService::~eDVBService()
115 eDVBService &eDVBService::operator=(const eDVBService &s)
117 m_service_name = s.m_service_name;
118 m_service_name_sort = s.m_service_name_sort;
119 m_provider_name = s.m_provider_name;
122 copyCache(s.m_cache);
126 void eDVBService::genSortName()
128 m_service_name_sort = removeDVBChars(m_service_name);
129 makeUpper(m_service_name_sort);
130 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
131 m_service_name_sort.erase(0, 1);
133 /* put unnamed services at the end, not at the beginning. */
134 if (m_service_name_sort.empty())
135 m_service_name_sort = "\xFF";
138 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
140 if (!ref.name.empty())
141 name = ref.name; // use renamed service name..
142 else if (!m_service_name.empty())
143 name = m_service_name;
149 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
151 return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
154 int eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore, bool simulate)
156 ePtr<eDVBResourceManager> res_mgr;
157 if ( eDVBResourceManager::getInstance( res_mgr ) )
158 eDebug("isPlayble... no res manager!!");
161 eDVBChannelID chid, chid_ignore;
162 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
163 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
164 return res_mgr->canAllocateChannel(chid, chid_ignore, simulate);
169 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
172 switch (query.m_type)
174 case eDVBChannelQuery::tName:
175 res = m_service_name_sort.find(query.m_string) != std::string::npos;
177 case eDVBChannelQuery::tProvider:
178 res = m_provider_name.find(query.m_string) != std::string::npos;
180 case eDVBChannelQuery::tType:
181 res = ref.getServiceType() == query.m_int;
183 case eDVBChannelQuery::tBouquet:
186 case eDVBChannelQuery::tSatellitePosition:
187 res = ((unsigned int)ref.getDVBNamespace().get())>>16 == (unsigned int)query.m_int;
189 case eDVBChannelQuery::tFlags:
190 res = (m_flags & query.m_int) == query.m_int;
192 case eDVBChannelQuery::tChannelID:
195 ref.getChannelID(chid);
196 res = chid == query.m_channelid;
199 case eDVBChannelQuery::tAND:
200 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
202 case eDVBChannelQuery::tOR:
203 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
205 case eDVBChannelQuery::tAny:
216 bool eDVBService::cacheEmpty()
219 for (int i=0; i < cacheMax; ++i)
220 if (m_cache[i] != -1)
225 void eDVBService::initCache()
227 m_cache = new int[cacheMax];
228 memset(m_cache, -1, sizeof(int) * cacheMax);
231 void eDVBService::copyCache(int *source)
236 m_cache = new int[cacheMax];
237 memcpy(m_cache, source, cacheMax * sizeof(int));
246 int eDVBService::getCacheEntry(cacheID id)
248 if (id >= cacheMax || !m_cache)
253 void eDVBService::setCacheEntry(cacheID id, int pid)
263 void eDVBDB::reloadServicelist()
265 loadServicelist(CONFIGDIR"/enigma2/lamedb");
268 void eDVBDB::parseServiceData(ePtr<eDVBService> s, std::string str)
270 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
272 size_t c=str.find(',');
275 if (c == std::string::npos)
281 v=str.substr(2, c-2);
284 // eDebug("%c ... %s", p, v.c_str());
286 s->m_provider_name=v;
289 sscanf(v.c_str(), "%x", &s->m_flags);
293 sscanf(v.c_str(), "%02d%x", &cid, &val);
294 s->setCacheEntry((eDVBService::cacheID)cid,val);
298 sscanf(v.c_str(), "%04x", &val);
299 s->m_ca.push_front((uint16_t)val);
304 /* THIS CODE IS BAD. it should be replaced by somethine better. */
305 void eDVBDB::loadServicelist(const char *file)
307 eDebug("---- opening lame channel db");
308 FILE *f=fopen(file, "rt");
309 if (!f && strcmp(file, CONFIGDIR"/enigma2/lamedb") == 0)
312 if ( !stat("lamedb", &s) )
314 if ( !stat(CONFIGDIR"/enigma2", &s) )
316 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
324 if ((!fgets(line, 256, f)) || sscanf(line, "eDVB services /%d/", &version) != 1)
326 eDebug("not a valid servicefile");
330 eDebug("reading services (version %d)", version);
331 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
333 eDebug("services invalid, no transponders");
338 // clear all transponders
342 if (!fgets(line, 256, f))
344 if (!strcmp(line, "end\n"))
346 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
347 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
348 if (original_network_id == -1)
350 eDVBChannelID channelid = eDVBChannelID(
351 eDVBNamespace(dvb_namespace),
352 eTransportStreamID(transport_stream_id),
353 eOriginalNetworkID(original_network_id));
355 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
359 if (!strcmp(line, "/\n"))
363 eDVBFrontendParametersSatellite sat;
364 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion,
366 system=eDVBFrontendParametersSatellite::System_DVB_S,
367 modulation=eDVBFrontendParametersSatellite::Modulation_QPSK,
368 rolloff=eDVBFrontendParametersSatellite::RollOff_alpha_0_35,
369 pilot=eDVBFrontendParametersSatellite::Pilot_Unknown;
371 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);
373 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);
374 sat.frequency = frequency;
375 sat.symbol_rate = symbol_rate;
376 sat.polarisation = polarisation;
378 sat.orbital_position =
379 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
380 sat.inversion = inversion;
382 sat.modulation = modulation;
383 sat.rolloff = rolloff;
385 feparm->setDVBS(sat);
386 feparm->setFlags(flags);
387 } else if (line[1]=='t')
389 eDVBFrontendParametersTerrestrial ter;
390 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion, flags=0;
391 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);
392 ter.frequency = frequency;
393 ter.bandwidth = bandwidth;
394 ter.code_rate_HP = code_rate_HP;
395 ter.code_rate_LP = code_rate_LP;
396 ter.modulation = modulation;
397 ter.transmission_mode = transmission_mode;
398 ter.guard_interval = guard_interval;
399 ter.hierarchy = hierarchy;
400 ter.inversion = inversion;
401 feparm->setDVBT(ter);
402 feparm->setFlags(flags);
403 } else if (line[1]=='c')
405 eDVBFrontendParametersCable cab;
406 int frequency, symbol_rate,
407 inversion=eDVBFrontendParametersCable::Inversion_Unknown,
408 modulation=eDVBFrontendParametersCable::Modulation_Auto,
409 fec_inner=eDVBFrontendParametersCable::FEC_Auto,
411 sscanf(line+3, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner, &flags);
412 cab.frequency = frequency;
413 cab.fec_inner = fec_inner;
414 cab.inversion = inversion;
415 cab.symbol_rate = symbol_rate;
416 cab.modulation = modulation;
417 feparm->setDVBC(cab);
418 feparm->setFlags(flags);
421 addChannelToList(channelid, feparm);
424 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
426 eDebug("services invalid, no services");
430 // clear all services
436 if (!fgets(line, 256, f))
438 if (!strcmp(line, "end\n"))
441 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
442 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
443 if (service_number == -1)
445 ePtr<eDVBService> s = new eDVBService;
446 eServiceReferenceDVB ref =
447 eServiceReferenceDVB(
448 eDVBNamespace(dvb_namespace),
449 eTransportStreamID(transport_stream_id),
450 eOriginalNetworkID(original_network_id),
451 eServiceID(service_id),
456 line[strlen(line)-1]=0;
458 s->m_service_name = line;
463 line[strlen(line)-1]=0;
464 if (line[1]!=':') // old ... (only service_provider)
465 s->m_provider_name=line;
467 parseServiceData(s, line);
471 eDebug("loaded %d services", count);
476 void eDVBDB::saveServicelist(const char *file)
478 eDebug("---- saving lame channel db");
479 FILE *f=fopen(file, "w");
480 int channels=0, services=0;
482 eFatal("couldn't save lame channel db!");
483 fprintf(f, "eDVB services /4/\n");
484 fprintf(f, "transponders\n");
485 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
486 i != m_channels.end(); ++i)
488 const eDVBChannelID &chid = i->first;
489 const channel &ch = i->second;
491 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
492 chid.transport_stream_id.get(), chid.original_network_id.get());
493 eDVBFrontendParametersSatellite sat;
494 eDVBFrontendParametersTerrestrial ter;
495 eDVBFrontendParametersCable cab;
496 unsigned int flags; // flagOnlyFree yet..
497 ch.m_frontendParameters->getFlags(flags);
498 if (!ch.m_frontendParameters->getDVBS(sat))
500 if (sat.system == eDVBFrontendParametersSatellite::System_DVB_S2)
502 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d\n",
503 sat.frequency, sat.symbol_rate,
504 sat.polarisation, sat.fec,
505 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
515 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d\n",
516 sat.frequency, sat.symbol_rate,
517 sat.polarisation, sat.fec,
518 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
519 sat.inversion, flags);
522 else if (!ch.m_frontendParameters->getDVBT(ter))
524 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d:%d\n",
525 ter.frequency, ter.bandwidth, ter.code_rate_HP,
526 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
527 ter.guard_interval, ter.hierarchy, ter.inversion, flags);
529 else if (!ch.m_frontendParameters->getDVBC(cab))
531 fprintf(f, "\tc %d:%d:%d:%d:%d:%d\n",
532 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner, flags);
537 fprintf(f, "end\nservices\n");
539 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
540 i != m_services.end(); ++i)
542 const eServiceReferenceDVB &s = i->first;
543 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
544 s.getServiceID().get(), s.getDVBNamespace().get(),
545 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
549 fprintf(f, "%s\n", i->second->m_service_name.c_str());
551 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
554 for (int x=0; x < eDVBService::cacheMax; ++x)
556 int entry = i->second->getCacheEntry((eDVBService::cacheID)x);
558 fprintf(f, ",c:%02d%04x", x, entry);
561 // write cached ca pids
562 for (CAID_LIST::const_iterator ca(i->second->m_ca.begin());
563 ca != i->second->m_ca.end(); ++ca)
564 fprintf(f, ",C:%04x", *ca);
566 if (i->second->m_flags)
567 fprintf(f, ",f:%x", i->second->m_flags);
572 fprintf(f, "end\nHave a lot of bugs!\n");
573 eDebug("saved %d channels and %d services!", channels, services);
577 void eDVBDB::saveServicelist()
579 saveServicelist(CONFIGDIR"/enigma2/lamedb");
582 void eDVBDB::loadBouquet(const char *path)
584 std::string bouquet_name = path;
585 if (!bouquet_name.length())
587 eDebug("Bouquet load failed.. no path given..");
590 size_t pos = bouquet_name.rfind('/');
591 if ( pos != std::string::npos )
592 bouquet_name.erase(0, pos+1);
593 if (bouquet_name.empty())
595 eDebug("Bouquet load failed.. no filename given..");
598 eBouquet &bouquet = m_bouquets[bouquet_name];
599 bouquet.m_filename = bouquet_name;
600 std::list<eServiceReference> &list = bouquet.m_services;
603 std::string p = CONFIGDIR"/enigma2/";
605 eDebug("loading bouquet... %s", p.c_str());
606 FILE *fp=fopen(p.c_str(), "rt");
611 if ( !stat(path, &s) )
613 rename(path, p.c_str() );
617 eDebug("failed to open.");
618 if ( strstr(path, "bouquets.tv") )
620 eDebug("recreate bouquets.tv");
621 bouquet.m_bouquet_name="Bouquets (TV)";
622 bouquet.flushChanges();
624 else if ( strstr(path, "bouquets.radio") )
626 eDebug("recreate bouquets.radio");
627 bouquet.m_bouquet_name="Bouquets (Radio)";
628 bouquet.flushChanges();
633 bool read_descr=false;
634 eServiceReference *e = NULL;
637 if (!fgets(line, 256, fp))
639 line[strlen(line)-1]=0;
640 if (strlen(line) && line[strlen(line)-1]=='\r')
641 line[strlen(line)-1]=0;
646 if (!strncmp(line, "#SERVICE", 8))
648 int offs = line[8] == ':' ? 10 : 9;
649 eServiceReference tmp(line+offs);
650 if (tmp.type != eServiceReference::idDVB)
652 eDebug("only DVB Bouquets supported");
655 if ( tmp.flags&eServiceReference::canDescent )
657 size_t pos = tmp.path.rfind('/');
659 std::string path = tmp.path;
660 if ( pos != std::string::npos )
661 path.erase(0, pos+1);
664 eDebug("Bouquet load failed.. no filename given..");
667 pos = path.find("FROM BOUQUET ");
668 if (pos != std::string::npos)
670 char endchr = path[pos+13];
673 eDebug("ignore invalid bouquet '%s' (only \" are allowed)",
674 tmp.toString().c_str());
677 char *beg = &path[pos+14];
678 char *end = strchr(beg, endchr);
679 path.assign(beg, end - beg);
683 snprintf(buf, 256, "FROM BOUQUET \"%s\" ORDER BY bouquet", path.c_str());
686 loadBouquet(path.c_str());
693 else if (read_descr && !strncmp(line, "#DESCRIPTION", 12))
695 int offs = line[12] == ':' ? 14 : 13;
699 else if (!strncmp(line, "#NAME ", 6))
700 bouquet.m_bouquet_name=line+6;
705 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
708 void eDVBDB::reloadBouquets()
711 loadBouquet("bouquets.tv");
712 loadBouquet("bouquets.radio");
713 // create default bouquets when missing
714 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
716 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
717 b.m_filename = "userbouquet.favourites.tv";
718 b.m_bouquet_name = "Favourites (TV)";
720 eServiceReference ref;
721 memset(ref.data, 0, sizeof(ref.data));
725 ref.path="FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
726 eBouquet &parent = m_bouquets["bouquets.tv"];
727 parent.m_services.push_back(ref);
728 parent.flushChanges();
730 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
732 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
733 b.m_filename = "userbouquet.favourites.radio";
734 b.m_bouquet_name = "Favourites (Radio)";
736 eServiceReference ref;
737 memset(ref.data, 0, sizeof(ref.data));
741 ref.path="FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
742 eBouquet &parent = m_bouquets["bouquets.radio"];
743 parent.m_services.push_back(ref);
744 parent.flushChanges();
748 eDVBDB *eDVBDB::instance;
750 using namespace xmlcc;
758 PyObject *eDVBDB::readSatellites(ePyObject sat_list, ePyObject sat_dict, ePyObject tp_dict)
760 if (!PyDict_Check(tp_dict)) {
761 PyErr_SetString(PyExc_StandardError,
763 eDebug("arg 2 is not a python dict");
766 else if (!PyDict_Check(sat_dict))
768 PyErr_SetString(PyExc_StandardError,
770 eDebug("arg 1 is not a python dict");
773 else if (!PyList_Check(sat_list))
775 PyErr_SetString(PyExc_StandardError,
777 eDebug("arg 0 is not a python list");
781 tree.setFilename("/etc/tuxbox/satellites.xml");
783 Element *root = tree.getRoot();
786 eDebug("couldn't open /etc/tuxbox/satellites.xml!!");
790 int tmp, *dest = NULL,
791 modulation, system, freq, sr, pol, fec, inv, pilot, rolloff, tsid, onid;
795 const ElementList &root_elements = root->getElementList();
796 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
798 // eDebug("element: %s", (*it)->name().c_str());
799 const Element *el = *it;
800 const ElementList &sat_elements = el->getElementList();
801 const AttributeList &sat_attributes = el->getAttributeList();
805 for (AttributeConstIterator it(sat_attributes.begin()); it != sat_attributes.end(); ++it)
807 // eDebug("\tattr: %s", at->name().c_str());
811 sat_name = PyString_FromString(at->value().c_str());
812 else if (name == "flags")
814 tmp = strtol(at->value().c_str(), &end_ptr, 10);
816 sat_flags = PyInt_FromLong(tmp);
818 else if (name == "position")
820 tmp = strtol(at->value().c_str(), &end_ptr, 10);
825 sat_pos = PyInt_FromLong(tmp);
829 if (sat_pos && sat_name)
831 ePyObject tplist = PyList_New(0);
832 ePyObject tuple = PyTuple_New(3);
834 sat_flags = PyInt_FromLong(0);
835 PyTuple_SET_ITEM(tuple, 0, sat_pos);
836 PyTuple_SET_ITEM(tuple, 1, sat_name);
837 PyTuple_SET_ITEM(tuple, 2, sat_flags);
838 PyList_Append(sat_list, tuple);
840 PyDict_SetItem(sat_dict, sat_pos, sat_name);
841 PyDict_SetItem(tp_dict, sat_pos, tplist);
842 for (ElementConstIterator it(sat_elements.begin()); it != sat_elements.end(); ++it)
844 //eDebug("\telement: %s", (*it)->name().c_str());
845 const AttributeList &tp_attributes = (*it)->getAttributeList();
846 AttributeConstIterator end = tp_attributes.end();
847 modulation = eDVBFrontendParametersSatellite::Modulation_QPSK;
848 system = eDVBFrontendParametersSatellite::System_DVB_S;
852 fec = eDVBFrontendParametersSatellite::FEC_Auto;
853 inv = eDVBFrontendParametersSatellite::Inversion_Unknown;
854 pilot = eDVBFrontendParametersSatellite::Pilot_Unknown;
855 rolloff = eDVBFrontendParametersSatellite::RollOff_alpha_0_35;
859 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
862 //eDebug("\t\tattr: %s", at->name().c_str());
864 if (name == "modulation") dest = &modulation;
865 else if (name == "system") dest = &system;
866 else if (name == "frequency") dest = &freq;
867 else if (name == "symbol_rate") dest = &sr;
868 else if (name == "polarization") dest = &pol;
869 else if (name == "fec_inner") dest = &fec;
870 else if (name == "inversion") dest = &inv;
871 else if (name == "rolloff") dest = &rolloff;
872 else if (name == "pilot") dest = &pilot;
873 else if (name == "tsid") dest = &tsid;
874 else if (name == "onid") dest = &onid;
877 //eDebug("\t\t\tvalue: %s", at->value().c_str());
878 tmp = strtol(at->value().c_str(), &end_ptr, 10);
883 if (freq && sr && pol != -1)
885 tuple = PyTuple_New(12);
886 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(0));
887 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
888 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
889 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(pol));
890 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
891 PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(system));
892 PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(modulation));
893 PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(inv));
894 PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(rolloff));
895 PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(pilot));
896 PyTuple_SET_ITEM(tuple, 10, PyInt_FromLong(tsid));
897 PyTuple_SET_ITEM(tuple, 11, PyInt_FromLong(onid));
898 PyList_Append(tplist, tuple);
911 Py_DECREF(sat_flags);
918 PyObject *eDVBDB::readCables(ePyObject cab_list, ePyObject tp_dict)
920 if (!PyDict_Check(tp_dict)) {
921 PyErr_SetString(PyExc_StandardError,
923 eDebug("arg 1 is not a python dict");
926 else if (!PyList_Check(cab_list))
928 PyErr_SetString(PyExc_StandardError,
930 eDebug("arg 0 is not a python list");
934 tree.setFilename("/etc/tuxbox/cables.xml");
936 Element *root = tree.getRoot();
939 eDebug("couldn't open /etc/tuxbox/cables.xml!!");
945 modulation, fec, freq, sr;
948 const ElementList &root_elements = root->getElementList();
949 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
951 // eDebug("element: %s", el->name().c_str());
952 const Element *el = *it;
953 const ElementList &cab_elements = el->getElementList();
954 const AttributeList &cab_attributes = el->getAttributeList();
957 for (AttributeConstIterator it(cab_attributes.begin()); it != cab_attributes.end(); ++it)
959 // eDebug("\tattr: %s", at->name().c_str());
963 cab_name = PyString_FromString(at->value().c_str());
964 else if (name == "flags")
966 tmp = strtol(at->value().c_str(), &end_ptr, 10);
968 cab_flags = PyInt_FromLong(tmp);
973 ePyObject tplist = PyList_New(0);
974 ePyObject tuple = PyTuple_New(2);
976 cab_flags = PyInt_FromLong(0);
977 PyTuple_SET_ITEM(tuple, 0, cab_name);
978 PyTuple_SET_ITEM(tuple, 1, cab_flags);
979 PyList_Append(cab_list, tuple);
981 PyDict_SetItem(tp_dict, cab_name, tplist);
982 for (ElementConstIterator it(cab_elements.begin()); it != cab_elements.end(); ++it)
984 // eDebug("\telement: %s", (*it)->name().c_str());
985 const AttributeList &tp_attributes = (*it)->getAttributeList();
986 AttributeConstIterator end = tp_attributes.end();
987 modulation = eDVBFrontendParametersCable::Modulation_QAM64;
988 fec = eDVBFrontendParametersCable::FEC_Auto;
991 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
993 // eDebug("\t\tattr: %s", at->name().c_str());
997 if (name == "modulation") dest = &modulation;
998 else if (name == "frequency") dest = &freq;
999 else if (name == "symbol_rate") dest = &sr;
1000 else if (name == "fec_inner") dest = &fec;
1003 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1010 while (freq > 999999)
1012 tuple = PyTuple_New(5);
1013 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(1));
1014 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
1015 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
1016 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(modulation));
1017 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
1018 PyList_Append(tplist, tuple);
1025 Py_DECREF(cab_flags);
1031 PyObject *eDVBDB::readTerrestrials(ePyObject ter_list, ePyObject tp_dict)
1033 if (!PyDict_Check(tp_dict)) {
1034 PyErr_SetString(PyExc_StandardError,
1036 eDebug("arg 1 is not a python dict");
1039 else if (!PyList_Check(ter_list))
1041 PyErr_SetString(PyExc_StandardError,
1043 eDebug("arg 0 is not a python list");
1047 tree.setFilename("/etc/tuxbox/terrestrial.xml");
1049 Element *root = tree.getRoot();
1052 eDebug("couldn't open /etc/tuxbox/terrestrial.xml!!");
1053 Py_INCREF(Py_False);
1056 const Attribute *at;
1059 freq, bw, constellation, crh = 5, crl = 5, guard = 4, transm, hierarchy, inv = 2;
1061 const ElementList &root_elements = root->getElementList();
1062 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
1064 // eDebug("element: %s", el->name().c_str());
1065 const Element *el = *it;
1066 const ElementList &ter_elements = el->getElementList();
1067 const AttributeList &ter_attributes = el->getAttributeList();
1069 ePyObject ter_flags;
1070 for (AttributeConstIterator it(ter_attributes.begin()); it != ter_attributes.end(); ++it)
1072 // eDebug("\tattr: %s", at->name().c_str());
1076 ter_name = PyString_FromString(at->value().c_str());
1077 else if (name == "flags")
1079 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1081 ter_flags = PyInt_FromLong(tmp);
1086 ePyObject tplist = PyList_New(0);
1087 ePyObject tuple = PyTuple_New(2);
1089 ter_flags = PyInt_FromLong(0);
1090 PyTuple_SET_ITEM(tuple, 0, ter_name);
1091 PyTuple_SET_ITEM(tuple, 1, ter_flags);
1092 PyList_Append(ter_list, tuple);
1094 PyDict_SetItem(tp_dict, ter_name, tplist);
1095 for (ElementConstIterator it(ter_elements.begin()); it != ter_elements.end(); ++it)
1097 // eDebug("\telement: %s", (*it)->name().c_str());
1098 const AttributeList &tp_attributes = (*it)->getAttributeList();
1099 AttributeConstIterator end = tp_attributes.end();
1101 bw = eDVBFrontendParametersTerrestrial::Bandwidth_Auto;
1102 constellation = eDVBFrontendParametersTerrestrial::Modulation_Auto;
1103 crh = eDVBFrontendParametersTerrestrial::FEC_Auto;
1104 crl = eDVBFrontendParametersTerrestrial::FEC_Auto;
1105 guard = eDVBFrontendParametersTerrestrial::GuardInterval_Auto;
1106 transm = eDVBFrontendParametersTerrestrial::TransmissionMode_Auto;
1107 hierarchy = eDVBFrontendParametersTerrestrial::Hierarchy_Auto;
1108 inv = eDVBFrontendParametersTerrestrial::Inversion_Unknown;
1109 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
1111 // eDebug("\t\tattr: %s", at->name().c_str());
1115 if (name == "centre_frequency") dest = &freq;
1116 else if (name == "bandwidth") dest = &bw;
1117 else if (name == "constellation") dest = &constellation;
1118 else if (name == "code_rate_hp") dest = &crh;
1119 else if (name == "code_rate_lp") dest = &crl;
1120 else if (name == "guard_interval") dest = &guard;
1121 else if (name == "transmission_mode") dest = &transm;
1122 else if (name == "hierarchy_information") dest = &hierarchy;
1123 else if (name == "inversion") dest = &inv;
1126 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1133 if (crh > 5) // our terrestrial.xml is buggy... 6 for AUTO
1135 if (crl > 5) // our terrestrial.xml is buggy... 6 for AUTO
1137 tuple = PyTuple_New(10);
1138 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1139 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
1140 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(bw));
1141 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(constellation));
1142 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(crh));
1143 PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(crl));
1144 PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(guard));
1145 PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(transm));
1146 PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(hierarchy));
1147 PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(inv));
1148 PyList_Append(tplist, tuple);
1155 Py_DECREF(ter_flags);
1166 RESULT eDVBDB::removeService(const eServiceReference &ref)
1168 if (ref.type == eServiceReference::idDVB)
1170 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1171 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1172 if (it != m_services.end())
1174 m_services.erase(it);
1181 RESULT eDVBDB::removeServices(int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1183 return removeServices(eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1186 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
1190 eTransportStreamID eTsid;
1191 eOriginalNetworkID eOnid;
1192 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1193 std::set<eDVBChannelID> removed_chids;
1194 while (it != m_channels.end())
1196 const eDVBChannelID &ch = it->first;
1199 it->second.m_frontendParameters->getSystem(system);
1200 if ( system == iDVBFrontend::feSatellite )
1202 eDVBFrontendParametersSatellite sat;
1203 it->second.m_frontendParameters->getDVBS(sat);
1204 if ((unsigned int)sat.orbital_position != orbpos)
1207 if ( remove && chid.dvbnamespace != eNs ) // namespace given?
1209 if ( system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000 )
1211 else if ( system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000 )
1213 else if ( chid.dvbnamespace != ch.dvbnamespace )
1216 else if ( system == iDVBFrontend::feCable || system == iDVBFrontend::feTerrestrial )
1218 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1220 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1224 eDebug("remove %08x %04x %04x",
1225 ch.dvbnamespace.get(),
1226 ch.original_network_id.get(),
1227 ch.transport_stream_id.get());
1228 removed_chids.insert(it->first);
1229 m_channels.erase(it++);
1234 if (!removed_chids.empty())
1236 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1237 while(service != m_services.end())
1240 service->first.getChannelID(chid);
1241 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1242 if (it != removed_chids.end())
1243 m_services.erase(service++);
1252 RESULT eDVBDB::removeServices(iDVBFrontendParameters *feparm)
1255 std::set<eDVBChannelID> removed_chids;
1256 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1257 while (it != m_channels.end())
1260 if (!feparm->calculateDifference(&(*it->second.m_frontendParameters), diff, false))
1264 removed_chids.insert(it->first);
1265 m_channels.erase(it++);
1273 if (!removed_chids.empty())
1275 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1276 while(service != m_services.end())
1279 service->first.getChannelID(chid);
1280 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1281 if (it != removed_chids.end())
1282 m_services.erase(service++);
1291 RESULT eDVBDB::addFlag(const eServiceReference &ref, unsigned int flagmask)
1293 if (ref.type == eServiceReference::idDVB)
1295 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1296 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1297 if (it != m_services.end())
1298 it->second->m_flags |= ~flagmask;
1304 RESULT eDVBDB::removeFlag(const eServiceReference &ref, unsigned int flagmask)
1306 if (ref.type == eServiceReference::idDVB)
1308 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1309 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1310 if (it != m_services.end())
1311 it->second->m_flags &= ~flagmask;
1317 RESULT eDVBDB::removeFlags(unsigned int flagmask, int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1319 return removeFlags(flagmask, eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1322 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
1325 eTransportStreamID eTsid;
1326 eOriginalNetworkID eOnid;
1327 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1328 std::set<eDVBChannelID> removed_chids;
1329 while (it != m_channels.end())
1331 const eDVBChannelID &ch = it->first;
1334 it->second.m_frontendParameters->getSystem(system);
1335 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
1337 eDVBFrontendParametersSatellite sat;
1338 it->second.m_frontendParameters->getDVBS(sat);
1339 if ((unsigned int)sat.orbital_position != orbpos)
1342 if ( remove && chid.dvbnamespace != eNs )
1344 if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1346 else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1348 else if ( chid.dvbnamespace != ch.dvbnamespace )
1351 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1353 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1356 removed_chids.insert(it->first);
1359 if (!removed_chids.empty())
1361 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1362 while(service != m_services.end())
1365 service->first.getChannelID(chid);
1366 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1367 if (it != removed_chids.end())
1368 service->second->m_flags &= ~flagmask;
1375 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
1378 std::map<eDVBChannelID, channel>::iterator it = m_channels.find(id);
1380 ch.m_frontendParameters = feparm;
1381 if (it != m_channels.end())
1384 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
1388 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
1390 m_channels.erase(id);
1394 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
1396 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
1397 if (i == m_channels.end())
1402 parm = i->second.m_frontendParameters;
1406 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
1408 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
1409 if (it == m_services.end())
1410 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
1414 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
1416 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
1417 i = m_services.find(reference);
1418 if (i == m_services.end())
1423 service = i->second;
1427 RESULT eDVBDB::flush()
1433 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
1435 std::string str = ref.path;
1438 eDebug("getBouquet failed.. no path given!");
1441 size_t pos = str.find("FROM BOUQUET \"");
1442 if ( pos != std::string::npos )
1444 str.erase(0, pos+14);
1445 pos = str.find('"');
1446 if ( pos != std::string::npos )
1453 eDebug("getBouquet failed.. couldn't parse bouquet name");
1456 std::map<std::string, eBouquet>::iterator i =
1457 m_bouquets.find(str);
1458 if (i == m_bouquets.end())
1463 bouquet = &i->second;
1467 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
1469 if ( source.path.find("FROM") != std::string::npos )
1471 if ( source.path.find("BOUQUET") != std::string::npos )
1472 query = new eDVBDBBouquetQuery(this, source, q);
1473 else if ( source.path.find("SATELLITES") != std::string::npos )
1474 query = new eDVBDBSatellitesQuery(this, source, q);
1475 else if ( source.path.find("PROVIDERS") != std::string::npos )
1476 query = new eDVBDBProvidersQuery(this, source, q);
1478 eFatal("invalid query %s", source.toString().c_str());
1481 query = new eDVBDBQuery(this, source, q);
1485 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
1487 eServiceID Sid(sid);
1488 eTransportStreamID Tsid(tsid);
1489 eOriginalNetworkID Onid(onid);
1490 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
1491 sit != m_services.end(); ++sit)
1493 if (sit->first.getTransportStreamID() == Tsid &&
1494 sit->first.getOriginalNetworkID() == Onid &&
1495 sit->first.getServiceID() == Sid)
1498 return eServiceReference();
1501 DEFINE_REF(eDVBDBQueryBase);
1503 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1504 :m_db(db), m_query(query), m_source(source)
1508 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1510 ePtr<eDVBService> a_service, b_service;
1511 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
1513 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
1515 if (a.name.empty() && m_db->getService(a, a_service))
1517 if (b.name.empty() && m_db->getService(b, b_service))
1523 case eDVBChannelQuery::tName:
1527 return a_service->m_service_name_sort < b_service->m_service_name_sort;
1530 std::string str = b.name;
1532 return a_service->m_service_name_sort < str;
1537 std::string str = a.name;
1539 return str < b_service->m_service_name_sort;
1543 std::string aa = a.name, bb = b.name;
1548 case eDVBChannelQuery::tProvider:
1549 return a_service->m_provider_name < b_service->m_provider_name;
1550 case eDVBChannelQuery::tType:
1551 return a.getServiceType() < b.getServiceType();
1552 case eDVBChannelQuery::tBouquet:
1554 case eDVBChannelQuery::tSatellitePosition:
1555 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
1562 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1563 :eDVBDBQueryBase(db, source, query)
1565 m_cursor = m_db->m_services.begin();
1568 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
1570 while (m_cursor != m_db->m_services.end())
1572 ePtr<eDVBService> service = m_cursor->second;
1573 if (service->isHidden())
1577 ref = m_cursor->first;
1579 int res = (!m_query) || service->checkFilter(ref, *m_query);
1588 ref.type = eServiceReference::idInvalid;
1593 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1594 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
1598 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
1600 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1601 std::list<eServiceReference> &list = bouquet.m_services;
1602 while (m_cursor != list.end())
1604 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1606 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1607 m_db->m_services.find(ref);
1609 int res = (!m_query) || it == m_db->m_services.end() || !(it->second->isHidden() && it->second->checkFilter(ref, *m_query));
1617 ref.type = eServiceReference::idInvalid;
1622 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1623 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1627 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1629 if (m_cursor != m_list.end())
1634 ref.type = eServiceReference::idInvalid;
1638 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1640 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1642 int x = (a.getDVBNamespace().get() >> 16);
1643 int y = (b.getDVBNamespace().get() >> 16);
1650 std::string aa = a.name, bb = b.name;
1656 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1657 :eDVBDBListQuery(db, source, query)
1659 std::set<unsigned int> found;
1660 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1661 it != m_db->m_services.end(); ++it)
1663 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1666 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1667 if (found.find(dvbnamespace) == found.end())
1669 found.insert(dvbnamespace);
1670 eServiceReferenceDVB ref;
1671 ref.setDVBNamespace(dvbnamespace);
1672 ref.flags=eServiceReference::flagDirectory;
1674 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1676 ref.path=buf+source.path;
1677 unsigned int pos=ref.path.find("FROM");
1678 ref.path.erase(pos);
1679 ref.path+="ORDER BY name";
1680 // eDebug("ref.path now %s", ref.path.c_str());
1681 m_list.push_back(ref);
1683 ref.path=buf+source.path;
1684 pos=ref.path.find("FROM");
1685 ref.path.erase(pos+5);
1686 ref.path+="PROVIDERS ORDER BY name";
1687 // eDebug("ref.path now %s", ref.path.c_str());
1688 m_list.push_back(ref);
1690 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1691 ref.path=buf+source.path;
1692 pos=ref.path.find("FROM");
1693 ref.path.erase(pos);
1694 ref.path+="ORDER BY name";
1695 // eDebug("ref.path now %s", ref.path.c_str());
1696 m_list.push_back(ref);
1700 m_cursor=m_list.begin();
1703 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1704 :eDVBDBListQuery(db, source, query)
1706 std::set<std::string> found;
1707 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1708 it != m_db->m_services.end(); ++it)
1710 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1713 const char *provider_name = it->second->m_provider_name.length() ?
1714 it->second->m_provider_name.c_str() :
1716 if (found.find(std::string(provider_name)) == found.end())
1718 found.insert(std::string(provider_name));
1719 eServiceReferenceDVB ref;
1721 ref.name=provider_name;
1722 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1723 ref.path=buf+source.path;
1724 unsigned int pos = ref.path.find("FROM");
1725 ref.flags=eServiceReference::flagDirectory;
1726 ref.path.erase(pos);
1727 ref.path+="ORDER BY name";
1728 // eDebug("ref.path now %s", ref.path.c_str());
1729 m_list.push_back(ref);
1733 m_cursor=m_list.begin();
1736 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1738 static int decodeType(const std::string &type)
1741 return eDVBChannelQuery::tName;
1742 else if (type == "provider")
1743 return eDVBChannelQuery::tProvider;
1744 else if (type == "type")
1745 return eDVBChannelQuery::tType;
1746 else if (type == "bouquet")
1747 return eDVBChannelQuery::tBouquet;
1748 else if (type == "satellitePosition")
1749 return eDVBChannelQuery::tSatellitePosition;
1750 else if (type == "channelID")
1751 return eDVBChannelQuery::tChannelID;
1752 else if (type == "flags")
1753 return eDVBChannelQuery::tFlags;
1758 /* never, NEVER write a parser in C++! */
1759 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1761 std::list<std::string>::const_iterator end_of_exp;
1769 while (end_of_exp != end)
1770 if (*end_of_exp == ")")
1775 if (end_of_exp == end)
1777 eDebug("expression parse: end of expression while searching for closing brace");
1782 // begin..end_of_exp
1783 int r = parseExpression(res, begin, end_of_exp);
1788 /* we had only one sub expression */
1789 if (end_of_exp == end)
1791 // eDebug("only one sub expression");
1795 /* otherwise we have an operator here.. */
1797 ePtr<eDVBChannelQuery> r2 = res;
1798 res = new eDVBChannelQuery();
1804 if (*end_of_exp == "||")
1805 res->m_type = eDVBChannelQuery::tOR;
1806 else if (*end_of_exp == "&&")
1807 res->m_type = eDVBChannelQuery::tAND;
1810 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1817 return parseExpression(res->m_p2, end_of_exp, end);
1820 // "begin" <op> "end"
1821 std::string type, op, val;
1823 res = new eDVBChannelQuery();
1827 while (begin != end)
1841 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1850 eDebug("malformed query: missing stuff");
1855 res->m_type = decodeType(type);
1857 if (res->m_type == -1)
1859 eDebug("malformed query: invalid type %s", type.c_str());
1866 else if (op == "!=")
1870 eDebug("invalid operator %s", op.c_str());
1875 res->m_string = val;
1877 if (res->m_type == eDVBChannelQuery::tChannelID)
1880 if (sscanf(val.c_str(), "%08x%04x%04x", &ns, &tsid, &onid) == 3)
1881 res->m_channelid = eDVBChannelID(eDVBNamespace(ns), eTransportStreamID(tsid), eOriginalNetworkID(onid));
1883 eDebug("couldn't parse channelid !! format should be hex NNNNNNNNTTTTOOOO (namespace, tsid, onid)");
1886 res->m_int = atoi(val.c_str());
1891 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1893 std::list<std::string> tokens;
1895 std::string current_token;
1896 std::string bouquet_name;
1898 // eDebug("splitting %s....", query.c_str());
1900 const char *splitchars="()";
1901 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1902 while (i <= query.size())
1904 int c = (i < query.size()) ? query[i] : ' ';
1907 int issplit = !!strchr(splitchars, c);
1908 int isaln = isalnum(c);
1909 int iswhite = c == ' ';
1910 int isquot = c == '\"';
1914 iswhite = issplit = 0;
1918 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1920 if (current_token.size())
1921 tokens.push_back(current_token);
1922 current_token.clear();
1925 if (!(iswhite || isquot))
1929 quotemode = !quotemode;
1930 lastsplit = issplit;
1934 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1936 // printf("%s\n", a->c_str());
1939 int sort = eDVBChannelQuery::tName;
1940 /* check for "ORDER BY ..." */
1942 std::list<std::string>::iterator it = tokens.begin();
1943 while (it != tokens.end())
1948 if (it != tokens.end() && *it == "BY")
1951 sort = decodeType(*it);
1956 else if (*it == "FROM")
1959 if (it != tokens.end() && *it == "BOUQUET")
1965 else if (it != tokens.end() && *it == "SATELLITES")
1967 else if (it != tokens.end() && *it == "PROVIDERS")
1971 eDebug("FROM unknown %s", (*it).c_str());
1981 eWarning("ORDER BY .. string invalid.");
1986 // eDebug("sort by %d", sort);
1988 /* now we recursivly parse that. */
1989 int r = parseExpression(res, tokens.begin(), tokens.end());
1991 /* we have an empty (but valid!) expression */
1994 res = new eDVBChannelQuery();
1996 res->m_type = eDVBChannelQuery::tAny;
2002 res->m_bouquet_name = bouquet_name;
2005 // eDebug("return: %d", r);
2009 DEFINE_REF(eDVBChannelQuery);