main.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/AboutDialog.h>
  33. #include <LibGUI/Action.h>
  34. #include <LibGUI/Application.h>
  35. #include <LibGUI/BoxLayout.h>
  36. #include <LibGUI/FilteringProxyModel.h>
  37. #include <LibGUI/ListView.h>
  38. #include <LibGUI/Menu.h>
  39. #include <LibGUI/MenuBar.h>
  40. #include <LibGUI/MessageBox.h>
  41. #include <LibGUI/Splitter.h>
  42. #include <LibGUI/TabWidget.h>
  43. #include <LibGUI/TextBox.h>
  44. #include <LibGUI/ToolBar.h>
  45. #include <LibGUI/ToolBarContainer.h>
  46. #include <LibGUI/TreeView.h>
  47. #include <LibGUI/Window.h>
  48. #include <LibMarkdown/Document.h>
  49. #include <LibWeb/OutOfProcessWebView.h>
  50. #include <libgen.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. int main(int argc, char* argv[])
  54. {
  55. if (pledge("stdio shared_buffer 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 shared_buffer 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* term_to_search_for_at_launch = nullptr;
  82. Core::ArgsParser args_parser;
  83. args_parser.add_positional_argument(term_to_search_for_at_launch, "Term to search for at launch", "term", 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. 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, strerror(source_result.error()), "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. page_view.load_html(html, URL::create_with_file_protocol(path));
  147. String page_and_section = model->page_and_section(tree_view.selection().first());
  148. window->set_title(String::formatted("{} - Help", page_and_section));
  149. };
  150. tree_view.on_selection_change = [&] {
  151. String path = model->page_path(tree_view.selection().first());
  152. if (path.is_null()) {
  153. page_view.load_empty_document();
  154. window->set_title("Help");
  155. return;
  156. }
  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. toolbar.add_action(*go_back_action);
  226. toolbar.add_action(*go_forward_action);
  227. auto menubar = GUI::MenuBar::construct();
  228. auto& app_menu = menubar->add_menu("Help");
  229. app_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  230. GUI::AboutDialog::show("Help", app_icon.bitmap_for_size(32), window);
  231. }));
  232. app_menu.add_separator();
  233. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  234. GUI::Application::the()->quit();
  235. }));
  236. auto& go_menu = menubar->add_menu("Go");
  237. go_menu.add_action(*go_back_action);
  238. go_menu.add_action(*go_forward_action);
  239. app->set_menubar(move(menubar));
  240. if (term_to_search_for_at_launch) {
  241. left_tab_bar.set_active_widget(&search_view);
  242. search_box.set_text(term_to_search_for_at_launch);
  243. if (auto model = search_list_view.model()) {
  244. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  245. search_model.set_filter_term(search_box.text());
  246. }
  247. } else {
  248. String path = "/usr/share/man/man7/Help-index.md";
  249. history.push(path);
  250. open_page(path);
  251. }
  252. window->set_focused_widget(&left_tab_bar);
  253. window->show();
  254. return app->exec();
  255. }