remove non working indication of playable services in servicelist
[enigma2.git] / lib / dvb / db.cpp
index 98c1582096980faa59a74da2f1f73227e8d9fd48..38a062a2a5cd1dc562462996bc575e2c911c1193 100644 (file)
@@ -1,6 +1,7 @@
 #include <errno.h>
 #include <lib/dvb/db.h>
 #include <lib/dvb/frontend.h>
+#include <lib/dvb/epgcache.h>
 #include <lib/base/eerror.h>
 #include <lib/base/estring.h>
 #include <dvbsi++/service_description_section.h>
 
 DEFINE_REF(eDVBService);
 
+RESULT eBouquet::addService(const eServiceReference &ref)
+{
+       list::iterator it =
+               std::find(m_services.begin(), m_services.end(), ref);
+       if ( it != m_services.end() )
+               return -1;
+       m_services.push_back(ref);
+       return 0;
+}
+
+RESULT eBouquet::removeService(const eServiceReference &ref)
+{
+       list::iterator it =
+               std::find(m_services.begin(), m_services.end(), ref);
+       if ( it == m_services.end() )
+               return -1;
+       m_services.erase(it);
+       return 0;
+}
+
+RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
+{
+       if ( pos < 0 || pos >= m_services.size() )
+               return -1;
+       ++pos;
+       list::iterator source=m_services.end();
+       list::iterator dest=m_services.end();
+       bool forward = false;
+       for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
+       {
+               if (dest == m_services.end() && !--pos)
+                       dest = it;
+               if (*it == ref)
+               {
+                       source = it;
+                       forward = pos>0;
+               }
+               if (dest != m_services.end() && source != m_services.end())
+                       break;
+       }
+       if (dest == m_services.end() || source == m_services.end() || source == dest)
+               return -1;
+       while (source != dest)
+       {
+               if (forward)
+                       std::iter_swap(source++, source);
+               else
+                       std::iter_swap(source--, source);
+       }
+       return 0;
+}
+
+RESULT eBouquet::flushChanges()
+{
+       FILE *f=fopen(m_path.c_str(), "wt");
+       if (!f)
+               return -1;
+       if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
+               goto err;
+       for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
+       {
+               eServiceReference tmp = *i;
+               std::string str = tmp.path;
+               if ( (i->flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
+               {
+                       unsigned int p1 = str.find("FROM BOUQUET \"");
+                       if (p1 == std::string::npos)
+                       {
+                               eDebug("doof... kaputt");
+                               continue;
+                       }
+                       str.erase(0, p1+14);
+                       p1 = str.find("\"");
+                       if (p1 == std::string::npos)
+                       {
+                               eDebug("doof2... kaputt");
+                               continue;
+                       }
+                       str.erase(p1);
+                       tmp.path=str;
+               }
+               if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
+                       goto err;
+               if ( i->name.length() )
+                       if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
+                               goto err;
+       }
+       fclose(f);
+       return 0;
+err:
+       fclose(f);
+       eDebug("couldn't write file %s", m_path.c_str());
+       return -1;
+}
+
 eDVBService::eDVBService()
 {
 }
@@ -29,18 +125,33 @@ eDVBService &eDVBService::operator=(const eDVBService &s)
        return *this;
 }
 
+void eDVBService::genSortName()
+{
+       m_service_name_sort = removeDVBChars(m_service_name);
+       makeUpper(m_service_name_sort);
+       while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
+               m_service_name_sort.erase(0, 1);
+       
+               /* put unnamed services at the end, not at the beginning. */
+       if (m_service_name_sort.empty())
+               m_service_name_sort = "\xFF";
+}
+
 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
 {
-       if ( ref.name.length() )
-               name = ref.name;
-       else
+       if (!ref.name.empty())  
+               name = ref.name; // use renamed service name..
+       else if (!m_service_name.empty())
                name = m_service_name;
+       else
+               name = "(...)";
        return 0;
 }
 
-int eDVBService::getLength(const eServiceReference &ref)
+RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr)
 {
-       return -1;
+       time_t t=-1;
+       return eEPGCache::getInstance()->lookupEventTime(ref, t, ptr);
 }
 
 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
