save epgcache to hdd on shutdown
[enigma2.git] / lib / dvb / epgcache.cpp
index fb121ae8e4dbadfe32546891e45ed8ab9a8a31ba..e48db9578dae0c146dade5dec1c481c83b29cb41 100644 (file)
@@ -191,6 +191,7 @@ void eEPGCache::DVBChannelAdded(eDVBChannel *chan)
 //             eDebug("[eEPGCache] add channel %p", chan);
                channel_data *data = new channel_data(this);
                data->channel = chan;
+               data->prevChannelState = -1;
                singleLock s(channel_map_lock);
                m_knownChannels.insert( std::pair<iDVBChannel*, channel_data* >(chan, data) );
                chan->connectStateChange(slot(*this, &eEPGCache::DVBChannelStateChanged), data->m_stateChangedConn);
@@ -256,25 +257,31 @@ void eEPGCache::DVBChannelStateChanged(iDVBChannel *chan)
        {
                int state=0;
                chan->getState(state);
-               switch (state)
+               if ( it->second->prevChannelState != state )
                {
-                       case iDVBChannel::state_ok:
+                       switch (state)
                        {
-                               eDebug("[eEPGCache] channel %p running", chan);
-                               DVBChannelRunning(chan);
-                               break;
-                       }
-                       case iDVBChannel::state_release:
-                       {
-                               eDebug("[eEPGCache] remove channel %p", chan);
-                               messages.send(Message(Message::leaveChannel, chan));
-                               while(!it->second->can_delete)
-                                       usleep(1000);
-                               delete it->second;
-                               m_knownChannels.erase(it);
-                               // -> gotMessage -> abortEPG
-                               break;
+                               case iDVBChannel::state_ok:
+                               {
+                                       eDebug("[eEPGCache] channel %p running", chan);
+                                       DVBChannelRunning(chan);
+                                       break;
+                               }
+                               case iDVBChannel::state_release:
+                               {
+                                       eDebug("[eEPGCache] remove channel %p", chan);
+                                       messages.send(Message(Message::leaveChannel, chan));
+                                       while(!it->second->can_delete)
+                                               usleep(1000);
+                                       delete it->second;
+                                       m_knownChannels.erase(it);
+                                       // -> gotMessage -> abortEPG
+                                       break;
+                               }
+                               default: // ignore all other events
+                                       return;
                        }
+                       it->second->prevChannelState = state;
                }
        }
 }
@@ -621,7 +628,6 @@ void eEPGCache::thread()
 
 void eEPGCache::load()
 {
-#if 0
        FILE *f = fopen("/hdd/epg.dat", "r");
        if (f)
        {
@@ -630,6 +636,7 @@ void eEPGCache::load()
                int size=0;
                int cnt=0;
                bool md5ok=false;
+#if 0
                if (!md5_file("/hdd/epg.dat", 1, md5))
                {
                        FILE *f = fopen("/hdd/epg.dat.md5", "r");
@@ -642,6 +649,7 @@ void eEPGCache::load()
                        }
                }
                if ( md5ok )
+#endif
                {
                        char text1[13];
                        fread( text1, 13, 1, f);
@@ -681,12 +689,10 @@ void eEPGCache::load()
                        fclose(f);
                }
        }
-#endif
 }
 
 void eEPGCache::save()
 {
-#if 0
        struct statfs s;
        off64_t tmp;
        if (statfs("/hdd", &s)<0)
@@ -733,6 +739,7 @@ void eEPGCache::save()
                eDebug("%d events written to /hdd/epg.dat", cnt);
                eventData::save(f);
                fclose(f);
+#if 0
                unsigned char md5[16];
                if (!md5_file("/hdd/epg.dat", 1, md5))
                {
@@ -743,8 +750,8 @@ void eEPGCache::save()
                                fclose(f);
                        }
                }
-       }
 #endif
+       }
 }
 
 eEPGCache::channel_data::channel_data(eEPGCache *ml)
@@ -1008,10 +1015,25 @@ RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, co
        {
                if (!t)
                        t = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
-
-               timeMap::iterator i = It->second.second.lower_bound(t);
-               if ( i != It->second.second.end() && t <= i->first+i->second->getDuration() )
+               timeMap::iterator i = It->second.second.lower_bound(t);  // find > or equal
+               if ( i != It->second.second.end() )
                {
+                       if ( i->second->getStartTime() != t )
+                       {
+                               timeMap::iterator x = i;
+                               --x;
+                               if ( x != It->second.second.end() )
+                               {
+                                       time_t start_time = x->second->getStartTime();
+                                       if (t < start_time)
+                                               return -1;
+                                       if (t > (start_time+x->second->getDuration()))
+                                               return -1;
+                                       i = x;
+                               }
+                               else
+                                       return -1;
+                       }
                        result = i->second;
                        return 0;
                }
@@ -1121,12 +1143,19 @@ RESULT eEPGCache::startTimeQuery(const eServiceReference &service, time_t begin,
                if ( begin != -1 )
                {
                        m_timemap_cursor = It->second.second.lower_bound(begin);
-                       if ( m_timemap_cursor != It->second.second.end() && m_timemap_cursor != It->second.second.begin() )
+                       if ( m_timemap_cursor != It->second.second.end() )
                        {
-                               timeMap::iterator it = m_timemap_cursor;
-                               --it;
-                               if ( (it->second->getStartTime() + it->second->getDuration()) > begin )
-                                       m_timemap_cursor = it;
+                               if ( m_timemap_cursor->second->getStartTime() != begin )
+                               {
+                                       timeMap::iterator x = m_timemap_cursor;
+                                       --x;
+                                       if ( x != It->second.second.end() )
+                                       {
+                                               time_t start_time = x->second->getStartTime();
+                                               if ( begin > start_time && begin < (start_time+x->second->getDuration()))
+                                                       m_timemap_cursor = x;
+                                       }
+                               }
                        }
                }
                else