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 /* THIS CODE IS BAD. it should be replaced by somethine better. */
269 void eDVBDB::loadServicelist(const char *file)
271 eDebug("---- opening lame channel db");
272 FILE *f=fopen(file, "rt");
273 if (!f && strcmp(file, CONFIGDIR"/enigma2/lamedb") == 0)
276 if ( !stat("lamedb", &s) )
278 if ( !stat(CONFIGDIR"/enigma2", &s) )
280 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
288 if ((!fgets(line, 256, f)) || sscanf(line, "eDVB services /%d/", &version) != 1)
290 eDebug("not a valid servicefile");
294 eDebug("reading services (version %d)", version);
295 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
297 eDebug("services invalid, no transponders");
302 // clear all transponders
306 if (!fgets(line, 256, f))
308 if (!strcmp(line, "end\n"))
310 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
311 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
312 if (original_network_id == -1)
314 eDVBChannelID channelid = eDVBChannelID(
315 eDVBNamespace(dvb_namespace),
316 eTransportStreamID(transport_stream_id),
317 eOriginalNetworkID(original_network_id));
319 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
323 if (!strcmp(line, "/\n"))
327 eDVBFrontendParametersSatellite sat;
328 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion,
330 system=eDVBFrontendParametersSatellite::System_DVB_S,
331 modulation=eDVBFrontendParametersSatellite::Modulation_QPSK,
332 rolloff=eDVBFrontendParametersSatellite::RollOff_alpha_0_35,
333 pilot=eDVBFrontendParametersSatellite::Pilot_Unknown;
335 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);
337 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);
338 sat.frequency = frequency;
339 sat.symbol_rate = symbol_rate;
340 sat.polarisation = polarisation;
342 sat.orbital_position =
343 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
344 sat.inversion = inversion;
346 sat.modulation = modulation;
347 sat.rolloff = rolloff;
349 feparm->setDVBS(sat);
350 feparm->setFlags(flags);
351 } else if (line[1]=='t')
353 eDVBFrontendParametersTerrestrial ter;
354 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion, flags=0;
355 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);
356 ter.frequency = frequency;
357 ter.bandwidth = bandwidth;
358 ter.code_rate_HP = code_rate_HP;
359 ter.code_rate_LP = code_rate_LP;
360 ter.modulation = modulation;
361 ter.transmission_mode = transmission_mode;
362 ter.guard_interval = guard_interval;
363 ter.hierarchy = hierarchy;
364 ter.inversion = inversion;
365 feparm->setDVBT(ter);
366 feparm->setFlags(flags);
367 } else if (line[1]=='c')
369 eDVBFrontendParametersCable cab;
370 int frequency, symbol_rate,
371 inversion=eDVBFrontendParametersCable::Inversion_Unknown,
372 modulation=eDVBFrontendParametersCable::Modulation_Auto,
373 fec_inner=eDVBFrontendParametersCable::FEC_Auto,
375 sscanf(line+3, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner, &flags);
376 cab.frequency = frequency;
377 cab.fec_inner = fec_inner;
378 cab.inversion = inversion;
379 cab.symbol_rate = symbol_rate;
380 cab.modulation = modulation;
381 feparm->setDVBC(cab);
382 feparm->setFlags(flags);
385 addChannelToList(channelid, feparm);
388 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
390 eDebug("services invalid, no services");
394 // clear all services
400 if (!fgets(line, 256, f))
402 if (!strcmp(line, "end\n"))
405 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
406 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
407 if (service_number == -1)
409 ePtr<eDVBService> s = new eDVBService;
410 eServiceReferenceDVB ref =
411 eServiceReferenceDVB(
412 eDVBNamespace(dvb_namespace),
413 eTransportStreamID(transport_stream_id),
414 eOriginalNetworkID(original_network_id),
415 eServiceID(service_id),
420 line[strlen(line)-1]=0;
422 s->m_service_name = line;
427 line[strlen(line)-1]=0;
428 std::string str=line;
430 if (str[1]!=':') // old ... (only service_provider)
432 s->m_provider_name=line;
434 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
436 size_t c=str.find(',');
439 if (c == std::string::npos)
445 v=str.substr(2, c-2);
448 // eDebug("%c ... %s", p, v.c_str());
450 s->m_provider_name=v;
453 sscanf(v.c_str(), "%x", &s->m_flags);
457 sscanf(v.c_str(), "%02d%x", &cid, &val);
458 s->setCacheEntry((eDVBService::cacheID)cid,val);
462 sscanf(v.c_str(), "%04x", &val);
463 s->m_ca.push_front((uint16_t)val);
469 eDebug("loaded %d services", count);
474 void eDVBDB::saveServicelist(const char *file)
476 eDebug("---- saving lame channel db");
477 FILE *f=fopen(file, "w");
478 int channels=0, services=0;
480 eFatal("couldn't save lame channel db!");
481 fprintf(f, "eDVB services /4/\n");
482 fprintf(f, "transponders\n");
483 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
484 i != m_channels.end(); ++i)
486 const eDVBChannelID &chid = i->first;
487 const channel &ch = i->second;
489 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
490 chid.transport_stream_id.get(), chid.original_network_id.get());
491 eDVBFrontendParametersSatellite sat;
492 eDVBFrontendParametersTerrestrial ter;
493 eDVBFrontendParametersCable cab;
494 unsigned int flags; // flagOnlyFree yet..
495 ch.m_frontendParameters->getFlags(flags);
496 if (!ch.m_frontendParameters->getDVBS(sat))
498 if (sat.system == eDVBFrontendParametersSatellite::System_DVB_S2)
500 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d\n",
501 sat.frequency, sat.symbol_rate,
502 sat.polarisation, sat.fec,
503 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
513 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d\n",
514 sat.frequency, sat.symbol_rate,
515 sat.polarisation, sat.fec,
516 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
517 sat.inversion, flags);
520 else if (!ch.m_frontendParameters->getDVBT(ter))
522 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d:%d\n",
523 ter.frequency, ter.bandwidth, ter.code_rate_HP,
524 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
525 ter.guard_interval, ter.hierarchy, ter.inversion, flags);
527 else if (!ch.m_frontendParameters->getDVBC(cab))
529 fprintf(f, "\tc %d:%d:%d:%d:%d:%d\n",
530 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner, flags);
535 fprintf(f, "end\nservices\n");
537 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
538 i != m_services.end(); ++i)
540 const eServiceReferenceDVB &s = i->first;
541 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
542 s.getServiceID().get(), s.getDVBNamespace().get(),
543 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
547 fprintf(f, "%s\n", i->second->m_service_name.c_str());
549 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
552 for (int x=0; x < eDVBService::cacheMax; ++x)
554 int entry = i->second->getCacheEntry((eDVBService::cacheID)x);
556 fprintf(f, ",c:%02d%04x", x, entry);
559 // write cached ca pids
560 for (CAID_LIST::const_iterator ca(i->second->m_ca.begin());
561 ca != i->second->m_ca.end(); ++ca)
562 fprintf(f, ",C:%04x", *ca);
564 if (i->second->m_flags)
565 fprintf(f, ",f:%x", i->second->m_flags);
570 fprintf(f, "end\nHave a lot of bugs!\n");
571 eDebug("saved %d channels and %d services!", channels, services);
575 void eDVBDB::saveServicelist()
577 saveServicelist(CONFIGDIR"/enigma2/lamedb");
580 void eDVBDB::loadBouquet(const char *path)
582 std::string bouquet_name = path;
583 if (!bouquet_name.length())
585 eDebug("Bouquet load failed.. no path given..");
588 size_t pos = bouquet_name.rfind('/');
589 if ( pos != std::string::npos )
590 bouquet_name.erase(0, pos+1);
591 if (bouquet_name.empty())
593 eDebug("Bouquet load failed.. no filename given..");
596 eBouquet &bouquet = m_bouquets[bouquet_name];
597 bouquet.m_filename = bouquet_name;
598 std::list<eServiceReference> &list = bouquet.m_services;
601 std::string p = CONFIGDIR"/enigma2/";
603 eDebug("loading bouquet... %s", p.c_str());
604 FILE *fp=fopen(p.c_str(), "rt");
609 if ( !stat(path, &s) )
611 rename(path, p.c_str() );
615 eDebug("failed to open.");
616 if ( strstr(path, "bouquets.tv") )
618 eDebug("recreate bouquets.tv");
619 bouquet.m_bouquet_name="Bouquets (TV)";
620 bouquet.flushChanges();
622 else if ( strstr(path, "bouquets.radio") )
624 eDebug("recreate bouquets.radio");
625 bouquet.m_bouquet_name="Bouquets (Radio)";
626 bouquet.flushChanges();
631 bool read_descr=false;
632 eServiceReference *e = NULL;
635 if (!fgets(line, 256, fp))
637 line[strlen(line)-1]=0;
638 if (strlen(line) && line[strlen(line)-1]=='\r')
639 line[strlen(line)-1]=0;
644 if (!strncmp(line, "#SERVICE", 8))
646 int offs = line[8] == ':' ? 10 : 9;
647 eServiceReference tmp(line+offs);
648 if (tmp.type != eServiceReference::idDVB)
650 eDebug("only DVB Bouquets supported");
653 if ( tmp.flags&eServiceReference::canDescent )
655 size_t pos = tmp.path.rfind('/');
657 std::string path = tmp.path;
658 if ( pos != std::string::npos )
659 path.erase(0, pos+1);
662 eDebug("Bouquet load failed.. no filename given..");
665 pos = path.find("FROM BOUQUET ");
666 if (pos != std::string::npos)
668 char endchr = path[pos+13];
671 eDebug("ignore invalid bouquet '%s' (only \" are allowed)",
672 tmp.toString().c_str());
675 char *beg = &path[pos+14];
676 char *end = strchr(beg, endchr);
677 path.assign(beg, end - beg);
681 snprintf(buf, 256, "FROM BOUQUET \"%s\" ORDER BY bouquet", path.c_str());
684 loadBouquet(path.c_str());
691 else if (read_descr && !strncmp(line, "#DESCRIPTION", 12))
693 int offs = line[12] == ':' ? 14 : 13;
697 else if (!strncmp(line, "#NAME ", 6))
698 bouquet.m_bouquet_name=line+6;
703 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
706 void eDVBDB::reloadBouquets()
709 loadBouquet("bouquets.tv");
710 loadBouquet("bouquets.radio");
711 // create default bouquets when missing
712 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
714 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
715 b.m_filename = "userbouquet.favourites.tv";
716 b.m_bouquet_name = "Favourites (TV)";
718 eServiceReference ref;
719 memset(ref.data, 0, sizeof(ref.data));
723 ref.path="FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
724 eBouquet &parent = m_bouquets["bouquets.tv"];
725 parent.m_services.push_back(ref);
726 parent.flushChanges();
728 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
730 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
731 b.m_filename = "userbouquet.favourites.radio";
732 b.m_bouquet_name = "Favourites (Radio)";
734 eServiceReference ref;
735 memset(ref.data, 0, sizeof(ref.data));
739 ref.path="FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
740 eBouquet &parent = m_bouquets["bouquets.radio"];
741 parent.m_services.push_back(ref);
742 parent.flushChanges();
746 eDVBDB *eDVBDB::instance;
748 using namespace xmlcc;
756 PyObject *eDVBDB::readSatellites(ePyObject sat_list, ePyObject sat_dict, ePyObject tp_dict)
758 if (!PyDict_Check(tp_dict)) {
759 PyErr_SetString(PyExc_StandardError,
761 eDebug("arg 2 is not a python dict");
764 else if (!PyDict_Check(sat_dict))
766 PyErr_SetString(PyExc_StandardError,
768 eDebug("arg 1 is not a python dict");
771 else if (!PyList_Check(sat_list))
773 PyErr_SetString(PyExc_StandardError,
775 eDebug("arg 0 is not a python list");
779 tree.setFilename("/etc/tuxbox/satellites.xml");
781 Element *root = tree.getRoot();
784 eDebug("couldn't open /etc/tuxbox/satellites.xml!!");
788 int tmp, *dest = NULL,
789 modulation, system, freq, sr, pol, fec, inv, pilot, rolloff, tsid, onid;
793 const ElementList &root_elements = root->getElementList();
794 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
796 // eDebug("element: %s", (*it)->name().c_str());
797 const Element *el = *it;
798 const ElementList &sat_elements = el->getElementList();
799 const AttributeList &sat_attributes = el->getAttributeList();
803 for (AttributeConstIterator it(sat_attributes.begin()); it != sat_attributes.end(); ++it)
805 // eDebug("\tattr: %s", at->name().c_str());
809 sat_name = PyString_FromString(at->value().c_str());
810 else if (name == "flags")
812 tmp = strtol(at->value().c_str(), &end_ptr, 10);
814 sat_flags = PyInt_FromLong(tmp);
816 else if (name == "position")
818 tmp = strtol(at->value().c_str(), &end_ptr, 10);
823 sat_pos = PyInt_FromLong(tmp);
827 if (sat_pos && sat_name)
829 ePyObject tplist = PyList_New(0);
830 ePyObject tuple = PyTuple_New(3);
832 sat_flags = PyInt_FromLong(0);
833 PyTuple_SET_ITEM(tuple, 0, sat_pos);
834 PyTuple_SET_ITEM(tuple, 1, sat_name);
835 PyTuple_SET_ITEM(tuple, 2, sat_flags);
836 PyList_Append(sat_list, tuple);
838 PyDict_SetItem(sat_dict, sat_pos, sat_name);
839 PyDict_SetItem(tp_dict, sat_pos, tplist);
840 for (ElementConstIterator it(sat_elements.begin()); it != sat_elements.end(); ++it)
842 // eDebug("\telement: %s", (*it)->name().c_str());
843 const AttributeList &tp_attributes = (*it)->getAttributeList();
844 AttributeConstIterator end = tp_attributes.end();
845 modulation = 1; // QPSK default
846 system = 0; // DVB-S default
850 fec = 0; // AUTO default
851 inv = 2; // AUTO default
852 pilot = 2; // AUTO default
853 rolloff = 0; // alpha 0.35
857 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
859 //eDebug("\t\tattr: %s", at->name().c_str());
862 if (name == "modulation") dest = &modulation;
863 else if (name == "system") dest = &system;
864 else if (name == "frequency") dest = &freq;
865 else if (name == "symbol_rate") dest = &sr;
866 else if (name == "polarization") dest = &pol;
867 else if (name == "fec_inner") dest = &fec;
868 else if (name == "inversion") dest = &inv;
869 else if (name == "rolloff") dest = &rolloff;
870 else if (name == "pilot") dest = &pilot;
871 else if (name == "tsid") dest = &tsid;
872 else if (name == "onid") dest = &onid;
875 tmp = strtol(at->value().c_str(), &end_ptr, 10);
880 if (freq && sr && pol != -1)
882 tuple = PyTuple_New(12);
883 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(0));
884 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
885 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
886 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(pol));
887 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
888 PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(system));
889 PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(modulation));
890 PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(inv));
891 PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(rolloff));
892 PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(pilot));
893 PyTuple_SET_ITEM(tuple, 10, PyInt_FromLong(tsid));
894 PyTuple_SET_ITEM(tuple, 11, PyInt_FromLong(onid));
895 PyList_Append(tplist, tuple);
908 Py_DECREF(sat_flags);
915 PyObject *eDVBDB::readCables(ePyObject cab_list, ePyObject tp_dict)
917 if (!PyDict_Check(tp_dict)) {
918 PyErr_SetString(PyExc_StandardError,
920 eDebug("arg 1 is not a python dict");
923 else if (!PyList_Check(cab_list))
925 PyErr_SetString(PyExc_StandardError,
927 eDebug("arg 0 is not a python list");
931 tree.setFilename("/etc/tuxbox/cables.xml");
933 Element *root = tree.getRoot();
936 eDebug("couldn't open /etc/tuxbox/cables.xml!!");
942 modulation, fec, freq, sr;
945 const ElementList &root_elements = root->getElementList();
946 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
948 // eDebug("element: %s", el->name().c_str());
949 const Element *el = *it;
950 const ElementList &cab_elements = el->getElementList();
951 const AttributeList &cab_attributes = el->getAttributeList();
954 for (AttributeConstIterator it(cab_attributes.begin()); it != cab_attributes.end(); ++it)
956 // eDebug("\tattr: %s", at->name().c_str());
960 cab_name = PyString_FromString(at->value().c_str());
961 else if (name == "flags")
963 tmp = strtol(at->value().c_str(), &end_ptr, 10);
965 cab_flags = PyInt_FromLong(tmp);
970 ePyObject tplist = PyList_New(0);
971 ePyObject tuple = PyTuple_New(2);
973 cab_flags = PyInt_FromLong(0);
974 PyTuple_SET_ITEM(tuple, 0, cab_name);
975 PyTuple_SET_ITEM(tuple, 1, cab_flags);
976 PyList_Append(cab_list, tuple);
978 PyDict_SetItem(tp_dict, cab_name, tplist);
979 for (ElementConstIterator it(cab_elements.begin()); it != cab_elements.end(); ++it)
981 // eDebug("\telement: %s", (*it)->name().c_str());
982 const AttributeList &tp_attributes = (*it)->getAttributeList();
983 AttributeConstIterator end = tp_attributes.end();
984 modulation = 3; // QAM64 default
985 fec = 0; // AUTO default
988 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
990 // eDebug("\t\tattr: %s", at->name().c_str());
994 if (name == "modulation") dest = &modulation;
995 else if (name == "frequency") dest = &freq;
996 else if (name == "symbol_rate") dest = &sr;
997 else if (name == "fec_inner") dest = &fec;
1000 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1007 while (freq > 999999)
1009 tuple = PyTuple_New(5);
1010 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(1));
1011 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
1012 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
1013 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(modulation));
1014 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
1015 PyList_Append(tplist, tuple);
1022 Py_DECREF(cab_flags);
1028 PyObject *eDVBDB::readTerrestrials(ePyObject ter_list, ePyObject tp_dict)
1030 if (!PyDict_Check(tp_dict)) {
1031 PyErr_SetString(PyExc_StandardError,
1033 eDebug("arg 1 is not a python dict");
1036 else if (!PyList_Check(ter_list))
1038 PyErr_SetString(PyExc_StandardError,
1040 eDebug("arg 0 is not a python list");
1044 tree.setFilename("/etc/tuxbox/terrestrial.xml");
1046 Element *root = tree.getRoot();
1049 eDebug("couldn't open /etc/tuxbox/terrestrial.xml!!");
1050 Py_INCREF(Py_False);
1053 const Attribute *at;
1056 freq, bw, constellation, crh = 5, crl = 5, guard = 4, transm, hierarchy, inv = 2;
1058 const ElementList &root_elements = root->getElementList();
1059 for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
1061 // eDebug("element: %s", el->name().c_str());
1062 const Element *el = *it;
1063 const ElementList &ter_elements = el->getElementList();
1064 const AttributeList &ter_attributes = el->getAttributeList();
1066 ePyObject ter_flags;
1067 for (AttributeConstIterator it(ter_attributes.begin()); it != ter_attributes.end(); ++it)
1069 // eDebug("\tattr: %s", at->name().c_str());
1073 ter_name = PyString_FromString(at->value().c_str());
1074 else if (name == "flags")
1076 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1078 ter_flags = PyInt_FromLong(tmp);
1083 ePyObject tplist = PyList_New(0);
1084 ePyObject tuple = PyTuple_New(2);
1086 ter_flags = PyInt_FromLong(0);
1087 PyTuple_SET_ITEM(tuple, 0, ter_name);
1088 PyTuple_SET_ITEM(tuple, 1, ter_flags);
1089 PyList_Append(ter_list, tuple);
1091 PyDict_SetItem(tp_dict, ter_name, tplist);
1092 for (ElementConstIterator it(ter_elements.begin()); it != ter_elements.end(); ++it)
1094 // eDebug("\telement: %s", (*it)->name().c_str());
1095 const AttributeList &tp_attributes = (*it)->getAttributeList();
1096 AttributeConstIterator end = tp_attributes.end();
1099 constellation = 1; // AUTO
1104 hierarchy = 4; // AUTO
1106 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
1108 // eDebug("\t\tattr: %s", at->name().c_str());
1112 if (name == "centre_frequency") dest = &freq;
1113 else if (name == "bandwidth") dest = &bw;
1114 else if (name == "constellation") dest = &constellation;
1115 else if (name == "code_rate_hp") dest = &crh;
1116 else if (name == "code_rate_lp") dest = &crl;
1117 else if (name == "guard_interval") dest = &guard;
1118 else if (name == "transmission_mode") dest = &transm;
1119 else if (name == "hierarchy_information") dest = &hierarchy;
1120 else if (name == "inversion") dest = &inv;
1123 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1130 if (crh > 5) // our terrestrial.xml is buggy... 6 for AUTO
1132 if (crl > 5) // our terrestrial.xml is buggy... 6 for AUTO
1134 tuple = PyTuple_New(10);
1135 PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1136 PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
1137 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(bw));
1138 PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(constellation));
1139 PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(crh));
1140 PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(crl));
1141 PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(guard));
1142 PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(transm));
1143 PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(hierarchy));
1144 PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(inv));
1145 PyList_Append(tplist, tuple);
1152 Py_DECREF(ter_flags);
1163 RESULT eDVBDB::removeService(const eServiceReference &ref)
1165 if (ref.type == eServiceReference::idDVB)
1167 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1168 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1169 if (it != m_services.end())
1171 m_services.erase(it);
1178 RESULT eDVBDB::removeServices(int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1180 return removeServices(eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1183 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
1187 eTransportStreamID eTsid;
1188 eOriginalNetworkID eOnid;
1189 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1190 std::set<eDVBChannelID> removed_chids;
1191 while (it != m_channels.end())
1193 const eDVBChannelID &ch = it->first;
1196 it->second.m_frontendParameters->getSystem(system);
1197 if ( system == iDVBFrontend::feSatellite )
1199 eDVBFrontendParametersSatellite sat;
1200 it->second.m_frontendParameters->getDVBS(sat);
1201 if ((unsigned int)sat.orbital_position != orbpos)
1204 if ( remove && chid.dvbnamespace != eNs )
1206 if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1208 else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1210 else if ( chid.dvbnamespace != ch.dvbnamespace )
1213 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1215 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1219 eDebug("remove %08x %04x %04x",
1220 ch.dvbnamespace.get(),
1221 ch.original_network_id.get(),
1222 ch.transport_stream_id.get());
1223 removed_chids.insert(it->first);
1224 m_channels.erase(it++);
1229 if (!removed_chids.empty())
1231 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1232 while(service != m_services.end())
1235 service->first.getChannelID(chid);
1236 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1237 if (it != removed_chids.end())
1238 m_services.erase(service++);
1247 RESULT eDVBDB::removeServices(iDVBFrontendParameters *feparm)
1250 std::set<eDVBChannelID> removed_chids;
1251 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1252 while (it != m_channels.end())
1255 if (!feparm->calculateDifference(&(*it->second.m_frontendParameters), diff, false))
1259 removed_chids.insert(it->first);
1260 m_channels.erase(it++);
1268 if (!removed_chids.empty())
1270 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1271 while(service != m_services.end())
1274 service->first.getChannelID(chid);
1275 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1276 if (it != removed_chids.end())
1277 m_services.erase(service++);
1286 RESULT eDVBDB::addFlag(const eServiceReference &ref, unsigned int flagmask)
1288 if (ref.type == eServiceReference::idDVB)
1290 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1291 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1292 if (it != m_services.end())
1293 it->second->m_flags |= ~flagmask;
1299 RESULT eDVBDB::removeFlag(const eServiceReference &ref, unsigned int flagmask)
1301 if (ref.type == eServiceReference::idDVB)
1303 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1304 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1305 if (it != m_services.end())
1306 it->second->m_flags &= ~flagmask;
1312 RESULT eDVBDB::removeFlags(unsigned int flagmask, int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1314 return removeFlags(flagmask, eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1317 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
1320 eTransportStreamID eTsid;
1321 eOriginalNetworkID eOnid;
1322 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1323 std::set<eDVBChannelID> removed_chids;
1324 while (it != m_channels.end())
1326 const eDVBChannelID &ch = it->first;
1329 it->second.m_frontendParameters->getSystem(system);
1330 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
1332 eDVBFrontendParametersSatellite sat;
1333 it->second.m_frontendParameters->getDVBS(sat);
1334 if ((unsigned int)sat.orbital_position != orbpos)
1337 if ( remove && chid.dvbnamespace != eNs )
1339 if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1341 else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1343 else if ( chid.dvbnamespace != ch.dvbnamespace )
1346 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1348 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1351 removed_chids.insert(it->first);
1354 if (!removed_chids.empty())
1356 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1357 while(service != m_services.end())
1360 service->first.getChannelID(chid);
1361 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1362 if (it != removed_chids.end())
1363 service->second->m_flags &= ~flagmask;
1370 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
1373 std::map<eDVBChannelID, channel>::iterator it = m_channels.find(id);
1375 ch.m_frontendParameters = feparm;
1376 if (it != m_channels.end())
1379 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
1383 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
1385 m_channels.erase(id);
1389 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
1391 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
1392 if (i == m_channels.end())
1397 parm = i->second.m_frontendParameters;
1401 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
1403 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
1404 if (it == m_services.end())
1405 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
1409 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
1411 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
1412 i = m_services.find(reference);
1413 if (i == m_services.end())
1418 service = i->second;
1422 RESULT eDVBDB::flush()
1428 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
1430 std::string str = ref.path;
1433 eDebug("getBouquet failed.. no path given!");
1436 size_t pos = str.find("FROM BOUQUET \"");
1437 if ( pos != std::string::npos )
1439 str.erase(0, pos+14);
1440 pos = str.find('"');
1441 if ( pos != std::string::npos )
1448 eDebug("getBouquet failed.. couldn't parse bouquet name");
1451 std::map<std::string, eBouquet>::iterator i =
1452 m_bouquets.find(str);
1453 if (i == m_bouquets.end())
1458 bouquet = &i->second;
1462 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
1464 if ( source.path.find("FROM") != std::string::npos )
1466 if ( source.path.find("BOUQUET") != std::string::npos )
1467 query = new eDVBDBBouquetQuery(this, source, q);
1468 else if ( source.path.find("SATELLITES") != std::string::npos )
1469 query = new eDVBDBSatellitesQuery(this, source, q);
1470 else if ( source.path.find("PROVIDERS") != std::string::npos )
1471 query = new eDVBDBProvidersQuery(this, source, q);
1473 eFatal("invalid query %s", source.toString().c_str());
1476 query = new eDVBDBQuery(this, source, q);
1480 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
1482 eServiceID Sid(sid);
1483 eTransportStreamID Tsid(tsid);
1484 eOriginalNetworkID Onid(onid);
1485 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
1486 sit != m_services.end(); ++sit)
1488 if (sit->first.getTransportStreamID() == Tsid &&
1489 sit->first.getOriginalNetworkID() == Onid &&
1490 sit->first.getServiceID() == Sid)
1493 return eServiceReference();
1496 DEFINE_REF(eDVBDBQueryBase);
1498 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1499 :m_db(db), m_query(query), m_source(source)
1503 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1505 ePtr<eDVBService> a_service, b_service;
1506 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
1508 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
1510 if (a.name.empty() && m_db->getService(a, a_service))
1512 if (b.name.empty() && m_db->getService(b, b_service))
1518 case eDVBChannelQuery::tName:
1522 return a_service->m_service_name_sort < b_service->m_service_name_sort;
1525 std::string str = b.name;
1527 return a_service->m_service_name_sort < str;
1532 std::string str = a.name;
1534 return str < b_service->m_service_name_sort;
1538 std::string aa = a.name, bb = b.name;
1543 case eDVBChannelQuery::tProvider:
1544 return a_service->m_provider_name < b_service->m_provider_name;
1545 case eDVBChannelQuery::tType:
1546 return a.getServiceType() < b.getServiceType();
1547 case eDVBChannelQuery::tBouquet:
1549 case eDVBChannelQuery::tSatellitePosition:
1550 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
1557 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1558 :eDVBDBQueryBase(db, source, query)
1560 m_cursor = m_db->m_services.begin();
1563 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
1565 while (m_cursor != m_db->m_services.end())
1567 ePtr<eDVBService> service = m_cursor->second;
1568 if (service->isHidden())
1572 ref = m_cursor->first;
1574 int res = (!m_query) || service->checkFilter(ref, *m_query);
1583 ref.type = eServiceReference::idInvalid;
1588 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1589 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
1593 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
1595 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1596 std::list<eServiceReference> &list = bouquet.m_services;
1597 while (m_cursor != list.end())
1599 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1601 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1602 m_db->m_services.find(ref);
1604 int res = (!m_query) || it == m_db->m_services.end() || !(it->second->isHidden() && it->second->checkFilter(ref, *m_query));
1612 ref.type = eServiceReference::idInvalid;
1617 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1618 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1622 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1624 if (m_cursor != m_list.end())
1629 ref.type = eServiceReference::idInvalid;
1633 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1635 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1637 int x = (a.getDVBNamespace().get() >> 16);
1638 int y = (b.getDVBNamespace().get() >> 16);
1645 std::string aa = a.name, bb = b.name;
1651 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1652 :eDVBDBListQuery(db, source, query)
1654 std::set<unsigned int> found;
1655 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1656 it != m_db->m_services.end(); ++it)
1658 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1661 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1662 if (found.find(dvbnamespace) == found.end())
1664 found.insert(dvbnamespace);
1665 eServiceReferenceDVB ref;
1666 ref.setDVBNamespace(dvbnamespace);
1667 ref.flags=eServiceReference::flagDirectory;
1669 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1671 ref.path=buf+source.path;
1672 unsigned int pos=ref.path.find("FROM");
1673 ref.path.erase(pos);
1674 ref.path+="ORDER BY name";
1675 // eDebug("ref.path now %s", ref.path.c_str());
1676 m_list.push_back(ref);
1678 ref.path=buf+source.path;
1679 pos=ref.path.find("FROM");
1680 ref.path.erase(pos+5);
1681 ref.path+="PROVIDERS ORDER BY name";
1682 // eDebug("ref.path now %s", ref.path.c_str());
1683 m_list.push_back(ref);
1685 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1686 ref.path=buf+source.path;
1687 pos=ref.path.find("FROM");
1688 ref.path.erase(pos);
1689 ref.path+="ORDER BY name";
1690 // eDebug("ref.path now %s", ref.path.c_str());
1691 m_list.push_back(ref);
1695 m_cursor=m_list.begin();
1698 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1699 :eDVBDBListQuery(db, source, query)
1701 std::set<std::string> found;
1702 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1703 it != m_db->m_services.end(); ++it)
1705 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1708 const char *provider_name = it->second->m_provider_name.length() ?
1709 it->second->m_provider_name.c_str() :
1711 if (found.find(std::string(provider_name)) == found.end())
1713 found.insert(std::string(provider_name));
1714 eServiceReferenceDVB ref;
1716 ref.name=provider_name;
1717 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1718 ref.path=buf+source.path;
1719 unsigned int pos = ref.path.find("FROM");
1720 ref.flags=eServiceReference::flagDirectory;
1721 ref.path.erase(pos);
1722 ref.path+="ORDER BY name";
1723 // eDebug("ref.path now %s", ref.path.c_str());
1724 m_list.push_back(ref);
1728 m_cursor=m_list.begin();
1731 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1733 static int decodeType(const std::string &type)
1736 return eDVBChannelQuery::tName;
1737 else if (type == "provider")
1738 return eDVBChannelQuery::tProvider;
1739 else if (type == "type")
1740 return eDVBChannelQuery::tType;
1741 else if (type == "bouquet")
1742 return eDVBChannelQuery::tBouquet;
1743 else if (type == "satellitePosition")
1744 return eDVBChannelQuery::tSatellitePosition;
1745 else if (type == "channelID")
1746 return eDVBChannelQuery::tChannelID;
1747 else if (type == "flags")
1748 return eDVBChannelQuery::tFlags;
1753 /* never, NEVER write a parser in C++! */
1754 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1756 std::list<std::string>::const_iterator end_of_exp;
1764 while (end_of_exp != end)
1765 if (*end_of_exp == ")")
1770 if (end_of_exp == end)
1772 eDebug("expression parse: end of expression while searching for closing brace");
1777 // begin..end_of_exp
1778 int r = parseExpression(res, begin, end_of_exp);
1783 /* we had only one sub expression */
1784 if (end_of_exp == end)
1786 // eDebug("only one sub expression");
1790 /* otherwise we have an operator here.. */
1792 ePtr<eDVBChannelQuery> r2 = res;
1793 res = new eDVBChannelQuery();
1799 if (*end_of_exp == "||")
1800 res->m_type = eDVBChannelQuery::tOR;
1801 else if (*end_of_exp == "&&")
1802 res->m_type = eDVBChannelQuery::tAND;
1805 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1812 return parseExpression(res->m_p2, end_of_exp, end);
1815 // "begin" <op> "end"
1816 std::string type, op, val;
1818 res = new eDVBChannelQuery();
1822 while (begin != end)
1836 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1845 eDebug("malformed query: missing stuff");
1850 res->m_type = decodeType(type);
1852 if (res->m_type == -1)
1854 eDebug("malformed query: invalid type %s", type.c_str());
1861 else if (op == "!=")
1865 eDebug("invalid operator %s", op.c_str());
1870 res->m_string = val;
1872 if (res->m_type == eDVBChannelQuery::tChannelID)
1875 if (sscanf(val.c_str(), "%08x%04x%04x", &ns, &tsid, &onid) == 3)
1876 res->m_channelid = eDVBChannelID(eDVBNamespace(ns), eTransportStreamID(tsid), eOriginalNetworkID(onid));
1878 eDebug("couldn't parse channelid !! format should be hex NNNNNNNNTTTTOOOO (namespace, tsid, onid)");
1881 res->m_int = atoi(val.c_str());
1886 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1888 std::list<std::string> tokens;
1890 std::string current_token;
1891 std::string bouquet_name;
1893 // eDebug("splitting %s....", query.c_str());
1895 const char *splitchars="()";
1896 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1897 while (i <= query.size())
1899 int c = (i < query.size()) ? query[i] : ' ';
1902 int issplit = !!strchr(splitchars, c);
1903 int isaln = isalnum(c);
1904 int iswhite = c == ' ';
1905 int isquot = c == '\"';
1909 iswhite = issplit = 0;
1913 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1915 if (current_token.size())
1916 tokens.push_back(current_token);
1917 current_token.clear();
1920 if (!(iswhite || isquot))
1924 quotemode = !quotemode;
1925 lastsplit = issplit;
1929 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1931 // printf("%s\n", a->c_str());
1934 int sort = eDVBChannelQuery::tName;
1935 /* check for "ORDER BY ..." */
1937 std::list<std::string>::iterator it = tokens.begin();
1938 while (it != tokens.end())
1943 if (it != tokens.end() && *it == "BY")
1946 sort = decodeType(*it);
1951 else if (*it == "FROM")
1954 if (it != tokens.end() && *it == "BOUQUET")
1960 else if (it != tokens.end() && *it == "SATELLITES")
1962 else if (it != tokens.end() && *it == "PROVIDERS")
1966 eDebug("FROM unknown %s", (*it).c_str());
1976 eWarning("ORDER BY .. string invalid.");
1981 // eDebug("sort by %d", sort);
1983 /* now we recursivly parse that. */
1984 int r = parseExpression(res, tokens.begin(), tokens.end());
1986 /* we have an empty (but valid!) expression */
1989 res = new eDVBChannelQuery();
1991 res->m_type = eDVBChannelQuery::tAny;
1997 res->m_bouquet_name = bouquet_name;
2000 // eDebug("return: %d", r);
2004 DEFINE_REF(eDVBChannelQuery);