@@ -155,8 +266,6 @@ void eDVBDB::load()
                                sat.fec = fec;
                                sat.orbital_position = orbital_position;
                                sat.inversion = inversion;
-                               // ...
-//                             t.setSatellite(frequency, symbol_rate, polarisation, fec, sat, inversion);
                                feparm->setDVBS(sat);
                        } else if (line[1]=='t')
                        {
@@ -219,9 +328,8 @@ void eDVBDB::load()
                        line[strlen(line)-1]=0;
 
                s->m_service_name = line;
-               s->m_service_name_sort = removeDVBChars(line);
-               makeUpper(s->m_service_name_sort);
-
+               s->genSortName();
+                
                fgets(line, 256, f);
                if (strlen(line))
                        line[strlen(line)-1]=0;
@@ -368,6 +476,18 @@ void eDVBDB::loadBouquet(const char *path)
        if (!fp)
        {
                eDebug("failed to open.");
+               if ( strstr(path, "bouquets.tv") )
+               {
+                       eDebug("recreate bouquets.tv");
+                       bouquet.m_bouquet_name="Bouquets (TV)";
+                       bouquet.flushChanges();
+               }
+               else if ( strstr(path, "bouquets.radio") )
+               {
+                       eDebug("recreate bouquets.radio");
+                       bouquet.m_bouquet_name="Bouquets (Radio)";
+                       bouquet.flushChanges();
+               }
                return;
        }
        char line[256];
@@ -408,7 +528,6 @@ void eDVBDB::loadBouquet(const char *path)
                                        char buf[256];
                                        snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], str.c_str());
                                        tmp.path = buf;
-                                       eDebug("read bouquet %s", tmp.toString().c_str());
                                }
                                list.push_back(tmp);
                                e = &list.back();
@@ -429,20 +548,43 @@ void eDVBDB::loadBouquet(const char *path)
        eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
 }
 
-void eDVBDB::saveBouquet(const char *path)
-{
-
-}
-
 void eDVBDB::loadBouquets()
 {
        loadBouquet("bouquets.tv");
        loadBouquet("bouquets.radio");
-}
-
-void eDVBDB::saveBouquets()
-{
-
+// create default bouquets when missing
+       if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
+       {
+               eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
+               b.m_path = "userbouquet.favourites.tv";
+               b.m_bouquet_name = "Favourites (TV)";
+               b.flushChanges();
+               eServiceReference ref;
+               memset(ref.data, 0, sizeof(ref.data));
+               ref.type=1;
+               ref.flags=7;
+               ref.data[0]=1;
+               ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
+               eBouquet &parent = m_bouquets["bouquets.tv"];
+               parent.m_services.push_back(ref);
+               parent.flushChanges();
+       }
+       if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
+       {
+               eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
+               b.m_path = "userbouquet.favourites.radio";
+               b.m_bouquet_name = "Favourites (Radio)";
+               b.flushChanges();
+               eServiceReference ref;
+               memset(ref.data, 0, sizeof(ref.data));
+               ref.type=1;
+               ref.flags=7;
+               ref.data[0]=1;
+               ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
+               eBouquet &parent = m_bouquets["bouquets.radio"];
+               parent.m_services.push_back(ref);
+               parent.flushChanges();
+       }
 }
 
 eDVBDB::eDVBDB()
@@ -502,7 +644,13 @@ RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBServic
        return 0;
 }
 
