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::HasParent() const
52 return !m_parent
.empty();
55 bool CContextMenuItem::Execute(const std::shared_ptr
<CFileItem
>& item
) const
57 if (!item
|| m_library
.empty() || IsGroup())
60 ADDON::AddonPtr addon
;
61 if (!CServiceBroker::GetAddonMgr().GetAddon(m_addonId
, addon
, ADDON::OnlyEnabled::CHOICE_YES
))
64 bool reuseLanguageInvoker
= false;
65 if (addon
->ExtraInfo().find("reuselanguageinvoker") != addon
->ExtraInfo().end())
66 reuseLanguageInvoker
= addon
->ExtraInfo().at("reuselanguageinvoker") == "true";
69 LanguageInvokerPtr
invoker(new CContextItemAddonInvoker(&CServiceBroker::GetXBPython(), item
));
70 return (CScriptInvocationManager::GetInstance().ExecuteAsync(m_library
, invoker
, addon
, m_args
, reuseLanguageInvoker
) != -1);
76 bool CContextMenuItem::operator==(const CContextMenuItem
& other
) const
78 if (IsGroup() && other
.IsGroup())
79 return (m_groupId
== other
.m_groupId
&& m_parent
== other
.m_parent
);
81 return (IsGroup() == other
.IsGroup())
82 && (m_parent
== other
.m_parent
)
83 && (m_library
== other
.m_library
)
84 && (m_addonId
== other
.m_addonId
)
85 && (m_args
== other
.m_args
);
88 std::string
CContextMenuItem::ToString() const
91 return StringUtils::Format("CContextMenuItem[group, id={}, parent={}, addon={}]", m_groupId
,
94 return StringUtils::Format("CContextMenuItem[item, parent={}, library={}, addon={}]", m_parent
,
95 m_library
, m_addonId
);
98 CContextMenuItem
CContextMenuItem::CreateGroup(const std::string
& label
, const std::string
& parent
,
99 const std::string
& groupId
, const std::string
& addonId
)
101 CContextMenuItem menuItem
;
102 menuItem
.m_label
= label
;
103 menuItem
.m_parent
= parent
;
104 menuItem
.m_groupId
= groupId
;
105 menuItem
.m_addonId
= addonId
;
109 CContextMenuItem
CContextMenuItem::CreateItem(const std::string
& label
, const std::string
& parent
,
110 const std::string
& library
, const std::string
& condition
, const std::string
& addonId
, const std::vector
<std::string
>& args
)
112 CContextMenuItem menuItem
;
113 menuItem
.m_label
= label
;
114 menuItem
.m_parent
= parent
;
115 menuItem
.m_library
= library
;
116 menuItem
.m_visibilityCondition
= condition
;
117 menuItem
.m_addonId
= addonId
;
118 menuItem
.m_args
= args
;