main.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "FileArgument.h"
  7. #include "MainWidget.h"
  8. #include <LibCore/ArgsParser.h>
  9. #include <LibCore/File.h>
  10. #include <LibCore/StandardPaths.h>
  11. #include <LibGUI/Menubar.h>
  12. #include <LibGUI/MessageBox.h>
  13. using namespace TextEditor;
  14. int main(int argc, char** argv)
  15. {
  16. if (pledge("stdio recvfd sendfd thread rpath cpath wpath unix", nullptr) < 0) {
  17. perror("pledge");
  18. return 1;
  19. }
  20. auto app = GUI::Application::construct(argc, argv);
  21. char const* preview_mode = "auto";
  22. char const* file_to_edit = nullptr;
  23. Core::ArgsParser parser;
  24. parser.add_option(preview_mode, "Preview mode, one of 'none', 'html', 'markdown', 'auto'", "preview-mode", '\0', "mode");
  25. parser.add_positional_argument(file_to_edit, "File to edit, with optional starting line and column number", "file[:line[:column]]", Core::ArgsParser::Required::No);
  26. parser.parse(argc, argv);
  27. String file_to_edit_full_path;
  28. if (file_to_edit) {
  29. FileArgument parsed_argument(file_to_edit);
  30. file_to_edit_full_path = Core::File::real_path_for(parsed_argument.filename());
  31. dbgln("unveil for: {}", file_to_edit_full_path);
  32. if (unveil(file_to_edit_full_path.characters(), "rwc") < 0) {
  33. perror("unveil");
  34. return 1;
  35. }
  36. }
  37. if (unveil(Core::StandardPaths::config_directory().characters(), "rw") < 0) {
  38. perror("unveil");
  39. return 1;
  40. }
  41. if (unveil("/res", "r") < 0) {
  42. perror("unveil");
  43. return 1;
  44. }
  45. if (unveil("/tmp/portal/launch", "rw") < 0) {
  46. perror("unveil");
  47. return 1;
  48. }
  49. if (unveil("/tmp/portal/webcontent", "rw") < 0) {
  50. perror("unveil");
  51. return 1;
  52. }
  53. if (unveil("/tmp/portal/filesystemaccess", "rw") < 0) {
  54. perror("unveil");
  55. return 1;
  56. }
  57. unveil(nullptr, nullptr);
  58. StringView preview_mode_view = preview_mode;
  59. auto app_icon = GUI::Icon::default_icon("app-text-editor");
  60. auto window = GUI::Window::construct();
  61. window->resize(640, 400);
  62. auto& text_widget = window->set_main_widget<MainWidget>();
  63. text_widget.editor().set_focus(true);
  64. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  65. if (text_widget.request_close())
  66. return GUI::Window::CloseRequestDecision::Close;
  67. return GUI::Window::CloseRequestDecision::StayOpen;
  68. };
  69. if (preview_mode_view == "auto") {
  70. text_widget.set_auto_detect_preview_mode(true);
  71. } else if (preview_mode_view == "markdown") {
  72. text_widget.set_preview_mode(MainWidget::PreviewMode::Markdown);
  73. } else if (preview_mode_view == "html") {
  74. text_widget.set_preview_mode(MainWidget::PreviewMode::HTML);
  75. } else if (preview_mode_view == "none") {
  76. text_widget.set_preview_mode(MainWidget::PreviewMode::None);
  77. } else {
  78. warnln("Invalid mode '{}'", preview_mode);
  79. return 1;
  80. }
  81. auto menubar = GUI::Menubar::construct();
  82. text_widget.initialize_menubar(menubar);
  83. window->set_menubar(menubar);
  84. if (file_to_edit) {
  85. // A file name was passed, parse any possible line and column numbers included.
  86. FileArgument parsed_argument(file_to_edit);
  87. auto file = Core::File::open(file_to_edit_full_path, Core::OpenMode::ReadOnly);
  88. if (file.is_error()) {
  89. GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", file_to_edit_full_path, file.error()));
  90. return 1;
  91. }
  92. if (!text_widget.read_file_and_close(file.value()->leak_fd(), file_to_edit_full_path))
  93. return 1;
  94. text_widget.editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0));
  95. }
  96. text_widget.update_title();
  97. window->show();
  98. window->set_icon(app_icon.bitmap_for_size(16));
  99. window->set_menubar(menubar);
  100. return app->exec();
  101. }