main.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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::create(arguments));
  20. Config::pledge_domain("TextEditor");
  21. app->set_config_domain("TextEditor"_string);
  22. auto preview_mode = "auto"sv;
  23. StringView file_to_edit;
  24. Core::ArgsParser parser;
  25. parser.add_option(preview_mode, "Preview mode, one of 'none', 'html', 'markdown', 'auto'", "preview-mode", '\0', "mode");
  26. parser.add_positional_argument(file_to_edit, "File to edit, with optional starting line and column number", "file[:line[:column]]", Core::ArgsParser::Required::No);
  27. parser.parse(arguments);
  28. TRY(Core::System::unveil("/res", "r"));
  29. TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
  30. TRY(Core::System::unveil("/tmp/session/%sid/portal/webcontent", "rw"));
  31. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  32. TRY(Core::System::unveil(nullptr, nullptr));
  33. auto app_icon = GUI::Icon::default_icon("app-text-editor"sv);
  34. auto window = GUI::Window::construct();
  35. window->resize(640, 400);
  36. auto text_widget = TRY(window->set_main_widget<MainWidget>());
  37. text_widget->editor().set_focus(true);
  38. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  39. if (text_widget->request_close())
  40. return GUI::Window::CloseRequestDecision::Close;
  41. return GUI::Window::CloseRequestDecision::StayOpen;
  42. };
  43. if (preview_mode == "auto") {
  44. text_widget->set_auto_detect_preview_mode(true);
  45. } else if (preview_mode == "markdown") {
  46. text_widget->set_preview_mode(MainWidget::PreviewMode::Markdown);
  47. } else if (preview_mode == "html") {
  48. text_widget->set_preview_mode(MainWidget::PreviewMode::HTML);
  49. } else if (preview_mode == "none") {
  50. text_widget->set_preview_mode(MainWidget::PreviewMode::None);
  51. } else {
  52. warnln("Invalid mode '{}'", preview_mode);
  53. return 1;
  54. }
  55. TRY(text_widget->initialize_menubar(*window));
  56. text_widget->update_title();
  57. window->show();
  58. window->set_icon(app_icon.bitmap_for_size(16));
  59. if (!file_to_edit.is_empty()) {
  60. auto filename = TRY(String::from_utf8(file_to_edit));
  61. FileArgument parsed_argument(filename);
  62. FileSystemAccessClient::Client::the().set_silence_errors(FileSystemAccessClient::ErrorFlag::NoEntries);
  63. auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, parsed_argument.filename().to_deprecated_string());
  64. if (response.is_error()) {
  65. if (response.error().code() == ENOENT)
  66. text_widget->open_nonexistent_file(parsed_argument.filename().to_deprecated_string());
  67. } else {
  68. TRY(text_widget->read_file(response.value().filename(), response.value().stream()));
  69. text_widget->editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0));
  70. }
  71. text_widget->update_title();
  72. FileSystemAccessClient::Client::the().set_silence_errors(FileSystemAccessClient::ErrorFlag::None);
  73. }
  74. text_widget->update_statusbar();
  75. return app->exec();
  76. }