Repository: xbmc/xbmc
File: xbmc/interfaces/AnnouncementManager.cpp (C++)

Code before:
const std::string CAnnouncementManager::ANNOUNCEMENT_SENDER = "xbmc";

namespace
{

void CopyPVRTagInfoToObject(const PVR::CPVRChannel& channel, bool copyPlayerId, CVariant& object)
{
  auto& objItem = object["item"];

  objItem["type"] = "channel";
  objItem["title"] = channel.ChannelName();
  objItem["channeltype"] = channel.IsRadio() ? "radio" : "tv";

  if (copyPlayerId)
  {
    object["player"]["playerid"] =
        static_cast<int>(channel.IsRadio() ? PLAYLIST::Id::TYPE_MUSIC : PLAYLIST::Id::TYPE_VIDEO);
  }

  objItem["id"] = channel.ChannelID();
}

bool ShouldLookup(const CFileItem& item)
{
  return !item.HasProperty(LOOKUP_PROPERTY) || item.GetProperty(LOOKUP_PROPERTY).asBoolean();
}

void CopyVideoTagInfoToObject(const CFileItem& constItem, CVariant& object)
{
  /* Make a copy of the file item so it can be modified */
  CFileItem item = constItem;

  CVideoInfoTag& tag = *item.GetVideoInfoTag();

  auto& objItem = object["item"];
  int id = tag.GetDatabaseId();

  //! @todo Can be removed once this is properly handled when starting playback of a file
  if (id <= 0 && !item.GetPath().empty() && ShouldLookup(item))
  {
    CVideoDatabase videodatabase;
    if (videodatabase.Open())
    {
      std::string path = item.GetPath();
      std::string videoInfoTagPath(tag.m_strFileNameAndPath);
      if (StringUtils::StartsWith(videoInfoTagPath, "removable://"))
        path = videoInfoTagPath;
      if (videodatabase.LoadVideoInfo(path, tag, VideoDbDetailsNone))
        id = tag.GetDatabaseId();

      videodatabase.Close();

Review comment (refactor):
Nitpick: This can be replaced by a simple `return item.GetProperty(LOOKUP_PROPERTY).asBoolean(true)`. Maybe we do not need the new helper function at all?

Code after:
const std::string CAnnouncementManager::ANNOUNCEMENT_SENDER = "xbmc";

namespace
{

void CopyPVRTagInfoToObject(const PVR::CPVRChannel& channel, bool copyPlayerId, CVariant& object)
{
  auto& objItem = object["item"];

  objItem["type"] = "channel";
  objItem["title"] = channel.ChannelName();
  objItem["channeltype"] = channel.IsRadio() ? "radio" : "tv";

  if (copyPlayerId)
  {
    object["player"]["playerid"] =
        static_cast<int>(channel.IsRadio() ? PLAYLIST::Id::TYPE_MUSIC : PLAYLIST::Id::TYPE_VIDEO);
  }

  objItem["id"] = channel.ChannelID();
}

void CopyVideoTagInfoToObject(CFileItem& item, CVariant& object)
{
  CVideoInfoTag& tag = *item.GetVideoInfoTag();

  auto& objItem = object["item"];
  int id = tag.GetDatabaseId();

  //! @todo Can be removed once this is properly handled when starting playback of a file
  if (id <= 0 && !item.GetPath().empty() && item.GetProperty(LOOKUP_PROPERTY).asBoolean(true))
  {
    CVideoDatabase videodatabase;
    if (videodatabase.Open())
    {
      std::string videoInfoTagPath = tag.m_strFileNameAndPath;
      std::string path;
      if (StringUtils::StartsWith(videoInfoTagPath, "removable://"))
        path = videoInfoTagPath;
      else
        path = item.GetPath();
      if (videodatabase.LoadVideoInfo(path, tag, VideoDbDetailsNone))
        id = tag.GetDatabaseId();

      videodatabase.Close();
    }
    else
    {
      CLog::LogFC(LOGWARNING, LOGANNOUNCE,
                  "Unable to open video database. Can not load video tag for announcement!");
    }