2 * Copyright (C) 2015-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 "ContextMenuItem.h"
12 #include "GUIInfoManager.h"
13 #include "addons/AddonManager.h"
14 #include "addons/IAddon.h"
15 #include "guilib/GUIComponent.h"
16 #include "guilib/LocalizeStrings.h"
18 #include "interfaces/generic/ScriptInvocationManager.h"
19 #include "interfaces/python/ContextItemAddonInvoker.h"
20 #include "interfaces/python/XBPython.h"
22 #include "ServiceBroker.h"
23 #include "utils/StringUtils.h"
25 std::string
CStaticContextMenuAction::GetLabel(const CFileItem
& item
) const
27 return g_localizeStrings
.Get(m_label
);
30 bool CContextMenuItem::IsVisible(const CFileItem
& item
) const
32 if (!m_infoBoolRegistered
)
34 m_infoBool
= CServiceBroker::GetGUI()->GetInfoManager().Register(m_visibilityCondition
, 0);
35 m_infoBoolRegistered
= true;
37 return IsGroup() || (m_infoBool
&& m_infoBool
->Get(INFO::DEFAULT_CONTEXT
, &item
));
40 bool CContextMenuItem::IsParentOf(const CContextMenuItem
& other
) const
42 return !m_groupId
.empty() && (m_groupId
== other
.m_parent
);
45 bool CContextMenuItem::IsGroup() const
47 return !m_groupId
.empty();
50 bool CContextMenuItem::Execute(const std::shared_ptr
<CFileItem
>& item
) const
52 if (!item
|| m_library
.empty() || IsGroup())
55 ADDON::AddonPtr addon
;
56 if (!CServiceBroker::GetAddonMgr().GetAddon(m_addonId
, addon
, ADDON::OnlyEnabled::CHOICE_YES
))
59 bool reuseLanguageInvoker
= false;
60 if (addon
->ExtraInfo().find("reuselanguageinvoker") != addon
->ExtraInfo().end())
61 reuseLanguageInvoker
= addon
->ExtraInfo().at("reuselanguageinvoker") == "true";
64 LanguageInvokerPtr
invoker(new CContextItemAddonInvoker(&CServiceBroker::GetXBPython(), item
));
65 return (CScriptInvocationManager::GetInstance().ExecuteAsync(m_library
, invoker
, addon
, m_args
, reuseLanguageInvoker
) != -1);
71 bool CContextMenuItem::operator==(const CContextMenuItem
& other
) const
73 if (IsGroup() && other
.IsGroup())
74 return (m_groupId
== other
.m_groupId
&& m_parent
== other
.m_parent
);
76 return (IsGroup() == other
.IsGroup())
77 && (m_parent
== other
.m_parent
)
78 && (m_library
== other
.m_library
)
79 && (m_addonId
== other
.m_addonId
)
80 && (m_args
== other
.m_args
);
83 std::string
CContextMenuItem::ToString() const
86 return StringUtils::Format("CContextMenuItem[group, id={}, parent={}, addon={}]", m_groupId
,
89 return StringUtils::Format("CContextMenuItem[item, parent={}, library={}, addon={}]", m_parent
,
90 m_library
, m_addonId
);
93 CContextMenuItem
CContextMenuItem::CreateGroup(const std::string
& label
, const std::string
& parent
,
94 const std::string
& groupId
, const std::string
& addonId
)
96 CContextMenuItem menuItem
;
97 menuItem
.m_label
= label
;
98 menuItem
.m_parent
= parent
;
99 menuItem
.m_groupId
= groupId
;
100 menuItem
.m_addonId
= addonId
;
104 CContextMenuItem
CContextMenuItem::CreateItem(const std::string
& label
, const std::string
& parent
,
105 const std::string
& library
, const std::string
& condition
, const std::string
& addonId
, const std::vector
<std::string
>& args
)
107 CContextMenuItem menuItem
;
108 menuItem
.m_label
= label
;
109 menuItem
.m_parent
= parent
;
110 menuItem
.m_library
= library
;
111 menuItem
.m_visibilityCondition
= condition
;
112 menuItem
.m_addonId
= addonId
;
113 menuItem
.m_args
= args
;