add support for rolloff and pilot changing (needed for some 8PSK transponders)
[enigma2.git] / lib / dvb / db.cpp
1 #include <errno.h>
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>
13
14 DEFINE_REF(eDVBService);
15
16 RESULT eBouquet::addService(const eServiceReference &ref, eServiceReference before)
17 {
18         list::iterator it =
19                 std::find(m_services.begin(), m_services.end(), ref);
20         if ( it != m_services.end() )
21                 return -1;
22         if (before.valid())
23         {
24                 it = std::find(m_services.begin(), m_services.end(), before);
25                 m_services.insert(it, ref);
26         }
27         else
28                 m_services.push_back(ref);
29         return 0;
30 }
31
32 RESULT eBouquet::removeService(const eServiceReference &ref)
33 {
34         list::iterator it =
35                 std::find(m_services.begin(), m_services.end(), ref);
36         if ( it == m_services.end() )
37                 return -1;
38         m_services.erase(it);
39         return 0;
40 }
41
42 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
43 {
44         if ( pos < 0 || pos >= m_services.size() )
45                 return -1;
46         ++pos;
47         list::iterator source=m_services.end();
48         list::iterator dest=m_services.end();
49         bool forward = false;
50         for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
51         {
52                 if (dest == m_services.end() && !--pos)
53                         dest = it;
54                 if (*it == ref)
55                 {
56                         source = it;
57                         forward = pos>0;
58                 }
59                 if (dest != m_services.end() && source != m_services.end())
60                         break;
61         }
62         if (dest == m_services.end() || source == m_services.end() || source == dest)
63                 return -1;
64         while (source != dest)
65         {
66                 if (forward)
67                         std::iter_swap(source++, source);
68                 else
69                         std::iter_swap(source--, source);
70         }
71         return 0;
72 }
73
74 RESULT eBouquet::flushChanges()
75 {
76         FILE *f=fopen((CONFIGDIR"/enigma2/"+m_filename).c_str(), "w");
77         if (!f)
78                 return -1;
79         if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
80                 goto err;
81         for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
82         {
83                 eServiceReference tmp = *i;
84                 std::string str = tmp.path;
85                 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
86                         goto err;
87                 if ( i->name.length() )
88                         if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
89                                 goto err;
90         }
91         fclose(f);
92         return 0;
93 err:
94         fclose(f);
95         eDebug("couldn't write file %s", m_filename.c_str());
96         return -1;
97 }
98
99 RESULT eBouquet::setListName(const std::string &name)
100 {
101         m_bouquet_name = name;
102         return 0;
103 }
104
105 eDVBService::eDVBService()
106         :m_cache(0), m_flags(0)
107 {
108 }
109
110 eDVBService::~eDVBService()
111 {
112         delete [] m_cache;
113 }
114
115 eDVBService &eDVBService::operator=(const eDVBService &s)
116 {
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;
120         m_flags = s.m_flags;
121         m_ca = s.m_ca;
122         copyCache(s.m_cache);
123         return *this;
124 }
125
126 void eDVBService::genSortName()
127 {
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);
132
133                 /* put unnamed services at the end, not at the beginning. */
134         if (m_service_name_sort.empty())
135                 m_service_name_sort = "\xFF";
136 }
137
138 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
139 {
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;
144         else
145                 name = "(...)";
146         return 0;
147 }
148
149 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
150 {
151         return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
152 }
153
154 int eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
155 {
156         ePtr<eDVBResourceManager> res_mgr;
157         if ( eDVBResourceManager::getInstance( res_mgr ) )
158                 eDebug("isPlayble... no res manager!!");
159         else
160         {
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);
165         }
166         return 0;
167 }
168
169 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
170 {
171         int res = 0;
172         switch (query.m_type)
173         {
174         case eDVBChannelQuery::tName:
175                 res = m_service_name_sort.find(query.m_string) != std::string::npos;
176                 break;
177         case eDVBChannelQuery::tProvider:
178                 res = m_provider_name.find(query.m_string) != std::string::npos;
179                 break;
180         case eDVBChannelQuery::tType:
181                 res = ref.getServiceType() == query.m_int;
182                 break;
183         case eDVBChannelQuery::tBouquet:
184                 res = 0;
185                 break;
186         case eDVBChannelQuery::tSatellitePosition:
187                 res = ((unsigned int)ref.getDVBNamespace().get())>>16 == (unsigned int)query.m_int;
188                 break;
189         case eDVBChannelQuery::tFlags:
190                 res = (m_flags & query.m_int) == query.m_int;
191                 break;
192         case eDVBChannelQuery::tChannelID:
193         {
194                 eDVBChannelID chid;
195                 ref.getChannelID(chid);
196                 res = chid == query.m_channelid;
197                 break;
198         }
199         case eDVBChannelQuery::tAND:
200                 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
201                 break;
202         case eDVBChannelQuery::tOR:
203                 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
204                 break;
205         case eDVBChannelQuery::tAny:
206                 res = 1;
207                 break;
208         }
209
210         if (query.m_inverse)
211                 return !res;
212         else
213                 return res;
214 }
215
216 bool eDVBService::cacheEmpty()
217 {
218         if (m_cache)
219                 for (int i=0; i < cacheMax; ++i)
220                         if (m_cache[i] != -1)
221                                 return false;
222         return true;
223 }
224
225 void eDVBService::initCache()
226 {
227         m_cache = new int[cacheMax];
228         memset(m_cache, -1, sizeof(int) * cacheMax);
229 }
230
231 void eDVBService::copyCache(int *source)
232 {
233         if (source)
234         {
235                 if (!m_cache)
236                         m_cache = new int[cacheMax];
237                 memcpy(m_cache, source, cacheMax * sizeof(int));
238         }
239         else
240         {
241                 delete [] m_cache;
242                 m_cache = 0;
243         }
244 }
245
246 int eDVBService::getCacheEntry(cacheID id)
247 {
248         if (id >= cacheMax || !m_cache)
249                 return -1;
250         return m_cache[id];
251 }
252
253 void eDVBService::setCacheEntry(cacheID id, int pid)
254 {
255         if (!m_cache)
256                 initCache();
257         if (id < cacheMax)
258                 m_cache[id] = pid;
259 }
260
261 DEFINE_REF(eDVBDB);
262
263 void eDVBDB::reloadServicelist()
264 {
265         loadServicelist(CONFIGDIR"/enigma2/lamedb");
266 }
267
268         /* THIS CODE IS BAD. it should be replaced by somethine better. */
269 void eDVBDB::loadServicelist(const char *file)
270 {
271         eDebug("---- opening lame channel db");
272         FILE *f=fopen(file, "rt");
273         if (!f && strcmp(file, CONFIGDIR"/enigma2/lamedb") == 0)
274         {
275                 struct stat s;
276                 if ( !stat("lamedb", &s) )
277                 {
278                         if ( !stat(CONFIGDIR"/enigma2", &s) )
279                         {
280                                 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
281                                 reloadServicelist();
282                         }
283                 }
284                 return;
285         }
286         char line[256];
287         if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
288         {
289                 eDebug("not a servicefile");
290                 fclose(f);
291                 return;
292         }
293         eDebug("reading services");
294         if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
295         {
296                 eDebug("services invalid, no transponders");
297                 fclose(f);
298                 return;
299         }
300
301         // clear all transponders
302
303         while (!feof(f))
304         {
305                 if (!fgets(line, 256, f))
306                         break;
307                 if (!strcmp(line, "end\n"))
308                         break;
309                 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
310                 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
311                 if (original_network_id == -1)
312                         continue;
313                 eDVBChannelID channelid = eDVBChannelID(
314                         eDVBNamespace(dvb_namespace),
315                         eTransportStreamID(transport_stream_id),
316                         eOriginalNetworkID(original_network_id));
317
318                 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
319                 while (!feof(f))
320                 {
321                         fgets(line, 256, f);
322                         if (!strcmp(line, "/\n"))
323                                 break;
324                         if (line[1]=='s')
325                         {
326                                 eDVBFrontendParametersSatellite sat;
327                                 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion,
328                                         system=eDVBFrontendParametersSatellite::System::DVB_S,
329                                         modulation=eDVBFrontendParametersSatellite::Modulation::QPSK,
330                                         rolloff=eDVBFrontendParametersSatellite::RollOff::alpha_0_35,
331                                         pilot=eDVBFrontendParametersSatellite::Pilot::Unknown;
332                                 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);
333                                 sat.frequency = frequency;
334                                 sat.symbol_rate = symbol_rate;
335                                 sat.polarisation = polarisation;
336                                 sat.fec = fec;
337                                 sat.orbital_position =
338                                         orbital_position < 0 ? orbital_position + 3600 : orbital_position;
339                                 sat.inversion = inversion;
340                                 sat.system = system;
341                                 sat.modulation = modulation;
342                                 sat.rolloff = rolloff;
343                                 sat.pilot = pilot;
344                                 feparm->setDVBS(sat);
345                         } else if (line[1]=='t')
346                         {
347                                 eDVBFrontendParametersTerrestrial ter;
348                                 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
349                                 sscanf(line+3, "%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &bandwidth, &code_rate_HP, &code_rate_LP, &modulation, &transmission_mode, &guard_interval, &hierarchy, &inversion);
350                                 ter.frequency = frequency;
351                                 ter.bandwidth = bandwidth;
352                                 ter.code_rate_HP = code_rate_HP;
353                                 ter.code_rate_LP = code_rate_LP;
354                                 ter.modulation = modulation;
355                                 ter.transmission_mode = transmission_mode;
356                                 ter.guard_interval = guard_interval;
357                                 ter.hierarchy = hierarchy;
358                                 ter.inversion = inversion;
359                                 feparm->setDVBT(ter);
360                         } else if (line[1]=='c')
361                         {
362                                 eDVBFrontendParametersCable cab;
363                                 int frequency, symbol_rate,
364                                         inversion=eDVBFrontendParametersCable::Inversion::Unknown,
365                                         modulation=eDVBFrontendParametersCable::Modulation::Auto,
366                                         fec_inner=eDVBFrontendParametersCable::FEC::fAuto;
367                                 sscanf(line+3, "%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner);
368                                 cab.frequency = frequency;
369                                 cab.fec_inner = fec_inner;
370                                 cab.inversion = inversion;
371                                 cab.symbol_rate = symbol_rate;
372                                 cab.modulation = modulation;
373                                 feparm->setDVBC(cab);
374                         }
375                 }
376                 addChannelToList(channelid, feparm);
377         }
378
379         if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
380         {
381                 eDebug("services invalid, no services");
382                 return;
383         }
384
385         // clear all services
386
387         int count=0;
388
389         while (!feof(f))
390         {
391                 if (!fgets(line, 256, f))
392                         break;
393                 if (!strcmp(line, "end\n"))
394                         break;
395
396                 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
397                 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
398                 if (service_number == -1)
399                         continue;
400                 ePtr<eDVBService> s = new eDVBService;
401                 eServiceReferenceDVB ref =
402                                                 eServiceReferenceDVB(
403                                                 eDVBNamespace(dvb_namespace),
404                                                 eTransportStreamID(transport_stream_id),
405                                                 eOriginalNetworkID(original_network_id),
406                                                 eServiceID(service_id),
407                                                 service_type);
408                 count++;
409                 fgets(line, 256, f);
410                 if (strlen(line))
411                         line[strlen(line)-1]=0;
412
413                 s->m_service_name = line;
414                 s->genSortName();
415
416                 fgets(line, 256, f);
417                 if (strlen(line))
418                         line[strlen(line)-1]=0;
419                 std::string str=line;
420
421                 if (str[1]!=':')        // old ... (only service_provider)
422                 {
423                         s->m_provider_name=line;
424                 } else
425                         while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
426                         {
427                                 size_t c=str.find(',');
428                                 char p=str[0];
429                                 std::string v;
430                                 if (c == std::string::npos)
431                                 {
432                                         v=str.substr(2);
433                                         str="";
434                                 } else
435                                 {
436                                         v=str.substr(2, c-2);
437                                         str=str.substr(c+1);
438                                 }
439 //                              eDebug("%c ... %s", p, v.c_str());
440                                 if (p == 'p')
441                                         s->m_provider_name=v;
442                                 else if (p == 'f')
443                                 {
444                                         sscanf(v.c_str(), "%x", &s->m_flags);
445                                 } else if (p == 'c')
446                                 {
447                                         int cid, val;
448                                         sscanf(v.c_str(), "%02d%x", &cid, &val);
449                                         s->setCacheEntry((eDVBService::cacheID)cid,val);
450                                 } else if (p == 'C')
451                                 {
452                                         int val;
453                                         sscanf(v.c_str(), "%04x", &val);
454                                         s->m_ca.push_front((uint16_t)val);
455                                 }
456                         }
457                 addService(ref, s);
458         }
459
460         eDebug("loaded %d services", count);
461
462         fclose(f);
463 }
464
465 void eDVBDB::saveServicelist()
466 {
467         eDebug("---- saving lame channel db");
468         FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "w");
469         int channels=0, services=0;
470         if (!f)
471                 eFatal("couldn't save lame channel db!");
472         fprintf(f, "eDVB services /3/\n");
473         fprintf(f, "transponders\n");
474         for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
475                         i != m_channels.end(); ++i)
476         {
477                 const eDVBChannelID &chid = i->first;
478                 const channel &ch = i->second;
479
480                 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
481                                 chid.transport_stream_id.get(), chid.original_network_id.get());
482                 eDVBFrontendParametersSatellite sat;
483                 eDVBFrontendParametersTerrestrial ter;
484                 eDVBFrontendParametersCable cab;
485                 if (!ch.m_frontendParameters->getDVBS(sat))
486                 {
487                         if (sat.system == eDVBFrontendParametersSatellite::System::DVB_S2)
488                         {
489                                 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d:%d:%d",
490                                         sat.frequency, sat.symbol_rate,
491                                         sat.polarisation, sat.fec,
492                                         sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
493                                         sat.inversion,
494                                         sat.system,
495                                         sat.modulation,
496                                         sat.rolloff);
497                                 if (sat.modulation == eDVBFrontendParametersSatellite::Modulation::M8PSK)
498                                         fprintf(f, ":%d\n", sat.pilot);
499                                 else
500                                         fprintf(f, "\n");
501                         }
502                         else
503                         {
504                                 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
505                                         sat.frequency, sat.symbol_rate,
506                                         sat.polarisation, sat.fec,
507                                         sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
508                                         sat.inversion);
509                         }
510                 }
511                 else if (!ch.m_frontendParameters->getDVBT(ter))
512                 {
513                         fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
514                                 ter.frequency, ter.bandwidth, ter.code_rate_HP,
515                                 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
516                                 ter.guard_interval, ter.hierarchy, ter.inversion);
517                 }
518                 else if (!ch.m_frontendParameters->getDVBC(cab))
519                 {
520                         fprintf(f, "\tc %d:%d:%d:%d:%d\n",
521                                 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner);
522                 }
523                 fprintf(f, "/\n");
524                 channels++;
525         }
526         fprintf(f, "end\nservices\n");
527
528         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
529                 i != m_services.end(); ++i)
530         {
531                 const eServiceReferenceDVB &s = i->first;
532                 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
533                                 s.getServiceID().get(), s.getDVBNamespace().get(),
534                                 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
535                                 s.getServiceType(),
536                                 0);
537
538                 fprintf(f, "%s\n", i->second->m_service_name.c_str());
539
540                 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
541
542                 // write cached pids
543                 for (int x=0; x < eDVBService::cacheMax; ++x)
544                 {
545                         int entry = i->second->getCacheEntry((eDVBService::cacheID)x);
546                         if (entry != -1)
547                                 fprintf(f, ",c:%02d%04x", x, entry);
548                 }
549
550                 // write cached ca pids
551                 for (CAID_LIST::const_iterator ca(i->second->m_ca.begin());
552                         ca != i->second->m_ca.end(); ++ca)
553                         fprintf(f, ",C:%04x", *ca);
554
555                 if (i->second->m_flags)
556                         fprintf(f, ",f:%x", i->second->m_flags);
557
558                 fprintf(f, "\n");
559                 services++;
560         }
561         fprintf(f, "end\nHave a lot of bugs!\n");
562         eDebug("saved %d channels and %d services!", channels, services);
563         fclose(f);
564 }
565
566 void eDVBDB::loadBouquet(const char *path)
567 {
568         std::string bouquet_name = path;
569         if (!bouquet_name.length())
570         {
571                 eDebug("Bouquet load failed.. no path given..");
572                 return;
573         }
574         size_t pos = bouquet_name.rfind('/');
575         if ( pos != std::string::npos )
576                 bouquet_name.erase(0, pos+1);
577         if (bouquet_name.empty())
578         {
579                 eDebug("Bouquet load failed.. no filename given..");
580                 return;
581         }
582         eBouquet &bouquet = m_bouquets[bouquet_name];
583         bouquet.m_filename = bouquet_name;
584         std::list<eServiceReference> &list = bouquet.m_services;
585         list.clear();
586
587         std::string p = CONFIGDIR"/enigma2/";
588         p+=path;
589         eDebug("loading bouquet... %s", p.c_str());
590         FILE *fp=fopen(p.c_str(), "rt");
591         int entries=0;
592         if (!fp)
593         {
594                 struct stat s;
595                 if ( !stat(path, &s) )
596                 {
597                         rename(path, p.c_str() );
598                         loadBouquet(path);
599                         return;
600                 }
601                 eDebug("failed to open.");
602                 if ( strstr(path, "bouquets.tv") )
603                 {
604                         eDebug("recreate bouquets.tv");
605                         bouquet.m_bouquet_name="Bouquets (TV)";
606                         bouquet.flushChanges();
607                 }
608                 else if ( strstr(path, "bouquets.radio") )
609                 {
610                         eDebug("recreate bouquets.radio");
611                         bouquet.m_bouquet_name="Bouquets (Radio)";
612                         bouquet.flushChanges();
613                 }
614                 return;
615         }
616         char line[256];
617         bool read_descr=false;
618         eServiceReference *e = NULL;
619         while (1)
620         {
621                 if (!fgets(line, 256, fp))
622                         break;
623                 line[strlen(line)-1]=0;
624                 if (strlen(line) && line[strlen(line)-1]=='\r')
625                         line[strlen(line)-1]=0;
626                 if (!line[0])
627                         break;
628                 if (line[0]=='#')
629                 {
630                         if (!strncmp(line, "#SERVICE", 8))
631                         {
632                                 int offs = line[8] == ':' ? 10 : 9;
633                                 eServiceReference tmp(line+offs);
634                                 if (tmp.type != eServiceReference::idDVB)
635                                 {
636                                         eDebug("only DVB Bouquets supported");
637                                         continue;
638                                 }
639                                 if ( tmp.flags&eServiceReference::canDescent )
640                                 {
641                                         size_t pos = tmp.path.rfind('/');
642                                         char buf[256];
643                                         std::string path = tmp.path;
644                                         if ( pos != std::string::npos )
645                                                 path.erase(0, pos+1);
646                                         if (path.empty())
647                                         {
648                                                 eDebug("Bouquet load failed.. no filename given..");
649                                                 continue;
650                                         }
651                                         pos = path.find("FROM BOUQUET ");
652                                         if (pos != std::string::npos)
653                                         {
654                                                 char endchr = path[pos+13];
655                                                 if (endchr != '"')
656                                                 {
657                                                         eDebug("ignore invalid bouquet '%s' (only \" are allowed)",
658                                                                 tmp.toString().c_str());
659                                                         continue;
660                                                 }
661                                                 char *beg = &path[pos+14];
662                                                 char *end = strchr(beg, endchr);
663                                                 path.assign(beg, end - beg);
664                                         }
665                                         else
666                                         {
667                                                 snprintf(buf, 256, "FROM BOUQUET \"%s\" ORDER BY bouquet", path.c_str());
668                                                 tmp.path = buf;
669                                         }
670                                         loadBouquet(path.c_str());
671                                 }
672                                 list.push_back(tmp);
673                                 e = &list.back();
674                                 read_descr=true;
675                                 ++entries;
676                         }
677                         else if (read_descr && !strncmp(line, "#DESCRIPTION", 12))
678                         {
679                                 int offs = line[12] == ':' ? 14 : 13;
680                                 e->name = line+offs;
681                                 read_descr=false;
682                         }
683                         else if (!strncmp(line, "#NAME ", 6))
684                                 bouquet.m_bouquet_name=line+6;
685                         continue;
686                 }
687         }
688         fclose(fp);
689         eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
690 }
691
692 void eDVBDB::reloadBouquets()
693 {
694         m_bouquets.clear();
695         loadBouquet("bouquets.tv");
696         loadBouquet("bouquets.radio");
697 // create default bouquets when missing
698         if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
699         {
700                 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
701                 b.m_filename = "userbouquet.favourites.tv";
702                 b.m_bouquet_name = "Favourites (TV)";
703                 b.flushChanges();
704                 eServiceReference ref;
705                 memset(ref.data, 0, sizeof(ref.data));
706                 ref.type=1;
707                 ref.flags=7;
708                 ref.data[0]=1;
709                 ref.path="FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
710                 eBouquet &parent = m_bouquets["bouquets.tv"];
711                 parent.m_services.push_back(ref);
712                 parent.flushChanges();
713         }
714         if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
715         {
716                 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
717                 b.m_filename = "userbouquet.favourites.radio";
718                 b.m_bouquet_name = "Favourites (Radio)";
719                 b.flushChanges();
720                 eServiceReference ref;
721                 memset(ref.data, 0, sizeof(ref.data));
722                 ref.type=1;
723                 ref.flags=7;
724                 ref.data[0]=2;
725                 ref.path="FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
726                 eBouquet &parent = m_bouquets["bouquets.radio"];
727                 parent.m_services.push_back(ref);
728                 parent.flushChanges();
729         }
730 }
731
732 eDVBDB *eDVBDB::instance;
733
734 using namespace xmlcc;
735
736 eDVBDB::eDVBDB()
737 {
738         instance = this;
739         reloadServicelist();
740 }
741
742 PyObject *eDVBDB::readSatellites(ePyObject sat_list, ePyObject sat_dict, ePyObject tp_dict)
743 {
744         if (!PyDict_Check(tp_dict)) {
745                 PyErr_SetString(PyExc_StandardError,
746                         "type error");
747                         eDebug("arg 2 is not a python dict");
748                 return NULL;
749         }
750         else if (!PyDict_Check(sat_dict))
751         {
752                 PyErr_SetString(PyExc_StandardError,
753                         "type error");
754                         eDebug("arg 1 is not a python dict");
755                 return NULL;
756         }
757         else if (!PyList_Check(sat_list))
758         {
759                 PyErr_SetString(PyExc_StandardError,
760                         "type error");
761                         eDebug("arg 0 is not a python list");
762                 return NULL;
763         }
764         XMLTree tree;
765         tree.setFilename("/etc/tuxbox/satellites.xml");
766         tree.read();
767         Element *root = tree.getRoot();
768         if (!root)
769         {
770                 eDebug("couldn't open /etc/tuxbox/satellites.xml!!");
771                 Py_INCREF(Py_False);
772                 return Py_False;
773         }
774         int tmp, *dest = NULL,
775                 modulation, system, freq, sr, pol, fec, inv, pilot, rolloff;
776         char *end_ptr;
777         const Attribute *at;
778         std::string name;
779         const ElementList &root_elements = root->getElementList();
780         for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
781         {
782 //              eDebug("element: %s", (*it)->name().c_str());
783                 const Element *el = *it;
784                 const ElementList &sat_elements = el->getElementList();
785                 const AttributeList &sat_attributes = el->getAttributeList();
786                 ePyObject sat_name;
787                 ePyObject sat_pos;
788                 ePyObject sat_flags;
789                 for (AttributeConstIterator it(sat_attributes.begin()); it != sat_attributes.end(); ++it)
790                 {
791 //                      eDebug("\tattr: %s", at->name().c_str());
792                         at = *it;
793                         name = at->name();
794                         if (name == "name")
795                                 sat_name = PyString_FromString(at->value().c_str());
796                         else if (name == "flags")
797                         {
798                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
799                                 if (!*end_ptr)
800                                         sat_flags = PyInt_FromLong(tmp);
801                         }
802                         else if (name == "position")
803                         {
804                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
805                                 if (!*end_ptr)
806                                 {
807                                         if (tmp < 0)
808                                                 tmp = 3600 + tmp;
809                                         sat_pos = PyInt_FromLong(tmp);
810                                 }
811                         }
812                 }
813                 if (sat_pos && sat_name)
814                 {
815                         ePyObject tplist = PyList_New(0);
816                         ePyObject tuple = PyTuple_New(3);
817                         if (!sat_flags)
818                                 sat_flags = PyInt_FromLong(0);
819                         PyTuple_SET_ITEM(tuple, 0, sat_pos);
820                         PyTuple_SET_ITEM(tuple, 1, sat_name);
821                         PyTuple_SET_ITEM(tuple, 2, sat_flags);
822                         PyList_Append(sat_list, tuple);
823                         Py_DECREF(tuple);
824                         PyDict_SetItem(sat_dict, sat_pos, sat_name);
825                         PyDict_SetItem(tp_dict, sat_pos, tplist);
826                         for (ElementConstIterator it(sat_elements.begin()); it != sat_elements.end(); ++it)
827                         {
828 //                              eDebug("\telement: %s", (*it)->name().c_str());
829                                 const AttributeList &tp_attributes = (*it)->getAttributeList();
830                                 AttributeConstIterator end = tp_attributes.end();
831                                 modulation = 1; // QPSK default
832                                 system = 0; // DVB-S default
833                                 freq = 0;
834                                 sr = 0;
835                                 pol = -1;
836                                 fec = 0; // AUTO default
837                                 inv = 2; // AUTO default
838                                 pilot = 2; // AUTO default
839                                 rolloff = 0; // alpha 0.35
840                                 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
841                                 {
842 //                                      eDebug("\t\tattr: %s", at->name().c_str());
843                                         at = *it;
844                                         name = at->name();
845                                         if (name == "modulation") dest = &modulation;
846                                         else if (name == "system") dest = &system;
847                                         else if (name == "frequency") dest = &freq;
848                                         else if (name == "symbol_rate") dest = &sr;
849                                         else if (name == "polarization") dest = &pol;
850                                         else if (name == "fec_inner") dest = &fec;
851                                         else if (name == "inversion") dest = &inv;
852                                         else if (name == "rolloff") dest = &rolloff;
853                                         else if (name == "pilot") dest = &pilot;
854                                         if (dest)
855                                         {
856                                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
857                                                 if (!*end_ptr)
858                                                         *dest = tmp;
859                                         }
860                                 }
861                                 if (freq && sr && pol != -1)
862                                 {
863                                         tuple = PyTuple_New(10);
864                                         PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(0));
865                                         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
866                                         PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
867                                         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(pol));
868                                         PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
869                                         PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(system));
870                                         PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(modulation));
871                                         PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(inv));
872                                         PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(rolloff));
873                                         PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(pilot));
874                                         PyList_Append(tplist, tuple);
875                                         Py_DECREF(tuple);
876                                 }
877                         }
878                         Py_DECREF(tplist);
879                 }
880                 else
881                 {
882                         if (sat_pos)
883                                 Py_DECREF(sat_pos);
884                         if (sat_name)
885                                 Py_DECREF(sat_name);
886                         if (sat_flags)
887                                 Py_DECREF(sat_flags);
888                 }
889         }
890         Py_INCREF(Py_True);
891         return Py_True;
892 }
893
894 PyObject *eDVBDB::readCables(ePyObject cab_list, ePyObject tp_dict)
895 {
896         if (!PyDict_Check(tp_dict)) {
897                 PyErr_SetString(PyExc_StandardError,
898                         "type error");
899                         eDebug("arg 1 is not a python dict");
900                 return NULL;
901         }
902         else if (!PyList_Check(cab_list))
903         {
904                 PyErr_SetString(PyExc_StandardError,
905                         "type error");
906                         eDebug("arg 0 is not a python list");
907                 return NULL;
908         }
909         XMLTree tree;
910         tree.setFilename("/etc/tuxbox/cables.xml");
911         tree.read();
912         Element *root = tree.getRoot();
913         if (!root)
914         {
915                 eDebug("couldn't open /etc/tuxbox/cables.xml!!");
916                 Py_INCREF(Py_False);
917                 return Py_False;
918         }
919         const Attribute *at;
920         int tmp, *dest,
921                 modulation, fec, freq, sr;
922         std::string name;
923         char *end_ptr;
924         const ElementList &root_elements = root->getElementList();
925         for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
926         {
927 //              eDebug("element: %s", el->name().c_str());
928                 const Element *el = *it;
929                 const ElementList &cab_elements = el->getElementList();
930                 const AttributeList &cab_attributes = el->getAttributeList();
931                 ePyObject cab_name;
932                 ePyObject cab_flags;
933                 for (AttributeConstIterator it(cab_attributes.begin()); it != cab_attributes.end(); ++it)
934                 {
935 //                      eDebug("\tattr: %s", at->name().c_str());
936                         at = *it;
937                         name = at->name();
938                         if (name == "name")
939                                 cab_name = PyString_FromString(at->value().c_str());
940                         else if (name == "flags")
941                         {
942                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
943                                 if (!*end_ptr)
944                                         cab_flags = PyInt_FromLong(tmp);
945                         }
946                 }
947                 if (cab_name)
948                 {
949                         ePyObject tplist = PyList_New(0);
950                         ePyObject tuple = PyTuple_New(2);
951                         if (!cab_flags)
952                                 cab_flags = PyInt_FromLong(0);
953                         PyTuple_SET_ITEM(tuple, 0, cab_name);
954                         PyTuple_SET_ITEM(tuple, 1, cab_flags);
955                         PyList_Append(cab_list, tuple);
956                         Py_DECREF(tuple);
957                         PyDict_SetItem(tp_dict, cab_name, tplist);
958                         for (ElementConstIterator it(cab_elements.begin()); it != cab_elements.end(); ++it)
959                         {
960 //                              eDebug("\telement: %s", (*it)->name().c_str());
961                                 const AttributeList &tp_attributes = (*it)->getAttributeList();
962                                 AttributeConstIterator end = tp_attributes.end();
963                                 modulation = 3; // QAM64 default
964                                 fec = 0; // AUTO default
965                                 freq = 0;
966                                 sr = 0;
967                                 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
968                                 {
969 //                                      eDebug("\t\tattr: %s", at->name().c_str());
970                                         at = *it;
971                                         dest = 0;
972                                         name = at->name();
973                                         if (name == "modulation") dest = &modulation;
974                                         else if (name == "frequency") dest = &freq;
975                                         else if (name == "symbol_rate") dest = &sr;
976                                         else if (name == "fec_inner") dest = &fec;
977                                         if (dest)
978                                         {
979                                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
980                                                 if (!*end_ptr)
981                                                         *dest = tmp;
982                                         }
983                                 }
984                                 if (freq && sr)
985                                 {
986                                         while (freq > 999999)
987                                                 freq /= 10;
988                                         tuple = PyTuple_New(5);
989                                         PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(1));
990                                         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
991                                         PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
992                                         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(modulation));
993                                         PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
994                                         PyList_Append(tplist, tuple);
995                                         Py_DECREF(tuple);
996                                 }
997                         }
998                         Py_DECREF(tplist);
999                 }
1000                 else if (cab_flags)
1001                         Py_DECREF(cab_flags);
1002         }
1003         Py_INCREF(Py_True);
1004         return Py_True;
1005 }
1006
1007 PyObject *eDVBDB::readTerrestrials(ePyObject ter_list, ePyObject tp_dict)
1008 {
1009         if (!PyDict_Check(tp_dict)) {
1010                 PyErr_SetString(PyExc_StandardError,
1011                         "type error");
1012                         eDebug("arg 1 is not a python dict");
1013                 return NULL;
1014         }
1015         else if (!PyList_Check(ter_list))
1016         {
1017                 PyErr_SetString(PyExc_StandardError,
1018                         "type error");
1019                         eDebug("arg 0 is not a python list");
1020                 return NULL;
1021         }
1022         XMLTree tree;
1023         tree.setFilename("/etc/tuxbox/terrestrial.xml");
1024         tree.read();
1025         Element *root = tree.getRoot();
1026         if (!root)
1027         {
1028                 eDebug("couldn't open /etc/tuxbox/terrestrial.xml!!");
1029                 Py_INCREF(Py_False);
1030                 return Py_False;
1031         }
1032         const Attribute *at;
1033         std::string name;
1034         int tmp, *dest,
1035                 freq, bw, constellation, crh = 5, crl = 5, guard = 4, transm, hierarchy, inv = 2;
1036         char *end_ptr;
1037         const ElementList &root_elements = root->getElementList();
1038         for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
1039         {
1040 //              eDebug("element: %s", el->name().c_str());
1041                 const Element *el = *it;
1042                 const ElementList &ter_elements = el->getElementList();
1043                 const AttributeList &ter_attributes = el->getAttributeList();
1044                 ePyObject ter_name;
1045                 ePyObject ter_flags;
1046                 for (AttributeConstIterator it(ter_attributes.begin()); it != ter_attributes.end(); ++it)
1047                 {
1048 //                      eDebug("\tattr: %s", at->name().c_str());
1049                         at = *it;
1050                         name = at->name();
1051                         if (name == "name")
1052                                 ter_name = PyString_FromString(at->value().c_str());
1053                         else if (name == "flags")
1054                         {
1055                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1056                                 if (!*end_ptr)
1057                                         ter_flags = PyInt_FromLong(tmp);
1058                         }
1059                 }
1060                 if (ter_name)
1061                 {
1062                         ePyObject tplist = PyList_New(0);
1063                         ePyObject tuple = PyTuple_New(2);
1064                         if (!ter_flags)
1065                                 ter_flags = PyInt_FromLong(0);
1066                         PyTuple_SET_ITEM(tuple, 0, ter_name);
1067                         PyTuple_SET_ITEM(tuple, 1, ter_flags);
1068                         PyList_Append(ter_list, tuple);
1069                         Py_DECREF(tuple);
1070                         PyDict_SetItem(tp_dict, ter_name, tplist);
1071                         for (ElementConstIterator it(ter_elements.begin()); it != ter_elements.end(); ++it)
1072                         {
1073 //                              eDebug("\telement: %s", (*it)->name().c_str());
1074                                 const AttributeList &tp_attributes = (*it)->getAttributeList();
1075                                 AttributeConstIterator end = tp_attributes.end();
1076                                 freq = 0;
1077                                 bw = 3; // AUTO
1078                                 constellation = 1; // AUTO
1079                                 crh = 5; // AUTO
1080                                 crl = 5; // AUTO
1081                                 guard = 4; // AUTO
1082                                 transm = 2; // AUTO
1083                                 hierarchy = 4; // AUTO
1084                                 inv = 2; // AUTO
1085                                 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
1086                                 {
1087 //                                      eDebug("\t\tattr: %s", at->name().c_str());
1088                                         at = *it;
1089                                         dest = 0;
1090                                         name = at->name();
1091                                         if (name == "centre_frequency") dest = &freq;
1092                                         else if (name == "bandwidth") dest = &bw;
1093                                         else if (name == "constellation") dest = &constellation;
1094                                         else if (name == "code_rate_hp") dest = &crh;
1095                                         else if (name == "code_rate_lp") dest = &crl;
1096                                         else if (name == "guard_interval") dest = &guard;
1097                                         else if (name == "transmission_mode") dest = &transm;
1098                                         else if (name == "hierarchy_information") dest = &hierarchy;
1099                                         else if (name == "inversion") dest = &inv;
1100                                         if (dest)
1101                                         {
1102                                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1103                                                 if (!*end_ptr)
1104                                                         *dest = tmp;
1105                                         }
1106                                 }
1107                                 if (freq)
1108                                 {
1109                                         if (crh > 5) // our terrestrial.xml is buggy... 6 for AUTO
1110                                                 crh = 5;
1111                                         if (crl > 5) // our terrestrial.xml is buggy... 6 for AUTO
1112                                                 crl = 5;
1113                                         tuple = PyTuple_New(10);
1114                                         PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1115                                         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
1116                                         PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(bw));
1117                                         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(constellation));
1118                                         PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(crh));
1119                                         PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(crl));
1120                                         PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(guard));
1121                                         PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(transm));
1122                                         PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(hierarchy));
1123                                         PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(inv));
1124                                         PyList_Append(tplist, tuple);
1125                                         Py_DECREF(tuple);
1126                                 }
1127                         }
1128                         Py_DECREF(tplist);
1129                 }
1130                 else if (ter_flags)
1131                         Py_DECREF(ter_flags);
1132         }
1133         Py_INCREF(Py_True);
1134         return Py_True;
1135 }
1136
1137 eDVBDB::~eDVBDB()
1138 {
1139         instance=NULL;
1140 }
1141
1142 RESULT eDVBDB::removeService(const eServiceReference &ref)
1143 {
1144         if (ref.type == eServiceReference::idDVB)
1145         {
1146                 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1147                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1148                 if (it != m_services.end())
1149                 {
1150                         m_services.erase(it);
1151                         return 0;
1152                 }
1153         }
1154         return -1;
1155 }
1156
1157 RESULT eDVBDB::removeServices(int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1158 {
1159         return removeServices(eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1160 }
1161
1162 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
1163 {
1164         RESULT ret=-1;
1165         eDVBNamespace eNs;
1166         eTransportStreamID eTsid;
1167         eOriginalNetworkID eOnid;
1168         std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1169         std::set<eDVBChannelID> removed_chids;
1170         while (it != m_channels.end())
1171         {
1172                 const eDVBChannelID &ch = it->first;
1173                 bool remove=true;
1174                 int system;
1175                 it->second.m_frontendParameters->getSystem(system);
1176                 if ( system == iDVBFrontend::feSatellite )
1177                 {
1178                         eDVBFrontendParametersSatellite sat;
1179                         it->second.m_frontendParameters->getDVBS(sat);
1180                         if ((unsigned int)sat.orbital_position != orbpos)
1181                                 remove=false;
1182                 }
1183                 if ( remove && chid.dvbnamespace != eNs )
1184                 {
1185                         if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1186                                 ;
1187                         else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1188                                 ;
1189                         else if ( chid.dvbnamespace != ch.dvbnamespace )
1190                                 remove=false;
1191                 }
1192                 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1193                         remove=false;
1194                 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1195                         remove=false;
1196                 if ( remove )
1197                 {
1198                         eDebug("remove %08x %04x %04x",
1199                                 ch.dvbnamespace.get(),
1200                                 ch.original_network_id.get(),
1201                                 ch.transport_stream_id.get());
1202                         removed_chids.insert(it->first);
1203                         m_channels.erase(it++);
1204                 }
1205                 else
1206                         ++it;
1207         }
1208         if (!removed_chids.empty())
1209         {
1210                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1211                 while(service != m_services.end())
1212                 {
1213                         eDVBChannelID chid;
1214                         service->first.getChannelID(chid);
1215                         std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1216                         if (it != removed_chids.end())
1217                                 m_services.erase(service++);
1218                         else
1219                                 ++service;
1220                         ret=0;
1221                 }
1222         }
1223         return ret;
1224 }
1225
1226 RESULT eDVBDB::addFlag(const eServiceReference &ref, unsigned int flagmask)
1227 {
1228         if (ref.type == eServiceReference::idDVB)
1229         {
1230                 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1231                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1232                 if (it != m_services.end())
1233                         it->second->m_flags |= ~flagmask;
1234                 return 0;
1235         }
1236         return -1;
1237 }
1238
1239 RESULT eDVBDB::removeFlag(const eServiceReference &ref, unsigned int flagmask)
1240 {
1241         if (ref.type == eServiceReference::idDVB)
1242         {
1243                 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1244                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1245                 if (it != m_services.end())
1246                         it->second->m_flags &= ~flagmask;
1247                 return 0;
1248         }
1249         return -1;
1250 }
1251
1252 RESULT eDVBDB::removeFlags(unsigned int flagmask, int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1253 {
1254         return removeFlags(flagmask, eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1255 }
1256
1257 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
1258 {
1259         eDVBNamespace eNs;
1260         eTransportStreamID eTsid;
1261         eOriginalNetworkID eOnid;
1262         std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1263         std::set<eDVBChannelID> removed_chids;
1264         while (it != m_channels.end())
1265         {
1266                 const eDVBChannelID &ch = it->first;
1267                 bool remove=true;
1268                 int system;
1269                 it->second.m_frontendParameters->getSystem(system);
1270                 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
1271                 {
1272                         eDVBFrontendParametersSatellite sat;
1273                         it->second.m_frontendParameters->getDVBS(sat);
1274                         if ((unsigned int)sat.orbital_position != orbpos)
1275                                 remove=false;
1276                 }
1277                 if ( remove && chid.dvbnamespace != eNs )
1278                 {
1279                         if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1280                                 ;
1281                         else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1282                                 ;
1283                         else if ( chid.dvbnamespace != ch.dvbnamespace )
1284                                 remove=false;
1285                 }
1286                 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1287                         remove=false;
1288                 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1289                         remove=false;
1290                 if ( remove )
1291                         removed_chids.insert(it->first);
1292                 ++it;
1293         }
1294         if (!removed_chids.empty())
1295         {
1296                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1297                 while(service != m_services.end())
1298                 {
1299                         eDVBChannelID chid;
1300                         service->first.getChannelID(chid);
1301                         std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1302                         if (it != removed_chids.end())
1303                                 service->second->m_flags &= ~flagmask;
1304                         ++service;
1305                 }
1306         }
1307         return 0;
1308 }
1309
1310 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
1311 {
1312         channel ch;
1313         assert(feparm);
1314         ch.m_frontendParameters = feparm;
1315         m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
1316         return 0;
1317 }
1318
1319 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
1320 {
1321         m_channels.erase(id);
1322         return 0;
1323 }
1324
1325 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
1326 {
1327         std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
1328         if (i == m_channels.end())
1329         {
1330                 parm = 0;
1331                 return -ENOENT;
1332         }
1333         parm = i->second.m_frontendParameters;
1334         return 0;
1335 }
1336
1337 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
1338 {
1339         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
1340         if (it == m_services.end())
1341                 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
1342         return 0;
1343 }
1344
1345 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
1346 {
1347         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
1348         i = m_services.find(reference);
1349         if (i == m_services.end())
1350         {
1351                 service = 0;
1352                 return -ENOENT;
1353         }
1354         service = i->second;
1355         return 0;
1356 }
1357
1358 RESULT eDVBDB::flush()
1359 {
1360         saveServicelist();
1361         return 0;
1362 }
1363
1364 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
1365 {
1366         std::string str = ref.path;
1367         if (str.empty())
1368         {
1369                 eDebug("getBouquet failed.. no path given!");
1370                 return -1;
1371         }
1372         size_t pos = str.find("FROM BOUQUET \"");
1373         if ( pos != std::string::npos )
1374         {
1375                 str.erase(0, pos+14);
1376                 pos = str.find('"');
1377                 if ( pos != std::string::npos )
1378                         str.erase(pos);
1379                 else
1380                         str.clear();
1381         }
1382         if (str.empty())
1383         {
1384                 eDebug("getBouquet failed.. couldn't parse bouquet name");
1385                 return -1;
1386         }
1387         std::map<std::string, eBouquet>::iterator i =
1388                 m_bouquets.find(str);
1389         if (i == m_bouquets.end())
1390         {
1391                 bouquet = 0;
1392                 return -ENOENT;
1393         }
1394         bouquet = &i->second;
1395         return 0;
1396 }
1397
1398 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
1399 {
1400         if ( source.path.find("FROM") != std::string::npos )
1401         {
1402                 if ( source.path.find("BOUQUET") != std::string::npos )
1403                         query = new eDVBDBBouquetQuery(this, source, q);
1404                 else if ( source.path.find("SATELLITES") != std::string::npos )
1405                         query = new eDVBDBSatellitesQuery(this, source, q);
1406                 else if ( source.path.find("PROVIDERS") != std::string::npos )
1407                         query = new eDVBDBProvidersQuery(this, source, q);
1408                 else
1409                         eFatal("invalid query %s", source.toString().c_str());
1410         }
1411         else
1412                 query = new eDVBDBQuery(this, source, q);
1413         return 0;
1414 }
1415
1416 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
1417 {
1418         eServiceID Sid(sid);
1419         eTransportStreamID Tsid(tsid);
1420         eOriginalNetworkID Onid(onid);
1421         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
1422                 sit != m_services.end(); ++sit)
1423         {
1424                 if (sit->first.getTransportStreamID() == Tsid &&
1425                         sit->first.getOriginalNetworkID() == Onid &&
1426                         sit->first.getServiceID() == Sid)
1427                         return sit->first;
1428         }
1429         return eServiceReference();
1430 }
1431
1432 DEFINE_REF(eDVBDBQueryBase);
1433
1434 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1435         :m_db(db), m_query(query), m_source(source)
1436 {
1437 }
1438
1439 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1440 {
1441         ePtr<eDVBService> a_service, b_service;
1442         int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
1443         
1444         if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
1445         {
1446                 if (a.name.empty() && m_db->getService(a, a_service))
1447                         return 1;
1448                 if (b.name.empty() && m_db->getService(b, b_service))
1449                         return 1;
1450         }
1451         
1452         switch (sortmode)
1453         {
1454         case eDVBChannelQuery::tName:
1455                 if (a_service)
1456                 {
1457                         if (b_service)
1458                                 return a_service->m_service_name_sort < b_service->m_service_name_sort;
1459                         else
1460                         {
1461                                 std::string str = b.name;
1462                                 makeUpper(str);
1463                                 return a_service->m_service_name_sort < str;
1464                         }
1465                 }
1466                 else if (b_service)
1467                 {
1468                         std::string str = a.name;
1469                         makeUpper(str);
1470                         return str < b_service->m_service_name_sort;
1471                 }
1472                 else
1473                 {
1474                         std::string aa = a.name, bb = b.name;
1475                         makeUpper(aa);
1476                         makeUpper(bb);
1477                         return aa < bb;
1478                 }
1479         case eDVBChannelQuery::tProvider:
1480                 return a_service->m_provider_name < b_service->m_provider_name;
1481         case eDVBChannelQuery::tType:
1482                 return a.getServiceType() < b.getServiceType();
1483         case eDVBChannelQuery::tBouquet:
1484                 return 0;
1485         case eDVBChannelQuery::tSatellitePosition:
1486                 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
1487         default:
1488                 return 1;
1489         }
1490         return 0;
1491 }
1492
1493 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1494         :eDVBDBQueryBase(db, source, query)
1495 {
1496         m_cursor = m_db->m_services.begin();
1497 }
1498
1499 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
1500 {
1501         while (m_cursor != m_db->m_services.end())
1502         {
1503                 ePtr<eDVBService> service = m_cursor->second;
1504                 if (service->isHidden())
1505                         ++m_cursor;
1506                 else
1507                 {
1508                         ref = m_cursor->first;
1509
1510                         int res = (!m_query) || service->checkFilter(ref, *m_query);
1511
1512                         ++m_cursor;
1513
1514                         if (res)
1515                                 return 0;
1516                 }
1517         }
1518
1519         ref.type = eServiceReference::idInvalid;
1520
1521         return 1;
1522 }
1523
1524 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1525         :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
1526 {
1527 }
1528
1529 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
1530 {
1531         eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1532         std::list<eServiceReference> &list = bouquet.m_services;
1533         while (m_cursor != list.end())
1534         {
1535                 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1536
1537                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1538                         m_db->m_services.find(ref);
1539
1540                 int res = (!m_query) || it == m_db->m_services.end() || !(it->second->isHidden() && it->second->checkFilter(ref, *m_query));
1541
1542                 ++m_cursor;
1543
1544                 if (res)
1545                         return 0;
1546         }
1547
1548         ref.type = eServiceReference::idInvalid;
1549
1550         return 1;
1551 }
1552
1553 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1554         :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1555 {
1556 }
1557
1558 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1559 {
1560         if (m_cursor != m_list.end())
1561         {
1562                 ref = *m_cursor++;
1563                 return 0;
1564         }
1565         ref.type = eServiceReference::idInvalid;
1566         return 1;
1567 }
1568
1569 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1570 {
1571         if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1572         {
1573                 int x = (a.getDVBNamespace().get() >> 16);
1574                 int y = (b.getDVBNamespace().get() >> 16);
1575                 if ( x > 1800 )
1576                         x -= 3600;
1577                 if ( y > 1800 )
1578                         y -= 3600;
1579                 return x < y;
1580         }
1581         std::string aa = a.name, bb = b.name;
1582         makeUpper(aa);
1583         makeUpper(bb);
1584         return aa < bb;
1585 }
1586
1587 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1588         :eDVBDBListQuery(db, source, query)
1589 {
1590         std::set<unsigned int> found;
1591         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1592                 it != m_db->m_services.end(); ++it)
1593         {
1594                 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1595                 if (res)
1596                 {
1597                         unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1598                         if (found.find(dvbnamespace) == found.end())
1599                         {
1600                                 found.insert(dvbnamespace);
1601                                 eServiceReferenceDVB ref;
1602                                 ref.setDVBNamespace(dvbnamespace);
1603                                 ref.flags=eServiceReference::flagDirectory;
1604                                 char buf[128];
1605                                 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1606
1607                                 ref.path=buf+source.path;
1608                                 unsigned int pos=ref.path.find("FROM");
1609                                 ref.path.erase(pos);
1610                                 ref.path+="ORDER BY name";
1611 //                              eDebug("ref.path now %s", ref.path.c_str());
1612                                 m_list.push_back(ref);
1613
1614                                 ref.path=buf+source.path;
1615                                 pos=ref.path.find("FROM");
1616                                 ref.path.erase(pos+5);
1617                                 ref.path+="PROVIDERS ORDER BY name";
1618 //                              eDebug("ref.path now %s", ref.path.c_str());
1619                                 m_list.push_back(ref);
1620
1621                                 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1622                                 ref.path=buf+source.path;
1623                                 pos=ref.path.find("FROM");
1624                                 ref.path.erase(pos);
1625                                 ref.path+="ORDER BY name";
1626 //                              eDebug("ref.path now %s", ref.path.c_str());
1627                                 m_list.push_back(ref);
1628                         }
1629                 }
1630         }
1631         m_cursor=m_list.begin();
1632 }
1633
1634 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1635         :eDVBDBListQuery(db, source, query)
1636 {
1637         std::set<std::string> found;
1638         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1639                 it != m_db->m_services.end(); ++it)
1640         {
1641                 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1642                 if (res)
1643                 {
1644                         const char *provider_name = it->second->m_provider_name.length() ?
1645                                 it->second->m_provider_name.c_str() :
1646                                 "Unknown";
1647                         if (found.find(std::string(provider_name)) == found.end())
1648                         {
1649                                 found.insert(std::string(provider_name));
1650                                 eServiceReferenceDVB ref;
1651                                 char buf[64];
1652                                 ref.name=provider_name;
1653                                 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1654                                 ref.path=buf+source.path;
1655                                 unsigned int pos = ref.path.find("FROM");
1656                                 ref.flags=eServiceReference::flagDirectory;
1657                                 ref.path.erase(pos);
1658                                 ref.path+="ORDER BY name";
1659 //                              eDebug("ref.path now %s", ref.path.c_str());
1660                                 m_list.push_back(ref);
1661                         }
1662                 }
1663         }
1664         m_cursor=m_list.begin();
1665 }
1666
1667 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1668
1669 static int decodeType(const std::string &type)
1670 {
1671         if (type == "name")
1672                 return eDVBChannelQuery::tName;
1673         else if (type == "provider")
1674                 return eDVBChannelQuery::tProvider;
1675         else if (type == "type")
1676                 return eDVBChannelQuery::tType;
1677         else if (type == "bouquet")
1678                 return eDVBChannelQuery::tBouquet;
1679         else if (type == "satellitePosition")
1680                 return eDVBChannelQuery::tSatellitePosition;
1681         else if (type == "channelID")
1682                 return eDVBChannelQuery::tChannelID;
1683         else if (type == "flags")
1684                 return eDVBChannelQuery::tFlags;
1685         else
1686                 return -1;
1687 }
1688
1689         /* never, NEVER write a parser in C++! */
1690 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1691 {
1692         std::list<std::string>::const_iterator end_of_exp;
1693         
1694         if (begin == end)
1695                 return 0;
1696         
1697         if (*begin == "(")
1698         {
1699                 end_of_exp = begin;
1700                 while (end_of_exp != end)
1701                         if (*end_of_exp == ")")
1702                                 break;
1703                         else
1704                                 ++end_of_exp;
1705         
1706                 if (end_of_exp == end)
1707                 {
1708                         eDebug("expression parse: end of expression while searching for closing brace");
1709                         return -1;
1710                 }
1711                 
1712                 ++begin;
1713                 // begin..end_of_exp
1714                 int r = parseExpression(res, begin, end_of_exp);
1715                 if (r)
1716                         return r;
1717                 ++end_of_exp;
1718                 
1719                         /* we had only one sub expression */
1720                 if (end_of_exp == end)
1721                 {
1722 //                      eDebug("only one sub expression");
1723                         return 0;
1724                 }
1725                 
1726                         /* otherwise we have an operator here.. */
1727                 
1728                 ePtr<eDVBChannelQuery> r2 = res;
1729                 res = new eDVBChannelQuery();
1730                 res->m_sort = 0;
1731                 res->m_p1 = r2;
1732                 res->m_inverse = 0;
1733                 r2 = 0;
1734                 
1735                 if (*end_of_exp == "||")
1736                         res->m_type = eDVBChannelQuery::tOR;
1737                 else if (*end_of_exp == "&&")
1738                         res->m_type = eDVBChannelQuery::tAND;
1739                 else
1740                 {
1741                         eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1742                         res = 0;
1743                         return 1;
1744                 }
1745                 
1746                 ++end_of_exp;
1747                 
1748                 return parseExpression(res->m_p2, end_of_exp, end);
1749         }
1750         
1751         // "begin" <op> "end"
1752         std::string type, op, val;
1753         
1754         res = new eDVBChannelQuery();
1755         res->m_sort = 0;
1756         
1757         int cnt = 0;
1758         while (begin != end)
1759         {
1760                 switch (cnt)
1761                 {
1762                 case 0:
1763                         type = *begin;
1764                         break;
1765                 case 1:
1766                         op = *begin;
1767                         break;
1768                 case 2:
1769                         val = *begin;
1770                         break;
1771                 case 3:
1772                         eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1773                         return 1;
1774                 }
1775                 ++begin;
1776                 ++cnt;
1777         }
1778         
1779         if (cnt != 3)
1780         {
1781                 eDebug("malformed query: missing stuff");
1782                 res = 0;
1783                 return 1;
1784         }
1785         
1786         res->m_type = decodeType(type);
1787         
1788         if (res->m_type == -1)
1789         {
1790                 eDebug("malformed query: invalid type %s", type.c_str());
1791                 res = 0;
1792                 return 1;
1793         }
1794         
1795         if (op == "==")
1796                 res->m_inverse = 0;
1797         else if (op == "!=")
1798                 res->m_inverse = 1;
1799         else
1800         {
1801                 eDebug("invalid operator %s", op.c_str());
1802                 res = 0;
1803                 return 1;
1804         }
1805         
1806         res->m_string = val;
1807
1808         if (res->m_type == eDVBChannelQuery::tChannelID)
1809         {
1810                 int ns, tsid, onid;
1811                 if (sscanf(val.c_str(), "%08x%04x%04x", &ns, &tsid, &onid) == 3)
1812                         res->m_channelid = eDVBChannelID(eDVBNamespace(ns), eTransportStreamID(tsid), eOriginalNetworkID(onid));
1813                 else
1814                         eDebug("couldn't parse channelid !! format should be hex NNNNNNNNTTTTOOOO (namespace, tsid, onid)");
1815         }
1816         else
1817                 res->m_int = atoi(val.c_str());
1818
1819         return 0;
1820 }
1821
1822 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1823 {
1824         std::list<std::string> tokens;
1825         
1826         std::string current_token;
1827         std::string bouquet_name;
1828
1829 //      eDebug("splitting %s....", query.c_str());
1830         unsigned int i = 0;
1831         const char *splitchars="()";
1832         int quotemode = 0, lastsplit = 0, lastalnum = 0;
1833         while (i <= query.size())
1834         {
1835                 int c = (i < query.size()) ? query[i] : ' ';
1836                 ++i;
1837                 
1838                 int issplit = !!strchr(splitchars, c);
1839                 int isaln = isalnum(c);
1840                 int iswhite = c == ' ';
1841                 int isquot = c == '\"';
1842                 
1843                 if (quotemode)
1844                 {
1845                         iswhite = issplit = 0;
1846                         isaln = lastalnum;
1847                 }
1848                 
1849                 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1850                 {
1851                         if (current_token.size())
1852                                 tokens.push_back(current_token);
1853                         current_token.clear();
1854                 }
1855                 
1856                 if (!(iswhite || isquot))
1857                         current_token += c;
1858                 
1859                 if (isquot)
1860                         quotemode = !quotemode;
1861                 lastsplit = issplit;
1862                 lastalnum = isaln;
1863         }
1864         
1865 //      for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1866 //      {
1867 //              printf("%s\n", a->c_str());
1868 //      }
1869
1870         int sort = eDVBChannelQuery::tName;
1871                 /* check for "ORDER BY ..." */
1872
1873         std::list<std::string>::iterator it = tokens.begin();
1874         while (it != tokens.end())
1875         {
1876                 if (*it == "ORDER")
1877                 {
1878                         tokens.erase(it++);
1879                         if (it != tokens.end() && *it == "BY")
1880                         {
1881                                 tokens.erase(it++);
1882                                 sort = decodeType(*it);
1883                                 tokens.erase(it++);
1884                         } else
1885                                 sort = -1;
1886                 }
1887                 else if (*it == "FROM")
1888                 {
1889                         tokens.erase(it++);
1890                         if (it != tokens.end() && *it == "BOUQUET")
1891                         {
1892                                 tokens.erase(it++);
1893                                 bouquet_name = *it;
1894                                 tokens.erase(it++);
1895                         }
1896                         else if (it != tokens.end() && *it == "SATELLITES")
1897                                 tokens.erase(it++);
1898                         else if (it != tokens.end() && *it == "PROVIDERS")
1899                                 tokens.erase(it++);
1900                         else
1901                         {
1902                                 eDebug("FROM unknown %s", (*it).c_str());
1903                                 tokens.erase(it++);
1904                         }
1905                 }
1906                 else
1907                         ++it;
1908         }
1909
1910         if (sort == -1)
1911         {
1912                 eWarning("ORDER BY .. string invalid.");
1913                 res = 0;
1914                 return -1;
1915         }
1916         
1917 //      eDebug("sort by %d", sort);
1918         
1919                 /* now we recursivly parse that. */
1920         int r = parseExpression(res, tokens.begin(), tokens.end());
1921         
1922                 /* we have an empty (but valid!) expression */
1923         if (!r && !res)
1924         {
1925                 res = new eDVBChannelQuery();
1926                 res->m_inverse = 0;
1927                 res->m_type = eDVBChannelQuery::tAny;
1928         }
1929         
1930         if (res)
1931         {
1932                 res->m_sort = sort;
1933                 res->m_bouquet_name = bouquet_name;
1934         }
1935
1936 //      eDebug("return: %d", r);
1937         return r;
1938 }
1939
1940 DEFINE_REF(eDVBChannelQuery);