main.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <LibDesktop/Launcher.h>
  32. #include <LibGUI/AboutDialog.h>
  33. #include <LibGUI/Action.h>
  34. #include <LibGUI/Application.h>
  35. #include <LibGUI/BoxLayout.h>
  36. #include <LibGUI/Menu.h>
  37. #include <LibGUI/MenuBar.h>
  38. #include <LibGUI/MessageBox.h>
  39. #include <LibGUI/Splitter.h>
  40. #include <LibGUI/TextEditor.h>
  41. #include <LibGUI/ToolBar.h>
  42. #include <LibGUI/ToolBarContainer.h>
  43. #include <LibGUI/TreeView.h>
  44. #include <LibGUI/Window.h>
  45. #include <LibMarkdown/Document.h>
  46. #include <LibWeb/Layout/LayoutNode.h>
  47. #include <LibWeb/PageView.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. auto app = GUI::Application::construct(argc, argv);
  58. if (pledge("stdio shared_buffer accept rpath unix", 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. if (unveil("/tmp/portal/launch", "rw") < 0) {
  71. perror("unveil");
  72. return 1;
  73. }
  74. unveil(nullptr, nullptr);
  75. auto app_icon = GUI::Icon::default_icon("app-help");
  76. auto window = GUI::Window::construct();
  77. window->set_icon(app_icon.bitmap_for_size(16));
  78. window->set_title("Help");
  79. window->resize(570, 500);
  80. auto& widget = window->set_main_widget<GUI::Widget>();
  81. widget.set_layout<GUI::VerticalBoxLayout>();
  82. widget.set_fill_with_background_color(true);
  83. widget.layout()->set_spacing(2);
  84. auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
  85. auto& toolbar = toolbar_container.add<GUI::ToolBar>();
  86. auto& splitter = widget.add<GUI::HorizontalSplitter>();
  87. auto model = ManualModel::create();
  88. auto& tree_view = splitter.add<GUI::TreeView>();
  89. tree_view.set_model(model);
  90. tree_view.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  91. tree_view.set_preferred_size(200, 500);
  92. auto& page_view = splitter.add<Web::PageView>();
  93. History history;
  94. RefPtr<GUI::Action> go_back_action;
  95. RefPtr<GUI::Action> go_forward_action;
  96. auto update_actions = [&]() {
  97. go_back_action->set_enabled(history.can_go_back());
  98. go_forward_action->set_enabled(history.can_go_forward());
  99. };
  100. auto open_page = [&](const String& path) {
  101. if (path.is_null()) {
  102. page_view.set_document(nullptr);
  103. return;
  104. }
  105. dbg() << "Opening page at " << path;
  106. auto file = Core::File::construct();
  107. file->set_filename(path);
  108. if (!file->open(Core::IODevice::OpenMode::ReadOnly)) {
  109. int saved_errno = errno;
  110. GUI::MessageBox::show(window, strerror(saved_errno), "Failed to open man page", GUI::MessageBox::Type::Error);
  111. return;
  112. }
  113. auto buffer = file->read_all();
  114. StringView source { (const char*)buffer.data(), buffer.size() };
  115. String html;
  116. {
  117. auto md_document = Markdown::Document::parse(source);
  118. ASSERT(md_document);
  119. html = md_document->render_to_html();
  120. }
  121. page_view.load_html(html, URL::create_with_file_protocol(path));
  122. String page_and_section = model->page_and_section(tree_view.selection().first());
  123. window->set_title(String::format("%s - Help", page_and_section.characters()));
  124. };
  125. tree_view.on_selection_change = [&] {
  126. String path = model->page_path(tree_view.selection().first());
  127. if (path.is_null()) {
  128. page_view.set_document(nullptr);
  129. window->set_title("Help");
  130. return;
  131. }
  132. history.push(path);
  133. update_actions();
  134. open_page(path);
  135. };
  136. tree_view.on_toggle = [&](const GUI::ModelIndex& index, const bool open) {
  137. model->update_section_node_on_toggle(index, open);
  138. };
  139. auto open_external = [&](auto& url) {
  140. if (!Desktop::Launcher::open(url)) {
  141. GUI::MessageBox::show(window,
  142. String::format("The link to '%s' could not be opened.", url.to_string().characters()),
  143. "Failed to open link",
  144. GUI::MessageBox::Type::Error);
  145. }
  146. };
  147. page_view.on_link_click = [&](auto& url, auto&, unsigned) {
  148. if (url.protocol() != "file") {
  149. open_external(url);
  150. return;
  151. }
  152. auto path = Core::File::real_path_for(url.path());
  153. if (!path.starts_with("/usr/share/man/")) {
  154. open_external(url);
  155. return;
  156. }
  157. auto tree_view_index = model->index_from_path(path);
  158. if (tree_view_index.has_value()) {
  159. dbg() << "Found path _" << path << "_ in model at index " << tree_view_index.value();
  160. tree_view.selection().set(tree_view_index.value());
  161. return;
  162. }
  163. history.push(path);
  164. update_actions();
  165. open_page(path);
  166. };
  167. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  168. history.go_back();
  169. update_actions();
  170. open_page(history.current());
  171. });
  172. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  173. history.go_forward();
  174. update_actions();
  175. open_page(history.current());
  176. });
  177. go_back_action->set_enabled(false);
  178. go_forward_action->set_enabled(false);
  179. toolbar.add_action(*go_back_action);
  180. toolbar.add_action(*go_forward_action);
  181. auto menubar = GUI::MenuBar::construct();
  182. auto& app_menu = menubar->add_menu("Help");
  183. app_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  184. GUI::AboutDialog::show("Help", app_icon.bitmap_for_size(32), window);
  185. }));
  186. app_menu.add_separator();
  187. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  188. GUI::Application::the()->quit();
  189. }));
  190. auto& go_menu = menubar->add_menu("Go");
  191. go_menu.add_action(*go_back_action);
  192. go_menu.add_action(*go_forward_action);
  193. app->set_menubar(move(menubar));
  194. window->set_focused_widget(&tree_view);
  195. window->show();
  196. return app->exec();
  197. }