add parameter to saveServicelist
[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(const char *file)
466 {
467         eDebug("---- saving lame channel db");
468         FILE *f=fopen(file, "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::saveServicelist()
567 {
568         saveServicelist(CONFIGDIR"/enigma2/lamedb");
569 }
570
571 void eDVBDB::loadBouquet(const char *path)
572 {
573         std::string bouquet_name = path;
574         if (!bouquet_name.length())
575         {
576                 eDebug("Bouquet load failed.. no path given..");
577                 return;
578         }
579         size_t pos = bouquet_name.rfind('/');
580         if ( pos != std::string::npos )
581                 bouquet_name.erase(0, pos+1);
582         if (bouquet_name.empty())
583         {
584                 eDebug("Bouquet load failed.. no filename given..");
585                 return;
586         }
587         eBouquet &bouquet = m_bouquets[bouquet_name];
588         bouquet.m_filename = bouquet_name;
589         std::list<eServiceReference> &list = bouquet.m_services;
590         list.clear();
591
592         std::string p = CONFIGDIR"/enigma2/";
593         p+=path;
594         eDebug("loading bouquet... %s", p.c_str());
595         FILE *fp=fopen(p.c_str(), "rt");
596         int entries=0;
597         if (!fp)
598         {
599                 struct stat s;
600                 if ( !stat(path, &s) )
601                 {
602                         rename(path, p.c_str() );
603                         loadBouquet(path);
604                         return;
605                 }
606                 eDebug("failed to open.");
607                 if ( strstr(path, "bouquets.tv") )
608                 {
609                         eDebug("recreate bouquets.tv");
610                         bouquet.m_bouquet_name="Bouquets (TV)";
611                         bouquet.flushChanges();
612                 }
613                 else if ( strstr(path, "bouquets.radio") )
614                 {
615                         eDebug("recreate bouquets.radio");
616                         bouquet.m_bouquet_name="Bouquets (Radio)";
617                         bouquet.flushChanges();
618                 }
619                 return;
620         }
621         char line[256];
622         bool read_descr=false;
623         eServiceReference *e = NULL;
624         while (1)
625         {
626                 if (!fgets(line, 256, fp))
627                         break;
628                 line[strlen(line)-1]=0;
629                 if (strlen(line) && line[strlen(line)-1]=='\r')
630                         line[strlen(line)-1]=0;
631                 if (!line[0])
632                         break;
633                 if (line[0]=='#')
634                 {
635                         if (!strncmp(line, "#SERVICE", 8))
636                         {
637                                 int offs = line[8] == ':' ? 10 : 9;
638                                 eServiceReference tmp(line+offs);
639                                 if (tmp.type != eServiceReference::idDVB)
640                                 {
641                                         eDebug("only DVB Bouquets supported");
642                                         continue;
643                                 }
644                                 if ( tmp.flags&eServiceReference::canDescent )
645                                 {
646                                         size_t pos = tmp.path.rfind('/');
647                                         char buf[256];
648                                         std::string path = tmp.path;
649                                         if ( pos != std::string::npos )
650                                                 path.erase(0, pos+1);
651                                         if (path.empty())
652                                         {
653                                                 eDebug("Bouquet load failed.. no filename given..");
654                                                 continue;
655                                         }
656                                         pos = path.find("FROM BOUQUET ");
657                                         if (pos != std::string::npos)
658                                         {
659                                                 char endchr = path[pos+13];
660                                                 if (endchr != '"')
661                                                 {
662                                                         eDebug("ignore invalid bouquet '%s' (only \" are allowed)",
663                                                                 tmp.toString().c_str());
664                                                         continue;
665                                                 }
666                                                 char *beg = &path[pos+14];
667                                                 char *end = strchr(beg, endchr);
668                                                 path.assign(beg, end - beg);
669                                         }
670                                         else
671                                         {
672                                                 snprintf(buf, 256, "FROM BOUQUET \"%s\" ORDER BY bouquet", path.c_str());
673                                                 tmp.path = buf;
674                                         }
675                                         loadBouquet(path.c_str());
676                                 }
677                                 list.push_back(tmp);
678                                 e = &list.back();
679                                 read_descr=true;
680                                 ++entries;
681                         }
682                         else if (read_descr && !strncmp(line, "#DESCRIPTION", 12))
683                         {
684                                 int offs = line[12] == ':' ? 14 : 13;
685                                 e->name = line+offs;
686                                 read_descr=false;
687                         }
688                         else if (!strncmp(line, "#NAME ", 6))
689                                 bouquet.m_bouquet_name=line+6;
690                         continue;
691                 }
692         }
693         fclose(fp);
694         eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
695 }
696
697 void eDVBDB::reloadBouquets()
698 {
699         m_bouquets.clear();
700         loadBouquet("bouquets.tv");
701         loadBouquet("bouquets.radio");
702 // create default bouquets when missing
703         if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
704         {
705                 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
706                 b.m_filename = "userbouquet.favourites.tv";
707                 b.m_bouquet_name = "Favourites (TV)";
708                 b.flushChanges();
709                 eServiceReference ref;
710                 memset(ref.data, 0, sizeof(ref.data));
711                 ref.type=1;
712                 ref.flags=7;
713                 ref.data[0]=1;
714                 ref.path="FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
715                 eBouquet &parent = m_bouquets["bouquets.tv"];
716                 parent.m_services.push_back(ref);
717                 parent.flushChanges();
718         }
719         if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
720         {
721                 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
722                 b.m_filename = "userbouquet.favourites.radio";
723                 b.m_bouquet_name = "Favourites (Radio)";
724                 b.flushChanges();
725                 eServiceReference ref;
726                 memset(ref.data, 0, sizeof(ref.data));
727                 ref.type=1;
728                 ref.flags=7;
729                 ref.data[0]=2;
730                 ref.path="FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
731                 eBouquet &parent = m_bouquets["bouquets.radio"];
732                 parent.m_services.push_back(ref);
733                 parent.flushChanges();
734         }
735 }
736
737 eDVBDB *eDVBDB::instance;
738
739 using namespace xmlcc;
740
741 eDVBDB::eDVBDB()
742 {
743         instance = this;
744         reloadServicelist();
745 }
746
747 PyObject *eDVBDB::readSatellites(ePyObject sat_list, ePyObject sat_dict, ePyObject tp_dict)
748 {
749         if (!PyDict_Check(tp_dict)) {
750                 PyErr_SetString(PyExc_StandardError,
751                         "type error");
752                         eDebug("arg 2 is not a python dict");
753                 return NULL;
754         }
755         else if (!PyDict_Check(sat_dict))
756         {
757                 PyErr_SetString(PyExc_StandardError,
758                         "type error");
759                         eDebug("arg 1 is not a python dict");
760                 return NULL;
761         }
762         else if (!PyList_Check(sat_list))
763         {
764                 PyErr_SetString(PyExc_StandardError,
765                         "type error");
766                         eDebug("arg 0 is not a python list");
767                 return NULL;
768         }
769         XMLTree tree;
770         tree.setFilename("/etc/tuxbox/satellites.xml");
771         tree.read();
772         Element *root = tree.getRoot();
773         if (!root)
774         {
775                 eDebug("couldn't open /etc/tuxbox/satellites.xml!!");
776                 Py_INCREF(Py_False);
777                 return Py_False;
778         }
779         int tmp, *dest = NULL,
780                 modulation, system, freq, sr, pol, fec, inv, pilot, rolloff;
781         char *end_ptr;
782         const Attribute *at;
783         std::string name;
784         const ElementList &root_elements = root->getElementList();
785         for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
786         {
787 //              eDebug("element: %s", (*it)->name().c_str());
788                 const Element *el = *it;
789                 const ElementList &sat_elements = el->getElementList();
790                 const AttributeList &sat_attributes = el->getAttributeList();
791                 ePyObject sat_name;
792                 ePyObject sat_pos;
793                 ePyObject sat_flags;
794                 for (AttributeConstIterator it(sat_attributes.begin()); it != sat_attributes.end(); ++it)
795                 {
796 //                      eDebug("\tattr: %s", at->name().c_str());
797                         at = *it;
798                         name = at->name();
799                         if (name == "name")
800                                 sat_name = PyString_FromString(at->value().c_str());
801                         else if (name == "flags")
802                         {
803                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
804                                 if (!*end_ptr)
805                                         sat_flags = PyInt_FromLong(tmp);
806                         }
807                         else if (name == "position")
808                         {
809                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
810                                 if (!*end_ptr)
811                                 {
812                                         if (tmp < 0)
813                                                 tmp = 3600 + tmp;
814                                         sat_pos = PyInt_FromLong(tmp);
815                                 }
816                         }
817                 }
818                 if (sat_pos && sat_name)
819                 {
820                         ePyObject tplist = PyList_New(0);
821                         ePyObject tuple = PyTuple_New(3);
822                         if (!sat_flags)
823                                 sat_flags = PyInt_FromLong(0);
824                         PyTuple_SET_ITEM(tuple, 0, sat_pos);
825                         PyTuple_SET_ITEM(tuple, 1, sat_name);
826                         PyTuple_SET_ITEM(tuple, 2, sat_flags);
827                         PyList_Append(sat_list, tuple);
828                         Py_DECREF(tuple);
829                         PyDict_SetItem(sat_dict, sat_pos, sat_name);
830                         PyDict_SetItem(tp_dict, sat_pos, tplist);
831                         for (ElementConstIterator it(sat_elements.begin()); it != sat_elements.end(); ++it)
832                         {
833 //                              eDebug("\telement: %s", (*it)->name().c_str());
834                                 const AttributeList &tp_attributes = (*it)->getAttributeList();
835                                 AttributeConstIterator end = tp_attributes.end();
836                                 modulation = 1; // QPSK default
837                                 system = 0; // DVB-S default
838                                 freq = 0;
839                                 sr = 0;
840                                 pol = -1;
841                                 fec = 0; // AUTO default
842                                 inv = 2; // AUTO default
843                                 pilot = 2; // AUTO default
844                                 rolloff = 0; // alpha 0.35
845                                 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
846                                 {
847 //                                      eDebug("\t\tattr: %s", at->name().c_str());
848                                         at = *it;
849                                         name = at->name();
850                                         if (name == "modulation") dest = &modulation;
851                                         else if (name == "system") dest = &system;
852                                         else if (name == "frequency") dest = &freq;
853                                         else if (name == "symbol_rate") dest = &sr;
854                                         else if (name == "polarization") dest = &pol;
855                                         else if (name == "fec_inner") dest = &fec;
856                                         else if (name == "inversion") dest = &inv;
857                                         else if (name == "rolloff") dest = &rolloff;
858                                         else if (name == "pilot") dest = &pilot;
859                                         if (dest)
860                                         {
861                                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
862                                                 if (!*end_ptr)
863                                                         *dest = tmp;
864                                         }
865                                 }
866                                 if (freq && sr && pol != -1)
867                                 {
868                                         tuple = PyTuple_New(10);
869                                         PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(0));
870                                         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
871                                         PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
872                                         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(pol));
873                                         PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
874                                         PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(system));
875                                         PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(modulation));
876                                         PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(inv));
877                                         PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(rolloff));
878                                         PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(pilot));
879                                         PyList_Append(tplist, tuple);
880                                         Py_DECREF(tuple);
881                                 }
882                         }
883                         Py_DECREF(tplist);
884                 }
885                 else
886                 {
887                         if (sat_pos)
888                                 Py_DECREF(sat_pos);
889                         if (sat_name)
890                                 Py_DECREF(sat_name);
891                         if (sat_flags)
892                                 Py_DECREF(sat_flags);
893                 }
894         }
895         Py_INCREF(Py_True);
896         return Py_True;
897 }
898
899 PyObject *eDVBDB::readCables(ePyObject cab_list, ePyObject tp_dict)
900 {
901         if (!PyDict_Check(tp_dict)) {
902                 PyErr_SetString(PyExc_StandardError,
903                         "type error");
904                         eDebug("arg 1 is not a python dict");
905                 return NULL;
906         }
907         else if (!PyList_Check(cab_list))
908         {
909                 PyErr_SetString(PyExc_StandardError,
910                         "type error");
911                         eDebug("arg 0 is not a python list");
912                 return NULL;
913         }
914         XMLTree tree;
915         tree.setFilename("/etc/tuxbox/cables.xml");
916         tree.read();
917         Element *root = tree.getRoot();
918         if (!root)
919         {
920                 eDebug("couldn't open /etc/tuxbox/cables.xml!!");
921                 Py_INCREF(Py_False);
922                 return Py_False;
923         }
924         const Attribute *at;
925         int tmp, *dest,
926                 modulation, fec, freq, sr;
927         std::string name;
928         char *end_ptr;
929         const ElementList &root_elements = root->getElementList();
930         for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
931         {
932 //              eDebug("element: %s", el->name().c_str());
933                 const Element *el = *it;
934                 const ElementList &cab_elements = el->getElementList();
935                 const AttributeList &cab_attributes = el->getAttributeList();
936                 ePyObject cab_name;
937                 ePyObject cab_flags;
938                 for (AttributeConstIterator it(cab_attributes.begin()); it != cab_attributes.end(); ++it)
939                 {
940 //                      eDebug("\tattr: %s", at->name().c_str());
941                         at = *it;
942                         name = at->name();
943                         if (name == "name")
944                                 cab_name = PyString_FromString(at->value().c_str());
945                         else if (name == "flags")
946                         {
947                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
948                                 if (!*end_ptr)
949                                         cab_flags = PyInt_FromLong(tmp);
950                         }
951                 }
952                 if (cab_name)
953                 {
954                         ePyObject tplist = PyList_New(0);
955                         ePyObject tuple = PyTuple_New(2);
956                         if (!cab_flags)
957                                 cab_flags = PyInt_FromLong(0);
958                         PyTuple_SET_ITEM(tuple, 0, cab_name);
959                         PyTuple_SET_ITEM(tuple, 1, cab_flags);
960                         PyList_Append(cab_list, tuple);
961                         Py_DECREF(tuple);
962                         PyDict_SetItem(tp_dict, cab_name, tplist);
963                         for (ElementConstIterator it(cab_elements.begin()); it != cab_elements.end(); ++it)
964                         {
965 //                              eDebug("\telement: %s", (*it)->name().c_str());
966                                 const AttributeList &tp_attributes = (*it)->getAttributeList();
967                                 AttributeConstIterator end = tp_attributes.end();
968                                 modulation = 3; // QAM64 default
969                                 fec = 0; // AUTO default
970                                 freq = 0;
971                                 sr = 0;
972                                 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
973                                 {
974 //                                      eDebug("\t\tattr: %s", at->name().c_str());
975                                         at = *it;
976                                         dest = 0;
977                                         name = at->name();
978                                         if (name == "modulation") dest = &modulation;
979                                         else if (name == "frequency") dest = &freq;
980                                         else if (name == "symbol_rate") dest = &sr;
981                                         else if (name == "fec_inner") dest = &fec;
982                                         if (dest)
983                                         {
984                                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
985                                                 if (!*end_ptr)
986                                                         *dest = tmp;
987                                         }
988                                 }
989                                 if (freq && sr)
990                                 {
991                                         while (freq > 999999)
992                                                 freq /= 10;
993                                         tuple = PyTuple_New(5);
994                                         PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(1));
995                                         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
996                                         PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(sr));
997                                         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(modulation));
998                                         PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(fec));
999                                         PyList_Append(tplist, tuple);
1000                                         Py_DECREF(tuple);
1001                                 }
1002                         }
1003                         Py_DECREF(tplist);
1004                 }
1005                 else if (cab_flags)
1006                         Py_DECREF(cab_flags);
1007         }
1008         Py_INCREF(Py_True);
1009         return Py_True;
1010 }
1011
1012 PyObject *eDVBDB::readTerrestrials(ePyObject ter_list, ePyObject tp_dict)
1013 {
1014         if (!PyDict_Check(tp_dict)) {
1015                 PyErr_SetString(PyExc_StandardError,
1016                         "type error");
1017                         eDebug("arg 1 is not a python dict");
1018                 return NULL;
1019         }
1020         else if (!PyList_Check(ter_list))
1021         {
1022                 PyErr_SetString(PyExc_StandardError,
1023                         "type error");
1024                         eDebug("arg 0 is not a python list");
1025                 return NULL;
1026         }
1027         XMLTree tree;
1028         tree.setFilename("/etc/tuxbox/terrestrial.xml");
1029         tree.read();
1030         Element *root = tree.getRoot();
1031         if (!root)
1032         {
1033                 eDebug("couldn't open /etc/tuxbox/terrestrial.xml!!");
1034                 Py_INCREF(Py_False);
1035                 return Py_False;
1036         }
1037         const Attribute *at;
1038         std::string name;
1039         int tmp, *dest,
1040                 freq, bw, constellation, crh = 5, crl = 5, guard = 4, transm, hierarchy, inv = 2;
1041         char *end_ptr;
1042         const ElementList &root_elements = root->getElementList();
1043         for (ElementConstIterator it(root_elements.begin()); it != root_elements.end(); ++it)
1044         {
1045 //              eDebug("element: %s", el->name().c_str());
1046                 const Element *el = *it;
1047                 const ElementList &ter_elements = el->getElementList();
1048                 const AttributeList &ter_attributes = el->getAttributeList();
1049                 ePyObject ter_name;
1050                 ePyObject ter_flags;
1051                 for (AttributeConstIterator it(ter_attributes.begin()); it != ter_attributes.end(); ++it)
1052                 {
1053 //                      eDebug("\tattr: %s", at->name().c_str());
1054                         at = *it;
1055                         name = at->name();
1056                         if (name == "name")
1057                                 ter_name = PyString_FromString(at->value().c_str());
1058                         else if (name == "flags")
1059                         {
1060                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1061                                 if (!*end_ptr)
1062                                         ter_flags = PyInt_FromLong(tmp);
1063                         }
1064                 }
1065                 if (ter_name)
1066                 {
1067                         ePyObject tplist = PyList_New(0);
1068                         ePyObject tuple = PyTuple_New(2);
1069                         if (!ter_flags)
1070                                 ter_flags = PyInt_FromLong(0);
1071                         PyTuple_SET_ITEM(tuple, 0, ter_name);
1072                         PyTuple_SET_ITEM(tuple, 1, ter_flags);
1073                         PyList_Append(ter_list, tuple);
1074                         Py_DECREF(tuple);
1075                         PyDict_SetItem(tp_dict, ter_name, tplist);
1076                         for (ElementConstIterator it(ter_elements.begin()); it != ter_elements.end(); ++it)
1077                         {
1078 //                              eDebug("\telement: %s", (*it)->name().c_str());
1079                                 const AttributeList &tp_attributes = (*it)->getAttributeList();
1080                                 AttributeConstIterator end = tp_attributes.end();
1081                                 freq = 0;
1082                                 bw = 3; // AUTO
1083                                 constellation = 1; // AUTO
1084                                 crh = 5; // AUTO
1085                                 crl = 5; // AUTO
1086                                 guard = 4; // AUTO
1087                                 transm = 2; // AUTO
1088                                 hierarchy = 4; // AUTO
1089                                 inv = 2; // AUTO
1090                                 for (AttributeConstIterator it(tp_attributes.begin()); it != end; ++it)
1091                                 {
1092 //                                      eDebug("\t\tattr: %s", at->name().c_str());
1093                                         at = *it;
1094                                         dest = 0;
1095                                         name = at->name();
1096                                         if (name == "centre_frequency") dest = &freq;
1097                                         else if (name == "bandwidth") dest = &bw;
1098                                         else if (name == "constellation") dest = &constellation;
1099                                         else if (name == "code_rate_hp") dest = &crh;
1100                                         else if (name == "code_rate_lp") dest = &crl;
1101                                         else if (name == "guard_interval") dest = &guard;
1102                                         else if (name == "transmission_mode") dest = &transm;
1103                                         else if (name == "hierarchy_information") dest = &hierarchy;
1104                                         else if (name == "inversion") dest = &inv;
1105                                         if (dest)
1106                                         {
1107                                                 tmp = strtol(at->value().c_str(), &end_ptr, 10);
1108                                                 if (!*end_ptr)
1109                                                         *dest = tmp;
1110                                         }
1111                                 }
1112                                 if (freq)
1113                                 {
1114                                         if (crh > 5) // our terrestrial.xml is buggy... 6 for AUTO
1115                                                 crh = 5;
1116                                         if (crl > 5) // our terrestrial.xml is buggy... 6 for AUTO
1117                                                 crl = 5;
1118                                         tuple = PyTuple_New(10);
1119                                         PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(2));
1120                                         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(freq));
1121                                         PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(bw));
1122                                         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(constellation));
1123                                         PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(crh));
1124                                         PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(crl));
1125                                         PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(guard));
1126                                         PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong(transm));
1127                                         PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong(hierarchy));
1128                                         PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong(inv));
1129                                         PyList_Append(tplist, tuple);
1130                                         Py_DECREF(tuple);
1131                                 }
1132                         }
1133                         Py_DECREF(tplist);
1134                 }
1135                 else if (ter_flags)
1136                         Py_DECREF(ter_flags);
1137         }
1138         Py_INCREF(Py_True);
1139         return Py_True;
1140 }
1141
1142 eDVBDB::~eDVBDB()
1143 {
1144         instance=NULL;
1145 }
1146
1147 RESULT eDVBDB::removeService(const eServiceReference &ref)
1148 {
1149         if (ref.type == eServiceReference::idDVB)
1150         {
1151                 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1152                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1153                 if (it != m_services.end())
1154                 {
1155                         m_services.erase(it);
1156                         return 0;
1157                 }
1158         }
1159         return -1;
1160 }
1161
1162 RESULT eDVBDB::removeServices(int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1163 {
1164         return removeServices(eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1165 }
1166
1167 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
1168 {
1169         RESULT ret=-1;
1170         eDVBNamespace eNs;
1171         eTransportStreamID eTsid;
1172         eOriginalNetworkID eOnid;
1173         std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1174         std::set<eDVBChannelID> removed_chids;
1175         while (it != m_channels.end())
1176         {
1177                 const eDVBChannelID &ch = it->first;
1178                 bool remove=true;
1179                 int system;
1180                 it->second.m_frontendParameters->getSystem(system);
1181                 if ( system == iDVBFrontend::feSatellite )
1182                 {
1183                         eDVBFrontendParametersSatellite sat;
1184                         it->second.m_frontendParameters->getDVBS(sat);
1185                         if ((unsigned int)sat.orbital_position != orbpos)
1186                                 remove=false;
1187                 }
1188                 if ( remove && chid.dvbnamespace != eNs )
1189                 {
1190                         if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1191                                 ;
1192                         else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1193                                 ;
1194                         else if ( chid.dvbnamespace != ch.dvbnamespace )
1195                                 remove=false;
1196                 }
1197                 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1198                         remove=false;
1199                 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1200                         remove=false;
1201                 if ( remove )
1202                 {
1203                         eDebug("remove %08x %04x %04x",
1204                                 ch.dvbnamespace.get(),
1205                                 ch.original_network_id.get(),
1206                                 ch.transport_stream_id.get());
1207                         removed_chids.insert(it->first);
1208                         m_channels.erase(it++);
1209                 }
1210                 else
1211                         ++it;
1212         }
1213         if (!removed_chids.empty())
1214         {
1215                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1216                 while(service != m_services.end())
1217                 {
1218                         eDVBChannelID chid;
1219                         service->first.getChannelID(chid);
1220                         std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1221                         if (it != removed_chids.end())
1222                                 m_services.erase(service++);
1223                         else
1224                                 ++service;
1225                         ret=0;
1226                 }
1227         }
1228         return ret;
1229 }
1230
1231 RESULT eDVBDB::addFlag(const eServiceReference &ref, unsigned int flagmask)
1232 {
1233         if (ref.type == eServiceReference::idDVB)
1234         {
1235                 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1236                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1237                 if (it != m_services.end())
1238                         it->second->m_flags |= ~flagmask;
1239                 return 0;
1240         }
1241         return -1;
1242 }
1243
1244 RESULT eDVBDB::removeFlag(const eServiceReference &ref, unsigned int flagmask)
1245 {
1246         if (ref.type == eServiceReference::idDVB)
1247         {
1248                 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
1249                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
1250                 if (it != m_services.end())
1251                         it->second->m_flags &= ~flagmask;
1252                 return 0;
1253         }
1254         return -1;
1255 }
1256
1257 RESULT eDVBDB::removeFlags(unsigned int flagmask, int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
1258 {
1259         return removeFlags(flagmask, eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
1260 }
1261
1262 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
1263 {
1264         eDVBNamespace eNs;
1265         eTransportStreamID eTsid;
1266         eOriginalNetworkID eOnid;
1267         std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
1268         std::set<eDVBChannelID> removed_chids;
1269         while (it != m_channels.end())
1270         {
1271                 const eDVBChannelID &ch = it->first;
1272                 bool remove=true;
1273                 int system;
1274                 it->second.m_frontendParameters->getSystem(system);
1275                 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
1276                 {
1277                         eDVBFrontendParametersSatellite sat;
1278                         it->second.m_frontendParameters->getDVBS(sat);
1279                         if ((unsigned int)sat.orbital_position != orbpos)
1280                                 remove=false;
1281                 }
1282                 if ( remove && chid.dvbnamespace != eNs )
1283                 {
1284                         if (system == iDVBFrontend::feCable && chid.dvbnamespace.get() == (int)0xFFFF0000)
1285                                 ;
1286                         else if (system == iDVBFrontend::feTerrestrial && chid.dvbnamespace.get() == (int)0xEEEE0000)
1287                                 ;
1288                         else if ( chid.dvbnamespace != ch.dvbnamespace )
1289                                 remove=false;
1290                 }
1291                 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
1292                         remove=false;
1293                 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
1294                         remove=false;
1295                 if ( remove )
1296                         removed_chids.insert(it->first);
1297                 ++it;
1298         }
1299         if (!removed_chids.empty())
1300         {
1301                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
1302                 while(service != m_services.end())
1303                 {
1304                         eDVBChannelID chid;
1305                         service->first.getChannelID(chid);
1306                         std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
1307                         if (it != removed_chids.end())
1308                                 service->second->m_flags &= ~flagmask;
1309                         ++service;
1310                 }
1311         }
1312         return 0;
1313 }
1314
1315 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
1316 {
1317         channel ch;
1318         assert(feparm);
1319         ch.m_frontendParameters = feparm;
1320         m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
1321         return 0;
1322 }
1323
1324 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
1325 {
1326         m_channels.erase(id);
1327         return 0;
1328 }
1329
1330 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
1331 {
1332         std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
1333         if (i == m_channels.end())
1334         {
1335                 parm = 0;
1336                 return -ENOENT;
1337         }
1338         parm = i->second.m_frontendParameters;
1339         return 0;
1340 }
1341
1342 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
1343 {
1344         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
1345         if (it == m_services.end())
1346                 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
1347         return 0;
1348 }
1349
1350 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
1351 {
1352         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
1353         i = m_services.find(reference);
1354         if (i == m_services.end())
1355         {
1356                 service = 0;
1357                 return -ENOENT;
1358         }
1359         service = i->second;
1360         return 0;
1361 }
1362
1363 RESULT eDVBDB::flush()
1364 {
1365         saveServicelist();
1366         return 0;
1367 }
1368
1369 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
1370 {
1371         std::string str = ref.path;
1372         if (str.empty())
1373         {
1374                 eDebug("getBouquet failed.. no path given!");
1375                 return -1;
1376         }
1377         size_t pos = str.find("FROM BOUQUET \"");
1378         if ( pos != std::string::npos )
1379         {
1380                 str.erase(0, pos+14);
1381                 pos = str.find('"');
1382                 if ( pos != std::string::npos )
1383                         str.erase(pos);
1384                 else
1385                         str.clear();
1386         }
1387         if (str.empty())
1388         {
1389                 eDebug("getBouquet failed.. couldn't parse bouquet name");
1390                 return -1;
1391         }
1392         std::map<std::string, eBouquet>::iterator i =
1393                 m_bouquets.find(str);
1394         if (i == m_bouquets.end())
1395         {
1396                 bouquet = 0;
1397                 return -ENOENT;
1398         }
1399         bouquet = &i->second;
1400         return 0;
1401 }
1402
1403 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
1404 {
1405         if ( source.path.find("FROM") != std::string::npos )
1406         {
1407                 if ( source.path.find("BOUQUET") != std::string::npos )
1408                         query = new eDVBDBBouquetQuery(this, source, q);
1409                 else if ( source.path.find("SATELLITES") != std::string::npos )
1410                         query = new eDVBDBSatellitesQuery(this, source, q);
1411                 else if ( source.path.find("PROVIDERS") != std::string::npos )
1412                         query = new eDVBDBProvidersQuery(this, source, q);
1413                 else
1414                         eFatal("invalid query %s", source.toString().c_str());
1415         }
1416         else
1417                 query = new eDVBDBQuery(this, source, q);
1418         return 0;
1419 }
1420
1421 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
1422 {
1423         eServiceID Sid(sid);
1424         eTransportStreamID Tsid(tsid);
1425         eOriginalNetworkID Onid(onid);
1426         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
1427                 sit != m_services.end(); ++sit)
1428         {
1429                 if (sit->first.getTransportStreamID() == Tsid &&
1430                         sit->first.getOriginalNetworkID() == Onid &&
1431                         sit->first.getServiceID() == Sid)
1432                         return sit->first;
1433         }
1434         return eServiceReference();
1435 }
1436
1437 DEFINE_REF(eDVBDBQueryBase);
1438
1439 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1440         :m_db(db), m_query(query), m_source(source)
1441 {
1442 }
1443
1444 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1445 {
1446         ePtr<eDVBService> a_service, b_service;
1447         int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
1448         
1449         if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
1450         {
1451                 if (a.name.empty() && m_db->getService(a, a_service))
1452                         return 1;
1453                 if (b.name.empty() && m_db->getService(b, b_service))
1454                         return 1;
1455         }
1456         
1457         switch (sortmode)
1458         {
1459         case eDVBChannelQuery::tName:
1460                 if (a_service)
1461                 {
1462                         if (b_service)
1463                                 return a_service->m_service_name_sort < b_service->m_service_name_sort;
1464                         else
1465                         {
1466                                 std::string str = b.name;
1467                                 makeUpper(str);
1468                                 return a_service->m_service_name_sort < str;
1469                         }
1470                 }
1471                 else if (b_service)
1472                 {
1473                         std::string str = a.name;
1474                         makeUpper(str);
1475                         return str < b_service->m_service_name_sort;
1476                 }
1477                 else
1478                 {
1479                         std::string aa = a.name, bb = b.name;
1480                         makeUpper(aa);
1481                         makeUpper(bb);
1482                         return aa < bb;
1483                 }
1484         case eDVBChannelQuery::tProvider:
1485                 return a_service->m_provider_name < b_service->m_provider_name;
1486         case eDVBChannelQuery::tType:
1487                 return a.getServiceType() < b.getServiceType();
1488         case eDVBChannelQuery::tBouquet:
1489                 return 0;
1490         case eDVBChannelQuery::tSatellitePosition:
1491                 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
1492         default:
1493                 return 1;
1494         }
1495         return 0;
1496 }
1497
1498 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1499         :eDVBDBQueryBase(db, source, query)
1500 {
1501         m_cursor = m_db->m_services.begin();
1502 }
1503
1504 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
1505 {
1506         while (m_cursor != m_db->m_services.end())
1507         {
1508                 ePtr<eDVBService> service = m_cursor->second;
1509                 if (service->isHidden())
1510                         ++m_cursor;
1511                 else
1512                 {
1513                         ref = m_cursor->first;
1514
1515                         int res = (!m_query) || service->checkFilter(ref, *m_query);
1516
1517                         ++m_cursor;
1518
1519                         if (res)
1520                                 return 0;
1521                 }
1522         }
1523
1524         ref.type = eServiceReference::idInvalid;
1525
1526         return 1;
1527 }
1528
1529 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1530         :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
1531 {
1532 }
1533
1534 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
1535 {
1536         eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1537         std::list<eServiceReference> &list = bouquet.m_services;
1538         while (m_cursor != list.end())
1539         {
1540                 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1541
1542                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1543                         m_db->m_services.find(ref);
1544
1545                 int res = (!m_query) || it == m_db->m_services.end() || !(it->second->isHidden() && it->second->checkFilter(ref, *m_query));
1546
1547                 ++m_cursor;
1548
1549                 if (res)
1550                         return 0;
1551         }
1552
1553         ref.type = eServiceReference::idInvalid;
1554
1555         return 1;
1556 }
1557
1558 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1559         :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1560 {
1561 }
1562
1563 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1564 {
1565         if (m_cursor != m_list.end())
1566         {
1567                 ref = *m_cursor++;
1568                 return 0;
1569         }
1570         ref.type = eServiceReference::idInvalid;
1571         return 1;
1572 }
1573
1574 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1575 {
1576         if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1577         {
1578                 int x = (a.getDVBNamespace().get() >> 16);
1579                 int y = (b.getDVBNamespace().get() >> 16);
1580                 if ( x > 1800 )
1581                         x -= 3600;
1582                 if ( y > 1800 )
1583                         y -= 3600;
1584                 return x < y;
1585         }
1586         std::string aa = a.name, bb = b.name;
1587         makeUpper(aa);
1588         makeUpper(bb);
1589         return aa < bb;
1590 }
1591
1592 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1593         :eDVBDBListQuery(db, source, query)
1594 {
1595         std::set<unsigned int> found;
1596         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1597                 it != m_db->m_services.end(); ++it)
1598         {
1599                 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1600                 if (res)
1601                 {
1602                         unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1603                         if (found.find(dvbnamespace) == found.end())
1604                         {
1605                                 found.insert(dvbnamespace);
1606                                 eServiceReferenceDVB ref;
1607                                 ref.setDVBNamespace(dvbnamespace);
1608                                 ref.flags=eServiceReference::flagDirectory;
1609                                 char buf[128];
1610                                 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1611
1612                                 ref.path=buf+source.path;
1613                                 unsigned int pos=ref.path.find("FROM");
1614                                 ref.path.erase(pos);
1615                                 ref.path+="ORDER BY name";
1616 //                              eDebug("ref.path now %s", ref.path.c_str());
1617                                 m_list.push_back(ref);
1618
1619                                 ref.path=buf+source.path;
1620                                 pos=ref.path.find("FROM");
1621                                 ref.path.erase(pos+5);
1622                                 ref.path+="PROVIDERS ORDER BY name";
1623 //                              eDebug("ref.path now %s", ref.path.c_str());
1624                                 m_list.push_back(ref);
1625
1626                                 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1627                                 ref.path=buf+source.path;
1628                                 pos=ref.path.find("FROM");
1629                                 ref.path.erase(pos);
1630                                 ref.path+="ORDER BY name";
1631 //                              eDebug("ref.path now %s", ref.path.c_str());
1632                                 m_list.push_back(ref);
1633                         }
1634                 }
1635         }
1636         m_cursor=m_list.begin();
1637 }
1638
1639 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1640         :eDVBDBListQuery(db, source, query)
1641 {
1642         std::set<std::string> found;
1643         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1644                 it != m_db->m_services.end(); ++it)
1645         {
1646                 int res = !it->second->isHidden() && it->second->checkFilter(it->first, *query);
1647                 if (res)
1648                 {
1649                         const char *provider_name = it->second->m_provider_name.length() ?
1650                                 it->second->m_provider_name.c_str() :
1651                                 "Unknown";
1652                         if (found.find(std::string(provider_name)) == found.end())
1653                         {
1654                                 found.insert(std::string(provider_name));
1655                                 eServiceReferenceDVB ref;
1656                                 char buf[64];
1657                                 ref.name=provider_name;
1658                                 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1659                                 ref.path=buf+source.path;
1660                                 unsigned int pos = ref.path.find("FROM");
1661                                 ref.flags=eServiceReference::flagDirectory;
1662                                 ref.path.erase(pos);
1663                                 ref.path+="ORDER BY name";
1664 //                              eDebug("ref.path now %s", ref.path.c_str());
1665                                 m_list.push_back(ref);
1666                         }
1667                 }
1668         }
1669         m_cursor=m_list.begin();
1670 }
1671
1672 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1673
1674 static int decodeType(const std::string &type)
1675 {
1676         if (type == "name")
1677                 return eDVBChannelQuery::tName;
1678         else if (type == "provider")
1679                 return eDVBChannelQuery::tProvider;
1680         else if (type == "type")
1681                 return eDVBChannelQuery::tType;
1682         else if (type == "bouquet")
1683                 return eDVBChannelQuery::tBouquet;
1684         else if (type == "satellitePosition")
1685                 return eDVBChannelQuery::tSatellitePosition;
1686         else if (type == "channelID")
1687                 return eDVBChannelQuery::tChannelID;
1688         else if (type == "flags")
1689                 return eDVBChannelQuery::tFlags;
1690         else
1691                 return -1;
1692 }
1693
1694         /* never, NEVER write a parser in C++! */
1695 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1696 {
1697         std::list<std::string>::const_iterator end_of_exp;
1698         
1699         if (begin == end)
1700                 return 0;
1701         
1702         if (*begin == "(")
1703         {
1704                 end_of_exp = begin;
1705                 while (end_of_exp != end)
1706                         if (*end_of_exp == ")")
1707                                 break;
1708                         else
1709                                 ++end_of_exp;
1710         
1711                 if (end_of_exp == end)
1712                 {
1713                         eDebug("expression parse: end of expression while searching for closing brace");
1714                         return -1;
1715                 }
1716                 
1717                 ++begin;
1718                 // begin..end_of_exp
1719                 int r = parseExpression(res, begin, end_of_exp);
1720                 if (r)
1721                         return r;
1722                 ++end_of_exp;
1723                 
1724                         /* we had only one sub expression */
1725                 if (end_of_exp == end)
1726                 {
1727 //                      eDebug("only one sub expression");
1728                         return 0;
1729                 }
1730                 
1731                         /* otherwise we have an operator here.. */
1732                 
1733                 ePtr<eDVBChannelQuery> r2 = res;
1734                 res = new eDVBChannelQuery();
1735                 res->m_sort = 0;
1736                 res->m_p1 = r2;
1737                 res->m_inverse = 0;
1738                 r2 = 0;
1739                 
1740                 if (*end_of_exp == "||")
1741                         res->m_type = eDVBChannelQuery::tOR;
1742                 else if (*end_of_exp == "&&")
1743                         res->m_type = eDVBChannelQuery::tAND;
1744                 else
1745                 {
1746                         eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1747                         res = 0;
1748                         return 1;
1749                 }
1750                 
1751                 ++end_of_exp;
1752                 
1753                 return parseExpression(res->m_p2, end_of_exp, end);
1754         }
1755         
1756         // "begin" <op> "end"
1757         std::string type, op, val;
1758         
1759         res = new eDVBChannelQuery();
1760         res->m_sort = 0;
1761         
1762         int cnt = 0;
1763         while (begin != end)
1764         {
1765                 switch (cnt)
1766                 {
1767                 case 0:
1768                         type = *begin;
1769                         break;
1770                 case 1:
1771                         op = *begin;
1772                         break;
1773                 case 2:
1774                         val = *begin;
1775                         break;
1776                 case 3:
1777                         eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1778                         return 1;
1779                 }
1780                 ++begin;
1781                 ++cnt;
1782         }
1783         
1784         if (cnt != 3)
1785         {
1786                 eDebug("malformed query: missing stuff");
1787                 res = 0;
1788                 return 1;
1789         }
1790         
1791         res->m_type = decodeType(type);
1792         
1793         if (res->m_type == -1)
1794         {
1795                 eDebug("malformed query: invalid type %s", type.c_str());
1796                 res = 0;
1797                 return 1;
1798         }
1799         
1800         if (op == "==")
1801                 res->m_inverse = 0;
1802         else if (op == "!=")
1803                 res->m_inverse = 1;
1804         else
1805         {
1806                 eDebug("invalid operator %s", op.c_str());
1807                 res = 0;
1808                 return 1;
1809         }
1810         
1811         res->m_string = val;
1812
1813         if (res->m_type == eDVBChannelQuery::tChannelID)
1814         {
1815                 int ns, tsid, onid;
1816                 if (sscanf(val.c_str(), "%08x%04x%04x", &ns, &tsid, &onid) == 3)
1817                         res->m_channelid = eDVBChannelID(eDVBNamespace(ns), eTransportStreamID(tsid), eOriginalNetworkID(onid));
1818                 else
1819                         eDebug("couldn't parse channelid !! format should be hex NNNNNNNNTTTTOOOO (namespace, tsid, onid)");
1820         }
1821         else
1822                 res->m_int = atoi(val.c_str());
1823
1824         return 0;
1825 }
1826
1827 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1828 {
1829         std::list<std::string> tokens;
1830         
1831         std::string current_token;
1832         std::string bouquet_name;
1833
1834 //      eDebug("splitting %s....", query.c_str());
1835         unsigned int i = 0;
1836         const char *splitchars="()";
1837         int quotemode = 0, lastsplit = 0, lastalnum = 0;
1838         while (i <= query.size())
1839         {
1840                 int c = (i < query.size()) ? query[i] : ' ';
1841                 ++i;
1842                 
1843                 int issplit = !!strchr(splitchars, c);
1844                 int isaln = isalnum(c);
1845                 int iswhite = c == ' ';
1846                 int isquot = c == '\"';
1847                 
1848                 if (quotemode)
1849                 {
1850                         iswhite = issplit = 0;
1851                         isaln = lastalnum;
1852                 }
1853                 
1854                 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1855                 {
1856                         if (current_token.size())
1857                                 tokens.push_back(current_token);
1858                         current_token.clear();
1859                 }
1860                 
1861                 if (!(iswhite || isquot))
1862                         current_token += c;
1863                 
1864                 if (isquot)
1865                         quotemode = !quotemode;
1866                 lastsplit = issplit;
1867                 lastalnum = isaln;
1868         }
1869         
1870 //      for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1871 //      {
1872 //              printf("%s\n", a->c_str());
1873 //      }
1874
1875         int sort = eDVBChannelQuery::tName;
1876                 /* check for "ORDER BY ..." */
1877
1878         std::list<std::string>::iterator it = tokens.begin();
1879         while (it != tokens.end())
1880         {
1881                 if (*it == "ORDER")
1882                 {
1883                         tokens.erase(it++);
1884                         if (it != tokens.end() && *it == "BY")
1885                         {
1886                                 tokens.erase(it++);
1887                                 sort = decodeType(*it);
1888                                 tokens.erase(it++);
1889                         } else
1890                                 sort = -1;
1891                 }
1892                 else if (*it == "FROM")
1893                 {
1894                         tokens.erase(it++);
1895                         if (it != tokens.end() && *it == "BOUQUET")
1896                         {
1897                                 tokens.erase(it++);
1898                                 bouquet_name = *it;
1899                                 tokens.erase(it++);
1900                         }
1901                         else if (it != tokens.end() && *it == "SATELLITES")
1902                                 tokens.erase(it++);
1903                         else if (it != tokens.end() && *it == "PROVIDERS")
1904                                 tokens.erase(it++);
1905                         else
1906                         {
1907                                 eDebug("FROM unknown %s", (*it).c_str());
1908                                 tokens.erase(it++);
1909                         }
1910                 }
1911                 else
1912                         ++it;
1913         }
1914
1915         if (sort == -1)
1916         {
1917                 eWarning("ORDER BY .. string invalid.");
1918                 res = 0;
1919                 return -1;
1920         }
1921         
1922 //      eDebug("sort by %d", sort);
1923         
1924                 /* now we recursivly parse that. */
1925         int r = parseExpression(res, tokens.begin(), tokens.end());
1926         
1927                 /* we have an empty (but valid!) expression */
1928         if (!r && !res)
1929         {
1930                 res = new eDVBChannelQuery();
1931                 res->m_inverse = 0;
1932                 res->m_type = eDVBChannelQuery::tAny;
1933         }
1934         
1935         if (res)
1936         {
1937                 res->m_sort = sort;
1938                 res->m_bouquet_name = bouquet_name;
1939         }
1940
1941 //      eDebug("return: %d", r);
1942         return r;
1943 }
1944
1945 DEFINE_REF(eDVBChannelQuery);