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.
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"
23 CContextMenuAddon::CContextMenuAddon(const AddonInfoPtr
& addonInfo
)
24 : CAddon(addonInfo
, AddonType::CONTEXTMENU_ITEM
)
26 const CAddonExtensions
* menu
= Type(AddonType::CONTEXTMENU_ITEM
)->GetElement("menu");
30 ParseMenu(menu
, "", tmp
);
34 //backwards compatibility. add first item definition
35 const CAddonExtensions
* elem
= Type(AddonType::CONTEXTMENU_ITEM
)->GetElement("item");
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(
51 URIUtils::AddFileToFolder(Path(), Type(AddonType::CONTEXTMENU_ITEM
)->LibName()),
54 m_items
.push_back(menuItem
);
59 CContextMenuAddon::~CContextMenuAddon() = default;
61 void CContextMenuAddon::ParseMenu(
62 const CAddonExtensions
* elem
,
63 const std::string
& parent
,
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
));
73 //anonymous group. create a new unique internal id.
75 ss
<< ID() << ++anonGroupCount
;
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
;
95 std::string arg
= element
.second
.GetValue("@args").asString();
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
);