-RESULT eDVBDB::getBouquet(const eServiceReference &ref, const eBouquet* &bouquet)
+RESULT eDVBDB::flush()
+{
+       save();
+       return 0;
+}
+
+RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
 {
        std::string str = ref.path;
        if (str.empty())
@@ -514,13 +662,11 @@ RESULT eDVBDB::getBouquet(const eServiceReference &ref, const eBouquet* &bouquet
        if ( pos != std::string::npos )
        {
                str.erase(0, pos+14);
-               eDebug("str now %s", str.c_str());
                pos = str.find('"');
                if ( pos != std::string::npos )
                        str.erase(pos);
                else
                        str.clear();
-               eDebug("str now %s", str.c_str());
        }
        if (str.empty())
        {
@@ -540,8 +686,17 @@ RESULT eDVBDB::getBouquet(const eServiceReference &ref, const eBouquet* &bouquet
 
 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
 {
-       if ( q && q->m_bouquet_name.length() )
-               query = new eDVBDBBouquetQuery(this, source, q);
+       if ( source.path.find("FROM") != std::string::npos )
+       {
+               if ( source.path.find("BOUQUET") != std::string::npos )
+                       query = new eDVBDBBouquetQuery(this, source, q);
+               else if ( source.path.find("SATELLITES") != std::string::npos )
+                       query = new eDVBDBSatellitesQuery(this, source, q);
+               else if ( source.path.find("PROVIDERS") != std::string::npos )
+                       query = new eDVBDBProvidersQuery(this, source, q);
+               else
+                       eFatal("invalid query %s", source.toString().c_str());
+       }
        else
                query = new eDVBDBQuery(this, source, q);
        return 0;
@@ -606,7 +761,8 @@ RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
                        return 0;
        }
 
-       ref = eServiceReferenceDVB();
+       ref.type = eServiceReference::idInvalid;
+
        return 1;
 }
 
@@ -634,11 +790,123 @@ RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
                        return 0;
        }
 
-       ref = eServiceReferenceDVB();
+       ref.type = eServiceReference::idInvalid;
+
+       return 1;
+}
+
+eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
+       :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
+{
+}
 
+RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
+{
+       if (m_cursor != m_list.end())
+       {
+               ref = *m_cursor++;
+               return 0;
+       }
+       ref.type = eServiceReference::idInvalid;
        return 1;
 }
 
+int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
+{
+       if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
+               return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
+       return a.name < b.name;
+}
+
+eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
+       :eDVBDBListQuery(db, source, query)
+{
+       for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
+               it != m_db->m_services.end(); ++it)
+       {
+               int res = it->second->checkFilter(it->first, *query);
+               if (res)
+               {
+                       unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
+                       bool found=0;
+                       for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
+                               if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
+                               {
+                                       found=true;
+                                       break;
+                               }
+                       if (!found)
+                       {
+                               eServiceReferenceDVB ref;
+                               ref.setDVBNamespace(dvbnamespace);
+                               char buf[64];
+// TODO get real satellite name..
+// but i dont like to parse the satellites.xml here.. and in the python part
+                               snprintf(buf, 64, "Services - %d", dvbnamespace>>16);
+                               ref.name=buf;
+                               snprintf(buf, 64, "(satellitePosition == %d) && ", dvbnamespace>>16);
+                               ref.path=buf+source.path;
+                               unsigned int pos=ref.path.find("FROM");
+                               ref.flags=eServiceReference::flagDirectory;
+                               ref.path.erase(pos);
+                               ref.path+="ORDER BY name";
+//                             eDebug("ref.path now %s", ref.path.c_str());
+                               m_list.push_back(ref);
+
+                               ref.path=buf+source.path;
+                               pos=ref.path.find("FROM");
+                               ref.path.erase(pos+5);
+                               ref.path+="PROVIDERS ORDER BY name";
+//                             eDebug("ref.path now %s", ref.path.c_str());
+                               snprintf(buf, 64, "Providers - %d", dvbnamespace>>16);
+                               ref.name=buf;
+                               m_list.push_back(ref);
+                       }
+               }
+       }
+       m_cursor=m_list.begin();
+}
+
+eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
+       :eDVBDBListQuery(db, source, query)
+{
+       for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
+               it != m_db->m_services.end(); ++it)
+       {
+               int res = it->second->checkFilter(it->first, *query);
+               if (res)
+               {
+                       bool found=0;
+
+                       const char *provider_name = it->second->m_provider_name.length() ?
+                               it->second->m_provider_name.c_str() :
+                               "Unknown";
+
+                       for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
+                               if (i->name == provider_name)
+                               {
+                                       found=true;
+                                       break;
+                               }
+                       if (!found)
+                       {
+                               eServiceReferenceDVB ref;
+                               char buf[64];
+                               ref.name=provider_name;
+                               snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
+                               ref.path=buf+source.path;
+                               unsigned int pos = ref.path.find("FROM");
+                               ref.flags=eServiceReference::flagDirectory;
+                               ref.path.erase(pos);
+                               ref.path+="ORDER BY name";
+//                             eDebug("ref.path now %s", ref.path.c_str());
+                               m_list.push_back(ref);
+                       }
+               }
+       }
+       m_cursor=m_list.begin();
+}
+
 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
 
 static int decodeType(const std::string &type)
