main.cpp 7.1 KB

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