add ability to copy providers or all services from satellites to favourites (this...
[enigma2.git] / lib / dvb / db.cpp
index 38a062a2a5cd1dc562462996bc575e2c911c1193..e0fadc5ea0fb6c277a455a5a2193a6f054744fd1 100644 (file)
@@ -1,5 +1,6 @@
 #include <errno.h>
 #include <lib/dvb/db.h>
+#include <lib/dvb/dvb.h>
 #include <lib/dvb/frontend.h>
 #include <lib/dvb/epgcache.h>
 #include <lib/base/eerror.h>
@@ -65,7 +66,7 @@ RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
 
 RESULT eBouquet::flushChanges()
 {
-       FILE *f=fopen(m_path.c_str(), "wt");
+       FILE *f=fopen((CONFIGDIR"/enigma2/"+m_filename).c_str(), "w");
        if (!f)
                return -1;
        if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
@@ -102,10 +103,16 @@ RESULT eBouquet::flushChanges()
        return 0;
 err:
        fclose(f);
-       eDebug("couldn't write file %s", m_path.c_str());
+       eDebug("couldn't write file %s", m_filename.c_str());
        return -1;
 }
 
+RESULT eBouquet::setListName(const std::string &name)
+{
+       m_bouquet_name = name;
+       return 0;
+}
+
 eDVBService::eDVBService()
 {
 }
@@ -131,7 +138,7 @@ void eDVBService::genSortName()
        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";
@@ -139,7 +146,7 @@ void eDVBService::genSortName()
 
 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
 {
-       if (!ref.name.empty())  
+       if (!ref.name.empty())
                name = ref.name; // use renamed service name..
        else if (!m_service_name.empty())
                name = m_service_name;
@@ -148,10 +155,24 @@ RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
        return 0;
 }
 
-RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr)
+RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
 {
-       time_t t=-1;
-       return eEPGCache::getInstance()->lookupEventTime(ref, t, ptr);
+       return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
+}
+
+bool eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
+{
+       ePtr<eDVBResourceManager> res_mgr;
+       if ( eDVBResourceManager::getInstance( res_mgr ) )
+               eDebug("isPlayble... no res manager!!");
+       else
+       {
+               eDVBChannelID chid, chid_ignore;
+               ((const eServiceReferenceDVB&)ref).getChannelID(chid);
+               ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
+               return res_mgr->canAllocateChannel(chid, chid_ignore);
+       }
+       return false;
 }
 
 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
@@ -205,18 +226,29 @@ int eDVBService::getCachePID(cacheID id)
 
 void eDVBService::setCachePID(cacheID id, int pid)
 {
-       m_cache[id] = pid;
+       if (pid == -1)
+               m_cache.erase(id);
+       else
+               m_cache[id] = pid;
 }
 
 DEFINE_REF(eDVBDB);
 
        /* THIS CODE IS BAD. it should be replaced by somethine better. */
-void eDVBDB::load()
+void eDVBDB::reloadServicelist()
 {
        eDebug("---- opening lame channel db");
-       FILE *f=fopen("lamedb", "rt");
+       FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "rt");
        if (!f)
+       {
+               struct stat s;
+               if ( !stat("lamedb", &s) )
+               {
+                       rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
+                       reloadServicelist();
+               }
                return;
+       }
        char line[256];
        if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
        {
@@ -264,7 +296,8 @@ void eDVBDB::load()
                                sat.symbol_rate = symbol_rate;
                                sat.polarisation = polarisation;
                                sat.fec = fec;
-                               sat.orbital_position = orbital_position;
+                               sat.orbital_position =
+                                       orbital_position < 0 ? orbital_position + 3600 : orbital_position;
                                sat.inversion = inversion;
                                feparm->setDVBS(sat);
                        } else if (line[1]=='t')
@@ -281,7 +314,7 @@ void eDVBDB::load()
                                ter.guard_interval = guard_interval;
                                ter.hierarchy = hierarchy;
                                ter.inversion = inversion;
-                               
+
                                feparm->setDVBT(ter);
                        } else if (line[1]=='c')
                        {
@@ -329,11 +362,10 @@ void eDVBDB::load()
 
                s->m_service_name = line;
                s->genSortName();
-                
+
                fgets(line, 256, f);
                if (strlen(line))
                        line[strlen(line)-1]=0;
-
                std::string str=line;
 
                if (str[1]!=':')        // old ... (only service_provider)
@@ -380,10 +412,10 @@ void eDVBDB::load()
        fclose(f);
 }
 
