Merge pull request #26148 from ksooo/fix-secondstotimestring-warning
[xbmc.git] / xbmc / addons / ContextMenuAddon.cpp
blob1717580da7e92c4633b1c548a33807f67167346a
1 /*
2 * Copyright (C) 2013-2018 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.
7 */
9 #include "ContextMenuAddon.h"
11 #include "ContextMenuItem.h"
12 #include "ContextMenuManager.h"
13 #include "addons/addoninfo/AddonType.h"
14 #include "guilib/LocalizeStrings.h"
15 #include "utils/StringUtils.h"
16 #include "utils/URIUtils.h"
18 #include <sstream>
20 namespace ADDON
23 CContextMenuAddon::CContextMenuAddon(const AddonInfoPtr& addonInfo)
24 : CAddon(addonInfo, AddonType::CONTEXTMENU_ITEM)
26 const CAddonExtensions* menu = Type(AddonType::CONTEXTMENU_ITEM)->GetElement("menu");
27 if (menu)
29 int tmp = 0;
30 ParseMenu(menu, "", tmp);
32 else
34 //backwards compatibility. add first item definition
35 const CAddonExtensions* elem = Type(AddonType::CONTEXTMENU_ITEM)->GetElement("item");
36 if (elem)
38 std::string visCondition = elem->GetValue("visible").asString();
39 if (visCondition.empty())
40 visCondition = "false";
42 std::string parent = elem->GetValue("parent").asString() == "kodi.core.manage"
43 ? CContextMenuManager::MANAGE.m_groupId : CContextMenuManager::MAIN.m_groupId;
45 auto label = elem->GetValue("label").asString();
46 if (StringUtils::IsNaturalNumber(label))
47 label = g_localizeStrings.GetAddonString(ID(), atoi(label.c_str()));
49 CContextMenuItem menuItem = CContextMenuItem::CreateItem(
50 label, parent,
51 URIUtils::AddFileToFolder(Path(), Type(AddonType::CONTEXTMENU_ITEM)->LibName()),
52 visCondition, ID());
54 m_items.push_back(menuItem);
59 CContextMenuAddon::~CContextMenuAddon() = default;
61 void CContextMenuAddon::ParseMenu(
62 const CAddonExtensions* elem,
63 const std::string& parent,
64 int& anonGroupCount)
66 auto menuId = elem->GetValue("@id").asString();
67 auto menuLabel = elem->GetValue("label").asString();
68 if (StringUtils::IsNaturalNumber(menuLabel))
69 menuLabel = g_localizeStrings.GetAddonString(ID(), std::stoi(menuLabel));
71 if (menuId.empty())
73 //anonymous group. create a new unique internal id.
74 std::stringstream ss;
75 ss << ID() << ++anonGroupCount;
76 menuId = ss.str();
79 m_items.push_back(CContextMenuItem::CreateGroup(menuLabel, parent, menuId, ID()));
81 for (const auto& subMenu : elem->GetElements("menu"))
82 ParseMenu(&subMenu.second, menuId, anonGroupCount);
84 for (const auto& element : elem->GetElements("item"))
86 std::string visCondition = element.second.GetValue("visible").asString();
87 std::string library = element.second.GetValue("@library").asString();
88 std::string label = element.second.GetValue("label").asString();
89 if (StringUtils::IsNaturalNumber(label))
90 label = g_localizeStrings.GetAddonString(ID(), atoi(label.c_str()));
92 std::vector<std::string> args;
93 args.push_back(ID());
95 std::string arg = element.second.GetValue("@args").asString();
96 if (!arg.empty())
97 args.push_back(arg);
99 if (!label.empty() && !library.empty() && !visCondition.empty())
101 auto menu = CContextMenuItem::CreateItem(label, menuId,
102 URIUtils::AddFileToFolder(Path(), library), visCondition, ID(), args);
103 m_items.push_back(menu);