main.cpp 9.7 KB

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