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