main.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. splitter.layout()->set_spacing(5);
  76. auto model = ManualModel::create();
  77. auto& left_tab_bar = splitter.add<GUI::TabWidget>();
  78. auto& tree_view_container = left_tab_bar.add_tab<GUI::Widget>("Browse");
  79. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  80. tree_view_container.layout()->set_margins({ 4, 4, 4, 4 });
  81. auto& tree_view = tree_view_container.add<GUI::TreeView>();
  82. auto& search_view = left_tab_bar.add_tab<GUI::Widget>("Search");
  83. search_view.set_layout<GUI::VerticalBoxLayout>();
  84. search_view.layout()->set_margins({ 4, 4, 4, 4 });
  85. auto& search_box = search_view.add<GUI::TextBox>();
  86. auto& search_list_view = search_view.add<GUI::ListView>();
  87. search_box.set_fixed_height(20);
  88. search_box.set_placeholder("Search...");
  89. search_box.on_change = [&] {
  90. if (auto model = search_list_view.model()) {
  91. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  92. search_model.set_filter_term(search_box.text());
  93. search_model.update();
  94. }
  95. };
  96. search_list_view.set_model(GUI::FilteringProxyModel::construct(model));
  97. search_list_view.model()->update();
  98. tree_view.set_model(model);
  99. left_tab_bar.set_fixed_width(200);
  100. auto& page_view = splitter.add<Web::OutOfProcessWebView>();
  101. History history;
  102. RefPtr<GUI::Action> go_back_action;
  103. RefPtr<GUI::Action> go_forward_action;
  104. auto update_actions = [&]() {
  105. go_back_action->set_enabled(history.can_go_back());
  106. go_forward_action->set_enabled(history.can_go_forward());
  107. };
  108. auto open_page = [&](const String& path) {
  109. if (path.is_null()) {
  110. window->set_title("Help");
  111. page_view.load_empty_document();
  112. return;
  113. }
  114. auto source_result = model->page_view(path);
  115. if (source_result.is_error()) {
  116. GUI::MessageBox::show(window, source_result.error().string(), "Failed to open man page", GUI::MessageBox::Type::Error);
  117. return;
  118. }
  119. auto source = source_result.value();
  120. String html;
  121. {
  122. auto md_document = Markdown::Document::parse(source);
  123. VERIFY(md_document);
  124. html = md_document->render_to_html();
  125. }
  126. auto url = URL::create_with_file_protocol(path);
  127. page_view.load_html(html, url);
  128. app->deferred_invoke([&, path](auto&) {
  129. auto tree_view_index = model->index_from_path(path);
  130. if (tree_view_index.has_value()) {
  131. tree_view.expand_tree(tree_view_index.value().parent());
  132. tree_view.selection().set(tree_view_index.value());
  133. String page_and_section = model->page_and_section(tree_view_index.value());
  134. window->set_title(String::formatted("{} - Help", page_and_section));
  135. } else {
  136. window->set_title("Help");
  137. }
  138. });
  139. };
  140. tree_view.on_selection_change = [&] {
  141. String path = model->page_path(tree_view.selection().first());
  142. if (path.is_null())
  143. return;
  144. history.push(path);
  145. update_actions();
  146. open_page(path);
  147. };
  148. tree_view.on_toggle = [&](const GUI::ModelIndex& index, const bool open) {
  149. model->update_section_node_on_toggle(index, open);
  150. };
  151. auto open_external = [&](auto& url) {
  152. if (!Desktop::Launcher::open(url)) {
  153. GUI::MessageBox::show(window,
  154. String::formatted("The link to '{}' could not be opened.", url),
  155. "Failed to open link",
  156. GUI::MessageBox::Type::Error);
  157. }
  158. };
  159. search_list_view.on_selection_change = [&] {
  160. const auto& index = search_list_view.selection().first();
  161. if (!index.is_valid())
  162. return;
  163. auto view_model = search_list_view.model();
  164. if (!view_model) {
  165. page_view.load_empty_document();
  166. return;
  167. }
  168. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(view_model);
  169. const auto& mapped_index = search_model.map(index);
  170. String path = model->page_path(mapped_index);
  171. if (path.is_null()) {
  172. page_view.load_empty_document();
  173. return;
  174. }
  175. tree_view.selection().clear();
  176. tree_view.selection().add(mapped_index);
  177. history.push(path);
  178. update_actions();
  179. open_page(path);
  180. };
  181. page_view.on_link_click = [&](auto& url, auto&, unsigned) {
  182. if (url.protocol() != "file") {
  183. open_external(url);
  184. return;
  185. }
  186. auto path = Core::File::real_path_for(url.path());
  187. if (!path.starts_with("/usr/share/man/")) {
  188. open_external(url);
  189. return;
  190. }
  191. auto tree_view_index = model->index_from_path(path);
  192. if (tree_view_index.has_value()) {
  193. dbgln("Found path _{}_ in model at index {}", path, tree_view_index.value());
  194. tree_view.selection().set(tree_view_index.value());
  195. return;
  196. }
  197. history.push(path);
  198. update_actions();
  199. open_page(path);
  200. };
  201. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  202. history.go_back();
  203. update_actions();
  204. open_page(history.current());
  205. });
  206. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  207. history.go_forward();
  208. update_actions();
  209. open_page(history.current());
  210. });
  211. go_back_action->set_enabled(false);
  212. go_forward_action->set_enabled(false);
  213. auto go_home_action = GUI::CommonActions::make_go_home_action([&](auto&) {
  214. String path = "/usr/share/man/man7/Help-index.md";
  215. history.push(path);
  216. update_actions();
  217. open_page(path);
  218. });
  219. toolbar.add_action(*go_back_action);
  220. toolbar.add_action(*go_forward_action);
  221. toolbar.add_action(*go_home_action);
  222. auto menubar = GUI::Menubar::construct();
  223. auto& file_menu = menubar->add_menu("&File");
  224. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  225. GUI::Application::the()->quit();
  226. }));
  227. auto& go_menu = menubar->add_menu("&Go");
  228. go_menu.add_action(*go_back_action);
  229. go_menu.add_action(*go_forward_action);
  230. go_menu.add_action(*go_home_action);
  231. auto& help_menu = menubar->add_menu("&Help");
  232. help_menu.add_action(GUI::CommonActions::make_about_action("Help", app_icon, window));
  233. window->set_menubar(move(menubar));
  234. if (start_page) {
  235. URL url = URL::create_with_url_or_path(start_page);
  236. if (url.is_valid() && url.path().ends_with(".md")) {
  237. history.push(url.path());
  238. update_actions();
  239. open_page(url.path());
  240. } else {
  241. left_tab_bar.set_active_widget(&search_view);
  242. search_box.set_text(start_page);
  243. if (auto model = search_list_view.model()) {
  244. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  245. search_model.set_filter_term(search_box.text());
  246. }
  247. }
  248. } else {
  249. go_home_action->activate();
  250. }
  251. auto& statusbar = widget.add<GUI::Statusbar>();
  252. app->on_action_enter = [&statusbar](GUI::Action const& action) {
  253. statusbar.set_override_text(action.status_tip());
  254. };
  255. app->on_action_leave = [&statusbar](GUI::Action const&) {
  256. statusbar.set_override_text({});
  257. };
  258. page_view.on_link_hover = [&](URL const& url) {
  259. if (url.is_valid())
  260. statusbar.set_text(url.to_string());
  261. else
  262. statusbar.set_text({});
  263. };
  264. window->set_focused_widget(&left_tab_bar);
  265. window->show();
  266. return app->exec();
  267. }