From afeb7e56f81c544422f665782c299f610ce5b68d Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 25 Jun 2024 07:14:40 +0200 Subject: changed: move CFileItem::GetTBNFile to ArtUtils --- xbmc/FileItem.cpp | 54 +++---------------------------- xbmc/FileItem.h | 2 -- xbmc/ThumbLoader.cpp | 5 ++- xbmc/utils/ArtUtils.cpp | 69 ++++++++++++++++++++++++++++++++++++++++ xbmc/utils/ArtUtils.h | 21 ++++++++++++ xbmc/utils/CMakeLists.txt | 2 ++ xbmc/utils/test/CMakeLists.txt | 1 + xbmc/utils/test/TestArtUtils.cpp | 63 ++++++++++++++++++++++++++++++++++++ xbmc/video/VideoDatabase.cpp | 7 ++-- 9 files changed, 168 insertions(+), 56 deletions(-) create mode 100644 xbmc/utils/ArtUtils.cpp create mode 100644 xbmc/utils/ArtUtils.h create mode 100644 xbmc/utils/test/TestArtUtils.cpp diff --git a/xbmc/FileItem.cpp b/xbmc/FileItem.cpp index 98360446a3..5065b51c46 100644 --- a/xbmc/FileItem.cpp +++ b/xbmc/FileItem.cpp @@ -52,6 +52,7 @@ #include "settings/SettingsComponent.h" #include "settings/lib/Setting.h" #include "utils/Archive.h" +#include "utils/ArtUtils.h" #include "utils/FileExtensionProvider.h" #include "utils/Mime.h" #include "utils/RegExp.h" @@ -1926,7 +1927,7 @@ std::string CFileItem::GetUserMusicThumb(bool alwaysCheckRemote /* = false */, b return ""; // we first check for .tbn or .tbn - std::string fileThumb(GetTBNFile()); + std::string fileThumb(ART::GetTBNFile(*this)); if (CFile::Exists(fileThumb)) return fileThumb; @@ -1984,53 +1985,6 @@ std::string CFileItem::GetUserMusicThumb(bool alwaysCheckRemote /* = false */, b return ""; } -// Gets the .tbn filename from a file or folder name. -// .ext -> .tbn -// / -> .tbn -std::string CFileItem::GetTBNFile() const -{ - std::string thumbFile; - std::string strFile = m_strPath; - - if (IsStack()) - { - std::string strPath, strReturn; - URIUtils::GetParentPath(m_strPath,strPath); - CFileItem item(CStackDirectory::GetFirstStackedFile(strFile),false); - std::string strTBNFile = item.GetTBNFile(); - strReturn = URIUtils::AddFileToFolder(strPath, URIUtils::GetFileName(strTBNFile)); - if (CFile::Exists(strReturn)) - return strReturn; - - strFile = URIUtils::AddFileToFolder(strPath,URIUtils::GetFileName(CStackDirectory::GetStackedTitlePath(strFile))); - } - - if (URIUtils::IsInRAR(strFile) || URIUtils::IsInZIP(strFile)) - { - std::string strPath = URIUtils::GetDirectory(strFile); - std::string strParent; - URIUtils::GetParentPath(strPath,strParent); - strFile = URIUtils::AddFileToFolder(strParent, URIUtils::GetFileName(m_strPath)); - } - - CURL url(strFile); - strFile = url.GetFileName(); - - if (m_bIsFolder && !IsFileFolder()) - URIUtils::RemoveSlashAtEnd(strFile); - - if (!strFile.empty()) - { - if (m_bIsFolder && !IsFileFolder()) - thumbFile = strFile + ".tbn"; // folder, so just add ".tbn" - else - thumbFile = URIUtils::ReplaceExtension(strFile, ".tbn"); - url.SetFileName(thumbFile); - thumbFile = url.Get(); - } - return thumbFile; -} - bool CFileItem::SkipLocalArt() const { return (m_strPath.empty() || StringUtils::StartsWithNoCase(m_strPath, "newsmartplaylist://") || @@ -2257,7 +2211,7 @@ std::string CFileItem::GetLocalFanart() const strPath2 = dir.GetStackedTitlePath(strFile); strFile = URIUtils::AddFileToFolder(strPath, URIUtils::GetFileName(strPath2)); CFileItem item(dir.GetFirstStackedFile(m_strPath),false); - std::string strTBNFile(URIUtils::ReplaceExtension(item.GetTBNFile(), "-fanart")); + std::string strTBNFile(URIUtils::ReplaceExtension(ART::GetTBNFile(item), "-fanart")); strFile2 = URIUtils::AddFileToFolder(strPath, URIUtils::GetFileName(strTBNFile)); } if (URIUtils::IsInRAR(strFile) || URIUtils::IsInZIP(strFile)) @@ -2663,7 +2617,7 @@ std::string CFileItem::FindTrailer() const strPath2 = dir.GetStackedTitlePath(strFile); strFile = URIUtils::AddFileToFolder(strPath,URIUtils::GetFileName(strPath2)); CFileItem item(dir.GetFirstStackedFile(m_strPath),false); - std::string strTBNFile(URIUtils::ReplaceExtension(item.GetTBNFile(), "-trailer")); + std::string strTBNFile(URIUtils::ReplaceExtension(ART::GetTBNFile(item), "-trailer")); strFile2 = URIUtils::AddFileToFolder(strPath,URIUtils::GetFileName(strTBNFile)); } if (URIUtils::IsInRAR(strFile) || URIUtils::IsInZIP(strFile)) diff --git a/xbmc/FileItem.h b/xbmc/FileItem.h index 2023c2e43e..140d130f48 100644 --- a/xbmc/FileItem.h +++ b/xbmc/FileItem.h @@ -443,8 +443,6 @@ public: */ std::string GetThumbHideIfUnwatched(const CFileItem* item) const; - // Gets the .tbn file associated with this item - std::string GetTBNFile() const; // Gets the folder image associated with this item (defaults to folder.jpg) std::string GetFolderThumb(const std::string &folderJPG = "folder.jpg") const; // Gets the correct movie title diff --git a/xbmc/ThumbLoader.cpp b/xbmc/ThumbLoader.cpp index 3e1508cc49..40db919d52 100644 --- a/xbmc/ThumbLoader.cpp +++ b/xbmc/ThumbLoader.cpp @@ -11,8 +11,11 @@ #include "FileItem.h" #include "ServiceBroker.h" #include "TextureCache.h" +#include "utils/ArtUtils.h" #include "utils/FileUtils.h" +using namespace KODI; + CThumbLoader::CThumbLoader() : CBackgroundInfoLoader() { @@ -117,7 +120,7 @@ std::string CProgramThumbLoader::GetLocalThumb(const CFileItem &item) } else { - std::string fileThumb(item.GetTBNFile()); + std::string fileThumb(ART::GetTBNFile(item)); if (CFileUtils::Exists(fileThumb)) return fileThumb; } diff --git a/xbmc/utils/ArtUtils.cpp b/xbmc/utils/ArtUtils.cpp new file mode 100644 index 0000000000..46251a1480 --- /dev/null +++ b/xbmc/utils/ArtUtils.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2024 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 "ArtUtils.h" + +#include "FileItem.h" +#include "filesystem/File.h" +#include "filesystem/StackDirectory.h" +#include "utils/URIUtils.h" + +using namespace XFILE; + +namespace KODI::ART +{ + +// Gets the .tbn filename from a file or folder name. +// .ext -> .tbn +// / -> .tbn +std::string GetTBNFile(const CFileItem& item) +{ + std::string thumbFile; + std::string strFile = item.GetPath(); + + if (item.IsStack()) + { + std::string strPath, strReturn; + URIUtils::GetParentPath(item.GetPath(), strPath); + CFileItem item(CStackDirectory::GetFirstStackedFile(strFile), false); + std::string strTBNFile = GetTBNFile(item); + strReturn = URIUtils::AddFileToFolder(strPath, URIUtils::GetFileName(strTBNFile)); + if (CFile::Exists(strReturn)) + return strReturn; + + strFile = URIUtils::AddFileToFolder( + strPath, URIUtils::GetFileName(CStackDirectory::GetStackedTitlePath(strFile))); + } + + if (URIUtils::IsInRAR(strFile) || URIUtils::IsInZIP(strFile)) + { + std::string strPath = URIUtils::GetDirectory(strFile); + std::string strParent; + URIUtils::GetParentPath(strPath, strParent); + strFile = URIUtils::AddFileToFolder(strParent, URIUtils::GetFileName(item.GetPath())); + } + + CURL url(strFile); + strFile = url.GetFileName(); + + if (item.m_bIsFolder && !item.IsFileFolder()) + URIUtils::RemoveSlashAtEnd(strFile); + + if (!strFile.empty()) + { + if (item.m_bIsFolder && !item.IsFileFolder()) + thumbFile = strFile + ".tbn"; // folder, so just add ".tbn" + else + thumbFile = URIUtils::ReplaceExtension(strFile, ".tbn"); + url.SetFileName(thumbFile); + thumbFile = url.Get(); + } + return thumbFile; +} + +} // namespace KODI::ART diff --git a/xbmc/utils/ArtUtils.h b/xbmc/utils/ArtUtils.h new file mode 100644 index 0000000000..b2ae688206 --- /dev/null +++ b/xbmc/utils/ArtUtils.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2024 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. + */ + +#pragma once + +#include + +class CFileItem; + +namespace KODI::ART +{ + +// Gets the .tbn file associated with an item +std::string GetTBNFile(const CFileItem& item); + +} // namespace KODI::ART diff --git a/xbmc/utils/CMakeLists.txt b/xbmc/utils/CMakeLists.txt index 13dbe058a9..7379405cd5 100644 --- a/xbmc/utils/CMakeLists.txt +++ b/xbmc/utils/CMakeLists.txt @@ -2,6 +2,7 @@ set(SOURCES ActorProtocol.cpp AlarmClock.cpp AliasShortcutUtils.cpp Archive.cpp + ArtUtils.cpp Base64.cpp BitstreamConverter.cpp BitstreamReader.cpp @@ -82,6 +83,7 @@ set(HEADERS ActorProtocol.h AlarmClock.h AliasShortcutUtils.h Archive.h + ArtUtils.h Base64.h BitstreamConverter.h BitstreamReader.h diff --git a/xbmc/utils/test/CMakeLists.txt b/xbmc/utils/test/CMakeLists.txt index 3cdc5d6448..99bd4faaf7 100644 --- a/xbmc/utils/test/CMakeLists.txt +++ b/xbmc/utils/test/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES TestAlarmClock.cpp TestAliasShortcutUtils.cpp TestArchive.cpp + TestArtUtils.cpp TestBase64.cpp TestBitstreamStats.cpp TestCharsetConverter.cpp diff --git a/xbmc/utils/test/TestArtUtils.cpp b/xbmc/utils/test/TestArtUtils.cpp new file mode 100644 index 0000000000..92770435ec --- /dev/null +++ b/xbmc/utils/test/TestArtUtils.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2024 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 "FileItem.h" +#include "platform/Filesystem.h" +#include "utils/ArtUtils.h" +#include "utils/FileUtils.h" +#include "utils/URIUtils.h" + +#include +#include + +#include +#include + +using namespace KODI; + +struct TbnTest +{ + std::string path; + std::string result; + bool isFolder = false; +}; + +class GetTbnTest : public testing::WithParamInterface, public testing::Test +{ +}; + +TEST_P(GetTbnTest, TbnTest) +{ + EXPECT_EQ(ART::GetTBNFile(CFileItem(GetParam().path, GetParam().isFolder)), GetParam().result); +} + +const auto tbn_tests = std::array{ + TbnTest{"/home/user/video.avi", "/home/user/video.tbn"}, + TbnTest{"/home/user/video/", "/home/user/video.tbn", true}, + TbnTest{"/home/user/bar.xbt", "/home/user/bar.tbn", true}, + TbnTest{"zip://%2fhome%2fuser%2fbar.zip/foo.avi", "/home/user/foo.tbn"}, + TbnTest{"stack:///home/user/foo-cd1.avi , /home/user/foo-cd2.avi", "/home/user/foo.tbn"}}; + +INSTANTIATE_TEST_SUITE_P(TestArtUtils, GetTbnTest, testing::ValuesIn(tbn_tests)); + +TEST(TestArtUtils, GetTbnStack) +{ + std::error_code ec; + auto path = KODI::PLATFORM::FILESYSTEM::temp_directory_path(ec); + ASSERT_TRUE(!ec); + const auto file_path = URIUtils::AddFileToFolder(path, "foo-cd1.tbn"); + { + std::ofstream of(file_path, std::ios::out); + } + const std::string stackPath = + fmt::format("stack://{} , {}", URIUtils::AddFileToFolder(path, "foo-cd1.avi"), + URIUtils::AddFileToFolder(path, "foo-cd2.avi")); + CFileItem item(stackPath, false); + EXPECT_EQ(ART::GetTBNFile(item), file_path); + CFileUtils::DeleteItem(file_path); +} diff --git a/xbmc/video/VideoDatabase.cpp b/xbmc/video/VideoDatabase.cpp index 1e9cf8993f..686dbbbe3d 100644 --- a/xbmc/video/VideoDatabase.cpp +++ b/xbmc/video/VideoDatabase.cpp @@ -44,6 +44,7 @@ #include "settings/Settings.h" #include "settings/SettingsComponent.h" #include "storage/MediaManager.h" +#include "utils/ArtUtils.h" #include "utils/FileUtils.h" #include "utils/GroupUtils.h" #include "utils/LabelFormatter.h" @@ -10710,7 +10711,7 @@ void CVideoDatabase::ExportToXML(const std::string &path, bool singleFile /* = t } else { - std::string nfoFile(URIUtils::ReplaceExtension(item.GetTBNFile(), ".nfo")); + std::string nfoFile(URIUtils::ReplaceExtension(ART::GetTBNFile(item), ".nfo")); if (item.IsOpticalMediaFile()) { @@ -10863,7 +10864,7 @@ void CVideoDatabase::ExportToXML(const std::string &path, bool singleFile /* = t } else { - std::string nfoFile(URIUtils::ReplaceExtension(item.GetTBNFile(), ".nfo")); + std::string nfoFile(URIUtils::ReplaceExtension(ART::GetTBNFile(item), ".nfo")); if (overwrite || !CFile::Exists(nfoFile, false)) { @@ -11064,7 +11065,7 @@ void CVideoDatabase::ExportToXML(const std::string &path, bool singleFile /* = t } else { - std::string nfoFile(URIUtils::ReplaceExtension(item.GetTBNFile(), ".nfo")); + std::string nfoFile(URIUtils::ReplaceExtension(ART::GetTBNFile(item), ".nfo")); if (overwrite || !CFile::Exists(nfoFile, false)) { -- cgit v1.2.3 From d3f3373dc9b430dc3e81c7caeed42c5c344b1731 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Sun, 30 Jun 2024 15:55:53 +0200 Subject: drop str prefix for string variables --- xbmc/utils/ArtUtils.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/xbmc/utils/ArtUtils.cpp b/xbmc/utils/ArtUtils.cpp index 46251a1480..c76192956e 100644 --- a/xbmc/utils/ArtUtils.cpp +++ b/xbmc/utils/ArtUtils.cpp @@ -24,42 +24,42 @@ namespace KODI::ART std::string GetTBNFile(const CFileItem& item) { std::string thumbFile; - std::string strFile = item.GetPath(); + std::string file = item.GetPath(); if (item.IsStack()) { - std::string strPath, strReturn; - URIUtils::GetParentPath(item.GetPath(), strPath); - CFileItem item(CStackDirectory::GetFirstStackedFile(strFile), false); - std::string strTBNFile = GetTBNFile(item); - strReturn = URIUtils::AddFileToFolder(strPath, URIUtils::GetFileName(strTBNFile)); - if (CFile::Exists(strReturn)) - return strReturn; + std::string path, returnPath; + URIUtils::GetParentPath(item.GetPath(), path); + CFileItem item(CStackDirectory::GetFirstStackedFile(file), false); + const std::string TBNFile = GetTBNFile(item); + returnPath = URIUtils::AddFileToFolder(path, URIUtils::GetFileName(TBNFile)); + if (CFile::Exists(returnPath)) + return returnPath; - strFile = URIUtils::AddFileToFolder( - strPath, URIUtils::GetFileName(CStackDirectory::GetStackedTitlePath(strFile))); + const std::string& stackPath = CStackDirectory::GetStackedTitlePath(file); + file = URIUtils::AddFileToFolder(path, URIUtils::GetFileName(stackPath)); } - if (URIUtils::IsInRAR(strFile) || URIUtils::IsInZIP(strFile)) + if (URIUtils::IsInRAR(file) || URIUtils::IsInZIP(file)) { - std::string strPath = URIUtils::GetDirectory(strFile); - std::string strParent; - URIUtils::GetParentPath(strPath, strParent); - strFile = URIUtils::AddFileToFolder(strParent, URIUtils::GetFileName(item.GetPath())); + const std::string path = URIUtils::GetDirectory(file); + std::string parent; + URIUtils::GetParentPath(path, parent); + file = URIUtils::AddFileToFolder(parent, URIUtils::GetFileName(item.GetPath())); } - CURL url(strFile); - strFile = url.GetFileName(); + CURL url(file); + file = url.GetFileName(); if (item.m_bIsFolder && !item.IsFileFolder()) - URIUtils::RemoveSlashAtEnd(strFile); + URIUtils::RemoveSlashAtEnd(file); - if (!strFile.empty()) + if (!file.empty()) { if (item.m_bIsFolder && !item.IsFileFolder()) - thumbFile = strFile + ".tbn"; // folder, so just add ".tbn" + thumbFile = file + ".tbn"; // folder, so just add ".tbn" else - thumbFile = URIUtils::ReplaceExtension(strFile, ".tbn"); + thumbFile = URIUtils::ReplaceExtension(file, ".tbn"); url.SetFileName(thumbFile); thumbFile = url.Get(); } -- cgit v1.2.3