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.
9 #include "BackgroundInfoLoader.h"
13 #include "threads/Thread.h"
14 #include "utils/log.h"
18 CBackgroundInfoLoader::CBackgroundInfoLoader() = default;
20 CBackgroundInfoLoader::~CBackgroundInfoLoader()
25 void CBackgroundInfoLoader::Reset()
27 m_pVecItems
= nullptr;
32 void CBackgroundInfoLoader::Run()
36 if (!m_vecItems
.empty())
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
)
51 if (LoadItemCached(pItem
.get()) && m_pObserver
)
52 m_pObserver
->OnItemLoaded(pItem
.get());
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
)
73 if (LoadItemLookup(pItem
.get()) && m_pObserver
)
74 m_pObserver
->OnItemLoaded(pItem
.get());
79 "CBackgroundInfoLoader::LoadItemLookup - Unhandled exception for item {}",
80 CURL::GetRedacted(pItem
->GetPath()));
89 CLog::Log(LOGERROR
, "{} - Unhandled exception", __FUNCTION__
);
95 void CBackgroundInfoLoader::Load(CFileItemList
& items
)
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
;
111 m_thread
= new CThread(this, "BackgroundLoader");
113 m_thread
->SetPriority(ThreadPriority::BELOW_NORMAL
);
116 void CBackgroundInfoLoader::StopAsync()
122 void CBackgroundInfoLoader::StopThread()
128 m_thread
->StopThread();
135 bool CBackgroundInfoLoader::IsLoading()
140 void CBackgroundInfoLoader::SetObserver(IBackgroundLoaderObserver
* pObserver
)
142 m_pObserver
= pObserver
;
145 void CBackgroundInfoLoader::SetProgressCallback(IProgressCallback
* pCallback
)
147 m_pProgressCallback
= pCallback
;