Repository: xbmc/xbmc
File: xbmc/favourites/FavouritesService.cpp (C++)

Code before:
    if (name && favourite->FirstChild())
    {
      const std::string favURL(
          CFavouritesURL(CExecString(favourite->FirstChild()->Value())).GetURL());
      if (!items.Contains(favURL))
      {
        const auto item{std::make_shared<CFileItem>(name)};
        item->SetPath(favURL);
        if (thumb)
          item->SetArt("thumb", thumb);
        items.Add(item);
      }
    }
    favourite = favourite->NextSiblingElement("favourite");
  }
  return true;
}
} // unnamed namespace

CFavouritesService::CFavouritesService(std::string_view userDataFolder)
  : m_favourites("favourites://")
{
  ReInit(userDataFolder);
}

void CFavouritesService::ReInit(std::string_view userDataFolder)
{
  std::unique_lock lock(m_criticalSection);

  m_userDataFolder = userDataFolder;
  m_favourites.Clear();
  m_targets.clear();
  m_favourites.SetContent("favourites");

  std::string favourites = "special://xbmc/system/favourites.xml";
  if (CFileUtils::Exists(favourites))
    LoadFromFile(favourites, m_favourites);
  else
    CLog::Log(LOGDEBUG, "CFavourites::Load - no system favourites found, skipping");

  favourites = URIUtils::AddFileToFolder(m_userDataFolder, "favourites.xml");
  if (CFileUtils::Exists(favourites))
    LoadFromFile(favourites, m_favourites);
  else
    CLog::Log(LOGDEBUG, "CFavourites::Load - no userdata favourites found, skipping");
}

void CFavouritesService::CleanupTargetsCache(const CFileItem& item)
{
  // Cleanup cache. Resume info etc. of cached target items might need refresh.
  std::unique_lock lock(m_criticalSection);

Review comment (bug):
I'd say SQ is wrong here, the parameter is not a read-only string. Switching to `std::string_view`  doesn't allow moving strings into the function anymore like here:

https://github.com/xbmc/xbmc/blob/5709641d55b7d49167a7e52573c542c11f523fee/xbmc/profiles/ProfileManager.cpp#L418

`GetProfileUserDataFolder()` returns a `std::string` which automatically gets moved into the function, avoiding a copy + allocation this way. When changing to `std::string_view` the temporary has to be copied.

Same applies to the constructor.

Code after:
    if (name && favourite->FirstChild())
    {
      const std::string favURL(
          CFavouritesURL(CExecString(favourite->FirstChild()->Value())).GetURL());
      if (!items.Contains(favURL))
      {
        const auto item{std::make_shared<CFileItem>(name)};
        item->SetPath(favURL);
        if (thumb)
          item->SetArt("thumb", thumb);
        items.Add(item);
      }
    }
    favourite = favourite->NextSiblingElement("favourite");
  }
  return true;
}
} // unnamed namespace

CFavouritesService::CFavouritesService(std::string userDataFolder) : m_favourites("favourites://")
{
  ReInit(std::move(userDataFolder));
}

void CFavouritesService::ReInit(std::string userDataFolder)
{
  std::unique_lock lock(m_criticalSection);

  m_userDataFolder = std::move(userDataFolder);
  m_favourites.Clear();
  m_targets.clear();
  m_favourites.SetContent("favourites");

  std::string favourites = "special://xbmc/system/favourites.xml";
  if (CFileUtils::Exists(favourites))
    LoadFromFile(favourites, m_favourites);
  else
    CLog::Log(LOGDEBUG, "CFavourites::Load - no system favourites found, skipping");

  favourites = URIUtils::AddFileToFolder(m_userDataFolder, "favourites.xml");
  if (CFileUtils::Exists(favourites))
    LoadFromFile(favourites, m_favourites);
  else
    CLog::Log(LOGDEBUG, "CFavourites::Load - no userdata favourites found, skipping");
}

void CFavouritesService::CleanupTargetsCache(const CFileItem& item)
{
  // Cleanup cache. Resume info etc. of cached target items might need refresh.
  std::unique_lock lock(m_criticalSection);