@@ -688,7 +956,7 @@ RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::cons
                        /* we had only one sub expression */
                if (end_of_exp == end)
                {
-                       eDebug("only one sub expression");
+//                     eDebug("only one sub expression");
                        return 0;
                }
                
@@ -786,7 +1054,7 @@ RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
        std::string current_token;
        std::string bouquet_name;
 
-       eDebug("splitting %s....", query.c_str());
+//     eDebug("splitting %s....", query.c_str());
        unsigned int i = 0;
        const char *splitchars="()";
        int quotemode = 0, lastsplit = 0, lastalnum = 0;
@@ -830,37 +1098,41 @@ RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
        int sort = eDVBChannelQuery::tName;
                /* check for "ORDER BY ..." */
 
-       while (tokens.size() > 2)
+       std::list<std::string>::iterator it = tokens.begin();
+       while (it != tokens.end())
        {
-               std::list<std::string>::iterator it = tokens.end();
-               --it; --it; --it;
                if (*it == "ORDER")
                {
-                       ++it;
-                       if (*it == "BY")
+                       tokens.erase(it++);
+                       if (it != tokens.end() && *it == "BY")
                        {
-                               ++it;
+                               tokens.erase(it++);
                                sort = decodeType(*it);
-                               tokens.pop_back(); // ...
-                               tokens.pop_back(); // BY
-                               tokens.pop_back(); // ORDER
+                               tokens.erase(it++);
                        } else
                                sort = -1;
                }
                else if (*it == "FROM")
                {
-                       ++it;
-                       if (*it == "BOUQUET")
+                       tokens.erase(it++);
+                       if (it != tokens.end() && *it == "BOUQUET")
                        {
-                               ++it;
+                               tokens.erase(it++);
                                bouquet_name = *it;
-                               tokens.pop_back(); // ...
-                               tokens.pop_back(); // FROM
-                               tokens.pop_back(); // BOUQUET
+                               tokens.erase(it++);
+                       }
+                       else if (it != tokens.end() && *it == "SATELLITES")
+                               tokens.erase(it++);
+                       else if (it != tokens.end() && *it == "PROVIDERS")
+                               tokens.erase(it++);
+                       else
+                       {
+                               eDebug("FROM unknown %s", (*it).c_str());
+                               tokens.erase(it++);
                        }
                }
                else
-                       break;
+                       ++it;
        }
 
        if (sort == -1)
@@ -870,7 +1142,7 @@ RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
                return -1;
        }
        
-       eDebug("sort by %d", sort);
+//     eDebug("sort by %d", sort);
        
                /* now we recursivly parse that. */
        int r = parseExpression(res, tokens.begin(), tokens.end());
@@ -881,7 +1153,7 @@ RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
                res->m_bouquet_name = bouquet_name;
        }
 
-       eDebug("return: %d", r);
+//     eDebug("return: %d", r);
        return r;
 }