main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. int main(int argc, char* argv[])
  53. {
  54. if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
  55. perror("pledge");
  56. return 1;
  57. }
  58. auto app = GUI::Application::construct(argc, argv);
  59. if (pledge("stdio shared_buffer accept rpath unix", nullptr) < 0) {
  60. perror("pledge");
  61. return 1;
  62. }
  63. if (unveil("/res", "r") < 0) {
  64. perror("unveil");
  65. return 1;
  66. }
  67. if (unveil("/usr/share/man", "r") < 0) {
  68. perror("unveil");
  69. return 1;
  70. }
  71. if (unveil("/tmp/portal/launch", "rw") < 0) {
  72. perror("unveil");
  73. return 1;
  74. }
  75. if (unveil("/tmp/portal/webcontent", "rw") < 0) {
  76. perror("unveil");
  77. return 1;
  78. }
  79. unveil(nullptr, nullptr);
  80. const char* start_page = nullptr;
  81. Core::ArgsParser args_parser;
  82. args_parser.add_positional_argument(start_page, "Page to open at launch", "page", Core::ArgsParser::Required::No);
  83. args_parser.parse(argc, argv);
  84. auto app_icon = GUI::Icon::default_icon("app-help");
  85. auto window = GUI::Window::construct();
  86. window->set_icon(app_icon.bitmap_for_size(16));
  87. window->set_title("Help");
  88. window->resize(570, 500);
  89. auto& widget = window->set_main_widget<GUI::Widget>();
  90. widget.set_layout<GUI::VerticalBoxLayout>();
  91. widget.set_fill_with_background_color(true);
  92. widget.layout()->set_spacing(2);
  93. auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
  94. auto& toolbar = toolbar_container.add<GUI::ToolBar>();
  95. auto& splitter = widget.add<GUI::HorizontalSplitter>();
  96. auto model = ManualModel::create();
  97. auto& left_tab_bar = splitter.add<GUI::TabWidget>();
  98. auto& tree_view_container = left_tab_bar.add_tab<GUI::Widget>("Browse");
  99. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  100. tree_view_container.layout()->set_margins({ 4, 4, 4, 4 });
  101. auto& tree_view = tree_view_container.add<GUI::TreeView>();
  102. auto& search_view = left_tab_bar.add_tab<GUI::Widget>("Search");
  103. search_view.set_layout<GUI::VerticalBoxLayout>();
  104. search_view.layout()->set_margins({ 4, 4, 4, 4 });
  105. auto& search_box = search_view.add<GUI::TextBox>();
  106. auto& search_list_view = search_view.add<GUI::ListView>();
  107. search_box.set_fixed_height(20);
  108. search_box.set_placeholder("Search...");
  109. search_box.on_change = [&] {
  110. if (auto model = search_list_view.model()) {
  111. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  112. search_model.set_filter_term(search_box.text());
  113. search_model.update();
  114. }
  115. };
  116. search_list_view.set_model(GUI::FilteringProxyModel::construct(model));
  117. search_list_view.model()->update();
  118. tree_view.set_model(model);
  119. left_tab_bar.set_fixed_width(200);
  120. auto& page_view = splitter.add<Web::OutOfProcessWebView>();
  121. History history;
  122. RefPtr<GUI::Action> go_back_action;
  123. RefPtr<GUI::Action> go_forward_action;
  124. auto update_actions = [&]() {
  125. go_back_action->set_enabled(history.can_go_back());
  126. go_forward_action->set_enabled(history.can_go_forward());
  127. };
  128. auto open_page = [&](const String& path) {
  129. if (path.is_null()) {
  130. window->set_title("Help");
  131. page_view.load_empty_document();
  132. return;
  133. }
  134. auto source_result = model->page_view(path);
  135. if (source_result.is_error()) {
  136. GUI::MessageBox::show(window, source_result.error().string(), "Failed to open man page", GUI::MessageBox::Type::Error);
  137. return;
  138. }
  139. auto source = source_result.value();
  140. String html;
  141. {
  142. auto md_document = Markdown::Document::parse(source);
  143. ASSERT(md_document);
  144. html = md_document->render_to_html();
  145. }
  146. auto url = URL::create_with_file_protocol(path);
  147. page_view.load_html(html, url);
  148. auto tree_view_index = model->index_from_path(path);
  149. if (tree_view_index.has_value())
  150. tree_view.expand_tree(tree_view_index.value().parent());
  151. String page_and_section = model->page_and_section(tree_view_index.value());
  152. window->set_title(String::formatted("{} - Help", page_and_section));
  153. };
  154. tree_view.on_selection_change = [&] {
  155. String path = model->page_path(tree_view.selection().first());
  156. history.push(path);
  157. update_actions();
  158. open_page(path);
  159. };
  160. tree_view.on_toggle = [&](const GUI::ModelIndex& index, const bool open) {
  161. 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 = [&](auto index) {
  172. if (!index.is_valid())
  173. return;
  174. if (auto model = search_list_view.model()) {
  175. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  176. index = search_model.map(index);
  177. } else {
  178. page_view.load_empty_document();
  179. return;
  180. }
  181. String path = model->page_path(index);
  182. if (path.is_null()) {
  183. page_view.load_empty_document();
  184. return;
  185. }
  186. tree_view.selection().clear();
  187. tree_view.selection().add(index);
  188. history.push(path);
  189. update_actions();
  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 = model->index_from_path(path);
  203. if (tree_view_index.has_value()) {
  204. dbgln("Found path _{}_ in 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. update_actions();
  210. open_page(path);
  211. };
  212. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  213. history.go_back();
  214. update_actions();
  215. open_page(history.current());
  216. });
  217. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  218. history.go_forward();
  219. update_actions();
  220. open_page(history.current());
  221. });
  222. go_back_action->set_enabled(false);
  223. go_forward_action->set_enabled(false);
  224. auto go_home_action = GUI::CommonActions::make_go_home_action([&](auto&) {
  225. String path = "/usr/share/man/man7/Help-index.md";
  226. history.push(path);
  227. update_actions();
  228. open_page(path);
  229. });
  230. toolbar.add_action(*go_back_action);
  231. toolbar.add_action(*go_forward_action);
  232. toolbar.add_action(*go_home_action);
  233. auto menubar = GUI::MenuBar::construct();
  234. auto& app_menu = menubar->add_menu("Help");
  235. app_menu.add_action(GUI::CommonActions::make_about_action("Help", app_icon, window));
  236. app_menu.add_separator();
  237. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  238. GUI::Application::the()->quit();
  239. }));
  240. auto& go_menu = menubar->add_menu("Go");
  241. go_menu.add_action(*go_back_action);
  242. go_menu.add_action(*go_forward_action);
  243. go_menu.add_action(*go_home_action);
  244. app->set_menubar(move(menubar));
  245. if (start_page) {
  246. URL url = URL::create_with_url_or_path(start_page);
  247. if (url.is_valid() && url.path().ends_with(".md")) {
  248. history.push(url.path());
  249. update_actions();
  250. open_page(url.path());
  251. } else {
  252. left_tab_bar.set_active_widget(&search_view);
  253. search_box.set_text(start_page);
  254. if (auto model = search_list_view.model()) {
  255. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  256. search_model.set_filter_term(search_box.text());
  257. }
  258. }
  259. } else {
  260. go_home_action->activate();
  261. }
  262. window->set_focused_widget(&left_tab_bar);
  263. window->show();
  264. return app->exec();
  265. }