From 4083eb84989e4de7e6aa8d963c2f13a0f0976eeb Mon Sep 17 00:00:00 2001
From: Sascha Hinck <SHinck@web.de>
Date: Sat, 25 Aug 2012 18:24:48 +0200
Subject: [PATCH] EPG: Cache FreeRecorderList query to remote Backend

When the EPG during LiveTV shows up cache the result from the first query
of GetFreeRecorderList to the backend in a local vector because it takes
time and is repeated for each row and update of the guidegrid which is not
acceptable e.g. for a windows remote frontend.
---
 mythtv/libs/libmythtv/tv_play.cpp          |   14 +++++++++++++-
 mythtv/libs/libmythtv/tv_play.h            |    1 +
 mythtv/programs/mythfrontend/guidegrid.cpp |    8 +++-----
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/mythtv/libs/libmythtv/tv_play.cpp b/mythtv/libs/libmythtv/tv_play.cpp
index 89e27df..f087fd6 100644
--- a/mythtv/libs/libmythtv/tv_play.cpp
+++ b/mythtv/libs/libmythtv/tv_play.cpp
@@ -7991,7 +7991,18 @@ QSet<uint> TV::IsTunableOn(
         excluded_cards.push_back(ctx->GetCardID());
 
     uint sourceid = ChannelUtil::GetSourceIDForChannel(chanid);
-    vector<uint> connected   = RemoteRequestFreeRecorderList(excluded_cards);
+    vector<uint> connected;
+    if (use_cache)
+    {
+        QMutexLocker locker(&is_tunable_cache_lock);
+        if (is_tunable_cache_recorder.empty())
+            is_tunable_cache_recorder = RemoteRequestFreeRecorderList(excluded_cards);
+
+        connected = is_tunable_cache_recorder;
+    }
+    else
+        connected = RemoteRequestFreeRecorderList(excluded_cards);	  
+   
     vector<uint> interesting = CardUtil::GetCardIDs(sourceid);
 
     // filter disconnected cards
@@ -8085,6 +8096,7 @@ void TV::ClearTunableCache(void)
     QMutexLocker locker(&is_tunable_cache_lock);
     LOG(VB_CHANNEL, LOG_INFO, LOC + "ClearTunableCache()");
     is_tunable_cache_inputs.clear();
+    is_tunable_cache_recorder.clear();
 }
 
 bool TV::StartEmbedding(const QRect &embedRect)
diff --git a/mythtv/libs/libmythtv/tv_play.h b/mythtv/libs/libmythtv/tv_play.h
index 7041f9f..d732cba 100644
--- a/mythtv/libs/libmythtv/tv_play.h
+++ b/mythtv/libs/libmythtv/tv_play.h
@@ -811,6 +811,7 @@ class MTV_PUBLIC TV : public QObject
     // IsTunable() cache, used by embedded program guide
     mutable QMutex                 is_tunable_cache_lock;
     QMap< uint,vector<InputInfo> > is_tunable_cache_inputs;
+    vector<uint> is_tunable_cache_recorder;
 
     // Channel group stuff
     /// \brief Lock necessary when modifying channel group variables.
diff --git a/mythtv/programs/mythfrontend/guidegrid.cpp b/mythtv/programs/mythfrontend/guidegrid.cpp
index 1990c4e..0be4bba 100644
--- a/mythtv/programs/mythfrontend/guidegrid.cpp
+++ b/mythtv/programs/mythfrontend/guidegrid.cpp
@@ -260,6 +260,9 @@ GuideGrid::GuideGrid(MythScreenStack *parent,
     m_currentStartTime = m_originalStartTime.addSecs(secsoffset);
     m_startChanID  = chanid;
     m_startChanNum = channum;
+
+    if (m_player)
+        m_player->ClearTunableCache();
 }
 
 bool GuideGrid::Create()
@@ -1511,9 +1514,6 @@ void GuideGrid::updateChannels(void)
 
     DBChannel *chinfo = GetChannelInfo(m_currentStartChannel);
 
-    if (m_player)
-        m_player->ClearTunableCache();
-
     for (unsigned int y = 0; (y < (unsigned int)m_channelCount) && chinfo; ++y)
     {
         unsigned int chanNumber = y + m_currentStartChannel;
@@ -1824,7 +1824,6 @@ void GuideGrid::cursorDown()
         fillProgramRowInfos(m_currentRow);
         m_guideGrid->SetRedraw();
         updateInfo();
-        updateChannels();
     }
 }
 
@@ -1842,7 +1841,6 @@ void GuideGrid::cursorUp()
         fillProgramRowInfos(m_currentRow);
         m_guideGrid->SetRedraw();
         updateInfo();
-        updateChannels();
     }
 }
 
