[pvr] remember last watched group
authorxhaggi <sascha.woo@gmail.com>
Wed, 8 Jan 2014 10:25:44 +0000 (11:25 +0100)
committerxhaggi <sascha.woo@gmail.com>
Wed, 7 May 2014 21:49:16 +0000 (23:49 +0200)
xbmc/pvr/PVRDatabase.cpp
xbmc/pvr/PVRDatabase.h
xbmc/pvr/PVRManager.cpp
xbmc/pvr/channels/PVRChannelGroup.cpp
xbmc/pvr/channels/PVRChannelGroup.h
xbmc/pvr/channels/PVRChannelGroups.cpp
xbmc/pvr/channels/PVRChannelGroups.h
xbmc/pvr/channels/PVRChannelGroupsContainer.cpp
xbmc/pvr/channels/PVRChannelGroupsContainer.h

index c598603..6688290 100644 (file)
@@ -112,7 +112,8 @@ void CPVRDatabase::CreateTables()
       "CREATE TABLE map_channelgroups_channels ("
         "idChannel      integer, "
         "idGroup        integer, "
-        "iChannelNumber integer"
+        "iChannelNumber integer, "
+        "iLastWatched   integer"
       ")"
   );
 
@@ -219,6 +220,9 @@ void CPVRDatabase::UpdateTables(int iVersion)
 
   if (iVersion < 22)
     m_pDS->exec("ALTER TABLE channels ADD bIsLocked bool");
+
+  if (iVersion < 23)
+    m_pDS->exec("ALTER TABLE channelgroups ADD iLastWatched integer");
 }
 
 int CPVRDatabase::GetLastChannelId(void)