-void eDVBDB::save()
+void eDVBDB::saveServicelist()
 {
        eDebug("---- saving lame channel db");
-       FILE *f=fopen("lamedb", "wt");
+       FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "w");
        int channels=0, services=0;
        if (!f)
                eFatal("couldn't save lame channel db!");
@@ -403,7 +435,8 @@ void eDVBDB::save()
                {
                        fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
                                sat.frequency, sat.symbol_rate,
-                               sat.polarisation, sat.fec, sat.orbital_position,
+                               sat.polarisation, sat.fec,
+                               sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
                                sat.inversion);
                }
                if (!ch.m_frontendParameters->getDVBT(ter))
@@ -466,15 +499,24 @@ void eDVBDB::loadBouquet(const char *path)
                return;
        }
        eBouquet &bouquet = m_bouquets[bouquet_name];
-       bouquet.m_path = path;
+       bouquet.m_filename = bouquet_name;
        std::list<eServiceReference> &list = bouquet.m_services;
        list.clear();
 
-       eDebug("loading bouquet... %s", path);
-       FILE *fp=fopen(path, "rt");
+       std::string p = CONFIGDIR"/enigma2/";
+       p+=path;
+       eDebug("loading bouquet... %s", p.c_str());
+       FILE *fp=fopen(p.c_str(), "rt");
        int entries=0;
        if (!fp)
        {
+               struct stat s;
+               if ( !stat(path, &s) )
+               {
+                       rename(path, p.c_str() );
+                       loadBouquet(path);
+                       return;
+               }
                eDebug("failed to open.");
                if ( strstr(path, "bouquets.tv") )
                {
@@ -515,18 +557,17 @@ void eDVBDB::loadBouquet(const char *path)
                                }
                                if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
                                {
-                                       std::string str = tmp.path;
-                                       unsigned int pos = str.rfind('/');
+                                       unsigned int pos = tmp.path.rfind('/');
                                        if ( pos != std::string::npos )
-                                               str.erase(0, pos+1);
-                                       if (str.empty())
+                                               tmp.path.erase(0, pos+1);
+                                       if (tmp.path.empty())
                                        {
                                                eDebug("Bouquet load failed.. no filename given..");
                                                continue;
                                        }
                                        loadBouquet(tmp.path.c_str());
                                        char buf[256];
-                                       snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], str.c_str());
+                                       snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], tmp.path.c_str());
                                        tmp.path = buf;
                                }
                                list.push_back(tmp);
@@ -548,15 +589,16 @@ void eDVBDB::loadBouquet(const char *path)
        eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
 }
 
-void eDVBDB::loadBouquets()
+void eDVBDB::reloadBouquets()
 {
+       m_bouquets.clear();
        loadBouquet("bouquets.tv");
        loadBouquet("bouquets.radio");
 // 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_filename = "userbouquet.favourites.tv";
                b.m_bouquet_name = "Favourites (TV)";
                b.flushChanges();
                eServiceReference ref;
@@ -572,7 +614,7 @@ void eDVBDB::loadBouquets()
        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_filename = "userbouquet.favourites.radio";
                b.m_bouquet_name = "Favourites (Radio)";
                b.flushChanges();
                eServiceReference ref;
@@ -587,15 +629,17 @@ void eDVBDB::loadBouquets()
        }
 }
 
+eDVBDB *eDVBDB::instance;
+
 eDVBDB::eDVBDB()
 {
-       load();
-       loadBouquets();
+       instance = this;
+       reloadServicelist();
 }
 
 eDVBDB::~eDVBDB()
 {
-//     save();
+       instance=NULL;
 }
 
 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
@@ -646,7 +690,7 @@ RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBServic
 
 RESULT eDVBDB::flush()
 {
-       save();
+       saveServicelist();
        return 0;
 }
 
@@ -814,7 +858,15 @@ RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
 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);
+       {
+               int x = (a.getDVBNamespace().get() >> 16);
+               int y = (b.getDVBNamespace().get() >> 16);
+               if ( x > 1800 )
+                       x -= 3600;
+               if ( y > 1800 )
+                       y -= 3600;
+               return x < y;
+       }
        return a.name < b.name;
 }
 
@@ -839,15 +891,12 @@ eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference
                        {
                                eServiceReferenceDVB ref;
                                ref.setDVBNamespace(dvbnamespace);
+                               ref.flags=eServiceReference::flagDirectory;
                                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());
@@ -858,8 +907,6 @@ eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference
                                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);
                        }
                }