Serenity Operating System
at hosted 207 lines 6.8 kB view raw
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 <LibMarkdown/MDDocument.h> 44#include <LibWeb/HtmlView.h> 45#include <LibWeb/Layout/LayoutNode.h> 46#include <LibWeb/Parser/CSSParser.h> 47#include <LibWeb/Parser/HTMLParser.h> 48#include <libgen.h> 49#include <stdio.h> 50#include <string.h> 51 52int main(int argc, char* argv[]) 53{ 54#ifdef __serenity__ 55 if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) { 56 perror("pledge"); 57 return 1; 58 } 59#endif 60 61 GUI::Application app(argc, argv); 62 63#ifdef __serenity__ 64 if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) { 65 perror("pledge"); 66 return 1; 67 } 68#endif 69 70 if (unveil("/res", "r") < 0) { 71 perror("unveil"); 72 return 1; 73 } 74 75 if (unveil("/usr/share/man", "r") < 0) { 76 perror("unveil"); 77 return 1; 78 } 79 80 unveil(nullptr, nullptr); 81 82 auto window = GUI::Window::construct(); 83 window->set_title("Help"); 84 window->set_rect(300, 200, 570, 500); 85 86 auto& widget = window->set_main_widget<GUI::Widget>(); 87 widget.set_layout<GUI::VerticalBoxLayout>(); 88 widget.layout()->set_spacing(0); 89 90 auto& toolbar = widget.add<GUI::ToolBar>(); 91 92 auto& splitter = widget.add<GUI::HorizontalSplitter>(); 93 94 auto model = ManualModel::create(); 95 96 auto& tree_view = splitter.add<GUI::TreeView>(); 97 tree_view.set_model(model); 98 tree_view.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill); 99 tree_view.set_preferred_size(200, 500); 100 101 auto& html_view = splitter.add<Web::HtmlView>(); 102 103 History history; 104 105 RefPtr<GUI::Action> go_back_action; 106 RefPtr<GUI::Action> go_forward_action; 107 108 auto update_actions = [&]() { 109 go_back_action->set_enabled(history.can_go_back()); 110 go_forward_action->set_enabled(history.can_go_forward()); 111 }; 112 113 auto open_page = [&](const String& path) { 114 if (path.is_null()) { 115 html_view.set_document(nullptr); 116 return; 117 } 118 119 dbg() << "Opening page at " << path; 120 121 auto file = Core::File::construct(); 122 file->set_filename(path); 123 124 if (!file->open(Core::IODevice::OpenMode::ReadOnly)) { 125 int saved_errno = errno; 126 GUI::MessageBox::show(strerror(saved_errno), "Failed to open man page", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window); 127 return; 128 } 129 auto buffer = file->read_all(); 130 StringView source { (const char*)buffer.data(), buffer.size() }; 131 132 MDDocument md_document; 133 bool success = md_document.parse(source); 134 ASSERT(success); 135 136 String html = md_document.render_to_html(); 137 auto html_document = Web::parse_html_document(html); 138 html_view.set_document(html_document); 139 140 String page_and_section = model->page_and_section(tree_view.selection().first()); 141 window->set_title(String::format("%s - Help", page_and_section.characters())); 142 }; 143 144 tree_view.on_selection_change = [&] { 145 String path = model->page_path(tree_view.selection().first()); 146 if (path.is_null()) { 147 html_view.set_document(nullptr); 148 return; 149 } 150 history.push(path); 151 update_actions(); 152 open_page(path); 153 }; 154 155 html_view.on_link_click = [&](const String& href) { 156 char* current_path = strdup(history.current().characters()); 157 char* dir_path = dirname(current_path); 158 char* path = realpath(String::format("%s/%s", dir_path, href.characters()).characters(), nullptr); 159 free(current_path); 160 history.push(path); 161 update_actions(); 162 open_page(path); 163 free(path); 164 }; 165 166 go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) { 167 history.go_back(); 168 update_actions(); 169 open_page(history.current()); 170 }); 171 172 go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) { 173 history.go_forward(); 174 update_actions(); 175 open_page(history.current()); 176 }); 177 178 go_back_action->set_enabled(false); 179 go_forward_action->set_enabled(false); 180 181 toolbar.add_action(*go_back_action); 182 toolbar.add_action(*go_forward_action); 183 184 auto menubar = make<GUI::MenuBar>(); 185 186 auto& app_menu = menubar->add_menu("Help"); 187 app_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) { 188 GUI::AboutDialog::show("Help", Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"), window); 189 })); 190 app_menu.add_separator(); 191 app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { 192 GUI::Application::the().quit(0); 193 })); 194 195 auto& go_menu = menubar->add_menu("Go"); 196 go_menu.add_action(*go_back_action); 197 go_menu.add_action(*go_forward_action); 198 199 app.set_menubar(move(menubar)); 200 201 window->set_focused_widget(&tree_view); 202 window->show(); 203 204 window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png")); 205 206 return app.exec(); 207}