main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. app->deferred_invoke([&, path] {
  142. auto tree_view_index = manual_model->index_from_path(path);
  143. if (tree_view_index.has_value()) {
  144. tree_view->expand_tree(tree_view_index.value().parent());
  145. tree_view->selection().set(tree_view_index.value());
  146. String page_and_section = manual_model->page_and_section(tree_view_index.value());
  147. window->set_title(String::formatted("{} - Help", page_and_section));
  148. } else {
  149. window->set_title("Help");
  150. }
  151. });
  152. };
  153. tree_view->on_selection_change = [&] {
  154. String path = manual_model->page_path(tree_view->selection().first());
  155. if (path.is_null())
  156. return;
  157. history.push(path);
  158. open_page(path);
  159. };
  160. tree_view->on_toggle = [&](GUI::ModelIndex const& index, bool open) {
  161. manual_model->update_section_node_on_toggle(index, open);
  162. };
  163. auto open_external = [&](auto& url) {
  164. if (!Desktop::Launcher::open(url)) {
  165. GUI::MessageBox::show(window,
  166. String::formatted("The link to '{}' could not be opened.", url),
  167. "Failed to open link",
  168. GUI::MessageBox::Type::Error);
  169. }
  170. };
  171. search_list_view->on_selection_change = [&] {
  172. auto const& index = search_list_view->selection().first();
  173. if (!index.is_valid())
  174. return;
  175. auto* view_model = search_list_view->model();
  176. if (!view_model) {
  177. page_view->load_empty_document();
  178. return;
  179. }
  180. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(view_model);
  181. auto const& mapped_index = search_model.map(index);
  182. String path = manual_model->page_path(mapped_index);
  183. if (path.is_null()) {
  184. page_view->load_empty_document();
  185. return;
  186. }
  187. tree_view->selection().clear();
  188. tree_view->selection().add(mapped_index);
  189. history.push(path);
  190. open_page(path);
  191. };
  192. page_view->on_link_click = [&](auto& url, auto&, unsigned) {
  193. if (url.protocol() != "file") {
  194. open_external(url);
  195. return;
  196. }
  197. auto path = Core::File::real_path_for(url.path());
  198. if (!path.starts_with("/usr/share/man/")) {
  199. open_external(url);
  200. return;
  201. }
  202. auto tree_view_index = manual_model->index_from_path(path);
  203. if (tree_view_index.has_value()) {
  204. dbgln("Found path _{}_ in manual_model at index {}", path, tree_view_index.value());
  205. tree_view->selection().set(tree_view_index.value());
  206. return;
  207. }
  208. history.push(path);
  209. open_page(path);
  210. };
  211. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  212. history.go_back();
  213. open_page(history.current());
  214. });
  215. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  216. history.go_forward();
  217. open_page(history.current());
  218. });
  219. go_back_action->set_enabled(false);
  220. go_forward_action->set_enabled(false);
  221. auto go_home_action = GUI::CommonActions::make_go_home_action([&](auto&) {
  222. String path = "/usr/share/man/man7/Help-index.md";
  223. history.push(path);
  224. open_page(path);
  225. });
  226. (void)TRY(toolbar->try_add_action(*go_back_action));
  227. (void)TRY(toolbar->try_add_action(*go_forward_action));
  228. (void)TRY(toolbar->try_add_action(*go_home_action));
  229. auto file_menu = TRY(window->try_add_menu("&File"));
  230. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
  231. GUI::Application::the()->quit();
  232. })));
  233. auto go_menu = TRY(window->try_add_menu("&Go"));
  234. TRY(go_menu->try_add_action(*go_back_action));
  235. TRY(go_menu->try_add_action(*go_forward_action));
  236. TRY(go_menu->try_add_action(*go_home_action));
  237. auto help_menu = TRY(window->try_add_menu("&Help"));
  238. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Help", app_icon, window)));
  239. auto context_menu = TRY(GUI::Menu::try_create());
  240. TRY(context_menu->try_add_action(*go_back_action));
  241. TRY(context_menu->try_add_action(*go_forward_action));
  242. TRY(context_menu->try_add_action(*go_home_action));
  243. TRY(context_menu->try_add_separator());
  244. RefPtr<GUI::Action> copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
  245. auto selected_text = page_view->selected_text();
  246. if (!selected_text.is_empty())
  247. GUI::Clipboard::the().set_plain_text(selected_text);
  248. });
  249. TRY(context_menu->try_add_action(*copy_action));
  250. RefPtr<GUI::Action> select_all_function = GUI::CommonActions::make_select_all_action([&](auto&) {
  251. page_view->select_all();
  252. });
  253. TRY(context_menu->try_add_action(*select_all_function));
  254. page_view->on_context_menu_request = [&](auto& screen_position) {
  255. copy_action->set_enabled(!page_view->selected_text().is_empty());
  256. context_menu->popup(screen_position);
  257. };
  258. bool set_start_page = false;
  259. if (start_page) {
  260. if (section != 0) {
  261. // > Help [section] [name]
  262. String path = String::formatted("/usr/share/man/man{}/{}.md", section, start_page);
  263. history.push(path);
  264. open_page(path);
  265. set_start_page = true;
  266. } else if (URL url = URL::create_with_url_or_path(start_page); url.is_valid() && url.path().ends_with(".md")) {
  267. // > Help [/path/to/documentation/file.md]
  268. history.push(url.path());
  269. open_page(url.path());
  270. set_start_page = true;
  271. } else {
  272. // > Help [query]
  273. // First, see if we can find the page by name
  274. char const* sections[] = {
  275. "1",
  276. "2",
  277. "3",
  278. "4",
  279. "5",
  280. "6",
  281. "7",
  282. "8"
  283. };
  284. for (auto s : sections) {
  285. String path = String::formatted("/usr/share/man/man{}/{}.md", s, start_page);
  286. if (Core::File::exists(path)) {
  287. history.push(path);
  288. open_page(path);
  289. set_start_page = true;
  290. }
  291. }
  292. // No match, so treat the input as a search query
  293. if (!set_start_page) {
  294. left_tab_bar->set_active_widget(search_view);
  295. search_box->set_text(start_page);
  296. if (auto* model = search_list_view->model()) {
  297. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  298. search_model.set_filter_term(search_box->text());
  299. }
  300. }
  301. }
  302. }
  303. if (!set_start_page)
  304. go_home_action->activate();
  305. auto statusbar = TRY(widget->try_add<GUI::Statusbar>());
  306. app->on_action_enter = [&statusbar](GUI::Action const& action) {
  307. statusbar->set_override_text(action.status_tip());
  308. };
  309. app->on_action_leave = [&statusbar](GUI::Action const&) {
  310. statusbar->set_override_text({});
  311. };
  312. page_view->on_link_hover = [&](URL const& url) {
  313. if (url.is_valid())
  314. statusbar->set_text(url.to_string());
  315. else
  316. statusbar->set_text({});
  317. };
  318. window->set_focused_widget(left_tab_bar);
  319. window->show();
  320. return app->exec();
  321. }