Small Refactor of MarkWatched function and execute it inside a separate job #644

Closed
wants to merge 3 commits into
from
@@ -643,15 +643,16 @@ bool CGUIWindowMusicNav::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
}
case CONTEXT_BUTTON_MARK_WATCHED:
- CGUIWindowVideoBase::MarkWatched(item,true);
- CUtil::DeleteVideoDatabaseDirectoryCache();
- Update(m_vecItems->GetPath());
- return true;
+ {
+ int newSelection = m_viewControl.GetSelectedItem() + 1;
+ CGUIWindowVideoBase::MarkWatched(item,true);
+ m_viewControl.SetSelectedItem(newSelection);
+
+ return true;
+ }
case CONTEXT_BUTTON_MARK_UNWATCHED:
CGUIWindowVideoBase::MarkWatched(item,false);
- CUtil::DeleteVideoDatabaseDirectoryCache();
- Update(m_vecItems->GetPath());
return true;
case CONTEXT_BUTTON_RENAME:
@@ -62,6 +62,8 @@
#include "utils/log.h"
#include "utils/FileUtils.h"
#include "utils/URIUtils.h"
+#include "utils/Job.h"
+#include "utils/JobManager.h"
#include "GUIUserMessages.h"
#include "addons/Skin.h"
#include "storage/MediaManager.h"
@@ -1285,14 +1287,10 @@ bool CGUIWindowVideoBase::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
MarkWatched(item,true);
m_viewControl.SetSelectedItem(newSelection);
- CUtil::DeleteVideoDatabaseDirectoryCache();
- Update(m_vecItems->GetPath());
return true;
}
case CONTEXT_BUTTON_MARK_UNWATCHED:
MarkWatched(item,false);
- CUtil::DeleteVideoDatabaseDirectoryCache();
- Update(m_vecItems->GetPath());
return true;
case CONTEXT_BUTTON_PLAY_AND_QUEUE:
return OnPlayAndQueueMedia(item);
@@ -1488,53 +1486,76 @@ void CGUIWindowVideoBase::OnDeleteItem(CFileItemPtr item)
CFileUtils::DeleteItem(item);
}
-void CGUIWindowVideoBase::MarkWatched(const CFileItemPtr &item, bool bMark)
+class CMarkWatchedJob : public CJob
{
- if (!g_settings.GetCurrentProfile().canWriteDatabases())
- return;
- // dont allow update while scanning
- CGUIDialogVideoScan* pDialogScan = (CGUIDialogVideoScan*)g_windowManager.GetWindow(WINDOW_DIALOG_VIDEO_SCAN);
- if (pDialogScan && pDialogScan->IsScanning())
+public:
+ CMarkWatchedJob(const CFileItemPtr& item, bool bMark)
+ : m_item(item), m_bMark(bMark)
{
- CGUIDialogOK::ShowAndGetInput(257, 0, 14057, 0);
- return;
}
- CVideoDatabase database;
- if (database.Open())
+ virtual bool DoWork()
{
- CFileItemList items;
- if (item->m_bIsFolder)
- {
- CStdString strPath = item->GetPath();
- CDirectory::GetDirectory(strPath, items);
- }
- else
- items.Add(item);
+ if (!g_settings.GetCurrentProfile().canWriteDatabases())
+ return false;
- for (int i=0;i<items.Size();++i)
+ MarkWatched(m_item, m_bMark);
+
+ CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE);
+ g_windowManager.SendThreadMessage(msg);
+
+ return true;
+ }
+
+ virtual const char *GetType() const { return "markwatched"; };
+
+private:
+ void MarkWatched(const CFileItemPtr &item, bool bMark)
+ {
+ CVideoDatabase database;
+ if (database.Open())
{
- CFileItemPtr pItem=items[i];
- if (pItem->m_bIsFolder)
+ CFileItemList items;
+ if (item->m_bIsFolder)
{
- MarkWatched(pItem, bMark);
- continue;
+ CStdString strPath = item->GetPath();
+ XFILE::CDirectory::GetDirectory(strPath, items);
}
+ else
+ items.Add(item);
- if (pItem->HasVideoInfoTag() &&
- (( bMark && pItem->GetVideoInfoTag()->m_playCount) ||
- (!bMark && !(pItem->GetVideoInfoTag()->m_playCount))))
- continue;
+ for (int i = 0; i < items.Size(); ++i)
+ {
+ CFileItemPtr pItem = items[i];
+ if (pItem->m_bIsFolder)
+ {
+ MarkWatched(pItem, bMark);
+ continue;
+ }
- // Clear resume bookmark
- if (bMark)
- database.ClearBookMarksOfFile(pItem->GetPath(), CBookmark::RESUME);
+ if (pItem->HasVideoInfoTag() &&
+ (( bMark && pItem->GetVideoInfoTag()->m_playCount) ||
+ (!bMark && !(pItem->GetVideoInfoTag()->m_playCount))))
+ continue;
- database.SetPlayCount(*pItem, bMark ? 1 : 0);
+ // Clear resume bookmark
+ if (bMark)
+ database.ClearBookMarksOfFile(pItem->GetPath(), CBookmark::RESUME);
+
+ database.SetPlayCount(*pItem, bMark ? 1 : 0);
+ }
+
+ database.Close();
}
-
- database.Close();
}
+
+ const CFileItemPtr &m_item;
+ bool m_bMark;
+};
+
+void CGUIWindowVideoBase::MarkWatched(const CFileItemPtr &pItem, bool bMark)
+{
+ CJobManager::GetInstance().AddJob(new CMarkWatchedJob(pItem, bMark), 0);
}
//Add change a title's name