[video] fix selection after changing video or extra art
[xbmc.git] / xbmc / pvr / providers / PVRProvider.cpp
blobab2e7a34cf0c958374621f45a623e4409db9fdf2
1 /*
2 * Copyright (C) 2012-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 "PVRProvider.h"
11 #include "ServiceBroker.h"
12 #include "guilib/LocalizeStrings.h"
13 #include "pvr/PVRDatabase.h"
14 #include "pvr/PVRManager.h"
15 #include "pvr/addons/PVRClient.h"
16 #include "pvr/addons/PVRClients.h"
17 #include "utils/StringUtils.h"
18 #include "utils/Variant.h"
19 #include "utils/log.h"
21 #include <memory>
22 #include <mutex>
23 #include <string>
25 using namespace PVR;
28 const std::string CPVRProvider::IMAGE_OWNER_PATTERN = "pvrprovider";
30 CPVRProvider::CPVRProvider(int iUniqueId, int iClientId)
31 : m_iUniqueId(iUniqueId),
32 m_iClientId(iClientId),
33 m_iconPath(IMAGE_OWNER_PATTERN),
34 m_thumbPath(IMAGE_OWNER_PATTERN)
38 CPVRProvider::CPVRProvider(const PVR_PROVIDER& provider, int iClientId)
39 : m_iUniqueId(provider.iUniqueId),
40 m_iClientId(iClientId),
41 m_strName(provider.strName),
42 m_type(provider.type),
43 m_iconPath(provider.strIconPath, IMAGE_OWNER_PATTERN),
44 m_strCountries(provider.strCountries),
45 m_strLanguages(provider.strLanguages),
46 m_thumbPath(IMAGE_OWNER_PATTERN)
50 CPVRProvider::CPVRProvider(int iClientId,
51 const std::string& addonProviderName,
52 const std::string& addonIconPath,
53 const std::string& addonThumbPath)
54 : m_iClientId(iClientId),
55 m_strName(addonProviderName),
56 m_type(PVR_PROVIDER_TYPE_ADDON),
57 m_iconPath(addonIconPath, IMAGE_OWNER_PATTERN),
58 m_bIsClientProvider(true),
59 m_thumbPath(addonThumbPath, IMAGE_OWNER_PATTERN)
63 bool CPVRProvider::operator==(const CPVRProvider& right) const
65 return (m_iUniqueId == right.m_iUniqueId && m_iClientId == right.m_iClientId);
68 bool CPVRProvider::operator!=(const CPVRProvider& right) const
70 return !(*this == right);
73 void CPVRProvider::Serialize(CVariant& value) const
75 value["providerid"] = m_iDatabaseId;
76 value["clientid"] = m_iClientId;
77 value["providername"] = m_strName;
78 switch (m_type)
80 case PVR_PROVIDER_TYPE_ADDON:
81 value["providertype"] = "addon";
82 break;
83 case PVR_PROVIDER_TYPE_SATELLITE:
84 value["providertype"] = "satellite";
85 break;
86 case PVR_PROVIDER_TYPE_CABLE:
87 value["providertype"] = "cable";
88 break;
89 case PVR_PROVIDER_TYPE_AERIAL:
90 value["providertype"] = "aerial";
91 break;
92 case PVR_PROVIDER_TYPE_IPTV:
93 value["providertype"] = "iptv";
94 break;
95 case PVR_PROVIDER_TYPE_OTHER:
96 value["providertype"] = "other";
97 break;
98 default:
99 value["state"] = "unknown";
100 break;
102 value["iconpath"] = GetClientIconPath();
103 value["countries"] = m_strCountries;
104 value["languages"] = m_strLanguages;
107 int CPVRProvider::GetDatabaseId() const
109 std::unique_lock<CCriticalSection> lock(m_critSection);
110 return m_iDatabaseId;
113 bool CPVRProvider::SetDatabaseId(int iDatabaseId)
115 std::unique_lock<CCriticalSection> lock(m_critSection);
117 if (m_iDatabaseId != iDatabaseId)
119 m_iDatabaseId = iDatabaseId;
120 return true;
123 return false;
126 int CPVRProvider::GetUniqueId() const
128 std::unique_lock<CCriticalSection> lock(m_critSection);
129 return m_iUniqueId;
132 int CPVRProvider::GetClientId() const
134 std::unique_lock<CCriticalSection> lock(m_critSection);
135 return m_iClientId;
138 std::string CPVRProvider::GetName() const
140 std::unique_lock<CCriticalSection> lock(m_critSection);
141 return m_strName;
144 bool CPVRProvider::SetName(const std::string& strName)
146 std::unique_lock<CCriticalSection> lock(m_critSection);
147 if (m_strName != strName)
149 m_strName = strName;
150 return true;
153 return false;
156 PVR_PROVIDER_TYPE CPVRProvider::GetType() const
158 std::unique_lock<CCriticalSection> lock(m_critSection);
159 return m_type;
162 bool CPVRProvider::SetType(PVR_PROVIDER_TYPE type)
164 std::unique_lock<CCriticalSection> lock(m_critSection);
165 if (m_type != type)
167 m_type = type;
168 return true;
171 return false;
174 std::string CPVRProvider::GetClientIconPath() const
176 std::unique_lock<CCriticalSection> lock(m_critSection);
177 return m_iconPath.GetClientImage();
180 std::string CPVRProvider::GetIconPath() const
182 std::unique_lock<CCriticalSection> lock(m_critSection);
183 return m_iconPath.GetLocalImage();
186 bool CPVRProvider::SetIconPath(const std::string& strIconPath)
188 std::unique_lock<CCriticalSection> lock(m_critSection);
189 if (GetClientIconPath() != strIconPath)
191 m_iconPath.SetClientImage(strIconPath);
192 return true;
195 return false;
198 namespace
201 const std::vector<std::string> Tokenize(const std::string& str)
203 return StringUtils::Split(str, PROVIDER_STRING_TOKEN_SEPARATOR);
206 const std::string DeTokenize(const std::vector<std::string>& tokens)
208 return StringUtils::Join(tokens, PROVIDER_STRING_TOKEN_SEPARATOR);
211 } // unnamed namespace
213 std::vector<std::string> CPVRProvider::GetCountries() const
215 std::unique_lock<CCriticalSection> lock(m_critSection);
217 return Tokenize(m_strCountries);
220 bool CPVRProvider::SetCountries(const std::vector<std::string>& countries)
222 std::unique_lock<CCriticalSection> lock(m_critSection);
223 const std::string strCountries = DeTokenize(countries);
224 if (m_strCountries != strCountries)
226 m_strCountries = strCountries;
227 return true;
230 return false;
233 std::string CPVRProvider::GetCountriesDBString() const
235 std::unique_lock<CCriticalSection> lock(m_critSection);
236 return m_strCountries;
239 bool CPVRProvider::SetCountriesFromDBString(const std::string& strCountries)
241 std::unique_lock<CCriticalSection> lock(m_critSection);
242 if (m_strCountries != strCountries)
244 m_strCountries = strCountries;
245 return true;
248 return false;
251 std::vector<std::string> CPVRProvider::GetLanguages() const
253 std::unique_lock<CCriticalSection> lock(m_critSection);
254 return Tokenize(m_strLanguages);
257 bool CPVRProvider::SetLanguages(const std::vector<std::string>& languages)
259 std::unique_lock<CCriticalSection> lock(m_critSection);
260 const std::string strLanguages = DeTokenize(languages);
261 if (m_strLanguages != strLanguages)
263 m_strLanguages = strLanguages;
264 return true;
267 return false;
270 std::string CPVRProvider::GetLanguagesDBString() const
272 std::unique_lock<CCriticalSection> lock(m_critSection);
273 return m_strLanguages;
276 bool CPVRProvider::SetLanguagesFromDBString(const std::string& strLanguages)
278 std::unique_lock<CCriticalSection> lock(m_critSection);
279 if (m_strLanguages != strLanguages)
281 m_strLanguages = strLanguages;
282 return true;
285 return false;
288 bool CPVRProvider::Persist(bool updateRecord /* = false */)
290 const std::shared_ptr<CPVRDatabase> database = CServiceBroker::GetPVRManager().GetTVDatabase();
291 if (database)
293 std::unique_lock<CCriticalSection> lock(m_critSection);
294 return database->Persist(*this, updateRecord);
297 return false;
300 bool CPVRProvider::DeleteFromDatabase()
302 const std::shared_ptr<CPVRDatabase> database = CServiceBroker::GetPVRManager().GetTVDatabase();
303 if (database)
305 std::unique_lock<CCriticalSection> lock(m_critSection);
306 return database->Delete(*this);
309 return false;
312 bool CPVRProvider::UpdateEntry(const std::shared_ptr<CPVRProvider>& fromProvider,
313 ProviderUpdateMode updateMode)
315 bool bChanged = false;
316 std::unique_lock<CCriticalSection> lock(m_critSection);
318 if (updateMode == ProviderUpdateMode::BY_DATABASE)
320 m_iDatabaseId = fromProvider->m_iDatabaseId;
322 m_strName = fromProvider->m_strName;
323 m_type = fromProvider->m_type;
324 m_iconPath = fromProvider->m_iconPath;
326 if (fromProvider->m_bIsClientProvider)
328 m_thumbPath = fromProvider->m_thumbPath;
329 m_bIsClientProvider = fromProvider->m_bIsClientProvider;
332 m_strCountries = fromProvider->m_strCountries;
333 m_strLanguages = fromProvider->m_strLanguages;
335 else if (updateMode == ProviderUpdateMode::BY_CLIENT)
337 if (m_strName != fromProvider->m_strName)
339 m_strName = fromProvider->m_strName;
340 bChanged = true;
343 if (m_type != fromProvider->m_type)
345 m_type = fromProvider->m_type;
346 bChanged = true;
349 if (m_iconPath != fromProvider->m_iconPath)
351 m_iconPath = fromProvider->m_iconPath;
352 bChanged = true;
355 if (fromProvider->m_bIsClientProvider)
357 m_thumbPath = fromProvider->m_thumbPath;
358 m_bIsClientProvider = fromProvider->m_bIsClientProvider;
361 if (m_strCountries != fromProvider->m_strCountries)
363 m_strCountries = fromProvider->m_strCountries;
364 bChanged = true;
367 if (m_strLanguages != fromProvider->m_strLanguages)
369 m_strLanguages = fromProvider->m_strLanguages;
370 bChanged = true;
374 return bChanged;
377 bool CPVRProvider::HasThumbPath() const
379 std::unique_lock<CCriticalSection> lock(m_critSection);
380 return (m_type == PVR_PROVIDER_TYPE_ADDON && !m_thumbPath.GetLocalImage().empty());
383 std::string CPVRProvider::GetThumbPath() const
385 std::unique_lock<CCriticalSection> lock(m_critSection);
386 return m_thumbPath.GetLocalImage();
389 std::string CPVRProvider::GetClientThumbPath() const
391 std::unique_lock<CCriticalSection> lock(m_critSection);
392 return m_thumbPath.GetClientImage();