code cleanup/optimization
[enigma2.git] / lib / dvb / db.cpp
index de2de1e4e885a42e3a1f1814dc10be0f5d748dc0..9f4f2dc6f7726385aa931c3fffa5645a80cc526b 100644 (file)
@@ -51,6 +51,49 @@ RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
        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()
 {
 }
@@ -70,6 +113,18 @@ 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.empty())
@@ -260,15 +315,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);
-               while ((!s->m_service_name_sort.empty()) && s->m_service_name_sort[0] == ' ')
-                       s->m_service_name_sort.erase(0, 1);
-               
-                       /* put unnamed services at the end, not at the beginning. */
-               if (s->m_service_name_sort.empty())
-                       s->m_service_name_sort = "\xFF";
-               
+               s->genSortName();
+                
                fgets(line, 256, f);
                if (strlen(line))
                        line[strlen(line)-1]=0;
@@ -388,49 +436,6 @@ void eDVBDB::save()
        fclose(f);
 }
 
-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;
-}
-
 void eDVBDB::loadBouquet(const char *path)
 {
        std::string bouquet_name = path;
@@ -458,6 +463,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];
@@ -518,20 +535,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()
@@ -591,6 +631,12 @@ RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBServic
        return 0;
 }
 
+RESULT eDVBDB::flush()
+{
+       save();
+       return 0;
+}
+
 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
 {
        std::string str = ref.path;