/xbmc/dialogs/GUIDialogTextViewer.cpp

http://github.com/xbmc/xbmc · C++ · 132 lines · 107 code · 16 blank · 9 comment · 6 complexity · dcc89ff0e7d309abe47b1635757d9e5c 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 "GUIDialogTextViewer.h"
  9. #include "GUIUserMessages.h"
  10. #include "ServiceBroker.h"
  11. #include "filesystem/File.h"
  12. #include "guilib/GUIComponent.h"
  13. #include "guilib/GUIWindowManager.h"
  14. #include "input/actions/Action.h"
  15. #include "input/actions/ActionIDs.h"
  16. #include "utils/URIUtils.h"
  17. #include "utils/log.h"
  18. using namespace XFILE;
  19. #define CONTROL_HEADING 1
  20. #define CONTROL_TEXTAREA 5
  21. CGUIDialogTextViewer::CGUIDialogTextViewer(void)
  22. : CGUIDialog(WINDOW_DIALOG_TEXT_VIEWER, "DialogTextViewer.xml")
  23. {
  24. m_loadType = KEEP_IN_MEMORY;
  25. }
  26. CGUIDialogTextViewer::~CGUIDialogTextViewer(void) = default;
  27. bool CGUIDialogTextViewer::OnAction(const CAction &action)
  28. {
  29. if (action.GetID() == ACTION_TOGGLE_FONT)
  30. {
  31. UseMonoFont(!m_mono);
  32. return true;
  33. }
  34. return CGUIDialog::OnAction(action);
  35. }
  36. bool CGUIDialogTextViewer::OnMessage(CGUIMessage& message)
  37. {
  38. switch ( message.GetMessage() )
  39. {
  40. case GUI_MSG_WINDOW_INIT:
  41. {
  42. CGUIDialog::OnMessage(message);
  43. SetHeading();
  44. SetText();
  45. UseMonoFont(m_mono);
  46. return true;
  47. }
  48. break;
  49. case GUI_MSG_NOTIFY_ALL:
  50. {
  51. if (message.GetParam1() == GUI_MSG_UPDATE)
  52. {
  53. SetText();
  54. SetHeading();
  55. return true;
  56. }
  57. }
  58. break;
  59. default:
  60. break;
  61. }
  62. return CGUIDialog::OnMessage(message);
  63. }
  64. void CGUIDialogTextViewer::SetText()
  65. {
  66. CGUIMessage msg(GUI_MSG_LABEL_SET, GetID(), CONTROL_TEXTAREA);
  67. msg.SetLabel(m_strText);
  68. OnMessage(msg);
  69. }
  70. void CGUIDialogTextViewer::SetHeading()
  71. {
  72. CGUIMessage msg(GUI_MSG_LABEL_SET, GetID(), CONTROL_HEADING);
  73. msg.SetLabel(m_strHeading);
  74. OnMessage(msg);
  75. }
  76. void CGUIDialogTextViewer::UseMonoFont(bool use)
  77. {
  78. m_mono = use;
  79. CGUIMessage msg(GUI_MSG_SET_TYPE, GetID(), CONTROL_TEXTAREA, use ? 1 : 0);
  80. OnMessage(msg);
  81. }
  82. void CGUIDialogTextViewer::OnDeinitWindow(int nextWindowID)
  83. {
  84. CGUIDialog::OnDeinitWindow(nextWindowID);
  85. // reset text area
  86. CGUIMessage msgReset(GUI_MSG_LABEL_RESET, GetID(), CONTROL_TEXTAREA);
  87. OnMessage(msgReset);
  88. // reset heading
  89. SET_CONTROL_LABEL(CONTROL_HEADING, "");
  90. }
  91. void CGUIDialogTextViewer::ShowForFile(const std::string& path, bool useMonoFont)
  92. {
  93. CFile file;
  94. if (file.Open(path))
  95. {
  96. std::string data;
  97. try
  98. {
  99. data.resize(file.GetLength()+1);
  100. file.Read(&data[0], file.GetLength());
  101. CGUIDialogTextViewer* pDialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogTextViewer>(WINDOW_DIALOG_TEXT_VIEWER);
  102. pDialog->SetHeading(URIUtils::GetFileName(path));
  103. pDialog->SetText(data);
  104. pDialog->UseMonoFont(useMonoFont);
  105. pDialog->Open();
  106. }
  107. catch(const std::bad_alloc&)
  108. {
  109. CLog::Log(LOGERROR, "Not enough memory to load text file %s", path.c_str());
  110. }
  111. catch(...)
  112. {
  113. CLog::Log(LOGERROR, "Exception while trying to view text file %s", path.c_str());
  114. }
  115. }
  116. }