main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "History.h"
  9. #include "ManualModel.h"
  10. #include <AK/URL.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/System.h>
  14. #include <LibDesktop/Launcher.h>
  15. #include <LibGUI/Action.h>
  16. #include <LibGUI/Application.h>
  17. #include <LibGUI/BoxLayout.h>
  18. #include <LibGUI/Clipboard.h>
  19. #include <LibGUI/FilteringProxyModel.h>
  20. #include <LibGUI/ListView.h>
  21. #include <LibGUI/Menu.h>
  22. #include <LibGUI/Menubar.h>
  23. #include <LibGUI/MessageBox.h>
  24. #include <LibGUI/Splitter.h>
  25. #include <LibGUI/Statusbar.h>
  26. #include <LibGUI/TabWidget.h>
  27. #include <LibGUI/TextBox.h>
  28. #include <LibGUI/Toolbar.h>
  29. #include <LibGUI/ToolbarContainer.h>
  30. #include <LibGUI/TreeView.h>
  31. #include <LibGUI/Window.h>
  32. #include <LibMain/Main.h>
  33. #include <LibMarkdown/Document.h>
  34. #include <LibWeb/OutOfProcessWebView.h>
  35. ErrorOr<int> serenity_main(Main::Arguments arguments)
  36. {
  37. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
  38. auto app = TRY(GUI::Application::try_create(arguments));
  39. TRY(Core::System::unveil("/res", "r"));
  40. TRY(Core::System::unveil("/usr/share/man", "r"));
  41. TRY(Core::System::unveil("/tmp/portal/launch", "rw"));
  42. TRY(Core::System::unveil("/tmp/portal/webcontent", "rw"));
  43. TRY(Core::System::unveil(nullptr, nullptr));
  44. char const* start_page = nullptr;
  45. unsigned section = 0;
  46. Core::ArgsParser args_parser;
  47. // FIXME: These custom Args are a hack. What we want to do is have an optional int arg, then an optional string.
  48. // However, when only a string is provided, it gets forwarded to the int argument since that is first, and
  49. // parsing fails. This hack instead forwards it to the start_page in that case.
  50. args_parser.add_positional_argument(Core::ArgsParser::Arg {
  51. .help_string = "Section of the man page",
  52. .name = "section",
  53. .min_values = 0,
  54. .max_values = 1,
  55. .accept_value = [&](char const* input) {
  56. // If it's a number, use it as the section
  57. if (auto number = StringView(input).to_int(); number.has_value()) {
  58. section = number.value();
  59. return true;
  60. }
  61. // Otherwise, use it as the start_page
  62. start_page = input;
  63. return true;
  64. } });
  65. args_parser.add_positional_argument(Core::ArgsParser::Arg {
  66. .help_string = "Help page to open. Either an absolute path to the markdown file, or a search query",
  67. .name = "page",
  68. .min_values = 0,
  69. .max_values = 1,
  70. .accept_value = [&](char const* input) {
  71. // If start_page was already set by our section arg, then it can't be set again
  72. if (start_page)
  73. return false;
  74. start_page = input;
  75. return true;
  76. } });
  77. args_parser.parse(arguments);
  78. auto app_icon = GUI::Icon::default_icon("app-help");
  79. auto window = TRY(GUI::Window::try_create());
  80. window->set_icon(app_icon.bitmap_for_size(16));
  81. window->set_title("Help");
  82. window->resize(570, 500);
  83. auto widget = TRY(window->try_set_main_widget<GUI::Widget>());
  84. (void)TRY(widget->try_set_layout<GUI::VerticalBoxLayout>());
  85. widget->set_fill_with_background_color(true);
  86. widget->layout()->set_spacing(2);
  87. auto toolbar_container = TRY(widget->try_add<GUI::ToolbarContainer>());
  88. auto toolbar = TRY(toolbar_container->try_add<GUI::Toolbar>());
  89. auto splitter = TRY(widget->try_add<GUI::HorizontalSplitter>());
  90. splitter->layout()->set_spacing(5);
  91. auto manual_model = ManualModel::create();
  92. auto left_tab_bar = TRY(splitter->try_add<GUI::TabWidget>());
  93. auto tree_view_container = TRY(left_tab_bar->try_add_tab<GUI::Widget>("Browse"));
  94. (void)TRY(tree_view_container->try_set_layout<GUI::VerticalBoxLayout>());
  95. tree_view_container->layout()->set_margins(4);
  96. auto tree_view = TRY(tree_view_container->try_add<GUI::TreeView>());
  97. auto search_view = TRY(left_tab_bar->try_add_tab<GUI::Widget>("Search"));
  98. (void)TRY(search_view->try_set_layout<GUI::VerticalBoxLayout>());
  99. search_view->layout()->set_margins(4);
  100. auto search_box = TRY(search_view->try_add<GUI::TextBox>());
  101. auto search_list_view = TRY(search_view->try_add<GUI::ListView>());
  102. search_box->set_fixed_height(20);
  103. search_box->set_placeholder("Search...");
  104. search_box->on_change = [&] {
  105. if (auto* model = search_list_view->model()) {
  106. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  107. search_model.set_filter_term(search_box->text());
  108. search_model.invalidate();
  109. }
  110. };
  111. search_list_view->set_model(TRY(GUI::FilteringProxyModel::create(manual_model)));
  112. search_list_view->model()->invalidate();
  113. tree_view->set_model(manual_model);
  114. left_tab_bar->set_fixed_width(200);
  115. auto page_view = TRY(splitter->try_add<Web::OutOfProcessWebView>());
  116. History history;
  117. RefPtr<GUI::Action> go_back_action;
  118. RefPtr<GUI::Action> go_forward_action;
  119. auto open_page = [&](String const& path) {
  120. go_back_action->set_enabled(history.can_go_back());
  121. go_forward_action->set_enabled(history.can_go_forward());
  122. if (path.is_null()) {
  123. window->set_title("Help");
  124. page_view->load_empty_document();
  125. return;
  126. }
  127. auto source_result = manual_model->page_view(path);
  128. if (source_result.is_error()) {
  129. GUI::MessageBox::show(window, String::formatted("{}", source_result.error()), "Failed to open man page", GUI::MessageBox::Type::Error);
  130. return;
  131. }
  132. auto source = source_result.value();
  133. String html;
  134. {
  135. auto md_document = Markdown::Document::parse(source);
  136. VERIFY(md_document);
  137. html = md_document->render_to_html();
  138. }
  139. auto url = URL::create_with_file_protocol(path);
  140. page_view->load_html(html, url);
  141. page_view->scroll_to_top();
  142. app->deferred_invoke([&, path] {
  143. auto tree_view_index = manual_model->index_from_path(path);
  144. if (tree_view_index.has_value()) {
  145. tree_view->expand_tree(tree_view_index.value().parent());
  146. tree_view->selection().set(tree_view_index.value());
  147. String page_and_section = manual_model->page_and_section(tree_view_index.value());
  148. window->set_title(String::formatted("{} - Help", page_and_section));
  149. } else {
  150. window->set_title("Help");
  151. }
  152. });
  153. };
  154. tree_view->on_selection_change = [&] {
  155. String path = manual_model->page_path(tree_view->selection().first());
  156. if (path.is_null())
  157. return;
  158. history.push(path);
  159. open_page(path);
  160. };
  161. tree_view->on_toggle = [&](GUI::ModelIndex const& index, bool open) {
  162. manual_model->update_section_node_on_toggle(index, open);
  163. };
  164. auto open_external = [&](auto& url) {
  165. if (!Desktop::Launcher::open(url)) {
  166. GUI::MessageBox::show(window,
  167. String::formatted("The link to '{}' could not be opened.", url),
  168. "Failed to open link",
  169. GUI::MessageBox::Type::Error);
  170. }
  171. };
  172. search_list_view->on_selection_change = [&] {
  173. auto const& index = search_list_view->selection().first();
  174. if (!index.is_valid())
  175. return;
  176. auto* view_model = search_list_view->model();
  177. if (!view_model) {
  178. page_view->load_empty_document();
  179. return;
  180. }
  181. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(view_model);
  182. auto const& mapped_index = search_model.map(index);
  183. String path = manual_model->page_path(mapped_index);
  184. if (path.is_null()) {
  185. page_view->load_empty_document();
  186. return;
  187. }
  188. tree_view->selection().clear();
  189. tree_view->selection().add(mapped_index);
  190. history.push(path);
  191. open_page(path);
  192. };
  193. page_view->on_link_click = [&](auto& url, auto&, unsigned) {
  194. if (url.protocol() != "file") {
  195. open_external(url);
  196. return;
  197. }
  198. auto path = Core::File::real_path_for(url.path());
  199. if (!path.starts_with("/usr/share/man/")) {
  200. open_external(url);
  201. return;
  202. }
  203. auto tree_view_index = manual_model->index_from_path(path);
  204. if (tree_view_index.has_value()) {
  205. dbgln("Found path _{}_ in manual_model at index {}", path, tree_view_index.value());
  206. tree_view->selection().set(tree_view_index.value());
  207. return;
  208. }
  209. history.push(path);
  210. open_page(path);
  211. };
  212. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  213. history.go_back();
  214. open_page(history.current());
  215. });
  216. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  217. history.go_forward();
  218. open_page(history.current());
  219. });
  220. go_back_action->set_enabled(false);
  221. go_forward_action->set_enabled(false);
  222. auto go_home_action = GUI::CommonActions::make_go_home_action([&](auto&) {
  223. String path = "/usr/share/man/man7/Help-index.md";
  224. history.push(path);
  225. open_page(path);
  226. });
  227. (void)TRY(toolbar->try_add_action(*go_back_action));
  228. (void)TRY(toolbar->try_add_action(*go_forward_action));
  229. (void)TRY(toolbar->try_add_action(*go_home_action));
  230. auto file_menu = TRY(window->try_add_menu("&File"));
  231. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
  232. GUI::Application::the()->quit();
  233. })));
  234. auto go_menu = TRY(window->try_add_menu("&Go"));
  235. TRY(go_menu->try_add_action(*go_back_action));
  236. TRY(go_menu->try_add_action(*go_forward_action));
  237. TRY(go_menu->try_add_action(*go_home_action));
  238. auto help_menu = TRY(window->try_add_menu("&Help"));
  239. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Help", app_icon, window)));
  240. auto context_menu = TRY(GUI::Menu::try_create());
  241. TRY(context_menu->try_add_action(*go_back_action));
  242. TRY(context_menu->try_add_action(*go_forward_action));
  243. TRY(context_menu->try_add_action(*go_home_action));
  244. TRY(context_menu->try_add_separator());
  245. RefPtr<GUI::Action> copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
  246. auto selected_text = page_view->selected_text();
  247. if (!selected_text.is_empty())
  248. GUI::Clipboard::the().set_plain_text(selected_text);
  249. });
  250. TRY(context_menu->try_add_action(*copy_action));
  251. RefPtr<GUI::Action> select_all_function = GUI::CommonActions::make_select_all_action([&](auto&) {
  252. page_view->select_all();
  253. });
  254. TRY(context_menu->try_add_action(*select_all_function));
  255. page_view->on_context_menu_request = [&](auto& screen_position) {
  256. copy_action->set_enabled(!page_view->selected_text().is_empty());
  257. context_menu->popup(screen_position);
  258. };
  259. bool set_start_page = false;
  260. if (start_page) {
  261. if (section != 0) {
  262. // > Help [section] [name]
  263. String path = String::formatted("/usr/share/man/man{}/{}.md", section, start_page);
  264. history.push(path);
  265. open_page(path);
  266. set_start_page = true;
  267. } else if (URL url = URL::create_with_url_or_path(start_page); url.is_valid() && url.path().ends_with(".md")) {
  268. // > Help [/path/to/documentation/file.md]
  269. history.push(url.path());
  270. open_page(url.path());
  271. set_start_page = true;
  272. } else {
  273. // > Help [query]
  274. // First, see if we can find the page by name
  275. char const* sections[] = {
  276. "1",
  277. "2",
  278. "3",
  279. "4",
  280. "5",
  281. "6",
  282. "7",
  283. "8"
  284. };
  285. for (auto s : sections) {
  286. String path = String::formatted("/usr/share/man/man{}/{}.md", s, start_page);
  287. if (Core::File::exists(path)) {
  288. history.push(path);
  289. open_page(path);
  290. set_start_page = true;
  291. break;
  292. }
  293. }
  294. // No match, so treat the input as a search query
  295. if (!set_start_page) {
  296. left_tab_bar->set_active_widget(search_view);
  297. search_box->set_text(start_page);
  298. if (auto* model = search_list_view->model()) {
  299. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  300. search_model.set_filter_term(search_box->text());
  301. }
  302. }
  303. }
  304. }
  305. if (!set_start_page)
  306. go_home_action->activate();
  307. auto statusbar = TRY(widget->try_add<GUI::Statusbar>());
  308. app->on_action_enter = [&statusbar](GUI::Action const& action) {
  309. statusbar->set_override_text(action.status_tip());
  310. };
  311. app->on_action_leave = [&statusbar](GUI::Action const&) {
  312. statusbar->set_override_text({});
  313. };
  314. page_view->on_link_hover = [&](URL const& url) {
  315. if (url.is_valid())
  316. statusbar->set_text(url.to_string());
  317. else
  318. statusbar->set_text({});
  319. };
  320. window->set_focused_widget(left_tab_bar);
  321. window->show();
  322. return app->exec();
  323. }