main.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 <LibCore/File.h>
  29. #include <LibGUI/GAboutDialog.h>
  30. #include <LibGUI/GAction.h>
  31. #include <LibGUI/GApplication.h>
  32. #include <LibGUI/GBoxLayout.h>
  33. #include <LibGUI/GMenu.h>
  34. #include <LibGUI/GMenuBar.h>
  35. #include <LibGUI/GMessageBox.h>
  36. #include <LibGUI/GSplitter.h>
  37. #include <LibGUI/GTextEditor.h>
  38. #include <LibGUI/GToolBar.h>
  39. #include <LibGUI/GTreeView.h>
  40. #include <LibGUI/GWindow.h>
  41. #include <LibHTML/HtmlView.h>
  42. #include <LibHTML/Layout/LayoutNode.h>
  43. #include <LibHTML/Parser/CSSParser.h>
  44. #include <LibHTML/Parser/HTMLParser.h>
  45. #include <LibMarkdown/MDDocument.h>
  46. #include <libgen.h>
  47. #include <stdio.h>
  48. int main(int argc, char* argv[])
  49. {
  50. if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
  51. perror("pledge");
  52. return 1;
  53. }
  54. GUI::Application app(argc, argv);
  55. if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
  56. perror("pledge");
  57. return 1;
  58. }
  59. if (unveil("/res", "r") < 0) {
  60. perror("unveil");
  61. return 1;
  62. }
  63. if (unveil("/usr/share/man", "r") < 0) {
  64. perror("unveil");
  65. return 1;
  66. }
  67. unveil(nullptr, nullptr);
  68. auto window = GUI::Window::construct();
  69. window->set_title("Help");
  70. window->set_rect(300, 200, 570, 500);
  71. auto widget = GUI::Widget::construct();
  72. widget->set_layout(make<GUI::VerticalBoxLayout>());
  73. widget->layout()->set_spacing(0);
  74. auto toolbar = GUI::ToolBar::construct(widget);
  75. auto splitter = GUI::HorizontalSplitter::construct(widget);
  76. auto model = ManualModel::create();
  77. auto tree_view = GUI::TreeView::construct(splitter);
  78. tree_view->set_model(model);
  79. tree_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  80. tree_view->set_preferred_size(200, 500);
  81. auto html_view = HtmlView::construct(splitter);
  82. History history;
  83. RefPtr<GUI::Action> go_back_action;
  84. RefPtr<GUI::Action> go_forward_action;
  85. auto update_actions = [&]() {
  86. go_back_action->set_enabled(history.can_go_back());
  87. go_forward_action->set_enabled(history.can_go_forward());
  88. };
  89. auto open_page = [&](const String& path) {
  90. if (path.is_null()) {
  91. html_view->set_document(nullptr);
  92. return;
  93. }
  94. dbg() << "Opening page at " << path;
  95. auto file = Core::File::construct();
  96. file->set_filename(path);
  97. if (!file->open(Core::IODevice::OpenMode::ReadOnly)) {
  98. int saved_errno = errno;
  99. GUI::MessageBox::show(strerror(saved_errno), "Failed to open man page", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
  100. return;
  101. }
  102. auto buffer = file->read_all();
  103. StringView source { (const char*)buffer.data(), (size_t)buffer.size() };
  104. MDDocument md_document;
  105. bool success = md_document.parse(source);
  106. ASSERT(success);
  107. String html = md_document.render_to_html();
  108. auto html_document = parse_html_document(html);
  109. html_view->set_document(html_document);
  110. String page_and_section = model->page_and_section(tree_view->selection().first());
  111. window->set_title(String::format("Help: %s", page_and_section.characters()));
  112. };
  113. tree_view->on_selection_change = [&] {
  114. String path = model->page_path(tree_view->selection().first());
  115. if (path.is_null()) {
  116. html_view->set_document(nullptr);
  117. return;
  118. }
  119. history.push(path);
  120. update_actions();
  121. open_page(path);
  122. };
  123. html_view->on_link_click = [&](const String& href) {
  124. char* current_path = strdup(history.current().characters());
  125. char* dir_path = dirname(current_path);
  126. char* path = realpath(String::format("%s/%s", dir_path, href.characters()).characters(), nullptr);
  127. free(current_path);
  128. history.push(path);
  129. update_actions();
  130. open_page(path);
  131. free(path);
  132. };
  133. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  134. history.go_back();
  135. update_actions();
  136. open_page(history.current());
  137. });
  138. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  139. history.go_forward();
  140. update_actions();
  141. open_page(history.current());
  142. });
  143. go_back_action->set_enabled(false);
  144. go_forward_action->set_enabled(false);
  145. toolbar->add_action(*go_back_action);
  146. toolbar->add_action(*go_forward_action);
  147. auto menubar = make<GUI::MenuBar>();
  148. auto app_menu = GUI::Menu::construct("Help");
  149. app_menu->add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  150. GUI::AboutDialog::show("Help", Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"), window);
  151. }));
  152. app_menu->add_separator();
  153. app_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  154. GUI::Application::the().quit(0);
  155. }));
  156. menubar->add_menu(move(app_menu));
  157. auto go_menu = GUI::Menu::construct("Go");
  158. go_menu->add_action(*go_back_action);
  159. go_menu->add_action(*go_forward_action);
  160. menubar->add_menu(move(go_menu));
  161. app.set_menubar(move(menubar));
  162. window->set_main_widget(widget);
  163. window->set_focused_widget(tree_view);
  164. window->show();
  165. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"));
  166. return app.exec();
  167. }