main.cpp 14 KB

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