2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-10-02 22:44:05 +03:00
|
|
|
#include "History.h"
|
2019-09-21 00:47:31 +03:00
|
|
|
#include "ManualModel.h"
|
2020-02-14 22:29:06 +01:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#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/TreeView.h>
|
|
|
|
#include <LibGUI/Window.h>
|
2020-03-08 12:05:14 +01:00
|
|
|
#include <LibMarkdown/MDDocument.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/HtmlView.h>
|
|
|
|
#include <LibWeb/Layout/LayoutNode.h>
|
|
|
|
#include <LibWeb/Parser/CSSParser.h>
|
|
|
|
#include <LibWeb/Parser/HTMLParser.h>
|
2019-10-02 22:44:05 +03:00
|
|
|
#include <libgen.h>
|
2020-01-12 02:23:55 +01:00
|
|
|
#include <stdio.h>
|
2020-03-08 12:05:14 +01:00
|
|
|
#include <string.h>
|
2019-09-21 00:47:31 +03:00
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2020-01-17 11:12:06 +01:00
|
|
|
if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
|
2020-01-12 02:23:55 +01:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
GUI::Application app(argc, argv);
|
2019-09-21 00:47:31 +03:00
|
|
|
|
2020-01-17 11:12:06 +01:00
|
|
|
if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
|
2020-01-12 02:23:55 +01:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:09:12 +01:00
|
|
|
if (unveil("/res", "r") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unveil("/usr/share/man", "r") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unveil(nullptr, nullptr);
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
auto window = GUI::Window::construct();
|
2019-09-21 00:47:31 +03:00
|
|
|
window->set_title("Help");
|
|
|
|
window->set_rect(300, 200, 570, 500);
|
|
|
|
|
2020-03-03 21:42:48 +01:00
|
|
|
auto& widget = window->set_main_widget<GUI::Widget>();
|
|
|
|
widget.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
widget.layout()->set_spacing(0);
|
2019-10-02 22:44:05 +03:00
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
auto& toolbar = widget.add<GUI::ToolBar>();
|
2019-10-02 22:44:05 +03:00
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
auto& splitter = widget.add<GUI::HorizontalSplitter>();
|
2019-09-21 00:47:31 +03:00
|
|
|
|
|
|
|
auto model = ManualModel::create();
|
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
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);
|
2019-09-21 00:47:31 +03:00
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
auto& html_view = splitter.add<Web::HtmlView>();
|
2019-09-21 00:47:31 +03:00
|
|
|
|
2019-10-02 22:44:05 +03:00
|
|
|
History history;
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
RefPtr<GUI::Action> go_back_action;
|
|
|
|
RefPtr<GUI::Action> go_forward_action;
|
2019-10-02 22:44:05 +03:00
|
|
|
|
|
|
|
auto update_actions = [&]() {
|
|
|
|
go_back_action->set_enabled(history.can_go_back());
|
|
|
|
go_forward_action->set_enabled(history.can_go_forward());
|
|
|
|
};
|
|
|
|
|
2019-12-09 17:45:40 +01:00
|
|
|
auto open_page = [&](const String& path) {
|
2019-09-21 00:47:31 +03:00
|
|
|
if (path.is_null()) {
|
2020-03-04 19:07:55 +01:00
|
|
|
html_view.set_document(nullptr);
|
2019-09-21 00:47:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbg() << "Opening page at " << path;
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
auto file = Core::File::construct();
|
2019-09-21 00:47:31 +03:00
|
|
|
file->set_filename(path);
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
if (!file->open(Core::IODevice::OpenMode::ReadOnly)) {
|
2019-09-21 00:47:31 +03:00
|
|
|
int saved_errno = errno;
|
2020-02-02 15:07:41 +01:00
|
|
|
GUI::MessageBox::show(strerror(saved_errno), "Failed to open man page", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
|
2019-09-21 00:47:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto buffer = file->read_all();
|
2020-03-01 12:35:09 +01:00
|
|
|
StringView source { (const char*)buffer.data(), buffer.size() };
|
2019-09-21 00:47:31 +03:00
|
|
|
|
|
|
|
MDDocument md_document;
|
|
|
|
bool success = md_document.parse(source);
|
|
|
|
ASSERT(success);
|
|
|
|
|
|
|
|
String html = md_document.render_to_html();
|
2020-03-07 10:27:02 +01:00
|
|
|
auto html_document = Web::parse_html_document(html);
|
2020-03-04 19:07:55 +01:00
|
|
|
html_view.set_document(html_document);
|
2019-09-21 00:47:31 +03:00
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
String page_and_section = model->page_and_section(tree_view.selection().first());
|
2020-03-13 23:21:35 +01:00
|
|
|
window->set_title(String::format("%s - Help", page_and_section.characters()));
|
2019-09-21 00:47:31 +03:00
|
|
|
};
|
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
tree_view.on_selection_change = [&] {
|
|
|
|
String path = model->page_path(tree_view.selection().first());
|
2019-10-02 22:44:05 +03:00
|
|
|
if (path.is_null()) {
|
2020-03-04 19:07:55 +01:00
|
|
|
html_view.set_document(nullptr);
|
2019-10-02 22:44:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
history.push(path);
|
|
|
|
update_actions();
|
|
|
|
open_page(path);
|
|
|
|
};
|
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
html_view.on_link_click = [&](const String& href) {
|
2019-10-02 22:44:05 +03:00
|
|
|
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);
|
|
|
|
history.push(path);
|
|
|
|
update_actions();
|
|
|
|
open_page(path);
|
|
|
|
free(path);
|
|
|
|
};
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
|
2019-10-02 22:44:05 +03:00
|
|
|
history.go_back();
|
|
|
|
update_actions();
|
|
|
|
open_page(history.current());
|
|
|
|
});
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
|
2019-10-02 22:44:05 +03:00
|
|
|
history.go_forward();
|
|
|
|
update_actions();
|
|
|
|
open_page(history.current());
|
|
|
|
});
|
|
|
|
|
|
|
|
go_back_action->set_enabled(false);
|
|
|
|
go_forward_action->set_enabled(false);
|
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
toolbar.add_action(*go_back_action);
|
|
|
|
toolbar.add_action(*go_forward_action);
|
2019-10-02 22:44:05 +03:00
|
|
|
|
2020-04-21 16:01:00 +02:00
|
|
|
auto menubar = GUI::MenuBar::construct();
|
2019-10-02 22:44:05 +03:00
|
|
|
|
2020-04-04 12:18:40 +02:00
|
|
|
auto& app_menu = menubar->add_menu("Help");
|
|
|
|
app_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
|
2020-02-06 13:39:17 +01:00
|
|
|
GUI::AboutDialog::show("Help", Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"), window);
|
2019-10-02 22:44:05 +03:00
|
|
|
}));
|
2020-04-04 12:18:40 +02:00
|
|
|
app_menu.add_separator();
|
|
|
|
app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
2020-02-02 15:07:41 +01:00
|
|
|
GUI::Application::the().quit(0);
|
2019-10-02 22:44:05 +03:00
|
|
|
}));
|
|
|
|
|
2020-04-04 12:18:40 +02:00
|
|
|
auto& go_menu = menubar->add_menu("Go");
|
|
|
|
go_menu.add_action(*go_back_action);
|
|
|
|
go_menu.add_action(*go_forward_action);
|
2019-10-02 22:44:05 +03:00
|
|
|
|
|
|
|
app.set_menubar(move(menubar));
|
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
window->set_focused_widget(&tree_view);
|
2019-09-21 00:47:31 +03:00
|
|
|
window->show();
|
|
|
|
|
2020-02-06 13:39:17 +01:00
|
|
|
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"));
|
2019-09-21 00:47:31 +03:00
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|