main.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. auto md_document = Markdown::Document::parse(source);
  110. ASSERT(md_document);
  111. String html = md_document->render_to_html();
  112. auto html_document = Web::parse_html_document(html);
  113. page_view.set_document(html_document);
  114. String page_and_section = model->page_and_section(tree_view.selection().first());
  115. window->set_title(String::format("%s - Help", page_and_section.characters()));
  116. };
  117. tree_view.on_selection_change = [&] {
  118. String path = model->page_path(tree_view.selection().first());
  119. if (path.is_null()) {
  120. page_view.set_document(nullptr);
  121. return;
  122. }
  123. history.push(path);
  124. update_actions();
  125. open_page(path);
  126. };
  127. page_view.on_link_click = [&](const String& href, auto&, unsigned) {
  128. char* current_path = strdup(history.current().characters());
  129. char* dir_path = dirname(current_path);
  130. char* path = realpath(String::format("%s/%s", dir_path, href.characters()).characters(), nullptr);
  131. free(current_path);
  132. auto tree_view_index = model->index_from_path(path);
  133. if (tree_view_index.has_value()) {
  134. dbg() << "Found path _" << path << "_ in model at index " << tree_view_index.value();
  135. tree_view.selection().set(tree_view_index.value());
  136. return;
  137. }
  138. history.push(path);
  139. update_actions();
  140. open_page(path);
  141. free(path);
  142. };
  143. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  144. history.go_back();
  145. update_actions();
  146. open_page(history.current());
  147. });
  148. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  149. history.go_forward();
  150. update_actions();
  151. open_page(history.current());
  152. });
  153. go_back_action->set_enabled(false);
  154. go_forward_action->set_enabled(false);
  155. toolbar.add_action(*go_back_action);
  156. toolbar.add_action(*go_forward_action);
  157. auto menubar = GUI::MenuBar::construct();
  158. auto& app_menu = menubar->add_menu("Help");
  159. app_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  160. GUI::AboutDialog::show("Help", Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"), window);
  161. }));
  162. app_menu.add_separator();
  163. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  164. GUI::Application::the().quit(0);
  165. }));
  166. auto& go_menu = menubar->add_menu("Go");
  167. go_menu.add_action(*go_back_action);
  168. go_menu.add_action(*go_forward_action);
  169. app.set_menubar(move(menubar));
  170. window->set_focused_widget(&tree_view);
  171. window->show();
  172. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"));
  173. return app.exec();
  174. }