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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/*
* Copyright (C) 2012-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 "PVRProviders.h"
#include "ServiceBroker.h"
#include "pvr/PVRCachedImages.h"
#include "pvr/PVRDatabase.h"
#include "pvr/PVRManager.h"
#include "pvr/addons/PVRClient.h"
#include "pvr/addons/PVRClients.h"
#include "pvr/providers/PVRProvider.h"
#include "settings/Settings.h"
#include "utils/log.h"
#include <algorithm>
#include <memory>
#include <mutex>
#include <numeric>
#include <string>
#include <vector>
using namespace PVR;
bool CPVRProvidersContainer::UpdateFromClient(const std::shared_ptr<CPVRProvider>& provider)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
const std::shared_ptr<CPVRProvider> providerToUpdate =
GetByClient(provider->GetClientId(), provider->GetUniqueId());
if (providerToUpdate)
{
return providerToUpdate->UpdateEntry(provider, ProviderUpdateMode::BY_CLIENT);
}
else
{
provider->SetDatabaseId(++m_iLastId);
InsertEntry(provider, ProviderUpdateMode::BY_CLIENT);
}
return true;
}
std::shared_ptr<CPVRProvider> CPVRProvidersContainer::GetByClient(int iClientId,
int iUniqueId) const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
const auto it = std::find_if(
m_providers.cbegin(), m_providers.cend(), [iClientId, iUniqueId](const auto& provider) {
return provider->GetClientId() == iClientId && provider->GetUniqueId() == iUniqueId;
});
return it != m_providers.cend() ? (*it) : std::shared_ptr<CPVRProvider>();
}
void CPVRProvidersContainer::InsertEntry(const std::shared_ptr<CPVRProvider>& newProvider,
ProviderUpdateMode updateMode)
{
bool found = false;
for (auto& provider : m_providers)
{
if (provider->GetClientId() == newProvider->GetClientId() &&
provider->GetUniqueId() == newProvider->GetUniqueId())
{
found = true;
provider->UpdateEntry(newProvider, updateMode);
}
}
if (!found)
{
m_providers.emplace_back(newProvider);
}
}
std::vector<std::shared_ptr<CPVRProvider>> CPVRProvidersContainer::GetProvidersList() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
return m_providers;
}
std::size_t CPVRProvidersContainer::GetNumProviders() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
return m_providers.size();
}
bool CPVRProviders::Update(const std::vector<std::shared_ptr<CPVRClient>>& clients)
{
return LoadFromDatabase(clients) && UpdateFromClients(clients);
}
void CPVRProviders::Unload()
{
// remove all tags
std::unique_lock<CCriticalSection> lock(m_critSection);
m_providers.clear();
}
bool CPVRProviders::LoadFromDatabase(const std::vector<std::shared_ptr<CPVRClient>>& clients)
{
const std::shared_ptr<CPVRDatabase> database = CServiceBroker::GetPVRManager().GetTVDatabase();
if (database)
{
m_iLastId = database->GetMaxProviderId();
CPVRProviders providers;
database->Get(providers, clients);
for (auto& provider : providers.GetProvidersList())
CheckAndAddEntry(provider, ProviderUpdateMode::BY_DATABASE);
}
return true;
}
bool CPVRProviders::UpdateFromClients(const std::vector<std::shared_ptr<CPVRClient>>& clients)
{
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (m_bIsUpdating)
return false;
m_bIsUpdating = true;
}
// Default client providers are the add-ons themselves, we retrieve both enabled
// and disabled add-ons as we don't want them removed from the DB
CPVRProviders newAddonProviderList;
std::vector<int> disabledClients;
std::vector<CVariant> clientProviderInfos =
CServiceBroker::GetPVRManager().Clients()->GetClientProviderInfos();
CLog::LogFC(LOGDEBUG, LOGPVR, "Adding default providers, found {} PVR add-ons",
clientProviderInfos.size());
for (const auto& clientInfo : clientProviderInfos)
{
auto addonProvider = std::make_shared<CPVRProvider>(
clientInfo["clientid"].asInteger32(), clientInfo["name"].asString(),
clientInfo["icon"].asString(), clientInfo["thumb"].asString());
newAddonProviderList.CheckAndAddEntry(addonProvider, ProviderUpdateMode::BY_CLIENT);
if (!clientInfo["enabled"].asBoolean())
disabledClients.emplace_back(clientInfo["clientid"].asInteger32());
}
UpdateDefaultEntries(newAddonProviderList);
// Client providers are retrieved from the clients
CLog::LogFC(LOGDEBUG, LOGPVR, "Updating providers");
CPVRProvidersContainer newProviderList;
std::vector<int> failedClients;
CServiceBroker::GetPVRManager().Clients()->GetProviders(clients, &newProviderList, failedClients);
return UpdateClientEntries(newProviderList, failedClients, disabledClients);
}
bool CPVRProviders::UpdateDefaultEntries(const CPVRProvidersContainer& newProviders)
{
bool bChanged = false;
std::unique_lock<CCriticalSection> lock(m_critSection);
// go through the provider list and check for updated or new providers
const auto newProviderList = newProviders.GetProvidersList();
bChanged = std::accumulate(
newProviderList.cbegin(), newProviderList.cend(), false,
[this](bool changed, const auto& newProvider) {
return (CheckAndPersistEntry(newProvider, ProviderUpdateMode::BY_CLIENT) != nullptr)
? true
: changed;
});
// check for deleted providers
for (std::vector<std::shared_ptr<CPVRProvider>>::iterator it = m_providers.begin();
it != m_providers.end();)
{
const std::shared_ptr<CPVRProvider> provider = *it;
if (!newProviders.GetByClient(provider->GetClientId(), provider->GetUniqueId()))
{
// provider was not found
bool bIgnoreProvider = false;
// ignore add-on any providers that are no PVR Client addon providers
if (!provider->IsClientProvider())
bIgnoreProvider = true;
if (bIgnoreProvider)
{
++it;
continue;
}
CLog::LogFC(LOGDEBUG, LOGPVR, "Deleted provider {} on client {}", provider->GetUniqueId(),
provider->GetClientId());
(*it)->DeleteFromDatabase();
it = m_providers.erase(it);
bChanged |= true;
}
else
{
++it;
}
}
return bChanged;
}
bool CPVRProviders::UpdateClientEntries(const CPVRProvidersContainer& newProviders,
const std::vector<int>& failedClients,
const std::vector<int>& disabledClients)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
// go through the provider list and check for updated or new providers
for (const auto& newProvider : newProviders.GetProvidersList())
{
CheckAndPersistEntry(newProvider, ProviderUpdateMode::BY_CLIENT);
}
// check for deleted providers
for (auto it = m_providers.begin(); it != m_providers.end();)
{
const std::shared_ptr<CPVRProvider> provider = *it;
if (!newProviders.GetByClient(provider->GetClientId(), provider->GetUniqueId()))
{
const bool bIgnoreProvider =
(provider->IsClientProvider() || // ignore add-on providers as they are a special case
std::any_of(failedClients.cbegin(), failedClients.cend(),
[&provider](const auto& failedClient) {
return failedClient == provider->GetClientId();
}) ||
std::any_of(disabledClients.cbegin(), disabledClients.cend(),
[&provider](const auto& disabledClient) {
return disabledClient == provider->GetClientId();
}));
if (bIgnoreProvider)
{
++it;
continue;
}
CLog::LogFC(LOGDEBUG, LOGPVR, "Deleted provider {} on client {}", provider->GetUniqueId(),
provider->GetClientId());
(*it)->DeleteFromDatabase();
it = m_providers.erase(it);
}
else
{
++it;
}
}
m_bIsUpdating = false;
return true;
}
std::shared_ptr<CPVRProvider> CPVRProviders::CheckAndAddEntry(
const std::shared_ptr<CPVRProvider>& newProvider, ProviderUpdateMode updateMode)
{
bool bChanged = false;
std::unique_lock<CCriticalSection> lock(m_critSection);
std::shared_ptr<CPVRProvider> provider =
GetByClient(newProvider->GetClientId(), newProvider->GetUniqueId());
if (provider)
{
bChanged = provider->UpdateEntry(newProvider, updateMode);
}
else
{
// We don't set an id as this came from the DB so it has one already
InsertEntry(newProvider, updateMode);
if (newProvider->GetDatabaseId() > m_iLastId)
m_iLastId = newProvider->GetDatabaseId();
provider = newProvider;
bChanged = true;
}
if (bChanged)
return provider;
return {};
}
std::shared_ptr<CPVRProvider> CPVRProviders::CheckAndPersistEntry(
const std::shared_ptr<CPVRProvider>& newProvider, ProviderUpdateMode updateMode)
{
bool bChanged = false;
std::unique_lock<CCriticalSection> lock(m_critSection);
std::shared_ptr<CPVRProvider> provider =
GetByClient(newProvider->GetClientId(), newProvider->GetUniqueId());
if (provider)
{
bChanged = provider->UpdateEntry(newProvider, updateMode);
if (bChanged)
provider->Persist(true);
CLog::LogFC(LOGDEBUG, LOGPVR, "Updated provider {} on client {}", newProvider->GetUniqueId(),
newProvider->GetClientId());
}
else
{
newProvider->SetDatabaseId(++m_iLastId);
InsertEntry(newProvider, updateMode);
newProvider->Persist();
CLog::LogFC(LOGDEBUG, LOGPVR, "Added provider {} on client {}", newProvider->GetUniqueId(),
newProvider->GetClientId());
provider = newProvider;
bChanged = true;
}
if (bChanged)
return provider;
return {};
}
bool CPVRProviders::PersistUserChanges(const std::vector<std::shared_ptr<CPVRProvider>>& providers)
{
for (const auto& provider : providers)
{
provider->Persist(true);
CLog::LogFC(LOGDEBUG, LOGPVR, "Updated provider {} on client {}", provider->GetUniqueId(),
provider->GetClientId());
}
return true;
}
std::shared_ptr<CPVRProvider> CPVRProviders::GetById(int iProviderId) const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
const auto it =
std::find_if(m_providers.cbegin(), m_providers.cend(), [iProviderId](const auto& provider) {
return provider->GetDatabaseId() == iProviderId;
});
return it != m_providers.cend() ? (*it) : std::shared_ptr<CPVRProvider>();
}
void CPVRProviders::RemoveEntry(const std::shared_ptr<CPVRProvider>& provider)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
m_providers.erase(
std::remove_if(m_providers.begin(), m_providers.end(),
[&provider](const std::shared_ptr<CPVRProvider>& providerToRemove) {
return provider->GetClientId() == providerToRemove->GetClientId() &&
provider->GetUniqueId() == providerToRemove->GetUniqueId();
}),
m_providers.end());
}
int CPVRProviders::CleanupCachedImages()
{
std::vector<std::string> urlsToCheck;
{
std::unique_lock<CCriticalSection> lock(m_critSection);
for (const auto& provider : m_providers)
{
urlsToCheck.emplace_back(provider->GetClientIconPath());
urlsToCheck.emplace_back(provider->GetClientThumbPath());
}
}
static const std::vector<PVRImagePattern> urlPatterns = {{CPVRProvider::IMAGE_OWNER_PATTERN, ""}};
return CPVRCachedImages::Cleanup(urlPatterns, urlsToCheck);
}
|