2 * Copyright (C) 2012-2019 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 "PVRGUIChannelIconUpdater.h"
12 #include "FileItemList.h"
13 #include "ServiceBroker.h"
15 #include "filesystem/Directory.h"
16 #include "guilib/LocalizeStrings.h"
17 #include "pvr/channels/PVRChannel.h"
18 #include "pvr/channels/PVRChannelGroup.h"
19 #include "pvr/channels/PVRChannelGroupMember.h"
20 #include "pvr/guilib/PVRGUIProgressHandler.h"
21 #include "settings/AdvancedSettings.h"
22 #include "settings/Settings.h"
23 #include "settings/SettingsComponent.h"
24 #include "utils/FileUtils.h"
25 #include "utils/URIUtils.h"
26 #include "utils/log.h"
35 void CPVRGUIChannelIconUpdater::SearchAndUpdateMissingChannelIcons() const
37 const std::string iconPath
= CServiceBroker::GetSettingsComponent()->GetSettings()->GetString(CSettings::SETTING_PVRMENU_ICONPATH
);
41 // fetch files in icon path for fast lookup
42 CFileItemList fileItemList
;
43 XFILE::CDirectory::GetDirectory(iconPath
, fileItemList
, ".jpg|.png|.tbn", XFILE::DIR_FLAG_DEFAULTS
);
45 if (fileItemList
.IsEmpty())
48 CLog::Log(LOGINFO
, "Starting PVR channel icon search");
50 // create a map for fast lookup of normalized file base name
51 std::map
<std::string
, std::string
> fileItemMap
;
52 for (const auto& item
: fileItemList
)
54 std::string baseName
= URIUtils::GetFileName(item
->GetPath());
55 URIUtils::RemoveExtension(baseName
);
56 StringUtils::ToLower(baseName
);
57 fileItemMap
.insert({baseName
, item
->GetPath()});
60 std::unique_ptr
<CPVRGUIProgressHandler
> progressHandler
;
61 if (!m_groups
.empty())
62 progressHandler
= std::make_unique
<CPVRGUIProgressHandler
>(
63 g_localizeStrings
.Get(19286)); // Searching for channel icons
65 for (const auto& group
: m_groups
)
67 const std::vector
<std::shared_ptr
<CPVRChannelGroupMember
>> members
= group
->GetMembers();
69 for (const auto& member
: members
)
71 const std::shared_ptr
<CPVRChannel
> channel
= member
->Channel();
73 progressHandler
->UpdateProgress(channel
->ChannelName(), channelIndex
++,
74 static_cast<unsigned int>(members
.size()));
76 // skip if an icon is already set and exists
77 if (CFileUtils::Exists(channel
->IconPath()))
80 // reset icon before searching for a new one
81 channel
->SetIconPath("");
83 const std::string strChannelUid
= StringUtils::Format("{:08}", channel
->UniqueID());
84 std::string strLegalClientChannelName
=
85 CUtil::MakeLegalFileName(channel
->ClientChannelName());
86 StringUtils::ToLower(strLegalClientChannelName
);
87 std::string strLegalChannelName
= CUtil::MakeLegalFileName(channel
->ChannelName());
88 StringUtils::ToLower(strLegalChannelName
);
90 std::map
<std::string
, std::string
>::iterator itItem
;
91 if ((itItem
= fileItemMap
.find(strLegalClientChannelName
)) != fileItemMap
.end() ||
92 (itItem
= fileItemMap
.find(strLegalChannelName
)) != fileItemMap
.end() ||
93 (itItem
= fileItemMap
.find(strChannelUid
)) != fileItemMap
.end())
95 channel
->SetIconPath(itItem
->second
, CServiceBroker::GetSettingsComponent()
96 ->GetAdvancedSettings()
97 ->m_bPVRAutoScanIconsUserSet
);