/xbmc/guilib/GUIAction.cpp

http://github.com/xbmc/xbmc · C++ · 94 lines · 76 code · 9 blank · 9 comment · 19 complexity · 81f7d1cf0d038f2bbf1a5faf28db3b26 MD5 · raw file

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