main.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "History.h"
  8. #include "ManualModel.h"
  9. #include <AK/URL.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/File.h>
  12. #include <LibDesktop/Launcher.h>
  13. #include <LibGUI/Action.h>
  14. #include <LibGUI/Application.h>
  15. #include <LibGUI/BoxLayout.h>
  16. #include <LibGUI/FilteringProxyModel.h>
  17. #include <LibGUI/ListView.h>
  18. #include <LibGUI/Menu.h>
  19. #include <LibGUI/Menubar.h>
  20. #include <LibGUI/MessageBox.h>
  21. #include <LibGUI/Splitter.h>
  22. #include <LibGUI/Statusbar.h>
  23. #include <LibGUI/TabWidget.h>
  24. #include <LibGUI/TextBox.h>
  25. #include <LibGUI/Toolbar.h>
  26. #include <LibGUI/ToolbarContainer.h>
  27. #include <LibGUI/TreeView.h>
  28. #include <LibGUI/Window.h>
  29. #include <LibMarkdown/Document.h>
  30. #include <LibWeb/OutOfProcessWebView.h>
  31. #include <libgen.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. int main(int argc, char* argv[])
  36. {
  37. if (pledge("stdio recvfd sendfd rpath unix", nullptr) < 0) {
  38. perror("pledge");
  39. return 1;
  40. }
  41. auto app = GUI::Application::construct(argc, argv);
  42. if (unveil("/res", "r") < 0) {
  43. perror("unveil");
  44. return 1;
  45. }
  46. if (unveil("/usr/share/man", "r") < 0) {
  47. perror("unveil");
  48. return 1;
  49. }
  50. if (unveil("/tmp/portal/launch", "rw") < 0) {
  51. perror("unveil");
  52. return 1;
  53. }
  54. if (unveil("/tmp/portal/webcontent", "rw") < 0) {
  55. perror("unveil");
  56. return 1;
  57. }
  58. unveil(nullptr, nullptr);
  59. const char* start_page = nullptr;
  60. Core::ArgsParser args_parser;
  61. args_parser.add_positional_argument(start_page, "Page to open at launch", "page", Core::ArgsParser::Required::No);
  62. args_parser.parse(argc, argv);
  63. auto app_icon = GUI::Icon::default_icon("app-help");
  64. auto window = GUI::Window::construct();
  65. window->set_icon(app_icon.bitmap_for_size(16));
  66. window->set_title("Help");
  67. window->resize(570, 500);
  68. auto& widget = window->set_main_widget<GUI::Widget>();
  69. widget.set_layout<GUI::VerticalBoxLayout>();
  70. widget.set_fill_with_background_color(true);
  71. widget.layout()->set_spacing(2);
  72. auto& toolbar_container = widget.add<GUI::ToolbarContainer>();
  73. auto& toolbar = toolbar_container.add<GUI::Toolbar>();
  74. auto& splitter = widget.add<GUI::HorizontalSplitter>();
  75. auto model = ManualModel::create();
  76. auto& left_tab_bar = splitter.add<GUI::TabWidget>();
  77. auto& tree_view_container = left_tab_bar.add_tab<GUI::Widget>("Browse");
  78. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  79. tree_view_container.layout()->set_margins({ 4, 4, 4, 4 });
  80. auto& tree_view = tree_view_container.add<GUI::TreeView>();
  81. auto& search_view = left_tab_bar.add_tab<GUI::Widget>("Search");
  82. search_view.set_layout<GUI::VerticalBoxLayout>();
  83. search_view.layout()->set_margins({ 4, 4, 4, 4 });
  84. auto& search_box = search_view.add<GUI::TextBox>();
  85. auto& search_list_view = search_view.add<GUI::ListView>();
  86. search_box.set_fixed_height(20);
  87. search_box.set_placeholder("Search...");
  88. search_box.on_change = [&] {
  89. if (auto model = search_list_view.model()) {
  90. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  91. search_model.set_filter_term(search_box.text());
  92. search_model.update();
  93. }
  94. };
  95. search_list_view.set_model(GUI::FilteringProxyModel::construct(model));
  96. search_list_view.model()->update();
  97. tree_view.set_model(model);
  98. left_tab_bar.set_fixed_width(200);
  99. auto& page_view = splitter.add<Web::OutOfProcessWebView>();
  100. History history;
  101. RefPtr<GUI::Action> go_back_action;
  102. RefPtr<GUI::Action> go_forward_action;
  103. auto update_actions = [&]() {
  104. go_back_action->set_enabled(history.can_go_back());
  105. go_forward_action->set_enabled(history.can_go_forward());
  106. };
  107. auto open_page = [&](const String& path) {
  108. if (path.is_null()) {
  109. window->set_title("Help");
  110. page_view.load_empty_document();
  111. return;
  112. }
  113. auto source_result = model->page_view(path);
  114. if (source_result.is_error()) {
  115. GUI::MessageBox::show(window, source_result.error().string(), "Failed to open man page", GUI::MessageBox::Type::Error);
  116. return;
  117. }
  118. auto source = source_result.value();
  119. String html;
  120. {
  121. auto md_document = Markdown::Document::parse(source);
  122. VERIFY(md_document);
  123. html = md_document->render_to_html();
  124. }
  125. auto url = URL::create_with_file_protocol(path);
  126. page_view.load_html(html, url);
  127. auto tree_view_index = model->index_from_path(path);
  128. if (tree_view_index.has_value())
  129. tree_view.expand_tree(tree_view_index.value().parent());
  130. String page_and_section = model->page_and_section(tree_view_index.value());
  131. window->set_title(String::formatted("{} - Help", page_and_section));
  132. };
  133. tree_view.on_selection_change = [&] {
  134. String path = model->page_path(tree_view.selection().first());
  135. history.push(path);
  136. update_actions();
  137. open_page(path);
  138. };
  139. tree_view.on_toggle = [&](const GUI::ModelIndex& index, const bool open) {
  140. model->update_section_node_on_toggle(index, open);
  141. };
  142. auto open_external = [&](auto& url) {
  143. if (!Desktop::Launcher::open(url)) {
  144. GUI::MessageBox::show(window,
  145. String::formatted("The link to '{}' could not be opened.", url),
  146. "Failed to open link",
  147. GUI::MessageBox::Type::Error);
  148. }
  149. };
  150. search_list_view.on_selection = [&](auto index) {
  151. if (!index.is_valid())
  152. return;
  153. if (auto model = search_list_view.model()) {
  154. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  155. index = search_model.map(index);
  156. } else {
  157. page_view.load_empty_document();
  158. return;
  159. }
  160. String path = model->page_path(index);
  161. if (path.is_null()) {
  162. page_view.load_empty_document();
  163. return;
  164. }
  165. tree_view.selection().clear();
  166. tree_view.selection().add(index);
  167. history.push(path);
  168. update_actions();
  169. open_page(path);
  170. };
  171. page_view.on_link_click = [&](auto& url, auto&, unsigned) {
  172. if (url.protocol() != "file") {
  173. open_external(url);
  174. return;
  175. }
  176. auto path = Core::File::real_path_for(url.path());
  177. if (!path.starts_with("/usr/share/man/")) {
  178. open_external(url);
  179. return;
  180. }
  181. auto tree_view_index = model->index_from_path(path);
  182. if (tree_view_index.has_value()) {
  183. dbgln("Found path _{}_ in model at index {}", path, tree_view_index.value());
  184. tree_view.selection().set(tree_view_index.value());
  185. return;
  186. }
  187. history.push(path);
  188. update_actions();
  189. open_page(path);
  190. };
  191. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  192. history.go_back();
  193. update_actions();
  194. open_page(history.current());
  195. });
  196. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  197. history.go_forward();
  198. update_actions();
  199. open_page(history.current());
  200. });
  201. go_back_action->set_enabled(false);
  202. go_forward_action->set_enabled(false);
  203. auto go_home_action = GUI::CommonActions::make_go_home_action([&](auto&) {
  204. String path = "/usr/share/man/man7/Help-index.md";
  205. history.push(path);
  206. update_actions();
  207. open_page(path);
  208. });
  209. toolbar.add_action(*go_back_action);
  210. toolbar.add_action(*go_forward_action);
  211. toolbar.add_action(*go_home_action);
  212. auto menubar = GUI::Menubar::construct();
  213. auto& file_menu = menubar->add_menu("&File");
  214. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  215. GUI::Application::the()->quit();
  216. }));
  217. auto& go_menu = menubar->add_menu("&Go");
  218. go_menu.add_action(*go_back_action);
  219. go_menu.add_action(*go_forward_action);
  220. go_menu.add_action(*go_home_action);
  221. auto& help_menu = menubar->add_menu("&Help");
  222. help_menu.add_action(GUI::CommonActions::make_about_action("Help", app_icon, window));
  223. window->set_menubar(move(menubar));
  224. if (start_page) {
  225. URL url = URL::create_with_url_or_path(start_page);
  226. if (url.is_valid() && url.path().ends_with(".md")) {
  227. history.push(url.path());
  228. update_actions();
  229. open_page(url.path());
  230. } else {
  231. left_tab_bar.set_active_widget(&search_view);
  232. search_box.set_text(start_page);
  233. if (auto model = search_list_view.model()) {
  234. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  235. search_model.set_filter_term(search_box.text());
  236. }
  237. }
  238. } else {
  239. go_home_action->activate();
  240. }
  241. auto& statusbar = widget.add<GUI::Statusbar>();
  242. app->on_action_enter = [&statusbar](GUI::Action const& action) {
  243. statusbar.set_override_text(action.status_tip());
  244. };
  245. app->on_action_leave = [&statusbar](GUI::Action const&) {
  246. statusbar.set_override_text({});
  247. };
  248. page_view.on_link_hover = [&](URL const& url) {
  249. if (url.is_valid())
  250. statusbar.set_text(url.to_string());
  251. else
  252. statusbar.set_text({});
  253. };
  254. window->set_focused_widget(&left_tab_bar);
  255. window->show();
  256. return app->exec();
  257. }