main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "History.h"
  27. #include "ManualModel.h"
  28. #include <AK/URL.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/File.h>
  31. #include <LibDesktop/Launcher.h>
  32. #include <LibGUI/Action.h>
  33. #include <LibGUI/Application.h>
  34. #include <LibGUI/BoxLayout.h>
  35. #include <LibGUI/FilteringProxyModel.h>
  36. #include <LibGUI/ListView.h>
  37. #include <LibGUI/Menu.h>
  38. #include <LibGUI/Menubar.h>
  39. #include <LibGUI/MessageBox.h>
  40. #include <LibGUI/Splitter.h>
  41. #include <LibGUI/TabWidget.h>
  42. #include <LibGUI/TextBox.h>
  43. #include <LibGUI/Toolbar.h>
  44. #include <LibGUI/ToolbarContainer.h>
  45. #include <LibGUI/TreeView.h>
  46. #include <LibGUI/Window.h>
  47. #include <LibMarkdown/Document.h>
  48. #include <LibWeb/OutOfProcessWebView.h>
  49. #include <libgen.h>
  50. #include <stdio.h>
  51. #include <string.h>
  52. #include <unistd.h>
  53. int main(int argc, char* argv[])
  54. {
  55. if (pledge("stdio recvfd sendfd accept rpath unix cpath fattr", nullptr) < 0) {
  56. perror("pledge");
  57. return 1;
  58. }
  59. auto app = GUI::Application::construct(argc, argv);
  60. if (pledge("stdio recvfd sendfd accept rpath unix", nullptr) < 0) {
  61. perror("pledge");
  62. return 1;
  63. }
  64. if (unveil("/res", "r") < 0) {
  65. perror("unveil");
  66. return 1;
  67. }
  68. if (unveil("/usr/share/man", "r") < 0) {
  69. perror("unveil");
  70. return 1;
  71. }
  72. if (unveil("/tmp/portal/launch", "rw") < 0) {
  73. perror("unveil");
  74. return 1;
  75. }
  76. if (unveil("/tmp/portal/webcontent", "rw") < 0) {
  77. perror("unveil");
  78. return 1;
  79. }
  80. unveil(nullptr, nullptr);
  81. const char* start_page = nullptr;
  82. Core::ArgsParser args_parser;
  83. args_parser.add_positional_argument(start_page, "Page to open at launch", "page", Core::ArgsParser::Required::No);
  84. args_parser.parse(argc, argv);
  85. auto app_icon = GUI::Icon::default_icon("app-help");
  86. auto window = GUI::Window::construct();
  87. window->set_icon(app_icon.bitmap_for_size(16));
  88. window->set_title("Help");
  89. window->resize(570, 500);
  90. auto& widget = window->set_main_widget<GUI::Widget>();
  91. widget.set_layout<GUI::VerticalBoxLayout>();
  92. widget.set_fill_with_background_color(true);
  93. widget.layout()->set_spacing(2);
  94. auto& toolbar_container = widget.add<GUI::ToolbarContainer>();
  95. auto& toolbar = toolbar_container.add<GUI::Toolbar>();
  96. auto& splitter = widget.add<GUI::HorizontalSplitter>();
  97. auto model = ManualModel::create();
  98. auto& left_tab_bar = splitter.add<GUI::TabWidget>();
  99. auto& tree_view_container = left_tab_bar.add_tab<GUI::Widget>("Browse");
  100. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  101. tree_view_container.layout()->set_margins({ 4, 4, 4, 4 });
  102. auto& tree_view = tree_view_container.add<GUI::TreeView>();
  103. auto& search_view = left_tab_bar.add_tab<GUI::Widget>("Search");
  104. search_view.set_layout<GUI::VerticalBoxLayout>();
  105. search_view.layout()->set_margins({ 4, 4, 4, 4 });
  106. auto& search_box = search_view.add<GUI::TextBox>();
  107. auto& search_list_view = search_view.add<GUI::ListView>();
  108. search_box.set_fixed_height(20);
  109. search_box.set_placeholder("Search...");
  110. search_box.on_change = [&] {
  111. if (auto model = search_list_view.model()) {
  112. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  113. search_model.set_filter_term(search_box.text());
  114. search_model.update();
  115. }
  116. };
  117. search_list_view.set_model(GUI::FilteringProxyModel::construct(model));
  118. search_list_view.model()->update();
  119. tree_view.set_model(model);
  120. left_tab_bar.set_fixed_width(200);
  121. auto& page_view = splitter.add<Web::OutOfProcessWebView>();
  122. History history;
  123. RefPtr<GUI::Action> go_back_action;
  124. RefPtr<GUI::Action> go_forward_action;
  125. auto update_actions = [&]() {
  126. go_back_action->set_enabled(history.can_go_back());
  127. go_forward_action->set_enabled(history.can_go_forward());
  128. };
  129. auto open_page = [&](const String& path) {
  130. if (path.is_null()) {
  131. window->set_title("Help");
  132. page_view.load_empty_document();
  133. return;
  134. }
  135. auto source_result = model->page_view(path);
  136. if (source_result.is_error()) {
  137. GUI::MessageBox::show(window, source_result.error().string(), "Failed to open man page", GUI::MessageBox::Type::Error);
  138. return;
  139. }
  140. auto source = source_result.value();
  141. String html;
  142. {
  143. auto md_document = Markdown::Document::parse(source);
  144. VERIFY(md_document);
  145. html = md_document->render_to_html();
  146. }
  147. auto url = URL::create_with_file_protocol(path);
  148. page_view.load_html(html, url);
  149. auto tree_view_index = model->index_from_path(path);
  150. if (tree_view_index.has_value())
  151. tree_view.expand_tree(tree_view_index.value().parent());
  152. String page_and_section = model->page_and_section(tree_view_index.value());
  153. window->set_title(String::formatted("{} - Help", page_and_section));
  154. };
  155. tree_view.on_selection_change = [&] {
  156. String path = model->page_path(tree_view.selection().first());
  157. history.push(path);
  158. update_actions();
  159. open_page(path);
  160. };
  161. tree_view.on_toggle = [&](const GUI::ModelIndex& index, const bool open) {
  162. 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 = [&](auto index) {
  173. if (!index.is_valid())
  174. return;
  175. if (auto model = search_list_view.model()) {
  176. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  177. index = search_model.map(index);
  178. } else {
  179. page_view.load_empty_document();
  180. return;
  181. }
  182. String path = model->page_path(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(index);
  189. history.push(path);
  190. update_actions();
  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 = model->index_from_path(path);
  204. if (tree_view_index.has_value()) {
  205. dbgln("Found path _{}_ in 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. update_actions();
  211. open_page(path);
  212. };
  213. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  214. history.go_back();
  215. update_actions();
  216. open_page(history.current());
  217. });
  218. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  219. history.go_forward();
  220. update_actions();
  221. open_page(history.current());
  222. });
  223. go_back_action->set_enabled(false);
  224. go_forward_action->set_enabled(false);
  225. auto go_home_action = GUI::CommonActions::make_go_home_action([&](auto&) {
  226. String path = "/usr/share/man/man7/Help-index.md";
  227. history.push(path);
  228. update_actions();
  229. open_page(path);
  230. });
  231. toolbar.add_action(*go_back_action);
  232. toolbar.add_action(*go_forward_action);
  233. toolbar.add_action(*go_home_action);
  234. auto menubar = GUI::Menubar::construct();
  235. auto& app_menu = menubar->add_menu("File");
  236. app_menu.add_action(GUI::CommonActions::make_about_action("Help", app_icon, window));
  237. app_menu.add_separator();
  238. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  239. GUI::Application::the()->quit();
  240. }));
  241. auto& go_menu = menubar->add_menu("Go");
  242. go_menu.add_action(*go_back_action);
  243. go_menu.add_action(*go_forward_action);
  244. go_menu.add_action(*go_home_action);
  245. window->set_menubar(move(menubar));
  246. if (start_page) {
  247. URL url = URL::create_with_url_or_path(start_page);
  248. if (url.is_valid() && url.path().ends_with(".md")) {
  249. history.push(url.path());
  250. update_actions();
  251. open_page(url.path());
  252. } else {
  253. left_tab_bar.set_active_widget(&search_view);
  254. search_box.set_text(start_page);
  255. if (auto model = search_list_view.model()) {
  256. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  257. search_model.set_filter_term(search_box.text());
  258. }
  259. }
  260. } else {
  261. go_home_action->activate();
  262. }
  263. window->set_focused_widget(&left_tab_bar);
  264. window->show();
  265. return app->exec();
  266. }