add function to remove single service,
[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 <dvbsi++/service_description_section.h>
9 #include <dvbsi++/descriptor_tag.h>
10 #include <dvbsi++/service_descriptor.h>
11 #include <dvbsi++/satellite_delivery_system_descriptor.h>
12
13 DEFINE_REF(eDVBService);
14
15 RESULT eBouquet::addService(const eServiceReference &ref)
16 {
17         list::iterator it =
18                 std::find(m_services.begin(), m_services.end(), ref);
19         if ( it != m_services.end() )
20                 return -1;
21         m_services.push_back(ref);
22         return 0;
23 }
24
25 RESULT eBouquet::removeService(const eServiceReference &ref)
26 {
27         list::iterator it =
28                 std::find(m_services.begin(), m_services.end(), ref);
29         if ( it == m_services.end() )
30                 return -1;
31         m_services.erase(it);
32         return 0;
33 }
34
35 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
36 {
37         if ( pos < 0 || pos >= m_services.size() )
38                 return -1;
39         ++pos;
40         list::iterator source=m_services.end();
41         list::iterator dest=m_services.end();
42         bool forward = false;
43         for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
44         {
45                 if (dest == m_services.end() && !--pos)
46                         dest = it;
47                 if (*it == ref)
48                 {
49                         source = it;
50                         forward = pos>0;
51                 }
52                 if (dest != m_services.end() && source != m_services.end())
53                         break;
54         }
55         if (dest == m_services.end() || source == m_services.end() || source == dest)
56                 return -1;
57         while (source != dest)
58         {
59                 if (forward)
60                         std::iter_swap(source++, source);
61                 else
62                         std::iter_swap(source--, source);
63         }
64         return 0;
65 }
66
67 RESULT eBouquet::flushChanges()
68 {
69         FILE *f=fopen((CONFIGDIR"/enigma2/"+m_filename).c_str(), "w");
70         if (!f)
71                 return -1;
72         if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
73                 goto err;
74         for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
75         {
76                 eServiceReference tmp = *i;
77                 std::string str = tmp.path;
78                 if ( (i->flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
79                 {
80                         unsigned int p1 = str.find("FROM BOUQUET \"");
81                         if (p1 == std::string::npos)
82                         {
83                                 eDebug("doof... kaputt");
84                                 continue;
85                         }
86                         str.erase(0, p1+14);
87                         p1 = str.find("\"");
88                         if (p1 == std::string::npos)
89                         {
90                                 eDebug("doof2... kaputt");
91                                 continue;
92                         }
93                         str.erase(p1);
94                         tmp.path=str;
95                 }
96                 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
97                         goto err;
98                 if ( i->name.length() )
99                         if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
100                                 goto err;
101         }
102         fclose(f);
103         return 0;
104 err:
105         fclose(f);
106         eDebug("couldn't write file %s", m_filename.c_str());
107         return -1;
108 }
109
110 RESULT eBouquet::setListName(const std::string &name)
111 {
112         m_bouquet_name = name;
113         return 0;
114 }
115
116 eDVBService::eDVBService()
117         :m_flags(0)
118 {
119 }
120
121 eDVBService::~eDVBService()
122 {
123 }
124
125 eDVBService &eDVBService::operator=(const eDVBService &s)
126 {
127         m_service_name = s.m_service_name;
128         m_service_name_sort = s.m_service_name_sort;
129         m_provider_name = s.m_provider_name;
130         m_flags = s.m_flags;
131         m_ca = s.m_ca;
132         m_cache = s.m_cache;
133         return *this;
134 }
135
136 void eDVBService::genSortName()
137 {
138         m_service_name_sort = removeDVBChars(m_service_name);
139         makeUpper(m_service_name_sort);
140         while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
141                 m_service_name_sort.erase(0, 1);
142
143                 /* put unnamed services at the end, not at the beginning. */
144         if (m_service_name_sort.empty())
145                 m_service_name_sort = "\xFF";
146 }
147
148 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
149 {
150         if (!ref.name.empty())
151                 name = ref.name; // use renamed service name..
152         else if (!m_service_name.empty())
153                 name = m_service_name;
154         else
155                 name = "(...)";
156         return 0;
157 }
158
159 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
160 {
161         return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
162 }
163
164 bool eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
165 {
166         ePtr<eDVBResourceManager> res_mgr;
167         if ( eDVBResourceManager::getInstance( res_mgr ) )
168                 eDebug("isPlayble... no res manager!!");
169         else
170         {
171                 eDVBChannelID chid, chid_ignore;
172                 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
173                 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
174                 return res_mgr->canAllocateChannel(chid, chid_ignore);
175         }
176         return false;
177 }
178
179 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
180 {
181         int res = 0;
182         switch (query.m_type)
183         {
184         case eDVBChannelQuery::tName:
185                 res = m_service_name_sort.find(query.m_string) != std::string::npos;
186                 break;
187         case eDVBChannelQuery::tProvider:
188                 res = m_provider_name.find(query.m_string) != std::string::npos;
189                 break;
190         case eDVBChannelQuery::tType:
191                 res = ref.getServiceType() == query.m_int;
192                 break;
193         case eDVBChannelQuery::tBouquet:
194                 res = 0;
195                 break;
196         case eDVBChannelQuery::tSatellitePosition:
197                 res = ((unsigned int)ref.getDVBNamespace().get())>>16 == (unsigned int)query.m_int;
198                 break;
199         case eDVBChannelQuery::tFlags:
200                 res = (m_flags & query.m_int) == query.m_int;
201                 break;
202         case eDVBChannelQuery::tChannelID:
203         {
204                 eDVBChannelID chid;
205                 ref.getChannelID(chid);
206                 res = chid == query.m_channelid;
207                 break;
208         }
209         case eDVBChannelQuery::tAND:
210                 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
211                 break;
212         case eDVBChannelQuery::tOR:
213                 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
214                 break;
215         case eDVBChannelQuery::tAny:
216                 res = 1;
217                 break;
218         }
219
220         if (query.m_inverse)
221                 return !res;
222         else
223                 return res;
224 }
225
226 int eDVBService::getCachePID(cacheID id)
227 {
228         std::map<int, int>::iterator it = m_cache.find(id);
229         if ( it != m_cache.end() )
230                 return it->second;
231         return -1;
232 }
233
234 void eDVBService::setCachePID(cacheID id, int pid)
235 {
236         if (pid == -1)
237                 m_cache.erase(id);
238         else
239                 m_cache[id] = pid;
240 }
241
242 DEFINE_REF(eDVBDB);
243
244         /* THIS CODE IS BAD. it should be replaced by somethine better. */
245 void eDVBDB::reloadServicelist()
246 {
247         eDebug("---- opening lame channel db");
248         FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "rt");
249         if (!f)
250         {
251                 struct stat s;
252                 if ( !stat("lamedb", &s) )
253                 {
254                         if ( !stat(CONFIGDIR"/enigma2", &s) )
255                         {
256                                 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
257                                 reloadServicelist();
258                         }
259                 }
260                 return;
261         }
262         char line[256];
263         if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
264         {
265                 eDebug("not a servicefile");
266                 fclose(f);
267                 return;
268         }
269         eDebug("reading services");
270         if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
271         {
272                 eDebug("services invalid, no transponders");
273                 fclose(f);
274                 return;
275         }
276
277         // clear all transponders
278
279         while (!feof(f))
280         {
281                 if (!fgets(line, 256, f))
282                         break;
283                 if (!strcmp(line, "end\n"))
284                         break;
285                 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
286                 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
287                 if (original_network_id == -1)
288                         continue;
289                 eDVBChannelID channelid = eDVBChannelID(
290                         eDVBNamespace(dvb_namespace),
291                         eTransportStreamID(transport_stream_id),
292                         eOriginalNetworkID(original_network_id));
293
294                 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
295                 while (!feof(f))
296                 {
297                         fgets(line, 256, f);
298                         if (!strcmp(line, "/\n"))
299                                 break;
300                         if (line[1]=='s')
301                         {
302                                 eDVBFrontendParametersSatellite sat;
303                                 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
304                                 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
305                                 sat.frequency = frequency;
306                                 sat.symbol_rate = symbol_rate;
307                                 sat.polarisation = polarisation;
308                                 sat.fec = fec;
309                                 sat.orbital_position =
310                                         orbital_position < 0 ? orbital_position + 3600 : orbital_position;
311                                 sat.inversion = inversion;
312                                 feparm->setDVBS(sat);
313                         } else if (line[1]=='t')
314                         {
315                                 eDVBFrontendParametersTerrestrial ter;
316                                 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
317                                 sscanf(line+2, "%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &bandwidth, &code_rate_HP, &code_rate_LP, &modulation, &transmission_mode, &guard_interval, &hierarchy, &inversion);
318                                 ter.frequency = frequency;
319                                 ter.bandwidth = bandwidth;
320                                 ter.code_rate_HP = code_rate_HP;
321                                 ter.code_rate_LP = code_rate_LP;
322                                 ter.modulation = modulation;
323                                 ter.transmission_mode = transmission_mode;
324                                 ter.guard_interval = guard_interval;
325                                 ter.hierarchy = hierarchy;
326                                 ter.inversion = inversion;
327                                 feparm->setDVBT(ter);
328                         } else if (line[1]=='c')
329                         {
330                                 eDVBFrontendParametersCable cab;
331                                 int frequency, symbol_rate,
332                                         inversion=eDVBFrontendParametersCable::Inversion::Unknown,
333                                         modulation=eDVBFrontendParametersCable::Modulation::Auto,
334                                         fec_inner=eDVBFrontendParametersCable::FEC::fAuto;
335                                 sscanf(line+2, "%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner);
336                                 cab.frequency = frequency;
337                                 cab.fec_inner = fec_inner;
338                                 cab.inversion = inversion;
339                                 cab.symbol_rate = symbol_rate;
340                                 cab.modulation = modulation;
341                                 feparm->setDVBC(cab);
342                         }
343                 }
344                 addChannelToList(channelid, feparm);
345         }
346
347         if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
348         {
349                 eDebug("services invalid, no services");
350                 return;
351         }
352
353         // clear all services
354
355         int count=0;
356
357         while (!feof(f))
358         {
359                 if (!fgets(line, 256, f))
360                         break;
361                 if (!strcmp(line, "end\n"))
362                         break;
363
364                 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
365                 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
366                 if (service_number == -1)
367                         continue;
368                 ePtr<eDVBService> s = new eDVBService;
369                 eServiceReferenceDVB ref =
370                                                 eServiceReferenceDVB(
371                                                 eDVBNamespace(dvb_namespace),
372                                                 eTransportStreamID(transport_stream_id),
373                                                 eOriginalNetworkID(original_network_id),
374                                                 eServiceID(service_id),
375                                                 service_type);
376                 count++;
377                 fgets(line, 256, f);
378                 if (strlen(line))
379                         line[strlen(line)-1]=0;
380
381                 s->m_service_name = line;
382                 s->genSortName();
383
384                 fgets(line, 256, f);
385                 if (strlen(line))
386                         line[strlen(line)-1]=0;
387                 std::string str=line;
388
389                 if (str[1]!=':')        // old ... (only service_provider)
390                 {
391                         s->m_provider_name=line;
392                 } else
393                         while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
394                         {
395                                 unsigned int c=str.find(',');
396                                 char p=str[0];
397                                 std::string v;
398                                 if (c == std::string::npos)
399                                 {
400                                         v=str.substr(2);
401                                         str="";
402                                 } else
403                                 {
404                                         v=str.substr(2, c-2);
405                                         str=str.substr(c+1);
406                                 }
407 //                              eDebug("%c ... %s", p, v.c_str());
408                                 if (p == 'p')
409                                         s->m_provider_name=v;
410                                 else if (p == 'f')
411                                 {
412                                         sscanf(v.c_str(), "%x", &s->m_flags);
413                                 } else if (p == 'c')
414                                 {
415                                         int cid, val;
416                                         sscanf(v.c_str(), "%02d%04x", &cid, &val);
417                                         s->m_cache[cid]=val;
418                                 } else if (p == 'C')
419                                 {
420                                         int val;
421                                         sscanf(v.c_str(), "%04x", &val);
422                                         s->m_ca.insert(val);
423                                 }
424                         }
425                 addService(ref, s);
426         }
427
428         eDebug("loaded %d services", count);
429
430         fclose(f);
431 }
432
433 void eDVBDB::saveServicelist()
434 {
435         eDebug("---- saving lame channel db");
436         FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "w");
437         int channels=0, services=0;
438         if (!f)
439                 eFatal("couldn't save lame channel db!");
440         fprintf(f, "eDVB services /3/\n");
441         fprintf(f, "transponders\n");
442         for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
443                         i != m_channels.end(); ++i)
444         {
445                 const eDVBChannelID &chid = i->first;
446                 const channel &ch = i->second;
447
448                 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
449                                 chid.transport_stream_id.get(), chid.original_network_id.get());
450                 eDVBFrontendParametersSatellite sat;
451                 eDVBFrontendParametersTerrestrial ter;
452                 eDVBFrontendParametersCable cab;
453                 if (!ch.m_frontendParameters->getDVBS(sat))
454                 {
455                         fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
456                                 sat.frequency, sat.symbol_rate,
457                                 sat.polarisation, sat.fec,
458                                 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
459                                 sat.inversion);
460                 }
461                 if (!ch.m_frontendParameters->getDVBT(ter))
462                 {
463                         fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
464                                 ter.frequency, ter.bandwidth, ter.code_rate_HP,
465                                 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
466                                 ter.guard_interval, ter.hierarchy, ter.inversion);
467                 }
468                 if (!ch.m_frontendParameters->getDVBC(cab))
469                 {
470                         fprintf(f, "\tc %d:%d:%d:%d:%d\n",
471                                 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner);
472                 }
473                 fprintf(f, "/\n");
474                 channels++;
475         }
476         fprintf(f, "end\nservices\n");
477
478         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
479                 i != m_services.end(); ++i)
480         {
481                 const eServiceReferenceDVB &s = i->first;
482                 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
483                                 s.getServiceID().get(), s.getDVBNamespace().get(),
484                                 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
485                                 s.getServiceType(),
486                                 0);
487
488                 fprintf(f, "%s\n", i->second->m_service_name.c_str());
489
490                 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
491
492                 // write cached pids
493                 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
494                         ca != i->second->m_cache.end(); ++ca)
495                         fprintf(f, ",c:%02d%04x", ca->first, ca->second);
496
497                 // write cached ca pids
498                 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
499                         ca != i->second->m_ca.end(); ++ca)
500                         fprintf(f, ",C:%04x", *ca);
501
502                 if (i->second->m_flags)
503                         fprintf(f, ",f:%x", i->second->m_flags);
504
505                 fprintf(f, "\n");
506                 services++;
507         }
508         fprintf(f, "end\nHave a lot of bugs!\n");
509         eDebug("saved %d channels and %d services!", channels, services);
510         fclose(f);
511 }
512
513 void eDVBDB::loadBouquet(const char *path)
514 {
515         std::string bouquet_name = path;
516         if (!bouquet_name.length())
517         {
518                 eDebug("Bouquet load failed.. no path given..");
519                 return;
520         }
521         unsigned int pos = bouquet_name.rfind('/');
522         if ( pos != std::string::npos )
523                 bouquet_name.erase(0, pos+1);
524         if (bouquet_name.empty())
525         {
526                 eDebug("Bouquet load failed.. no filename given..");
527                 return;
528         }
529         eBouquet &bouquet = m_bouquets[bouquet_name];
530         bouquet.m_filename = bouquet_name;
531         std::list<eServiceReference> &list = bouquet.m_services;
532         list.clear();
533
534         std::string p = CONFIGDIR"/enigma2/";
535         p+=path;
536         eDebug("loading bouquet... %s", p.c_str());
537         FILE *fp=fopen(p.c_str(), "rt");
538         int entries=0;
539         if (!fp)
540         {
541                 struct stat s;
542                 if ( !stat(path, &s) )
543                 {
544                         rename(path, p.c_str() );
545                         loadBouquet(path);
546                         return;
547                 }
548                 eDebug("failed to open.");
549                 if ( strstr(path, "bouquets.tv") )
550                 {
551                         eDebug("recreate bouquets.tv");
552                         bouquet.m_bouquet_name="Bouquets (TV)";
553                         bouquet.flushChanges();
554                 }
555                 else if ( strstr(path, "bouquets.radio") )
556                 {
557                         eDebug("recreate bouquets.radio");
558                         bouquet.m_bouquet_name="Bouquets (Radio)";
559                         bouquet.flushChanges();
560                 }
561                 return;
562         }
563         char line[256];
564         bool read_descr=false;
565         eServiceReference *e = NULL;
566         while (1)
567         {
568                 if (!fgets(line, 256, fp))
569                         break;
570                 line[strlen(line)-1]=0;
571                 if (strlen(line) && line[strlen(line)-1]=='\r')
572                         line[strlen(line)-1]=0;
573                 if (!line[0])
574                         break;
575                 if (line[0]=='#')
576                 {
577                         if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
578                         {
579                                 int offs = line[8] == ':' ? 10 : 9;
580                                 eServiceReference tmp(line+offs);
581                                 if (tmp.type != eServiceReference::idDVB)
582                                 {
583                                         eDebug("only DVB Bouquets supported");
584                                         continue;
585                                 }
586                                 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
587                                 {
588                                         unsigned int pos = tmp.path.rfind('/');
589                                         if ( pos != std::string::npos )
590                                                 tmp.path.erase(0, pos+1);
591                                         if (tmp.path.empty())
592                                         {
593                                                 eDebug("Bouquet load failed.. no filename given..");
594                                                 continue;
595                                         }
596                                         loadBouquet(tmp.path.c_str());
597                                         char buf[256];
598                                         snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], tmp.path.c_str());
599                                         tmp.path = buf;
600                                 }
601                                 list.push_back(tmp);
602                                 e = &list.back();
603                                 read_descr=true;
604                                 ++entries;
605                         }
606                         else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
607                         {
608                                 e->name = line+13;
609                                 read_descr=false;
610                         }
611                         else if (!strncmp(line, "#NAME ", 6))
612                                 bouquet.m_bouquet_name=line+6;
613                         continue;
614                 }
615         }
616         fclose(fp);
617         eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
618 }
619
620 void eDVBDB::reloadBouquets()
621 {
622         m_bouquets.clear();
623         loadBouquet("bouquets.tv");
624         loadBouquet("bouquets.radio");
625 // create default bouquets when missing
626         if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
627         {
628                 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
629                 b.m_filename = "userbouquet.favourites.tv";
630                 b.m_bouquet_name = "Favourites (TV)";
631                 b.flushChanges();
632                 eServiceReference ref;
633                 memset(ref.data, 0, sizeof(ref.data));
634                 ref.type=1;
635                 ref.flags=7;
636                 ref.data[0]=1;
637                 ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
638                 eBouquet &parent = m_bouquets["bouquets.tv"];
639                 parent.m_services.push_back(ref);
640                 parent.flushChanges();
641         }
642         if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
643         {
644                 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
645                 b.m_filename = "userbouquet.favourites.radio";
646                 b.m_bouquet_name = "Favourites (Radio)";
647                 b.flushChanges();
648                 eServiceReference ref;
649                 memset(ref.data, 0, sizeof(ref.data));
650                 ref.type=1;
651                 ref.flags=7;
652                 ref.data[0]=2;
653                 ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
654                 eBouquet &parent = m_bouquets["bouquets.radio"];
655                 parent.m_services.push_back(ref);
656                 parent.flushChanges();
657         }
658 }
659
660 eDVBDB *eDVBDB::instance;
661
662 eDVBDB::eDVBDB()
663 {
664         instance = this;
665         reloadServicelist();
666 }
667
668 eDVBDB::~eDVBDB()
669 {
670         instance=NULL;
671 }
672
673 RESULT eDVBDB::removeService(eServiceReferenceDVB service)
674 {
675         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
676         if (it != m_services.end())
677         {
678                 m_services.erase(it);
679                 return 0;
680         }
681         return -1;
682 }
683
684 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
685 {
686         RESULT ret=-1;
687         eDVBNamespace eNs;
688         eTransportStreamID eTsid;
689         eOriginalNetworkID eOnid;
690         std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
691         std::set<eDVBChannelID> removed_chids;
692         while (it != m_channels.end())
693         {
694                 const eDVBChannelID &ch = it->first;
695                 bool remove=true;
696                 int system;
697                 it->second.m_frontendParameters->getSystem(system);
698                 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
699                 {
700                         eDVBFrontendParametersSatellite sat;
701                         it->second.m_frontendParameters->getDVBS(sat);
702                         if ((unsigned int)sat.orbital_position != orbpos)
703                                 remove=false;
704                 }
705                 if ( remove && chid.dvbnamespace != eNs && chid.dvbnamespace != ch.dvbnamespace )
706                         remove=false;
707                 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
708                         remove=false;
709                 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
710                         remove=false;
711                 if ( remove )
712                 {
713                         removed_chids.insert(it->first);
714                         m_channels.erase(it++);
715                 }
716                 else
717                         ++it;
718         }
719         if (!removed_chids.empty())
720         {
721                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
722                 while(service != m_services.end())
723                 {
724                         eDVBChannelID chid;
725                         service->first.getChannelID(chid);
726                         std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
727                         if (it != removed_chids.end())
728                                 m_services.erase(service++);
729                         else
730                                 ++service;
731                         ret=0;
732                 }
733         }
734         return ret;
735 }
736
737 RESULT eDVBDB::addFlag(eServiceReferenceDVB service, unsigned int flagmask)
738 {
739         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
740         if (it != m_services.end())
741         {
742                 it->second->m_flags |= ~flagmask;
743         }
744         return 0;
745 }
746
747 RESULT eDVBDB::removeFlag(eServiceReferenceDVB service, unsigned int flagmask)
748 {
749         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
750         if (it != m_services.end())
751                 it->second->m_flags &= ~flagmask;
752         return 0;
753 }
754
755 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
756 {
757         eDVBNamespace eNs;
758         eTransportStreamID eTsid;
759         eOriginalNetworkID eOnid;
760         std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
761         std::set<eDVBChannelID> removed_chids;
762         while (it != m_channels.end())
763         {
764                 const eDVBChannelID &ch = it->first;
765                 bool remove=true;
766                 int system;
767                 it->second.m_frontendParameters->getSystem(system);
768                 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
769                 {
770                         eDVBFrontendParametersSatellite sat;
771                         it->second.m_frontendParameters->getDVBS(sat);
772                         if ((unsigned int)sat.orbital_position != orbpos)
773                                 remove=false;
774                 }
775                 if ( remove && chid.dvbnamespace != eNs && chid.dvbnamespace != ch.dvbnamespace )
776                         remove=false;
777                 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
778                         remove=false;
779                 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
780                         remove=false;
781                 if ( remove )
782                         removed_chids.insert(it->first);
783                 ++it;
784         }
785         if (!removed_chids.empty())
786         {
787                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
788                 while(service != m_services.end())
789                 {
790                         eDVBChannelID chid;
791                         service->first.getChannelID(chid);
792                         std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
793                         if (it != removed_chids.end())
794                                 service->second->m_flags &= ~flagmask;
795                         ++service;
796                 }
797         }
798         return 0;
799 }
800
801
802 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
803 {
804         channel ch;
805         assert(feparm);
806         ch.m_frontendParameters = feparm;
807         m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
808         return 0;
809 }
810
811 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
812 {
813         m_channels.erase(id);
814         return 0;
815 }
816
817 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
818 {
819         std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
820         if (i == m_channels.end())
821         {
822                 parm = 0;
823                 return -ENOENT;
824         }
825         parm = i->second.m_frontendParameters;
826         return 0;
827 }
828
829 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
830 {
831         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
832         if (it == m_services.end())
833         {
834                 service->m_flags |= eDVBService::dxNewFound;
835                 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
836         }
837         else
838                 it->second->m_flags &= ~eDVBService::dxNewFound;
839         return 0;
840 }
841
842 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
843 {
844         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
845         i = m_services.find(reference);
846         if (i == m_services.end())
847         {
848                 service = 0;
849                 return -ENOENT;
850         }
851         service = i->second;
852         return 0;
853 }
854
855 RESULT eDVBDB::flush()
856 {
857         saveServicelist();
858         return 0;
859 }
860
861 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
862 {
863         std::string str = ref.path;
864         if (str.empty())
865         {
866                 eDebug("getBouquet failed.. no path given!");
867                 return -1;
868         }
869         unsigned int pos = str.find("FROM BOUQUET \"");
870         if ( pos != std::string::npos )
871         {
872                 str.erase(0, pos+14);
873                 pos = str.find('"');
874                 if ( pos != std::string::npos )
875                         str.erase(pos);
876                 else
877                         str.clear();
878         }
879         if (str.empty())
880         {
881                 eDebug("getBouquet failed.. couldn't parse bouquet name");
882                 return -1;
883         }
884         std::map<std::string, eBouquet>::iterator i =
885                 m_bouquets.find(str);
886         if (i == m_bouquets.end())
887         {
888                 bouquet = 0;
889                 return -ENOENT;
890         }
891         bouquet = &i->second;
892         return 0;
893 }
894
895 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
896 {
897         if ( source.path.find("FROM") != std::string::npos )
898         {
899                 if ( source.path.find("BOUQUET") != std::string::npos )
900                         query = new eDVBDBBouquetQuery(this, source, q);
901                 else if ( source.path.find("SATELLITES") != std::string::npos )
902                         query = new eDVBDBSatellitesQuery(this, source, q);
903                 else if ( source.path.find("PROVIDERS") != std::string::npos )
904                         query = new eDVBDBProvidersQuery(this, source, q);
905                 else
906                         eFatal("invalid query %s", source.toString().c_str());
907         }
908         else
909                 query = new eDVBDBQuery(this, source, q);
910         return 0;
911 }
912
913 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
914 {
915         eServiceID Sid(sid);
916         eTransportStreamID Tsid(tsid);
917         eOriginalNetworkID Onid(onid);
918         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
919                 sit != m_services.end(); ++sit)
920         {
921                 if (sit->first.getTransportStreamID() == Tsid &&
922                         sit->first.getOriginalNetworkID() == Onid &&
923                         sit->first.getServiceID() == Sid)
924                         return sit->first;
925         }
926         return eServiceReference();
927 }
928
929 DEFINE_REF(eDVBDBQueryBase);
930
931 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
932         :m_db(db), m_query(query), m_source(source)
933 {
934 }
935
936 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
937 {
938         ePtr<eDVBService> a_service, b_service;
939         
940         int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
941         
942         if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
943         {
944                 if (m_db->getService(a, a_service))
945                         return 1;
946                 if (m_db->getService(b, b_service))
947                         return 1;
948         }
949         
950         switch (sortmode)
951         {
952         case eDVBChannelQuery::tName:
953                 return a_service->m_service_name_sort < b_service->m_service_name_sort;
954         case eDVBChannelQuery::tProvider:
955                 return a_service->m_provider_name < b_service->m_provider_name;
956         case eDVBChannelQuery::tType:
957                 return a.getServiceType() < b.getServiceType();
958         case eDVBChannelQuery::tBouquet:
959                 return 0;
960         case eDVBChannelQuery::tSatellitePosition:
961                 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
962         default:
963                 return 1;
964         }
965         return 0;
966 }
967
968 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
969         :eDVBDBQueryBase(db, source, query)
970 {
971         m_cursor = m_db->m_services.begin();
972 }
973
974 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
975 {
976         while (m_cursor != m_db->m_services.end())
977         {
978                 ref = m_cursor->first;
979
980                 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
981
982                 ++m_cursor;
983
984                 if (res)
985                         return 0;
986         }
987
988         ref.type = eServiceReference::idInvalid;
989
990         return 1;
991 }
992
993 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
994         :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
995 {
996 }
997
998 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
999 {
1000         eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1001         std::list<eServiceReference> &list = bouquet.m_services;
1002         while (m_cursor != list.end())
1003         {
1004                 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1005
1006                 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1007                         m_db->m_services.find(ref);
1008
1009                 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
1010
1011                 ++m_cursor;
1012
1013                 if (res)
1014                         return 0;
1015         }
1016
1017         ref.type = eServiceReference::idInvalid;
1018
1019         return 1;
1020 }
1021
1022 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1023         :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1024 {
1025 }
1026
1027 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1028 {
1029         if (m_cursor != m_list.end())
1030         {
1031                 ref = *m_cursor++;
1032                 return 0;
1033         }
1034         ref.type = eServiceReference::idInvalid;
1035         return 1;
1036 }
1037
1038 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1039 {
1040         if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1041         {
1042                 int x = (a.getDVBNamespace().get() >> 16);
1043                 int y = (b.getDVBNamespace().get() >> 16);
1044                 if ( x > 1800 )
1045                         x -= 3600;
1046                 if ( y > 1800 )
1047                         y -= 3600;
1048                 return x < y;
1049         }
1050         return a.name < b.name;
1051 }
1052
1053 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1054         :eDVBDBListQuery(db, source, query)
1055 {
1056         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1057                 it != m_db->m_services.end(); ++it)
1058         {
1059                 int res = it->second->checkFilter(it->first, *query);
1060                 if (res)
1061                 {
1062                         unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1063                         bool found=0;
1064                         for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
1065                                 if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
1066                                 {
1067                                         found=true;
1068                                         break;
1069                                 }
1070                         if (!found)
1071                         {
1072                                 eServiceReferenceDVB ref;
1073                                 ref.setDVBNamespace(dvbnamespace);
1074                                 ref.flags=eServiceReference::flagDirectory;
1075                                 char buf[128];
1076                                 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1077
1078                                 ref.path=buf+source.path;
1079                                 unsigned int pos=ref.path.find("FROM");
1080                                 ref.path.erase(pos);
1081                                 ref.path+="ORDER BY name";
1082 //                              eDebug("ref.path now %s", ref.path.c_str());
1083                                 m_list.push_back(ref);
1084
1085                                 ref.path=buf+source.path;
1086                                 pos=ref.path.find("FROM");
1087                                 ref.path.erase(pos+5);
1088                                 ref.path+="PROVIDERS ORDER BY name";
1089 //                              eDebug("ref.path now %s", ref.path.c_str());
1090                                 m_list.push_back(ref);
1091
1092                                 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1093                                 ref.path=buf+source.path;
1094                                 pos=ref.path.find("FROM");
1095                                 ref.path.erase(pos);
1096                                 ref.path+="ORDER BY name";
1097 //                              eDebug("ref.path now %s", ref.path.c_str());
1098                                 m_list.push_back(ref);
1099                         }
1100                 }
1101         }
1102         m_cursor=m_list.begin();
1103 }
1104
1105 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1106         :eDVBDBListQuery(db, source, query)
1107 {
1108         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1109                 it != m_db->m_services.end(); ++it)
1110         {
1111                 int res = it->second->checkFilter(it->first, *query);
1112                 if (res)
1113                 {
1114                         bool found=0;
1115
1116                         const char *provider_name = it->second->m_provider_name.length() ?
1117                                 it->second->m_provider_name.c_str() :
1118                                 "Unknown";
1119
1120                         for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
1121                                 if (i->name == provider_name)
1122                                 {
1123                                         found=true;
1124                                         break;
1125                                 }
1126                         if (!found)
1127                         {
1128                                 eServiceReferenceDVB ref;
1129                                 char buf[64];
1130                                 ref.name=provider_name;
1131                                 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1132                                 ref.path=buf+source.path;
1133                                 unsigned int pos = ref.path.find("FROM");
1134                                 ref.flags=eServiceReference::flagDirectory;
1135                                 ref.path.erase(pos);
1136                                 ref.path+="ORDER BY name";
1137 //                              eDebug("ref.path now %s", ref.path.c_str());
1138                                 m_list.push_back(ref);
1139                         }
1140                 }
1141         }
1142         m_cursor=m_list.begin();
1143 }
1144
1145 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1146
1147 static int decodeType(const std::string &type)
1148 {
1149         if (type == "name")
1150                 return eDVBChannelQuery::tName;
1151         else if (type == "provider")
1152                 return eDVBChannelQuery::tProvider;
1153         else if (type == "type")
1154                 return eDVBChannelQuery::tType;
1155         else if (type == "bouquet")
1156                 return eDVBChannelQuery::tBouquet;
1157         else if (type == "satellitePosition")
1158                 return eDVBChannelQuery::tSatellitePosition;
1159         else if (type == "channelID")
1160                 return eDVBChannelQuery::tChannelID;
1161         else if (type == "flags")
1162                 return eDVBChannelQuery::tFlags;
1163         else
1164                 return -1;
1165 }
1166
1167         /* never, NEVER write a parser in C++! */
1168 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1169 {
1170         std::list<std::string>::const_iterator end_of_exp;
1171         
1172         if (begin == end)
1173         {
1174                 eDebug("empty expression!");
1175                 return 0;
1176         }
1177         
1178         if (*begin == "(")
1179         {
1180                 end_of_exp = begin;
1181                 while (end_of_exp != end)
1182                         if (*end_of_exp == ")")
1183                                 break;
1184                         else
1185                                 ++end_of_exp;
1186         
1187                 if (end_of_exp == end)
1188                 {
1189                         eDebug("expression parse: end of expression while searching for closing brace");
1190                         return -1;
1191                 }
1192                 
1193                 ++begin;
1194                 // begin..end_of_exp
1195                 int r = parseExpression(res, begin, end_of_exp);
1196                 if (r)
1197                         return r;
1198                 ++end_of_exp;
1199                 
1200                         /* we had only one sub expression */
1201                 if (end_of_exp == end)
1202                 {
1203 //                      eDebug("only one sub expression");
1204                         return 0;
1205                 }
1206                 
1207                         /* otherwise we have an operator here.. */
1208                 
1209                 ePtr<eDVBChannelQuery> r2 = res;
1210                 res = new eDVBChannelQuery();
1211                 res->m_sort = 0;
1212                 res->m_p1 = r2;
1213                 res->m_inverse = 0;
1214                 r2 = 0;
1215                 
1216                 if (*end_of_exp == "||")
1217                         res->m_type = eDVBChannelQuery::tOR;
1218                 else if (*end_of_exp == "&&")
1219                         res->m_type = eDVBChannelQuery::tAND;
1220                 else
1221                 {
1222                         eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1223                         res = 0;
1224                         return 1;
1225                 }
1226                 
1227                 ++end_of_exp;
1228                 
1229                 return parseExpression(res->m_p2, end_of_exp, end);
1230         }
1231         
1232         // "begin" <op> "end"
1233         std::string type, op, val;
1234         
1235         res = new eDVBChannelQuery();
1236         res->m_sort = 0;
1237         
1238         int cnt = 0;
1239         while (begin != end)
1240         {
1241                 switch (cnt)
1242                 {
1243                 case 0:
1244                         type = *begin;
1245                         break;
1246                 case 1:
1247                         op = *begin;
1248                         break;
1249                 case 2:
1250                         val = *begin;
1251                         break;
1252                 case 3:
1253                         eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1254                         return 1;
1255                 }
1256                 ++begin;
1257                 ++cnt;
1258         }
1259         
1260         if (cnt != 3)
1261         {
1262                 eDebug("malformed query: missing stuff");
1263                 res = 0;
1264                 return 1;
1265         }
1266         
1267         res->m_type = decodeType(type);
1268         
1269         if (res->m_type == -1)
1270         {
1271                 eDebug("malformed query: invalid type %s", type.c_str());
1272                 res = 0;
1273                 return 1;
1274         }
1275         
1276         if (op == "==")
1277                 res->m_inverse = 0;
1278         else if (op == "!=")
1279                 res->m_inverse = 1;
1280         else
1281         {
1282                 eDebug("invalid operator %s", op.c_str());
1283                 res = 0;
1284                 return 1;
1285         }
1286         
1287         res->m_string = val;
1288         res->m_int = atoi(val.c_str());
1289 //      res->m_channelid = eDVBChannelID(val);
1290         
1291         return 0;
1292 }
1293
1294 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1295 {
1296         std::list<std::string> tokens;
1297         
1298         std::string current_token;
1299         std::string bouquet_name;
1300
1301 //      eDebug("splitting %s....", query.c_str());
1302         unsigned int i = 0;
1303         const char *splitchars="()";
1304         int quotemode = 0, lastsplit = 0, lastalnum = 0;
1305         while (i <= query.size())
1306         {
1307                 int c = (i < query.size()) ? query[i] : ' ';
1308                 ++i;
1309                 
1310                 int issplit = !!strchr(splitchars, c);
1311                 int isaln = isalnum(c);
1312                 int iswhite = c == ' ';
1313                 int isquot = c == '\"';
1314                 
1315                 if (quotemode)
1316                 {
1317                         iswhite = issplit = 0;
1318                         isaln = lastalnum;
1319                 }
1320                 
1321                 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1322                 {
1323                         if (current_token.size())
1324                                 tokens.push_back(current_token);
1325                         current_token.clear();
1326                 }
1327                 
1328                 if (!(iswhite || isquot))
1329                         current_token += c;
1330                 
1331                 if (isquot)
1332                         quotemode = !quotemode;
1333                 lastsplit = issplit;
1334                 lastalnum = isaln;
1335         }
1336         
1337 //      for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1338 //      {
1339 //              printf("%s\n", a->c_str());
1340 //      }
1341
1342         int sort = eDVBChannelQuery::tName;
1343                 /* check for "ORDER BY ..." */
1344
1345         std::list<std::string>::iterator it = tokens.begin();
1346         while (it != tokens.end())
1347         {
1348                 if (*it == "ORDER")
1349                 {
1350                         tokens.erase(it++);
1351                         if (it != tokens.end() && *it == "BY")
1352                         {
1353                                 tokens.erase(it++);
1354                                 sort = decodeType(*it);
1355                                 tokens.erase(it++);
1356                         } else
1357                                 sort = -1;
1358                 }
1359                 else if (*it == "FROM")
1360                 {
1361                         tokens.erase(it++);
1362                         if (it != tokens.end() && *it == "BOUQUET")
1363                         {
1364                                 tokens.erase(it++);
1365                                 bouquet_name = *it;
1366                                 tokens.erase(it++);
1367                         }
1368                         else if (it != tokens.end() && *it == "SATELLITES")
1369                                 tokens.erase(it++);
1370                         else if (it != tokens.end() && *it == "PROVIDERS")
1371                                 tokens.erase(it++);
1372                         else
1373                         {
1374                                 eDebug("FROM unknown %s", (*it).c_str());
1375                                 tokens.erase(it++);
1376                         }
1377                 }
1378                 else
1379                         ++it;
1380         }
1381
1382         if (sort == -1)
1383         {
1384                 eWarning("ORDER BY .. string invalid.");
1385                 res = 0;
1386                 return -1;
1387         }
1388         
1389 //      eDebug("sort by %d", sort);
1390         
1391                 /* now we recursivly parse that. */
1392         int r = parseExpression(res, tokens.begin(), tokens.end());
1393         
1394                 /* we have an empty (but valid!) expression */
1395         if (!r && !res)
1396         {
1397                 res = new eDVBChannelQuery();
1398                 res->m_inverse = 0;
1399                 res->m_type = eDVBChannelQuery::tAny;
1400         }
1401         
1402         if (res)
1403         {
1404                 res->m_sort = sort;
1405                 res->m_bouquet_name = bouquet_name;
1406         }
1407
1408 //      eDebug("return: %d", r);
1409         return r;
1410 }
1411
1412 DEFINE_REF(eDVBChannelQuery);