1 /*
2  *  Copyright (C) 2005-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 "GUIAction.h"
10 
11 #include "GUIComponent.h"
12 #include "GUIInfoManager.h"
13 #include "GUIWindowManager.h"
14 #include "ServiceBroker.h"
15 #include "utils/StringUtils.h"
16 
CGUIAction(int controlID)17 CGUIAction::CGUIAction(int controlID)
18 {
19   SetNavigation(controlID);
20 }
21 
ExecuteActions(int controlID,int parentID,const CGUIListItemPtr & item) const22 bool CGUIAction::ExecuteActions(int controlID, int parentID, const CGUIListItemPtr &item /* = NULL */) const
23 {
24   if (m_actions.empty())
25     return false;
26 
27   CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager();
28   // take a copy of actions that satisfy our conditions
29   std::vector<std::string> actions;
30   for (const auto &i : m_actions)
31   {
32     if (i.condition.empty() || infoMgr.EvaluateBool(i.condition, 0, item))
33     {
34       if (!StringUtils::IsInteger(i.action))
35         actions.emplace_back(i.action);
36     }
37   }
38   // execute them
39   bool retval = false;
40   for (const auto &i : actions)
41   {
42     CGUIMessage msg(GUI_MSG_EXECUTE, controlID, parentID, 0, 0, item);
43     msg.SetStringParam(i);
44     if (m_sendThreadMessages)
45       CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
46     else
47       CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
48     retval = true;
49   }
50   return retval;
51 }
52 
GetNavigation() const53 int CGUIAction::GetNavigation() const
54 {
55   CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager();
56   for (const auto &i : m_actions)
57   {
58     if (StringUtils::IsInteger(i.action))
59     {
60       if (i.condition.empty() || infoMgr.EvaluateBool(i.condition))
61         return atoi(i.action.c_str());
62     }
63   }
64   return 0;
65 }
66 
SetNavigation(int id)67 void CGUIAction::SetNavigation(int id)
68 {
69   if (id == 0)
70     return;
71 
72   std::string strId = StringUtils::Format("%i", id);
73   for (auto &i : m_actions)
74   {
75     if (StringUtils::IsInteger(i.action) && i.condition.empty())
76     {
77       i.action = std::move(strId);
78       return;
79     }
80   }
81   m_actions.emplace_back();
82   m_actions.back().action = std::move(strId);
83 }
84 
HasActionsMeetingCondition() const85 bool CGUIAction::HasActionsMeetingCondition() const
86 {
87   CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager();
88   for (const auto &i : m_actions)
89   {
90     if (i.condition.empty() || infoMgr.EvaluateBool(i.condition))
91       return true;
92   }
93   return false;
94 }
95