main.cpp 3.4 KB

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