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