main.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/System.h>
  11. #include <LibFileSystemAccessClient/Client.h>
  12. #include <LibGUI/Menubar.h>
  13. #include <LibGUI/MessageBox.h>
  14. #include <LibMain/Main.h>
  15. using namespace TextEditor;
  16. ErrorOr<int> serenity_main(Main::Arguments arguments)
  17. {
  18. TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath unix"));
  19. auto app = TRY(GUI::Application::try_create(arguments));
  20. Config::pledge_domain("TextEditor");
  21. auto preview_mode = "auto"sv;
  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(arguments);
  27. TRY(Core::System::unveil("/res", "r"));
  28. TRY(Core::System::unveil("/tmp/user/%uid/portal/launch", "rw"));
  29. TRY(Core::System::unveil("/tmp/user/%uid/portal/webcontent", "rw"));
  30. TRY(Core::System::unveil("/tmp/portal/filesystemaccess", "rw"));
  31. TRY(Core::System::unveil(nullptr, nullptr));
  32. auto app_icon = GUI::Icon::default_icon("app-text-editor"sv);
  33. auto window = TRY(GUI::Window::try_create());
  34. window->resize(640, 400);
  35. auto text_widget = TRY(window->try_set_main_widget<MainWidget>());
  36. text_widget->editor().set_focus(true);
  37. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  38. if (text_widget->request_close())
  39. return GUI::Window::CloseRequestDecision::Close;
  40. return GUI::Window::CloseRequestDecision::StayOpen;
  41. };
  42. if (preview_mode == "auto") {
  43. text_widget->set_auto_detect_preview_mode(true);
  44. } else if (preview_mode == "markdown") {
  45. text_widget->set_preview_mode(MainWidget::PreviewMode::Markdown);
  46. } else if (preview_mode == "html") {
  47. text_widget->set_preview_mode(MainWidget::PreviewMode::HTML);
  48. } else if (preview_mode == "none") {
  49. text_widget->set_preview_mode(MainWidget::PreviewMode::None);
  50. } else {
  51. warnln("Invalid mode '{}'", preview_mode);
  52. return 1;
  53. }
  54. text_widget->initialize_menubar(*window);
  55. window->show();
  56. window->set_icon(app_icon.bitmap_for_size(16));
  57. if (file_to_edit) {
  58. FileArgument parsed_argument(file_to_edit);
  59. auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, parsed_argument.filename());
  60. if (response.is_error()) {
  61. if (response.error().code() == ENOENT)
  62. text_widget->open_nonexistent_file(parsed_argument.filename());
  63. else
  64. return 1;
  65. } else {
  66. if (!text_widget->read_file(*response.value()))
  67. return 1;
  68. text_widget->editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0));
  69. }
  70. }
  71. text_widget->update_title();
  72. text_widget->update_statusbar();
  73. return app->exec();
  74. }