2 * Copyright (C) 2005-2015 Team Kodi
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Kodi; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "BackgroundInfoLoader.h"
23 #include "threads/SingleLock.h"
24 #include "utils/log.h"
27 CBackgroundInfoLoader::CBackgroundInfoLoader() : m_thread (NULL
)
31 m_pProgressCallback
=NULL
;
36 CBackgroundInfoLoader::~CBackgroundInfoLoader()
41 void CBackgroundInfoLoader::Run()
45 if (!m_vecItems
.empty())
49 // Stage 1: All "fast" stuff we have already cached
50 for (std::vector
<CFileItemPtr
>::const_iterator iter
= m_vecItems
.begin(); iter
!= m_vecItems
.end(); ++iter
)
52 CFileItemPtr pItem
= *iter
;
54 // Ask the callback if we should abort
55 if ((m_pProgressCallback
&& m_pProgressCallback
->Abort()) || m_bStop
)
60 if (LoadItemCached(pItem
.get()) && m_pObserver
)
61 m_pObserver
->OnItemLoaded(pItem
.get());
65 CLog::Log(LOGERROR
, "CBackgroundInfoLoader::LoadItemCached - Unhandled exception for item %s", CURL::GetRedacted(pItem
->GetPath()).c_str());
69 // Stage 2: All "slow" stuff that we need to lookup
70 for (std::vector
<CFileItemPtr
>::const_iterator iter
= m_vecItems
.begin(); iter
!= m_vecItems
.end(); ++iter
)
72 CFileItemPtr pItem
= *iter
;
74 // Ask the callback if we should abort
75 if ((m_pProgressCallback
&& m_pProgressCallback
->Abort()) || m_bStop
)
80 if (LoadItemLookup(pItem
.get()) && m_pObserver
)
81 m_pObserver
->OnItemLoaded(pItem
.get());
85 CLog::Log(LOGERROR
, "CBackgroundInfoLoader::LoadItemLookup - Unhandled exception for item %s", CURL::GetRedacted(pItem
->GetPath()).c_str());
96 CLog::Log(LOGERROR
, "%s - Unhandled exception", __FUNCTION__
);
100 void CBackgroundInfoLoader::Load(CFileItemList
& items
)
107 CSingleLock
lock(m_lock
);
109 for (int nItem
=0; nItem
< items
.Size(); nItem
++)
110 m_vecItems
.push_back(items
[nItem
]);
112 m_pVecItems
= &items
;
116 m_thread
= new CThread(this, "BackgroundLoader");
118 m_thread
->SetPriority(THREAD_PRIORITY_BELOW_NORMAL
);
121 void CBackgroundInfoLoader::StopAsync()
127 void CBackgroundInfoLoader::StopThread()
133 m_thread
->StopThread();
139 m_bIsLoading
= false;
142 bool CBackgroundInfoLoader::IsLoading()
147 void CBackgroundInfoLoader::SetObserver(IBackgroundLoaderObserver
* pObserver
)
149 m_pObserver
= pObserver
;
152 void CBackgroundInfoLoader::SetProgressCallback(IProgressCallback
* pCallback
)
154 m_pProgressCallback
= pCallback
;