main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/Layout/LayoutNode.h>
  50. #include <LibWeb/PageView.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_text("Search...");
  108. search_box.on_focusin = [&] {
  109. if (search_box.text() == "Search...")
  110. search_box.set_text("");
  111. };
  112. search_box.on_change = [&] {
  113. if (auto model = search_list_view.model()) {
  114. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  115. search_model.set_filter_term(search_box.text());
  116. search_model.update();
  117. }
  118. };
  119. search_box.on_focusout = [&] {
  120. if (search_box.text().is_empty()) {
  121. if (auto model = search_list_view.model()) {
  122. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  123. search_model.set_filter_term("");
  124. }
  125. search_box.set_text("Search...");
  126. }
  127. };
  128. search_list_view.set_model(GUI::FilteringProxyModel::construct(model));
  129. search_list_view.model()->update();
  130. tree_view.set_model(model);
  131. left_tab_bar.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  132. left_tab_bar.set_preferred_size(200, 500);
  133. auto& page_view = splitter.add<Web::PageView>();
  134. History history;
  135. RefPtr<GUI::Action> go_back_action;
  136. RefPtr<GUI::Action> go_forward_action;
  137. auto update_actions = [&]() {
  138. go_back_action->set_enabled(history.can_go_back());
  139. go_forward_action->set_enabled(history.can_go_forward());
  140. };
  141. auto open_page = [&](const String& path) {
  142. if (path.is_null()) {
  143. page_view.set_document(nullptr);
  144. return;
  145. }
  146. auto source_result = model->page_view(path);
  147. if (source_result.is_error()) {
  148. GUI::MessageBox::show(window, strerror(source_result.error()), "Failed to open man page", GUI::MessageBox::Type::Error);
  149. return;
  150. }
  151. auto source = source_result.value();
  152. String html;
  153. {
  154. auto md_document = Markdown::Document::parse(source);
  155. ASSERT(md_document);
  156. html = md_document->render_to_html();
  157. }
  158. page_view.load_html(html, URL::create_with_file_protocol(path));
  159. String page_and_section = model->page_and_section(tree_view.selection().first());
  160. window->set_title(String::format("%s - Help", page_and_section.characters()));
  161. };
  162. tree_view.on_selection_change = [&] {
  163. String path = model->page_path(tree_view.selection().first());
  164. if (path.is_null()) {
  165. page_view.set_document(nullptr);
  166. window->set_title("Help");
  167. return;
  168. }
  169. history.push(path);
  170. update_actions();
  171. open_page(path);
  172. };
  173. tree_view.on_toggle = [&](const GUI::ModelIndex& index, const bool open) {
  174. model->update_section_node_on_toggle(index, open);
  175. };
  176. auto open_external = [&](auto& url) {
  177. if (!Desktop::Launcher::open(url)) {
  178. GUI::MessageBox::show(window,
  179. String::format("The link to '%s' could not be opened.", url.to_string().characters()),
  180. "Failed to open link",
  181. GUI::MessageBox::Type::Error);
  182. }
  183. };
  184. search_list_view.on_selection = [&](auto index) {
  185. if (!index.is_valid())
  186. return;
  187. if (auto model = search_list_view.model()) {
  188. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
  189. index = search_model.map(index);
  190. } else {
  191. page_view.set_document(nullptr);
  192. return;
  193. }
  194. String path = model->page_path(index);
  195. if (path.is_null()) {
  196. page_view.set_document(nullptr);
  197. return;
  198. }
  199. tree_view.selection().clear();
  200. tree_view.selection().add(index);
  201. history.push(path);
  202. update_actions();
  203. open_page(path);
  204. };
  205. page_view.on_link_click = [&](auto& url, auto&, unsigned) {
  206. if (url.protocol() != "file") {
  207. open_external(url);
  208. return;
  209. }
  210. auto path = Core::File::real_path_for(url.path());
  211. if (!path.starts_with("/usr/share/man/")) {
  212. open_external(url);
  213. return;
  214. }
  215. auto tree_view_index = model->index_from_path(path);
  216. if (tree_view_index.has_value()) {
  217. dbg() << "Found path _" << path << "_ in model at index " << tree_view_index.value();
  218. tree_view.selection().set(tree_view_index.value());
  219. return;
  220. }
  221. history.push(path);
  222. update_actions();
  223. open_page(path);
  224. };
  225. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  226. history.go_back();
  227. update_actions();
  228. open_page(history.current());
  229. });
  230. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  231. history.go_forward();
  232. update_actions();
  233. open_page(history.current());
  234. });
  235. go_back_action->set_enabled(false);
  236. go_forward_action->set_enabled(false);
  237. toolbar.add_action(*go_back_action);
  238. toolbar.add_action(*go_forward_action);
  239. auto menubar = GUI::MenuBar::construct();
  240. auto& app_menu = menubar->add_menu("Help");
  241. app_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  242. GUI::AboutDialog::show("Help", app_icon.bitmap_for_size(32), window);
  243. }));
  244. app_menu.add_separator();
  245. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  246. GUI::Application::the()->quit();
  247. }));
  248. auto& go_menu = menubar->add_menu("Go");
  249. go_menu.add_action(*go_back_action);
  250. go_menu.add_action(*go_forward_action);
  251. app->set_menubar(move(menubar));
  252. if (term_to_search_for_at_launch) {
  253. left_tab_bar.set_active_widget(&search_view);
  254. search_box.set_text(term_to_search_for_at_launch);
  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. window->set_focused_widget(&left_tab_bar);
  261. window->show();
  262. return app->exec();
  263. }