main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <LibConfig/Client.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/File.h>
  11. #include <LibCore/StandardPaths.h>
  12. #include <LibGUI/Menubar.h>
  13. #include <LibGUI/MessageBox.h>
  14. using namespace TextEditor;
  15. int main(int argc, char** argv)
  16. {
  17. if (pledge("stdio recvfd sendfd thread rpath cpath wpath unix", nullptr) < 0) {
  18. perror("pledge");
  19. return 1;
  20. }
  21. auto app = GUI::Application::construct(argc, argv);
  22. Config::pledge_domains("TextEditor");
  23. char const* preview_mode = "auto";
  24. char const* file_to_edit = nullptr;
  25. Core::ArgsParser parser;
  26. parser.add_option(preview_mode, "Preview mode, one of 'none', 'html', 'markdown', 'auto'", "preview-mode", '\0', "mode");
  27. parser.add_positional_argument(file_to_edit, "File to edit, with optional starting line and column number", "file[:line[:column]]", Core::ArgsParser::Required::No);
  28. parser.parse(argc, argv);
  29. String file_to_edit_full_path;
  30. if (file_to_edit) {
  31. FileArgument parsed_argument(file_to_edit);
  32. file_to_edit_full_path = Core::File::absolute_path(parsed_argument.filename());
  33. VERIFY(!file_to_edit_full_path.is_empty());
  34. if (Core::File::exists(parsed_argument.filename())) {
  35. dbgln("unveil for: {}", file_to_edit_full_path);
  36. if (unveil(file_to_edit_full_path.characters(), "r") < 0) {
  37. perror("unveil");
  38. return 1;
  39. }
  40. }
  41. }
  42. if (unveil("/res", "r") < 0) {
  43. perror("unveil");
  44. return 1;
  45. }
  46. if (unveil("/tmp/portal/launch", "rw") < 0) {
  47. perror("unveil");
  48. return 1;
  49. }
  50. if (unveil("/tmp/portal/webcontent", "rw") < 0) {
  51. perror("unveil");
  52. return 1;
  53. }
  54. if (unveil("/tmp/portal/filesystemaccess", "rw") < 0) {
  55. perror("unveil");
  56. return 1;
  57. }
  58. unveil(nullptr, nullptr);
  59. StringView preview_mode_view = preview_mode;
  60. auto app_icon = GUI::Icon::default_icon("app-text-editor");
  61. auto window = GUI::Window::construct();
  62. window->resize(640, 400);
  63. auto& text_widget = window->set_main_widget<MainWidget>();
  64. text_widget.editor().set_focus(true);
  65. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  66. if (text_widget.request_close())
  67. return GUI::Window::CloseRequestDecision::Close;
  68. return GUI::Window::CloseRequestDecision::StayOpen;
  69. };
  70. if (preview_mode_view == "auto") {
  71. text_widget.set_auto_detect_preview_mode(true);
  72. } else if (preview_mode_view == "markdown") {
  73. text_widget.set_preview_mode(MainWidget::PreviewMode::Markdown);
  74. } else if (preview_mode_view == "html") {
  75. text_widget.set_preview_mode(MainWidget::PreviewMode::HTML);
  76. } else if (preview_mode_view == "none") {
  77. text_widget.set_preview_mode(MainWidget::PreviewMode::None);
  78. } else {
  79. warnln("Invalid mode '{}'", preview_mode);
  80. return 1;
  81. }
  82. text_widget.initialize_menubar(*window);
  83. if (file_to_edit) {
  84. // A file name was passed, parse any possible line and column numbers included.
  85. FileArgument parsed_argument(file_to_edit);
  86. if (Core::File::exists(file_to_edit_full_path)) {
  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. } else {
  96. text_widget.open_nonexistent_file(file_to_edit_full_path);
  97. }
  98. }
  99. text_widget.update_title();
  100. window->show();
  101. window->set_icon(app_icon.bitmap_for_size(16));
  102. return app->exec();
  103. }