123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /*
- * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "History.h"
- #include "ManualModel.h"
- #include <AK/ByteBuffer.h>
- #include <LibCore/File.h>
- #include <LibGUI/AboutDialog.h>
- #include <LibGUI/Action.h>
- #include <LibGUI/Application.h>
- #include <LibGUI/BoxLayout.h>
- #include <LibGUI/Menu.h>
- #include <LibGUI/MenuBar.h>
- #include <LibGUI/MessageBox.h>
- #include <LibGUI/Splitter.h>
- #include <LibGUI/TextEditor.h>
- #include <LibGUI/ToolBar.h>
- #include <LibGUI/ToolBarContainer.h>
- #include <LibGUI/TreeView.h>
- #include <LibGUI/Window.h>
- #include <LibMarkdown/Document.h>
- #include <LibWeb/PageView.h>
- #include <LibWeb/Layout/LayoutNode.h>
- #include <LibWeb/Parser/CSSParser.h>
- #include <LibWeb/Parser/HTMLParser.h>
- #include <libgen.h>
- #include <stdio.h>
- #include <string.h>
- int main(int argc, char* argv[])
- {
- if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
- perror("pledge");
- return 1;
- }
- GUI::Application app(argc, argv);
- if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
- perror("pledge");
- return 1;
- }
- if (unveil("/res", "r") < 0) {
- perror("unveil");
- return 1;
- }
- if (unveil("/usr/share/man", "r") < 0) {
- perror("unveil");
- return 1;
- }
- unveil(nullptr, nullptr);
- auto window = GUI::Window::construct();
- window->set_title("Help");
- window->set_rect(300, 200, 570, 500);
- auto& widget = window->set_main_widget<GUI::Widget>();
- widget.set_layout<GUI::VerticalBoxLayout>();
- widget.set_fill_with_background_color(true);
- widget.layout()->set_spacing(2);
- auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
- auto& toolbar = toolbar_container.add<GUI::ToolBar>();
- auto& splitter = widget.add<GUI::HorizontalSplitter>();
- auto model = ManualModel::create();
- auto& tree_view = splitter.add<GUI::TreeView>();
- tree_view.set_model(model);
- tree_view.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
- tree_view.set_preferred_size(200, 500);
- auto& page_view = splitter.add<Web::PageView>();
- History history;
- RefPtr<GUI::Action> go_back_action;
- RefPtr<GUI::Action> go_forward_action;
- auto update_actions = [&]() {
- go_back_action->set_enabled(history.can_go_back());
- go_forward_action->set_enabled(history.can_go_forward());
- };
- auto open_page = [&](const String& path) {
- if (path.is_null()) {
- page_view.set_document(nullptr);
- return;
- }
- dbg() << "Opening page at " << path;
- auto file = Core::File::construct();
- file->set_filename(path);
- if (!file->open(Core::IODevice::OpenMode::ReadOnly)) {
- int saved_errno = errno;
- GUI::MessageBox::show(strerror(saved_errno), "Failed to open man page", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
- return;
- }
- auto buffer = file->read_all();
- StringView source { (const char*)buffer.data(), buffer.size() };
- auto md_document = Markdown::Document::parse(source);
- ASSERT(md_document);
- String html = md_document->render_to_html();
- auto html_document = Web::parse_html_document(html);
- page_view.set_document(html_document);
- String page_and_section = model->page_and_section(tree_view.selection().first());
- window->set_title(String::format("%s - Help", page_and_section.characters()));
- };
- tree_view.on_selection_change = [&] {
- String path = model->page_path(tree_view.selection().first());
- if (path.is_null()) {
- page_view.set_document(nullptr);
- return;
- }
- history.push(path);
- update_actions();
- open_page(path);
- };
- page_view.on_link_click = [&](const String& href, auto&, unsigned) {
- char* current_path = strdup(history.current().characters());
- char* dir_path = dirname(current_path);
- char* path = realpath(String::format("%s/%s", dir_path, href.characters()).characters(), nullptr);
- free(current_path);
- auto tree_view_index = model->index_from_path(path);
- if (tree_view_index.has_value()) {
- dbg() << "Found path _" << path << "_ in model at index " << tree_view_index.value();
- tree_view.selection().set(tree_view_index.value());
- return;
- }
- history.push(path);
- update_actions();
- open_page(path);
- free(path);
- };
- go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
- history.go_back();
- update_actions();
- open_page(history.current());
- });
- go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
- history.go_forward();
- update_actions();
- open_page(history.current());
- });
- go_back_action->set_enabled(false);
- go_forward_action->set_enabled(false);
- toolbar.add_action(*go_back_action);
- toolbar.add_action(*go_forward_action);
- auto menubar = GUI::MenuBar::construct();
- auto& app_menu = menubar->add_menu("Help");
- app_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
- GUI::AboutDialog::show("Help", Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"), window);
- }));
- app_menu.add_separator();
- app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
- GUI::Application::the().quit(0);
- }));
- auto& go_menu = menubar->add_menu("Go");
- go_menu.add_action(*go_back_action);
- go_menu.add_action(*go_forward_action);
- app.set_menubar(move(menubar));
- window->set_focused_widget(&tree_view);
- window->show();
- window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"));
- return app.exec();
- }
|