main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <LibCore/System.h>
  13. #include <LibDesktop/Launcher.h>
  14. #include <LibGUI/Action.h>
  15. #include <LibGUI/Application.h>
  16. #include <LibGUI/BoxLayout.h>
  17. #include <LibGUI/Clipboard.h>
  18. #include <LibGUI/FilteringProxyModel.h>
  19. #include <LibGUI/ListView.h>
  20. #include <LibGUI/Menu.h>
  21. #include <LibGUI/Menubar.h>
  22. #include <LibGUI/MessageBox.h>
  23. #include <LibGUI/Splitter.h>
  24. #include <LibGUI/Statusbar.h>
  25. #include <LibGUI/TabWidget.h>
  26. #include <LibGUI/TextBox.h>
  27. #include <LibGUI/Toolbar.h>
  28. #include <LibGUI/ToolbarContainer.h>
  29. #include <LibGUI/TreeView.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibMain/Main.h>
  32. #include <LibMarkdown/Document.h>
  33. #include <LibWeb/OutOfProcessWebView.h>
  34. ErrorOr<int> serenity_main(Main::Arguments arguments)
  35. {
  36. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
  37. auto app = TRY(GUI::Application::try_create(arguments));
  38. TRY(Core::System::unveil("/res", "r"));
  39. TRY(Core::System::unveil("/usr/share/man", "r"));
  40. TRY(Core::System::unveil("/tmp/portal/launch", "rw"));
  41. TRY(Core::System::unveil("/tmp/portal/webcontent", "rw"));
  42. TRY(Core::System::unveil(nullptr, nullptr));
  43. char const* start_page = nullptr;
  44. Core::ArgsParser args_parser;
  45. args_parser.add_positional_argument(start_page, "Page to open at launch", "page", Core::ArgsParser::Required::No);
  46. args_parser.parse(arguments);
  47. auto app_icon = GUI::Icon::default_icon("app-help");
  48. auto window = TRY(GUI::Window::try_create());
  49. window->set_icon(app_icon.bitmap_for_size(16));
  50. window->set_title("Help");
  51. window->resize(570, 500);
  52. auto widget = TRY(window->try_set_main_widget<GUI::Widget>());
  53. (void)TRY(widget->try_set_layout<GUI::VerticalBoxLayout>());
  54. widget->set_fill_with_background_color(true);
  55. widget->layout()->set_spacing(2);
  56. auto toolbar_container = TRY(widget->try_add<GUI::ToolbarContainer>());
  57. auto toolbar = TRY(toolbar_container->try_add<GUI::Toolbar>());
  58. auto splitter = TRY(widget->try_add<GUI::HorizontalSplitter>());
  59. splitter->layout()->set_spacing(5);
  60. auto manual_model = ManualModel::create();
  61. auto left_tab_bar = TRY(splitter->try_add<GUI::TabWidget>());
  62. auto tree_view_container = TRY(left_tab_bar->try_add_tab<GUI::Widget>("Browse"));
  63. (void)TRY(tree_view_container->try_set_layout<GUI::VerticalBoxLayout>());
  64. tree_view_container->layout()->set_margins(4);
  65. auto tree_view = TRY(tree_view_container->try_add<GUI::TreeView>());
  66. auto search_view = TRY(left_tab_bar->try_add_tab<GUI::Widget>("Search"));
  67. (void)TRY(search_view->try_set_layout<GUI::VerticalBoxLayout>());
  68. search_view->layout()->set_margins(4);
  69. auto search_box = TRY(search_view->try_add<GUI::TextBox>());
  70. auto search_list_view = TRY(search_view->try_add<GUI::ListView>());
  71. search_box->set_fixed_height(20);
  72. search_box->set_placeholder("Search...");
  73. search_box->on_change = [&] {
  74. if (auto* model = search_list_view->model()) {
  75. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  76. search_model.set_filter_term(search_box->text());
  77. search_model.invalidate();
  78. }
  79. };
  80. search_list_view->set_model(TRY(GUI::FilteringProxyModel::create(manual_model)));
  81. search_list_view->model()->invalidate();
  82. tree_view->set_model(manual_model);
  83. left_tab_bar->set_fixed_width(200);
  84. auto page_view = TRY(splitter->try_add<Web::OutOfProcessWebView>());
  85. History history;
  86. RefPtr<GUI::Action> go_back_action;
  87. RefPtr<GUI::Action> go_forward_action;
  88. auto open_page = [&](String const& path) {
  89. go_back_action->set_enabled(history.can_go_back());
  90. go_forward_action->set_enabled(history.can_go_forward());
  91. if (path.is_null()) {
  92. window->set_title("Help");
  93. page_view->load_empty_document();
  94. return;
  95. }
  96. auto source_result = manual_model->page_view(path);
  97. if (source_result.is_error()) {
  98. GUI::MessageBox::show(window, String::formatted("{}", source_result.error()), "Failed to open man page", GUI::MessageBox::Type::Error);
  99. return;
  100. }
  101. auto source = source_result.value();
  102. String html;
  103. {
  104. auto md_document = Markdown::Document::parse(source);
  105. VERIFY(md_document);
  106. html = md_document->render_to_html();
  107. }
  108. auto url = URL::create_with_file_protocol(path);
  109. page_view->load_html(html, url);
  110. app->deferred_invoke([&, path] {
  111. auto tree_view_index = manual_model->index_from_path(path);
  112. if (tree_view_index.has_value()) {
  113. tree_view->expand_tree(tree_view_index.value().parent());
  114. tree_view->selection().set(tree_view_index.value());
  115. String page_and_section = manual_model->page_and_section(tree_view_index.value());
  116. window->set_title(String::formatted("{} - Help", page_and_section));
  117. } else {
  118. window->set_title("Help");
  119. }
  120. });
  121. };
  122. tree_view->on_selection_change = [&] {
  123. String path = manual_model->page_path(tree_view->selection().first());
  124. if (path.is_null())
  125. return;
  126. history.push(path);
  127. open_page(path);
  128. };
  129. tree_view->on_toggle = [&](GUI::ModelIndex const& index, bool open) {
  130. manual_model->update_section_node_on_toggle(index, open);
  131. };
  132. auto open_external = [&](auto& url) {
  133. if (!Desktop::Launcher::open(url)) {
  134. GUI::MessageBox::show(window,
  135. String::formatted("The link to '{}' could not be opened.", url),
  136. "Failed to open link",
  137. GUI::MessageBox::Type::Error);
  138. }
  139. };
  140. search_list_view->on_selection_change = [&] {
  141. auto const& index = search_list_view->selection().first();
  142. if (!index.is_valid())
  143. return;
  144. auto* view_model = search_list_view->model();
  145. if (!view_model) {
  146. page_view->load_empty_document();
  147. return;
  148. }
  149. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(view_model);
  150. auto const& mapped_index = search_model.map(index);
  151. String path = manual_model->page_path(mapped_index);
  152. if (path.is_null()) {
  153. page_view->load_empty_document();
  154. return;
  155. }
  156. tree_view->selection().clear();
  157. tree_view->selection().add(mapped_index);
  158. history.push(path);
  159. open_page(path);
  160. };
  161. page_view->on_link_click = [&](auto& url, auto&, unsigned) {
  162. if (url.protocol() != "file") {
  163. open_external(url);
  164. return;
  165. }
  166. auto path = Core::File::real_path_for(url.path());
  167. if (!path.starts_with("/usr/share/man/")) {
  168. open_external(url);
  169. return;
  170. }
  171. auto tree_view_index = manual_model->index_from_path(path);
  172. if (tree_view_index.has_value()) {
  173. dbgln("Found path _{}_ in manual_model at index {}", path, tree_view_index.value());
  174. tree_view->selection().set(tree_view_index.value());
  175. return;
  176. }
  177. history.push(path);
  178. open_page(path);
  179. };
  180. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  181. history.go_back();
  182. open_page(history.current());
  183. });
  184. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  185. history.go_forward();
  186. open_page(history.current());
  187. });
  188. go_back_action->set_enabled(false);
  189. go_forward_action->set_enabled(false);
  190. auto go_home_action = GUI::CommonActions::make_go_home_action([&](auto&) {
  191. String path = "/usr/share/man/man7/Help-index.md";
  192. history.push(path);
  193. open_page(path);
  194. });
  195. (void)TRY(toolbar->try_add_action(*go_back_action));
  196. (void)TRY(toolbar->try_add_action(*go_forward_action));
  197. (void)TRY(toolbar->try_add_action(*go_home_action));
  198. auto file_menu = TRY(window->try_add_menu("&File"));
  199. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
  200. GUI::Application::the()->quit();
  201. })));
  202. auto go_menu = TRY(window->try_add_menu("&Go"));
  203. TRY(go_menu->try_add_action(*go_back_action));
  204. TRY(go_menu->try_add_action(*go_forward_action));
  205. TRY(go_menu->try_add_action(*go_home_action));
  206. auto help_menu = TRY(window->try_add_menu("&Help"));
  207. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Help", app_icon, window)));
  208. auto context_menu = TRY(GUI::Menu::try_create());
  209. TRY(context_menu->try_add_action(*go_back_action));
  210. TRY(context_menu->try_add_action(*go_forward_action));
  211. TRY(context_menu->try_add_action(*go_home_action));
  212. TRY(context_menu->try_add_separator());
  213. RefPtr<GUI::Action> copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
  214. auto selected_text = page_view->selected_text();
  215. if (!selected_text.is_empty())
  216. GUI::Clipboard::the().set_plain_text(selected_text);
  217. });
  218. TRY(context_menu->try_add_action(*copy_action));
  219. RefPtr<GUI::Action> select_all_function = GUI::CommonActions::make_select_all_action([&](auto&) {
  220. page_view->select_all();
  221. });
  222. TRY(context_menu->try_add_action(*select_all_function));
  223. page_view->on_context_menu_request = [&](auto& screen_position) {
  224. copy_action->set_enabled(!page_view->selected_text().is_empty());
  225. context_menu->popup(screen_position);
  226. };
  227. bool set_start_page = false;
  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. open_page(url.path());
  233. set_start_page = true;
  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. }
  243. if (!set_start_page)
  244. go_home_action->activate();
  245. auto statusbar = TRY(widget->try_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. }