@@ -648,6 +652,7 @@ bool CPVRDatabase::Get(CPVRChannelGroups &results)
       {
         CPVRChannelGroup data(m_pDS->fv("bIsRadio").get_asBool(), m_pDS->fv("idGroup").get_asInt(), m_pDS->fv("sName").get_asString());
         data.SetGroupType(m_pDS->fv("iGroupType").get_asInt());
+        data.SetLastWatched((time_t) m_pDS->fv("iLastWatched").get_asInt());
         results.Update(data);
 
         CLog::Log(LOGDEBUG, "PVR - %s - group '%s' loaded from the database", __FUNCTION__, data.GroupName().c_str());
@@ -842,11 +847,11 @@ bool CPVRDatabase::Persist(CPVRChannelGroup &group)
 
     /* insert a new entry when this is a new group, or replace the existing one otherwise */
     if (group.GroupID() <= 0)
-      strQuery = PrepareSQL("INSERT INTO channelgroups (bIsRadio, iGroupType, sName) VALUES (%i, %i, '%s')",
-          (group.IsRadio() ? 1 :0), group.GroupType(), group.GroupName().c_str());
+      strQuery = PrepareSQL("INSERT INTO channelgroups (bIsRadio, iGroupType, sName, iLastWatched) VALUES (%i, %i, '%s', %u)",
+          (group.IsRadio() ? 1 :0), group.GroupType(), group.GroupName().c_str(), group.LastWatched());
     else
-      strQuery = PrepareSQL("REPLACE INTO channelgroups (idGroup, bIsRadio, iGroupType, sName) VALUES (%i, %i, %i, '%s')",
-          group.GroupID(), (group.IsRadio() ? 1 :0), group.GroupType(), group.GroupName().c_str());
+      strQuery = PrepareSQL("REPLACE INTO channelgroups (idGroup, bIsRadio, iGroupType, sName, iLastWatched) VALUES (%i, %i, %i, '%s', %u)",
+          group.GroupID(), (group.IsRadio() ? 1 :0), group.GroupType(), group.GroupName().c_str(), group.LastWatched());
 
     bReturn = ExecuteQuery(strQuery);
 
index ad1cbe9..340fc28 100644 (file)
@@ -59,7 +59,7 @@ namespace PVR
      * @brief Get the minimal database version that is required to operate correctly.
      * @return The minimal database version.
      */
-    virtual int GetSchemaVersion() const { return 22; };
+    virtual int GetSchemaVersion() const { return 23; };
 
     /*!
      * @brief Get the default sqlite database filename.
index 9e23a18..e863c96 100644 (file)
@@ -663,7 +663,9 @@ bool CPVRManager::ContinueLastChannel(void)
   if (channel && channel->HasPVRChannelInfoTag())
   {
     CLog::Log(LOGNOTICE, "PVRManager - %s - continue playback on channel '%s'", __FUNCTION__, channel->GetPVRChannelInfoTag()->ChannelName().c_str());
-    return StartPlayback(channel->GetPVRChannelInfoTag(), (CSettings::Get().GetInt("pvrplayback.startlast") == START_LAST_CHANNEL_MIN));
+    SetPlayingGroup(m_channelGroups->GetLastPlayedGroup());
+    StartPlayback(channel->GetPVRChannelInfoTag(), (CSettings::Get().GetInt("pvrplayback.startlast") == START_LAST_CHANNEL_MIN));
+    return true;
   }
 
   return false;
@@ -973,7 +975,8 @@ bool CPVRManager::OpenLiveStream(const CFileItem &channel)
     return bReturn;
 
   CPVRChannelPtr playingChannel;
-  bool bPersistChannel(false);
+  CPVRChannelGroupPtr group;
+  bool bPersistChanges(false);
   if ((bReturn = m_addons->OpenStream(*channel.GetPVRChannelInfoTag(), false)) != false)
   {
     CSingleLock lock(m_critSection);
@@ -983,18 +986,28 @@ bool CPVRManager::OpenLiveStream(const CFileItem &channel)
 
     if (m_addons->GetPlayingChannel(playingChannel))
     {
-      /* store current time in iLastWatched */
       time_t tNow;
       CDateTime::GetCurrentDateTime().GetAsTime(tNow);
+
+      /* update last watched timestamp for channel */
       playingChannel->SetLastWatched(tNow);
-      bPersistChannel = true;
 
-      m_channelGroups->SetLastPlayedGroup(GetPlayingGroup(playingChannel->IsRadio()));
+      /* update last watched timestamp for group */
+      group = g_PVRManager.GetPlayingGroup(playingChannel->IsRadio());
+      group->SetLastWatched(tNow);
+
+      /* update last played group */
+      m_channelGroups->SetLastPlayedGroup(group);
+
+      bPersistChanges = true;
     }
   }
 
-  if (bPersistChannel)
+  if (bPersistChanges)
+  {
     playingChannel->Persist();
+    group->Persist();
+  }
 
   return bReturn;
 }
@@ -1016,28 +1029,39 @@ bool CPVRManager::OpenRecordedStream(const CPVRRecording &tag)
 void CPVRManager::CloseStream(void)
 {
   CPVRChannelPtr channel;
-  bool bPersistChannel(false);
+  CPVRChannelGroupPtr group;
+  bool bPersistChanges(false);
 
   {
     CSingleLock lock(m_critSection);
 
     if (m_addons->GetPlayingChannel(channel))
     {
-      /* store current time in iLastWatched */
       time_t tNow;
       CDateTime::GetCurrentDateTime().GetAsTime(tNow);
+
+      /* update last watched timestamp for channel */
       channel->SetLastWatched(tNow);
-      bPersistChannel = true;
 
-      m_channelGroups->SetLastPlayedGroup(GetPlayingGroup(channel->IsRadio()));
+      /* update last watched timestamp for group */
+      group = g_PVRManager.GetPlayingGroup(channel->IsRadio());
+      group->SetLastWatched(tNow);
+
+      /* update last played group */
+      m_channelGroups->SetLastPlayedGroup(group);
+
+      bPersistChanges = true;
     }
 
     m_addons->CloseStream();
     SAFE_DELETE(m_currentFile);
   }
 
-  if (bPersistChannel)
+  if (bPersistChanges)
+  {
     channel->Persist();
+    group->Persist();
+  }
 }
 
 void CPVRManager::UpdateCurrentFile(void)
