main.cpp 11 KB

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