Skip to content

Commit

Permalink
Merge pull request xbmc#23885 from ksooo/favourites-fix-add-remove
Browse files Browse the repository at this point in the history
[favourites] Fix CFavouritesService::IsFavourited to only compare man…
  • Loading branch information
ksooo authored Oct 9, 2023
2 parents 2964bb4 + cd61b3a commit be9519b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
79 changes: 68 additions & 11 deletions xbmc/favourites/FavouritesService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ServiceBroker.h"
#include "Util.h"
#include "favourites/FavouritesURL.h"
#include "input/WindowTranslator.h"
#include "profiles/ProfileManager.h"
#include "settings/SettingsComponent.h"
#include "utils/ContentUtils.h"
Expand Down Expand Up @@ -204,27 +205,24 @@ void CFavouritesService::OnUpdated()
m_events.Publish(FavouritesUpdated{});
}

std::string CFavouritesService::GetFavouritesUrl(const CFileItem& item, int contextWindow) const
{
return CFavouritesURL(item, contextWindow).GetURL();
}

bool CFavouritesService::AddOrRemove(const CFileItem& item, int contextWindow)
{
auto favUrl = GetFavouritesUrl(item, contextWindow);
{
std::unique_lock<CCriticalSection> lock(m_criticalSection);
CFileItemPtr match = m_favourites.Get(favUrl);

const std::shared_ptr<CFileItem> match{GetFavourite(item, contextWindow)};
if (match)
{ // remove the item
m_favourites.Remove(match.get());
}
else
{ // create our new favourite item
const CFileItemPtr favourite(std::make_shared<CFileItem>(item.GetLabel()));
{
// create our new favourite item
const auto favourite{std::make_shared<CFileItem>(item.GetLabel())};
if (item.GetLabel().empty())
favourite->SetLabel(CUtil::GetTitleFromPath(item.GetPath(), item.m_bIsFolder));
favourite->SetArt("thumb", ContentUtils::GetPreferredArtImage(item));
const std::string favUrl{CFavouritesURL(item, contextWindow).GetURL()};
favourite->SetPath(favUrl);
m_favourites.Add(favourite);
}
Expand All @@ -234,10 +232,69 @@ bool CFavouritesService::AddOrRemove(const CFileItem& item, int contextWindow)
return true;
}

bool CFavouritesService::IsFavourited(const CFileItem& item, int contextWindow) const
std::shared_ptr<CFileItem> CFavouritesService::GetFavourite(const CFileItem& item,
int contextWindow) const
{
std::unique_lock<CCriticalSection> lock(m_criticalSection);
return m_favourites.Contains(GetFavouritesUrl(item, contextWindow));

const CFavouritesURL favURL{item, contextWindow};
const bool isVideoDb{URIUtils::IsVideoDb(favURL.GetTarget())};
const bool isMusicDb{URIUtils::IsMusicDb(favURL.GetTarget())};

for (const auto& favItem : m_favourites)
{
const CFavouritesURL favItemURL{*favItem, contextWindow};

// Compare the whole target URLs
if (favItemURL.GetTarget() == item.GetPath())
return favItem;

// Compare the target URLs ignoring optional parameters
if (favItemURL.GetAction() == favURL.GetAction() &&
(favItemURL.GetAction() != CFavouritesURL::Action::ACTIVATE_WINDOW ||
favItemURL.GetWindowID() == favURL.GetWindowID()))
{
if (favItemURL.GetTarget() == favURL.GetTarget())
return favItem;

// Check videodb and musicdb paths. Might be different strings pointing to same resource!
// Example: "musicdb://recentlyaddedalbums/4711/" and "musicdb://recentlyplayedalbums/4711/",
// both pointing to same album with db id 4711.
if ((isVideoDb && URIUtils::IsVideoDb(favItemURL.GetTarget())) ||
(isMusicDb && URIUtils::IsMusicDb(favItemURL.GetTarget())))
{
const std::shared_ptr<CFileItem> targetItem{ResolveFavourite(*favItem)};
if (targetItem && targetItem->IsSamePath(&item))
return favItem;
}
}
}
return {};
}

bool CFavouritesService::IsFavourited(const CFileItem& item, int contextWindow) const
{
return (GetFavourite(item, contextWindow) != nullptr);
}

std::shared_ptr<CFileItem> CFavouritesService::ResolveFavourite(const CFileItem& item) const
{
if (item.IsFavourite())
{
const CFavouritesURL favURL{item.GetPath()};
if (favURL.IsValid())
{
auto targetItem{std::make_shared<CFileItem>(favURL.GetTarget(), favURL.IsDir())};
targetItem->LoadDetails();
if (favURL.GetWindowID() != -1)
{
const std::string window{CWindowTranslator::TranslateWindow(favURL.GetWindowID())};
targetItem->SetProperty("targetwindow", CVariant{window});
}
return targetItem;
}
}
return {};
}

void CFavouritesService::GetAll(CFileItemList& items) const
Expand Down
6 changes: 4 additions & 2 deletions xbmc/favourites/FavouritesService.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "threads/CriticalSection.h"
#include "utils/EventStream.h"

#include <memory>
#include <string>
#include <vector>

Expand All @@ -25,6 +26,9 @@ class CFavouritesService
void ReInit(std::string userDataFolder);

bool IsFavourited(const CFileItem& item, int contextWindow) const;
std::shared_ptr<CFileItem> GetFavourite(const CFileItem& item, int contextWindow) const;
std::shared_ptr<CFileItem> ResolveFavourite(const CFileItem& favItem) const;

void GetAll(CFileItemList& items) const;
bool AddOrRemove(const CFileItem& item, int contextWindow);
bool Save(const CFileItemList& items);
Expand All @@ -46,11 +50,9 @@ class CFavouritesService

void OnUpdated();
bool Persist();
std::string GetFavouritesUrl(const CFileItem &item, int contextWindow) const;

std::string m_userDataFolder;
CFileItemList m_favourites;
CEventSource<FavouritesUpdated> m_events;
mutable CCriticalSection m_criticalSection;
};

0 comments on commit be9519b

Please sign in to comment.