blob: 8235f810965dca9160a5b5490da32fb61ec0a3e6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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;
}
|