summaryrefslogtreecommitdiffstats
path: root/xbmc/BackgroundInfoLoader.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 18:07:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 18:07:22 +0000
commitc04dcc2e7d834218ef2d4194331e383402495ae1 (patch)
tree7333e38d10d75386e60f336b80c2443c1166031d /xbmc/BackgroundInfoLoader.cpp
parentInitial commit. (diff)
downloadkodi-c04dcc2e7d834218ef2d4194331e383402495ae1.tar.xz
kodi-c04dcc2e7d834218ef2d4194331e383402495ae1.zip
Adding upstream version 2:20.4+dfsg.upstream/2%20.4+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'xbmc/BackgroundInfoLoader.cpp')
-rw-r--r--xbmc/BackgroundInfoLoader.cpp151
1 files changed, 151 insertions, 0 deletions
diff --git a/xbmc/BackgroundInfoLoader.cpp b/xbmc/BackgroundInfoLoader.cpp
new file mode 100644
index 0000000..8235f81
--- /dev/null
+++ b/xbmc/BackgroundInfoLoader.cpp
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2005-2018 Team Kodi
+ * This file is part of Kodi - https://kodi.tv
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ * See LICENSES/README.md for more information.
+ */
+
+#include "BackgroundInfoLoader.h"
+
+#include "FileItem.h"
+#include "URL.h"
+#include "threads/Thread.h"
+#include "utils/log.h"
+
+#include <mutex>
+
+CBackgroundInfoLoader::CBackgroundInfoLoader() : m_thread (NULL)
+{
+ m_bStop = true;
+ m_pObserver=NULL;
+ m_pProgressCallback=NULL;
+ m_pVecItems = NULL;
+ m_bIsLoading = false;
+}
+
+CBackgroundInfoLoader::~CBackgroundInfoLoader()
+{
+ StopThread();
+}
+
+void CBackgroundInfoLoader::Run()
+{
+ try
+ {
+ if (!m_vecItems.empty())
+ {
+ OnLoaderStart();
+
+ // Stage 1: All "fast" stuff we have already cached
+ for (std::vector<CFileItemPtr>::const_iterator iter = m_vecItems.begin(); iter != m_vecItems.end(); ++iter)
+ {
+ const CFileItemPtr& pItem = *iter;
+
+ // Ask the callback if we should abort
+ if ((m_pProgressCallback && m_pProgressCallback->Abort()) || m_bStop)
+ break;
+
+ try
+ {
+ if (LoadItemCached(pItem.get()) && m_pObserver)
+ m_pObserver->OnItemLoaded(pItem.get());
+ }
+ catch (...)
+ {
+ CLog::Log(LOGERROR,
+ "CBackgroundInfoLoader::LoadItemCached - Unhandled exception for item {}",
+ CURL::GetRedacted(pItem->GetPath()));
+ }
+ }
+
+ // Stage 2: All "slow" stuff that we need to lookup
+ for (std::vector<CFileItemPtr>::const_iterator iter = m_vecItems.begin(); iter != m_vecItems.end(); ++iter)
+ {
+ const CFileItemPtr& pItem = *iter;
+
+ // Ask the callback if we should abort
+ if ((m_pProgressCallback && m_pProgressCallback->Abort()) || m_bStop)
+ break;
+
+ try
+ {
+ if (LoadItemLookup(pItem.get()) && m_pObserver)
+ m_pObserver->OnItemLoaded(pItem.get());
+ }
+ catch (...)
+ {
+ CLog::Log(LOGERROR,
+ "CBackgroundInfoLoader::LoadItemLookup - Unhandled exception for item {}",
+ CURL::GetRedacted(pItem->GetPath()));
+ }
+ }
+ }
+
+ OnLoaderFinish();
+ m_bIsLoading = false;
+ }
+ catch (...)
+ {
+ m_bIsLoading = false;
+ CLog::Log(LOGERROR, "{} - Unhandled exception", __FUNCTION__);
+ }
+}
+
+void CBackgroundInfoLoader::Load(CFileItemList& items)
+{
+ StopThread();
+
+ if (items.IsEmpty())
+ return;
+
+ std::unique_lock<CCriticalSection> lock(m_lock);
+
+ for (int nItem=0; nItem < items.Size(); nItem++)
+ m_vecItems.push_back(items[nItem]);
+
+ m_pVecItems = &items;
+ m_bStop = false;
+ m_bIsLoading = true;
+
+ m_thread = new CThread(this, "BackgroundLoader");
+ m_thread->Create();
+ m_thread->SetPriority(ThreadPriority::BELOW_NORMAL);
+}
+
+void CBackgroundInfoLoader::StopAsync()
+{
+ m_bStop = true;
+}
+
+
+void CBackgroundInfoLoader::StopThread()
+{
+ StopAsync();
+
+ if (m_thread)
+ {
+ m_thread->StopThread();
+ delete m_thread;
+ m_thread = NULL;
+ }
+ m_vecItems.clear();
+ m_pVecItems = NULL;
+ m_bIsLoading = false;
+}
+
+bool CBackgroundInfoLoader::IsLoading()
+{
+ return m_bIsLoading;
+}
+
+void CBackgroundInfoLoader::SetObserver(IBackgroundLoaderObserver* pObserver)
+{
+ m_pObserver = pObserver;
+}
+
+void CBackgroundInfoLoader::SetProgressCallback(IProgressCallback* pCallback)
+{
+ m_pProgressCallback = pCallback;
+}
+