Serenity Operating System
at master 96 lines 3.5 kB view raw
1/* 2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include "FileArgument.h" 8#include "MainWidget.h" 9#include <LibConfig/Client.h> 10#include <LibCore/ArgsParser.h> 11#include <LibCore/System.h> 12#include <LibFileSystemAccessClient/Client.h> 13#include <LibGUI/Menubar.h> 14#include <LibGUI/MessageBox.h> 15#include <LibMain/Main.h> 16 17using namespace TextEditor; 18 19ErrorOr<int> serenity_main(Main::Arguments arguments) 20{ 21 TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath unix")); 22 23 auto app = TRY(GUI::Application::try_create(arguments)); 24 25 Config::pledge_domain("TextEditor"); 26 27 app->set_config_domain(TRY("TextEditor"_string)); 28 29 auto preview_mode = "auto"sv; 30 StringView file_to_edit; 31 Core::ArgsParser parser; 32 parser.add_option(preview_mode, "Preview mode, one of 'none', 'html', 'markdown', 'auto'", "preview-mode", '\0', "mode"); 33 parser.add_positional_argument(file_to_edit, "File to edit, with optional starting line and column number", "file[:line[:column]]", Core::ArgsParser::Required::No); 34 parser.parse(arguments); 35 36 TRY(Core::System::unveil("/res", "r")); 37 TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw")); 38 TRY(Core::System::unveil("/tmp/session/%sid/portal/webcontent", "rw")); 39 TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw")); 40 TRY(Core::System::unveil(nullptr, nullptr)); 41 42 auto app_icon = GUI::Icon::default_icon("app-text-editor"sv); 43 44 auto window = TRY(GUI::Window::try_create()); 45 window->resize(640, 400); 46 47 auto text_widget = TRY(window->set_main_widget<MainWidget>()); 48 49 text_widget->editor().set_focus(true); 50 51 window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision { 52 if (text_widget->request_close()) 53 return GUI::Window::CloseRequestDecision::Close; 54 return GUI::Window::CloseRequestDecision::StayOpen; 55 }; 56 57 if (preview_mode == "auto") { 58 text_widget->set_auto_detect_preview_mode(true); 59 } else if (preview_mode == "markdown") { 60 text_widget->set_preview_mode(MainWidget::PreviewMode::Markdown); 61 } else if (preview_mode == "html") { 62 text_widget->set_preview_mode(MainWidget::PreviewMode::HTML); 63 } else if (preview_mode == "none") { 64 text_widget->set_preview_mode(MainWidget::PreviewMode::None); 65 } else { 66 warnln("Invalid mode '{}'", preview_mode); 67 return 1; 68 } 69 70 text_widget->initialize_menubar(*window); 71 text_widget->update_title(); 72 73 window->show(); 74 window->set_icon(app_icon.bitmap_for_size(16)); 75 76 if (!file_to_edit.is_empty()) { 77 auto filename = TRY(String::from_utf8(file_to_edit)); 78 FileArgument parsed_argument(filename); 79 auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, parsed_argument.filename().to_deprecated_string()); 80 81 if (response.is_error()) { 82 if (response.error().code() == ENOENT) 83 text_widget->open_nonexistent_file(parsed_argument.filename().to_deprecated_string()); 84 else 85 return 1; 86 } else { 87 TRY(text_widget->read_file(response.value().filename(), response.value().stream())); 88 text_widget->editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0)); 89 } 90 91 text_widget->update_title(); 92 } 93 text_widget->update_statusbar(); 94 95 return app->exec(); 96}