main.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #include "History.h"
  27. #include "ManualModel.h"
  28. #include <AK/ByteBuffer.h>
  29. #include <LibCore/File.h>
  30. #include <LibGUI/AboutDialog.h>
  31. #include <LibGUI/Action.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/BoxLayout.h>
  34. #include <LibGUI/Menu.h>
  35. #include <LibGUI/MenuBar.h>
  36. #include <LibGUI/MessageBox.h>
  37. #include <LibGUI/Splitter.h>
  38. #include <LibGUI/TextEditor.h>
  39. #include <LibGUI/ToolBar.h>
  40. #include <LibGUI/ToolBarContainer.h>
  41. #include <LibGUI/TreeView.h>
  42. #include <LibGUI/Window.h>
  43. #include <LibMarkdown/Document.h>
  44. #include <LibWeb/PageView.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. int 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. GUI::Application app(argc, argv);
  58. if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
  59. perror("pledge");
  60. return 1;
  61. }
  62. if (unveil("/res", "r") < 0) {
  63. perror("unveil");
  64. return 1;
  65. }
  66. if (unveil("/usr/share/man", "r") < 0) {
  67. perror("unveil");
  68. return 1;
  69. }
  70. unveil(nullptr, nullptr);
  71. auto window = GUI::Window::construct();
  72. window->set_title("Help");
  73. window->set_rect(300, 200, 570, 500);
  74. auto& widget = window->set_main_widget<GUI::Widget>();
  75. widget.set_layout<GUI::VerticalBoxLayout>();
  76. widget.set_fill_with_background_color(true);
  77. widget.layout()->set_spacing(2);
  78. auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
  79. auto& toolbar = toolbar_container.add<GUI::ToolBar>();
  80. auto& splitter = widget.add<GUI::HorizontalSplitter>();
  81. auto model = ManualModel::create();
  82. auto& tree_view = splitter.add<GUI::TreeView>();
  83. tree_view.set_model(model);
  84. tree_view.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  85. tree_view.set_preferred_size(200, 500);
  86. auto& page_view = splitter.add<Web::PageView>();
  87. History history;
  88. RefPtr<GUI::Action> go_back_action;
  89. RefPtr<GUI::Action> go_forward_action;
  90. auto update_actions = [&]() {
  91. go_back_action->set_enabled(history.can_go_back());
  92. go_forward_action->set_enabled(history.can_go_forward());
  93. };
  94. auto open_page = [&](const String& path) {
  95. if (path.is_null()) {
  96. page_view.set_document(nullptr);
  97. return;
  98. }
  99. dbg() << "Opening page at " << path;
  100. auto file = Core::File::construct();
  101. file->set_filename(path);
  102. if (!file->open(Core::IODevice::OpenMode::ReadOnly)) {
  103. int saved_errno = errno;
  104. GUI::MessageBox::show(strerror(saved_errno), "Failed to open man page", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
  105. return;
  106. }
  107. auto buffer = file->read_all();
  108. StringView source { (const char*)buffer.data(), buffer.size() };
  109. Markdown::Document md_document;
  110. bool success = md_document.parse(source);
  111. ASSERT(success);
  112. String html = md_document.render_to_html();
  113. auto html_document = Web::parse_html_document(html);
  114. page_view.set_document(html_document);
  115. String page_and_section = model->page_and_section(tree_view.selection().first());
  116. window->set_title(String::format("%s - Help", page_and_section.characters()));
  117. };
  118. tree_view.on_selection_change = [&] {
  119. String path = model->page_path(tree_view.selection().first());
  120. if (path.is_null()) {
  121. page_view.set_document(nullptr);
  122. return;
  123. }
  124. history.push(path);
  125. update_actions();
  126. open_page(path);
  127. };
  128. page_view.on_link_click = [&](const String& href, auto&, unsigned) {
  129. char* current_path = strdup(history.current().characters());
  130. char* dir_path = dirname(current_path);
  131. char* path = realpath(String::format("%s/%s", dir_path, href.characters()).characters(), nullptr);
  132. free(current_path);
  133. auto tree_view_index = model->index_from_path(path);
  134. if (tree_view_index.has_value()) {
  135. dbg() << "Found path _" << path << "_ in model at index " << tree_view_index.value();
  136. tree_view.selection().set(tree_view_index.value());
  137. return;
  138. }
  139. history.push(path);
  140. update_actions();
  141. open_page(path);
  142. free(path);
  143. };
  144. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  145. history.go_back();
  146. update_actions();
  147. open_page(history.current());
  148. });
  149. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  150. history.go_forward();
  151. update_actions();
  152. open_page(history.current());
  153. });
  154. go_back_action->set_enabled(false);
  155. go_forward_action->set_enabled(false);
  156. toolbar.add_action(*go_back_action);
  157. toolbar.add_action(*go_forward_action);
  158. auto menubar = GUI::MenuBar::construct();
  159. auto& app_menu = menubar->add_menu("Help");
  160. app_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  161. GUI::AboutDialog::show("Help", Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"), window);
  162. }));
  163. app_menu.add_separator();
  164. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  165. GUI::Application::the().quit(0);
  166. }));
  167. auto& go_menu = menubar->add_menu("Go");
  168. go_menu.add_action(*go_back_action);
  169. go_menu.add_action(*go_forward_action);
  170. app.set_menubar(move(menubar));
  171. window->set_focused_widget(&tree_view);
  172. window->show();
  173. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"));
  174. return app.exec();
  175. }