Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, networkException <networkexception@serenityos.org>
4 * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
5 * Copyright (c) 2021, Antonio Di Stefano <tonio9681@gmail.com>
6 * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
7 *
8 * SPDX-License-Identifier: BSD-2-Clause
9 */
10
11#include "MainWidget.h"
12#include <LibCore/ArgsParser.h>
13#include <LibCore/DeprecatedFile.h>
14#include <LibCore/System.h>
15#include <LibFileSystemAccessClient/Client.h>
16#include <LibGUI/Application.h>
17#include <LibGUI/Icon.h>
18#include <LibGUI/Menu.h>
19#include <LibGUI/Menubar.h>
20#include <LibGUI/MessageBox.h>
21#include <LibGUI/Window.h>
22#include <LibMain/Main.h>
23
24ErrorOr<int> serenity_main(Main::Arguments arguments)
25{
26 TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath unix"));
27
28 auto app = TRY(GUI::Application::try_create(arguments));
29
30 StringView file_to_edit;
31
32 Core::ArgsParser parser;
33 parser.add_positional_argument(file_to_edit, "Theme file to edit", "file", Core::ArgsParser::Required::No);
34 parser.parse(arguments);
35
36 Optional<DeprecatedString> path = {};
37
38 if (!file_to_edit.is_empty())
39 path = Core::DeprecatedFile::absolute_path(file_to_edit);
40
41 TRY(Core::System::pledge("stdio recvfd sendfd thread rpath unix"));
42 TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
43 TRY(Core::System::unveil("/res", "r"));
44 TRY(Core::System::unveil(nullptr, nullptr));
45
46 auto app_icon = GUI::Icon::default_icon("app-theme-editor"sv);
47 auto window = GUI::Window::construct();
48
49 auto main_widget = TRY(window->set_main_widget<ThemeEditor::MainWidget>());
50
51 if (path.has_value()) {
52 // Note: This is deferred to ensure that the window has already popped and thus proper window stealing can be performed.
53 app->event_loop().deferred_invoke(
54 [&window, &path, &main_widget]() {
55 auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, path.value());
56 if (response.is_error())
57 GUI::MessageBox::show_error(window, DeprecatedString::formatted("Opening \"{}\" failed: {}", path.value(), response.error()));
58 else {
59 auto load_from_file_result = main_widget->load_from_file(response.value().filename(), response.value().release_stream());
60 if (load_from_file_result.is_error())
61 GUI::MessageBox::show_error(window, DeprecatedString::formatted("Loading theme from file has failed: {}", load_from_file_result.error()));
62 }
63 });
64 }
65
66 TRY(main_widget->initialize_menubar(window));
67 main_widget->update_title();
68
69 window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
70 return main_widget->request_close();
71 };
72
73 window->resize(820, 520);
74 window->set_resizable(false);
75 window->show();
76 window->set_icon(app_icon.bitmap_for_size(16));
77 return app->exec();
78}