Serenity Operating System
1/*
2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "History.h"
28#include "ManualModel.h"
29#include <AK/ByteBuffer.h>
30#include <LibCore/File.h>
31#include <LibGUI/AboutDialog.h>
32#include <LibGUI/Action.h>
33#include <LibGUI/Application.h>
34#include <LibGUI/BoxLayout.h>
35#include <LibGUI/Menu.h>
36#include <LibGUI/MenuBar.h>
37#include <LibGUI/MessageBox.h>
38#include <LibGUI/Splitter.h>
39#include <LibGUI/TextEditor.h>
40#include <LibGUI/ToolBar.h>
41#include <LibGUI/TreeView.h>
42#include <LibGUI/Window.h>
43#include <LibHTML/HtmlView.h>
44#include <LibHTML/Layout/LayoutNode.h>
45#include <LibHTML/Parser/CSSParser.h>
46#include <LibHTML/Parser/HTMLParser.h>
47#include <LibMarkdown/MDDocument.h>
48#include <libgen.h>
49#include <stdio.h>
50
51int main(int argc, char* argv[])
52{
53 if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
54 perror("pledge");
55 return 1;
56 }
57
58 GUI::Application app(argc, argv);
59
60 if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
61 perror("pledge");
62 return 1;
63 }
64
65 if (unveil("/res", "r") < 0) {
66 perror("unveil");
67 return 1;
68 }
69
70 if (unveil("/usr/share/man", "r") < 0) {
71 perror("unveil");
72 return 1;
73 }
74
75 unveil(nullptr, nullptr);
76
77 auto window = GUI::Window::construct();
78 window->set_title("Help");
79 window->set_rect(300, 200, 570, 500);
80
81 auto widget = GUI::Widget::construct();
82 widget->set_layout(make<GUI::VerticalBoxLayout>());
83 widget->layout()->set_spacing(0);
84
85 auto toolbar = widget->add<GUI::ToolBar>();
86
87 auto splitter = widget->add<GUI::HorizontalSplitter>();
88
89 auto model = ManualModel::create();
90
91 auto tree_view = splitter->add<GUI::TreeView>();
92 tree_view->set_model(model);
93 tree_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
94 tree_view->set_preferred_size(200, 500);
95
96 auto html_view = splitter->add<HtmlView>();
97
98 History history;
99
100 RefPtr<GUI::Action> go_back_action;
101 RefPtr<GUI::Action> go_forward_action;
102
103 auto update_actions = [&]() {
104 go_back_action->set_enabled(history.can_go_back());
105 go_forward_action->set_enabled(history.can_go_forward());
106 };
107
108 auto open_page = [&](const String& path) {
109 if (path.is_null()) {
110 html_view->set_document(nullptr);
111 return;
112 }
113
114 dbg() << "Opening page at " << path;
115
116 auto file = Core::File::construct();
117 file->set_filename(path);
118
119 if (!file->open(Core::IODevice::OpenMode::ReadOnly)) {
120 int saved_errno = errno;
121 GUI::MessageBox::show(strerror(saved_errno), "Failed to open man page", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
122 return;
123 }
124 auto buffer = file->read_all();
125 StringView source { (const char*)buffer.data(), (size_t)buffer.size() };
126
127 MDDocument md_document;
128 bool success = md_document.parse(source);
129 ASSERT(success);
130
131 String html = md_document.render_to_html();
132 auto html_document = parse_html_document(html);
133 html_view->set_document(html_document);
134
135 String page_and_section = model->page_and_section(tree_view->selection().first());
136 window->set_title(String::format("Help: %s", page_and_section.characters()));
137 };
138
139 tree_view->on_selection_change = [&] {
140 String path = model->page_path(tree_view->selection().first());
141 if (path.is_null()) {
142 html_view->set_document(nullptr);
143 return;
144 }
145 history.push(path);
146 update_actions();
147 open_page(path);
148 };
149
150 html_view->on_link_click = [&](const String& href) {
151 char* current_path = strdup(history.current().characters());
152 char* dir_path = dirname(current_path);
153 char* path = realpath(String::format("%s/%s", dir_path, href.characters()).characters(), nullptr);
154 free(current_path);
155 history.push(path);
156 update_actions();
157 open_page(path);
158 free(path);
159 };
160
161 go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
162 history.go_back();
163 update_actions();
164 open_page(history.current());
165 });
166
167 go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
168 history.go_forward();
169 update_actions();
170 open_page(history.current());
171 });
172
173 go_back_action->set_enabled(false);
174 go_forward_action->set_enabled(false);
175
176 toolbar->add_action(*go_back_action);
177 toolbar->add_action(*go_forward_action);
178
179 auto menubar = make<GUI::MenuBar>();
180
181 auto app_menu = GUI::Menu::construct("Help");
182 app_menu->add_action(GUI::Action::create("About", [&](const GUI::Action&) {
183 GUI::AboutDialog::show("Help", Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"), window);
184 }));
185 app_menu->add_separator();
186 app_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
187 GUI::Application::the().quit(0);
188 }));
189 menubar->add_menu(move(app_menu));
190
191 auto go_menu = GUI::Menu::construct("Go");
192 go_menu->add_action(*go_back_action);
193 go_menu->add_action(*go_forward_action);
194 menubar->add_menu(move(go_menu));
195
196 app.set_menubar(move(menubar));
197
198 window->set_main_widget(widget);
199 window->set_focused_widget(tree_view);
200 window->show();
201
202 window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"));
203
204 return app.exec();
205}