main.cpp 6.6 KB

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