main.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/InProcessWebView.h>
  50. #include <LibWeb/Layout/LayoutNode.h>
  51. #include <libgen.h>
  52. #include <stdio.h>
  53. #include <string.h>
  54. int main(int argc, char* argv[])
  55. {
  56. if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
  57. perror("pledge");
  58. return 1;
  59. }
  60. auto app = GUI::Application::construct(argc, argv);
  61. if (pledge("stdio shared_buffer accept rpath unix", nullptr) < 0) {
  62. perror("pledge");
  63. return 1;
  64. }
  65. if (unveil("/res", "r") < 0) {
  66. perror("unveil");
  67. return 1;
  68. }
  69. if (unveil("/usr/share/man", "r") < 0) {
  70. perror("unveil");
  71. return 1;
  72. }
  73. if (unveil("/tmp/portal/launch", "rw") < 0) {
  74. perror("unveil");
  75. return 1;
  76. }
  77. unveil(nullptr, nullptr);
  78. const char* term_to_search_for_at_launch = nullptr;
  79. Core::ArgsParser args_parser;
  80. args_parser.add_positional_argument(term_to_search_for_at_launch, "Term to search for at launch", "term", Core::ArgsParser::Required::No);
  81. args_parser.parse(argc, argv);
  82. auto app_icon = GUI::Icon::default_icon("app-help");
  83. auto window = GUI::Window::construct();
  84. window->set_icon(app_icon.bitmap_for_size(16));
  85. window->set_title("Help");
  86. window->resize(570, 500);
  87. auto& widget = window->set_main_widget<GUI::Widget>();
  88. widget.set_layout<GUI::VerticalBoxLayout>();
  89. widget.set_fill_with_background_color(true);
  90. widget.layout()->set_spacing(2);
  91. auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
  92. auto& toolbar = toolbar_container.add<GUI::ToolBar>();
  93. auto& splitter = widget.add<GUI::HorizontalSplitter>();
  94. auto model = ManualModel::create();
  95. auto& left_tab_bar = splitter.add<GUI::TabWidget>();
  96. auto& tree_view_container = left_tab_bar.add_tab<GUI::Widget>("Tree");
  97. tree_view_container.set_layout<GUI::VerticalBoxLayout>();
  98. tree_view_container.layout()->set_margins({ 4, 4, 4, 4 });
  99. auto& tree_view = tree_view_container.add<GUI::TreeView>();
  100. auto& search_view = left_tab_bar.add_tab<GUI::Widget>("Search");
  101. search_view.set_layout<GUI::VerticalBoxLayout>();
  102. search_view.layout()->set_margins({ 4, 4, 4, 4 });
  103. auto& search_box = search_view.add<GUI::TextBox>();
  104. auto& search_list_view = search_view.add<GUI::ListView>();
  105. search_box.set_preferred_size(0, 20);
  106. search_box.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  107. search_box.set_placeholder("Search...");
  108. search_box.on_change = [&] {
  109. if (auto model = search_list_view.model()) {
  110. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  111. search_model.set_filter_term(search_box.text());
  112. search_model.update();
  113. }
  114. };
  115. search_list_view.set_model(GUI::FilteringProxyModel::construct(model));
  116. search_list_view.model()->update();
  117. tree_view.set_model(model);
  118. left_tab_bar.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  119. left_tab_bar.set_preferred_size(200, 500);
  120. auto& page_view = splitter.add<Web::InProcessWebView>();
  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. page_view.set_document(nullptr);
  131. return;
  132. }
  133. auto source_result = model->page_view(path);
  134. if (source_result.is_error()) {
  135. GUI::MessageBox::show(window, strerror(source_result.error()), "Failed to open man page", GUI::MessageBox::Type::Error);
  136. return;
  137. }
  138. auto source = source_result.value();
  139. String html;
  140. {
  141. auto md_document = Markdown::Document::parse(source);
  142. ASSERT(md_document);
  143. html = md_document->render_to_html();
  144. }
  145. page_view.load_html(html, URL::create_with_file_protocol(path));
  146. String page_and_section = model->page_and_section(tree_view.selection().first());
  147. window->set_title(String::formatted("{} - Help", page_and_section));
  148. };
  149. tree_view.on_selection_change = [&] {
  150. String path = model->page_path(tree_view.selection().first());
  151. if (path.is_null()) {
  152. page_view.set_document(nullptr);
  153. window->set_title("Help");
  154. return;
  155. }
  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.set_document(nullptr);
  179. return;
  180. }
  181. String path = model->page_path(index);
  182. if (path.is_null()) {
  183. page_view.set_document(nullptr);
  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. toolbar.add_action(*go_back_action);
  225. toolbar.add_action(*go_forward_action);
  226. auto menubar = GUI::MenuBar::construct();
  227. auto& app_menu = menubar->add_menu("Help");
  228. app_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  229. GUI::AboutDialog::show("Help", app_icon.bitmap_for_size(32), window);
  230. }));
  231. app_menu.add_separator();
  232. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  233. GUI::Application::the()->quit();
  234. }));
  235. auto& go_menu = menubar->add_menu("Go");
  236. go_menu.add_action(*go_back_action);
  237. go_menu.add_action(*go_forward_action);
  238. app->set_menubar(move(menubar));
  239. if (term_to_search_for_at_launch) {
  240. left_tab_bar.set_active_widget(&search_view);
  241. search_box.set_text(term_to_search_for_at_launch);
  242. if (auto model = search_list_view.model()) {
  243. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  244. search_model.set_filter_term(search_box.text());
  245. }
  246. }
  247. window->set_focused_widget(&left_tab_bar);
  248. window->show();
  249. return app->exec();
  250. }