index 0ec84b4..57d148f 100644 (file)
@@ -52,7 +52,8 @@ CPVRChannelGroup::CPVRChannelGroup(void) :
     m_bChanged(false),
     m_bUsingBackendChannelOrder(false),
     m_bSelectedGroup(false),
-    m_bPreventSortAndRenumber(false)
+    m_bPreventSortAndRenumber(false),
+    m_iLastWatched(0)
 {
 }
 
@@ -65,7 +66,8 @@ CPVRChannelGroup::CPVRChannelGroup(bool bRadio, unsigned int iGroupId, const CSt
     m_bChanged(false),
     m_bUsingBackendChannelOrder(false),
     m_bSelectedGroup(false),
-    m_bPreventSortAndRenumber(false)
+    m_bPreventSortAndRenumber(false),
+    m_iLastWatched(0)
 {
 }
 
@@ -78,7 +80,8 @@ CPVRChannelGroup::CPVRChannelGroup(const PVR_CHANNEL_GROUP &group) :
     m_bChanged(false),
     m_bUsingBackendChannelOrder(false),
     m_bSelectedGroup(false),
-    m_bPreventSortAndRenumber(false)
+    m_bPreventSortAndRenumber(false),
+    m_iLastWatched(0)
 {
 }
 
@@ -110,6 +113,7 @@ CPVRChannelGroup::CPVRChannelGroup(const CPVRChannelGroup &group)
   m_bChanged                    = group.m_bChanged;
   m_bUsingBackendChannelOrder   = group.m_bUsingBackendChannelOrder;
   m_bUsingBackendChannelNumbers = group.m_bUsingBackendChannelNumbers;
+  m_iLastWatched                = group.m_iLastWatched;
 
   for (int iPtr = 0; iPtr < group.Size(); iPtr++)
     m_members.push_back(group.m_members.at(iPtr));
@@ -1220,6 +1224,29 @@ CStdString CPVRChannelGroup::GroupName(void) const
   return strReturn;
 }
 
+time_t CPVRChannelGroup::LastWatched(void) const
+{
+  CSingleLock lock(m_critSection);
+  return m_iLastWatched;
+}
+
+bool CPVRChannelGroup::SetLastWatched(time_t iLastWatched)
+{
+  CSingleLock lock(m_critSection);
+
+  if (m_iLastWatched != iLastWatched)
+  {
+    /* update last watched  */
+    m_iLastWatched = iLastWatched;
+    SetChanged();
+    m_bChanged = true;
+
+    return true;
+  }
+
+  return false;
+}
+
 bool CPVRChannelGroup::PreventSortAndRenumber(void) const
 {
   CSingleLock lock(m_critSection);
index 61b3cfb..d807cfe 100644 (file)
@@ -216,6 +216,18 @@ namespace PVR
     int GroupType(void) const;
 
     /*!
+     * @return Time group has been watched last.
+     */
+    time_t LastWatched() const;
+
+    /*!
+     * @brief Last time group has been watched
+     * @param iLastWatched The new value.
+     * @return True if something changed, false otherwise.
+     */
+    bool SetLastWatched(time_t iLastWatched);
+
+    /*!
      * @brief Set if sorting and renumbering should happen after adding/updating channels to group.
      * @param bPreventSortAndRenumber The new sorting and renumbering prevention value for this group.
      */
@@ -511,6 +523,7 @@ namespace PVR
     bool             m_bUsingBackendChannelNumbers; /*!< true to use the channel numbers from 1 backend, false otherwise */
     bool             m_bSelectedGroup;              /*!< true when this is the selected group, false otherwise */
     bool             m_bPreventSortAndRenumber;     /*!< true when sorting and renumbering should not be done after adding/updating channels to the group */
+    time_t           m_iLastWatched;                /*!< last time group has been watched */
     std::vector<PVRChannelGroupMember> m_members;
     CCriticalSection m_critSection;
     
index 2582a9b..6ee8f4b 100644 (file)
@@ -80,6 +80,7 @@ bool CPVRChannelGroups::Update(const CPVRChannelGroup &group, bool bSaveInDb)
       // create a new group if none was found
       updateGroup = CPVRChannelGroupPtr(new CPVRChannelGroup(m_bRadio, group.GroupID(), group.GroupName()));
       updateGroup->SetGroupType(group.GroupType());
+      updateGroup->SetLastWatched(group.LastWatched());
       m_groups.push_back(updateGroup);
     }
     else
