From 91fc82efa40513a52002cd8ef5cb4d16f270ff03 Mon Sep 17 00:00:00 2001
From: Sascha Hinck <SHinck@web.de>
Date: Wed, 15 Aug 2012 05:08:05 +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          |   17 ++++++++++++++++-
 mythtv/libs/libmythtv/tv_play.h            |    1 +
 mythtv/programs/mythfrontend/guidegrid.cpp |    8 +++-----
 3 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/mythtv/libs/libmythtv/tv_play.cpp b/mythtv/libs/libmythtv/tv_play.cpp
index bbd10de..b039781 100644
--- a/mythtv/libs/libmythtv/tv_play.cpp
+++ b/mythtv/libs/libmythtv/tv_play.cpp
@@ -7844,7 +7844,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
@@ -7938,6 +7952,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 d7b22ab..a1f806f 100644
--- a/mythtv/libs/libmythtv/tv_play.h
+++ b/mythtv/libs/libmythtv/tv_play.h
@@ -777,6 +777,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 de83c42..5106304 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()
@@ -1510,9 +1513,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;
@@ -1823,7 +1823,6 @@ void GuideGrid::cursorDown()
         fillProgramRowInfos(m_currentRow);
         m_guideGrid->SetRedraw();
         updateInfo();
-        updateChannels();
     }
 }
 
@@ -1841,7 +1840,6 @@ void GuideGrid::cursorUp()
         fillProgramRowInfos(m_currentRow);
         m_guideGrid->SetRedraw();
         updateInfo();
-        updateChannels();
     }
 }
 
