[video] Fix the refresh of movies with additional versions or extras
[xbmc.git] / xbmc / BackgroundInfoLoader.cpp
blob55d221ab04edaffb08574d97e3a7640479d0d367
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "BackgroundInfoLoader.h"
11 #include "FileItem.h"
12 #include "URL.h"
13 #include "threads/Thread.h"
14 #include "utils/log.h"
16 #include <mutex>
18 CBackgroundInfoLoader::CBackgroundInfoLoader() = default;
20 CBackgroundInfoLoader::~CBackgroundInfoLoader()
22 StopThread();
25 void CBackgroundInfoLoader::Reset()
27 m_pVecItems = nullptr;
28 m_vecItems.clear();
29 m_bIsLoading = false;
32 void CBackgroundInfoLoader::Run()
34 try
36 if (!m_vecItems.empty())
38 OnLoaderStart();
40 // Stage 1: All "fast" stuff we have already cached
41 for (std::vector<CFileItemPtr>::const_iterator iter = m_vecItems.begin(); iter != m_vecItems.end(); ++iter)
43 const CFileItemPtr& pItem = *iter;
45 // Ask the callback if we should abort
46 if ((m_pProgressCallback && m_pProgressCallback->Abort()) || m_bStop)
47 break;
49 try
51 if (LoadItemCached(pItem.get()) && m_pObserver)
52 m_pObserver->OnItemLoaded(pItem.get());
54 catch (...)
56 CLog::Log(LOGERROR,
57 "CBackgroundInfoLoader::LoadItemCached - Unhandled exception for item {}",
58 CURL::GetRedacted(pItem->GetPath()));
62 // Stage 2: All "slow" stuff that we need to lookup
63 for (std::vector<CFileItemPtr>::const_iterator iter = m_vecItems.begin(); iter != m_vecItems.end(); ++iter)
65 const CFileItemPtr& pItem = *iter;
67 // Ask the callback if we should abort
68 if ((m_pProgressCallback && m_pProgressCallback->Abort()) || m_bStop)
69 break;
71 try
73 if (LoadItemLookup(pItem.get()) && m_pObserver)
74 m_pObserver->OnItemLoaded(pItem.get());
76 catch (...)
78 CLog::Log(LOGERROR,
79 "CBackgroundInfoLoader::LoadItemLookup - Unhandled exception for item {}",
80 CURL::GetRedacted(pItem->GetPath()));
85 OnLoaderFinish();
87 catch (...)
89 CLog::Log(LOGERROR, "{} - Unhandled exception", __FUNCTION__);
92 Reset();
95 void CBackgroundInfoLoader::Load(CFileItemList& items)
97 StopThread();
99 if (items.IsEmpty())
100 return;
102 std::unique_lock<CCriticalSection> lock(m_lock);
104 for (int nItem=0; nItem < items.Size(); nItem++)
105 m_vecItems.push_back(items[nItem]);
107 m_pVecItems = &items;
108 m_bStop = false;
109 m_bIsLoading = true;
111 m_thread = new CThread(this, "BackgroundLoader");
112 m_thread->Create();
113 m_thread->SetPriority(ThreadPriority::BELOW_NORMAL);
116 void CBackgroundInfoLoader::StopAsync()
118 m_bStop = true;
122 void CBackgroundInfoLoader::StopThread()
124 StopAsync();
126 if (m_thread)
128 m_thread->StopThread();
129 delete m_thread;
130 m_thread = NULL;
132 Reset();
135 bool CBackgroundInfoLoader::IsLoading()
137 return m_bIsLoading;
140 void CBackgroundInfoLoader::SetObserver(IBackgroundLoaderObserver* pObserver)
142 m_pObserver = pObserver;
145 void CBackgroundInfoLoader::SetProgressCallback(IProgressCallback* pCallback)
147 m_pProgressCallback = pCallback;