@@ -334,6 +335,20 @@ CPVRChannelGroupPtr CPVRChannelGroups::GetLastGroup(void) const
   return empty;
 }
 
+CPVRChannelGroupPtr CPVRChannelGroups::GetLastPlayedGroup() const
+{
+  CSingleLock lock(m_critSection);
+
+  CPVRChannelGroupPtr group;
+  for (std::vector<CPVRChannelGroupPtr>::const_iterator it = m_groups.begin(); it != m_groups.end(); it++)
+  {
+    if ((*it)->LastWatched() > 0 && (!group || (*it)->LastWatched() > group->LastWatched()))
+      group = (*it);
+  }
+
+  return group;
+}
+
 std::vector<CPVRChannelGroupPtr> CPVRChannelGroups::GetMembers() const
 {
   CSingleLock lock(m_critSection);
index 37fd41e..8cd07c5 100644 (file)
@@ -109,6 +109,12 @@ namespace PVR
     CPVRChannelGroupPtr GetLastGroup(void) const;
     
     /*!
+     * @brief The group that was played last.
+     * @return The last watched group.
+     */
+    CPVRChannelGroupPtr GetLastPlayedGroup() const;
+
+    /*!
      * @brief Get the list of groups.
      * @param groups The list to store the results in.
      * @return The amount of items that were added.
index 3c76d1e..127659a 100644 (file)
@@ -268,16 +268,22 @@ void CPVRChannelGroupsContainer::SearchMissingChannelIcons(void)
 
 CFileItemPtr CPVRChannelGroupsContainer::GetLastPlayedChannel(void) const
 {
-  CFileItemPtr lastChannel = GetGroupAllTV()->GetLastPlayedChannel();
-  bool bHasTVChannel(lastChannel && lastChannel->HasPVRChannelInfoTag());
+  CPVRChannelGroupPtr group = GetLastPlayedGroup();
+  if (group)
+    return group->GetLastPlayedChannel();
 
-  CFileItemPtr lastRadioChannel = GetGroupAllRadio()->GetLastPlayedChannel();
-  bool bHasRadioChannel(lastRadioChannel && lastRadioChannel->HasPVRChannelInfoTag());
+  return CFileItemPtr(new CFileItem);
+}
+
+CPVRChannelGroupPtr CPVRChannelGroupsContainer::GetLastPlayedGroup() const
+{
+  CPVRChannelGroupPtr groupTV = m_groupsTV->GetLastPlayedGroup();
+  CPVRChannelGroupPtr groupRadio = m_groupsRadio->GetLastPlayedGroup();
 
-  if (!bHasTVChannel || (bHasRadioChannel && lastChannel->GetPVRChannelInfoTag()->LastWatched() < lastRadioChannel->GetPVRChannelInfoTag()->LastWatched()))
-    return lastRadioChannel;
+  if (!groupTV || (groupRadio && groupTV->LastWatched() < groupRadio->LastWatched()))
+    return groupRadio;
 
-  return lastChannel;
+  return groupTV;
 }
 
 bool CPVRChannelGroupsContainer::CreateChannel(const CPVRChannel &channel)
index e17d898..d6620bf 100644 (file)
@@ -180,6 +180,12 @@ namespace PVR
      */
     CFileItemPtr GetLastPlayedChannel(void) const;
 
+    /*!
+     * @brief The group that was played last.
+     * @return The last watched group.
+     */
+    CPVRChannelGroupPtr GetLastPlayedGroup() const;
+
     bool CreateChannel(const CPVRChannel &channel);
 
     /*!