1 /*
2  *  Copyright (C) 2012-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #include "PVRClientMenuHooks.h"
10 
11 #include "addons/kodi-dev-kit/include/kodi/c-api/addon-instance/pvr/pvr_menu_hook.h"
12 #include "guilib/LocalizeStrings.h"
13 #include "pvr/PVRContextMenus.h"
14 #include "utils/log.h"
15 
16 namespace PVR
17 {
18 
CPVRClientMenuHook(const std::string & addonId,const PVR_MENUHOOK & hook)19 CPVRClientMenuHook::CPVRClientMenuHook(const std::string& addonId, const PVR_MENUHOOK& hook)
20 : m_addonId(addonId),
21   m_hook(new PVR_MENUHOOK(hook))
22 {
23   if (hook.category != PVR_MENUHOOK_UNKNOWN &&
24       hook.category != PVR_MENUHOOK_ALL &&
25       hook.category != PVR_MENUHOOK_CHANNEL &&
26       hook.category != PVR_MENUHOOK_TIMER &&
27       hook.category != PVR_MENUHOOK_EPG &&
28       hook.category != PVR_MENUHOOK_RECORDING &&
29       hook.category != PVR_MENUHOOK_DELETED_RECORDING &&
30       hook.category != PVR_MENUHOOK_SETTING)
31     CLog::LogF(LOGERROR, "Unknown PVR_MENUHOOK_CAT value: {}", hook.category);
32 }
33 
operator ==(const CPVRClientMenuHook & right) const34 bool CPVRClientMenuHook::operator ==(const CPVRClientMenuHook& right) const
35 {
36   if (this == &right)
37     return true;
38 
39   return m_addonId == right.m_addonId &&
40          m_hook->iHookId == right.m_hook->iHookId &&
41          m_hook->iLocalizedStringId == right.m_hook->iLocalizedStringId &&
42          m_hook->category == right.m_hook->category;
43 }
44 
IsAllHook() const45 bool CPVRClientMenuHook::IsAllHook() const
46 {
47   return m_hook->category == PVR_MENUHOOK_ALL;
48 }
49 
IsChannelHook() const50 bool CPVRClientMenuHook::IsChannelHook() const
51 {
52   return m_hook->category == PVR_MENUHOOK_CHANNEL;
53 }
54 
IsTimerHook() const55 bool CPVRClientMenuHook::IsTimerHook() const
56 {
57   return m_hook->category == PVR_MENUHOOK_TIMER;
58 }
59 
IsEpgHook() const60 bool CPVRClientMenuHook::IsEpgHook() const
61 {
62   return m_hook->category == PVR_MENUHOOK_EPG;
63 }
64 
IsRecordingHook() const65 bool CPVRClientMenuHook::IsRecordingHook() const
66 {
67   return m_hook->category == PVR_MENUHOOK_RECORDING;
68 }
69 
IsDeletedRecordingHook() const70 bool CPVRClientMenuHook::IsDeletedRecordingHook() const
71 {
72   return m_hook->category == PVR_MENUHOOK_DELETED_RECORDING;
73 }
74 
IsSettingsHook() const75 bool CPVRClientMenuHook::IsSettingsHook() const
76 {
77   return m_hook->category == PVR_MENUHOOK_SETTING;
78 }
79 
GetAddonId() const80 std::string CPVRClientMenuHook::GetAddonId() const
81 {
82   return m_addonId;
83 }
84 
GetId() const85 unsigned int CPVRClientMenuHook::GetId() const
86 {
87   return m_hook->iHookId;
88 }
89 
GetLabelId() const90 unsigned int CPVRClientMenuHook::GetLabelId() const
91 {
92   return m_hook->iLocalizedStringId;
93 }
94 
GetLabel() const95 std::string CPVRClientMenuHook::GetLabel() const
96 {
97   return g_localizeStrings.GetAddonString(m_addonId, m_hook->iLocalizedStringId);
98 }
99 
AddHook(const PVR_MENUHOOK & addonHook)100 void CPVRClientMenuHooks::AddHook(const PVR_MENUHOOK& addonHook)
101 {
102   if (!m_hooks)
103     m_hooks.reset(new std::vector<CPVRClientMenuHook>());
104 
105   const CPVRClientMenuHook hook(m_addonId, addonHook);
106   m_hooks->emplace_back(hook);
107   CPVRContextMenuManager::GetInstance().AddMenuHook(hook);
108 }
109 
Clear()110 void CPVRClientMenuHooks::Clear()
111 {
112   if (!m_hooks)
113     return;
114 
115   for (const auto& hook : *m_hooks)
116     CPVRContextMenuManager::GetInstance().RemoveMenuHook(hook);
117 
118   m_hooks.reset();
119 }
120 
GetHooks(const std::function<bool (const CPVRClientMenuHook & hook)> & function) const121 std::vector<CPVRClientMenuHook> CPVRClientMenuHooks::GetHooks(
122     const std::function<bool(const CPVRClientMenuHook& hook)>& function) const
123 {
124   std::vector<CPVRClientMenuHook> hooks;
125 
126   if (!m_hooks)
127     return hooks;
128 
129   for (const CPVRClientMenuHook& hook : *m_hooks)
130   {
131     if (function(hook) || hook.IsAllHook())
132       hooks.emplace_back(hook);
133   }
134   return hooks;
135 }
136 
GetChannelHooks() const137 std::vector<CPVRClientMenuHook> CPVRClientMenuHooks::GetChannelHooks() const
138 {
139   return GetHooks([](const CPVRClientMenuHook& hook)
140   {
141     return hook.IsChannelHook();
142   });
143 }
144 
GetTimerHooks() const145 std::vector<CPVRClientMenuHook> CPVRClientMenuHooks::GetTimerHooks() const
146 {
147   return GetHooks([](const CPVRClientMenuHook& hook)
148   {
149     return hook.IsTimerHook();
150   });
151 }
152 
GetEpgHooks() const153 std::vector<CPVRClientMenuHook> CPVRClientMenuHooks::GetEpgHooks() const
154 {
155   return GetHooks([](const CPVRClientMenuHook& hook)
156   {
157     return hook.IsEpgHook();
158   });
159 }
160 
GetRecordingHooks() const161 std::vector<CPVRClientMenuHook> CPVRClientMenuHooks::GetRecordingHooks() const
162 {
163   return GetHooks([](const CPVRClientMenuHook& hook)
164   {
165     return hook.IsRecordingHook();
166   });
167 }
168 
GetDeletedRecordingHooks() const169 std::vector<CPVRClientMenuHook> CPVRClientMenuHooks::GetDeletedRecordingHooks() const
170 {
171   return GetHooks([](const CPVRClientMenuHook& hook)
172   {
173     return hook.IsDeletedRecordingHook();
174   });
175 }
176 
GetSettingsHooks() const177 std::vector<CPVRClientMenuHook> CPVRClientMenuHooks::GetSettingsHooks() const
178 {
179   return GetHooks([](const CPVRClientMenuHook& hook)
180   {
181     return hook.IsSettingsHook();
182   });
183 }
184 
185 } // namespace PVR
186