From 44ca55aaf8fb625963c316823576bcb1203354bf Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 23 Jan 2024 16:05:59 +0000 Subject: [PATCH] LibFileSystemAccessClient+Userland: Return file paths as ByteStrings Where it was straightforward to do so, I've updated the users to also use ByteStrings for their file paths, but most of them have a temporary String::from_byte_string() call instead. --- Userland/Applications/3DFileViewer/main.cpp | 6 +++--- Userland/Applications/Calendar/CalendarWidget.cpp | 2 +- Userland/Applications/Calendar/CalendarWidget.h | 2 +- Userland/Applications/Calendar/EventManager.h | 6 +++--- .../DisplaySettings/BackgroundSettingsWidget.cpp | 2 +- Userland/Applications/FontEditor/MainWidget.cpp | 2 +- Userland/Applications/HexEditor/HexEditorWidget.cpp | 7 +++---- Userland/Applications/HexEditor/HexEditorWidget.h | 2 +- Userland/Applications/ImageViewer/ViewWidget.cpp | 2 +- Userland/Applications/ImageViewer/main.cpp | 8 ++++---- Userland/Applications/Magnifier/main.cpp | 4 ++-- Userland/Applications/PixelPaint/ImageEditor.cpp | 2 +- Userland/Applications/PixelPaint/MainWidget.cpp | 4 ++-- .../Applications/Spreadsheet/SpreadsheetWidget.cpp | 10 +++++----- Userland/Applications/Spreadsheet/main.cpp | 2 +- Userland/Applications/TextEditor/MainWidget.cpp | 6 +++--- Userland/Applications/TextEditor/MainWidget.h | 2 +- Userland/Applications/ThemeEditor/MainWidget.cpp | 11 +++++------ Userland/Applications/ThemeEditor/MainWidget.h | 4 ++-- .../Applications/VideoPlayer/VideoPlayerWidget.cpp | 2 +- Userland/DevTools/GMLPlayground/MainWidget.cpp | 8 ++++---- .../Libraries/LibFileSystemAccessClient/Client.cpp | 3 +-- Userland/Libraries/LibFileSystemAccessClient/Client.h | 6 +++--- 23 files changed, 50 insertions(+), 53 deletions(-) diff --git a/Userland/Applications/3DFileViewer/main.cpp b/Userland/Applications/3DFileViewer/main.cpp index a7fd039b0a..ac7fd55fa9 100644 --- a/Userland/Applications/3DFileViewer/main.cpp +++ b/Userland/Applications/3DFileViewer/main.cpp @@ -153,7 +153,7 @@ void GLContextWidget::drop_event(GUI::DropEvent& event) auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), url.serialize_path()); if (response.is_error()) return; - load_file(response.value().filename(), response.value().release_stream()); + load_file(MUST(String::from_byte_string(response.value().filename())), response.value().release_stream()); } } @@ -389,7 +389,7 @@ ErrorOr serenity_main(Main::Arguments arguments) return; auto file = response.release_value(); - widget->load_file(file.filename(), file.release_stream()); + widget->load_file(MUST(String::from_byte_string(file.filename())), file.release_stream()); })); file_menu->add_separator(); file_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) { @@ -583,7 +583,7 @@ ErrorOr serenity_main(Main::Arguments arguments) GUI::MessageBox::show(window, ByteString::formatted("Opening \"{}\" failed: {}", filename, strerror(errno)), "Error"sv, GUI::MessageBox::Type::Error); return 1; } - widget->load_file(file.value().filename(), file.value().release_stream()); + widget->load_file(TRY(String::from_byte_string(file.value().filename())), file.value().release_stream()); return app->exec(); } diff --git a/Userland/Applications/Calendar/CalendarWidget.cpp b/Userland/Applications/Calendar/CalendarWidget.cpp index ada723d75a..722ca9412f 100644 --- a/Userland/Applications/Calendar/CalendarWidget.cpp +++ b/Userland/Applications/Calendar/CalendarWidget.cpp @@ -168,7 +168,7 @@ NonnullRefPtr CalendarWidget::create_save_action(GUI::Action& save_ return; } - auto response = FileSystemAccessClient::Client::the().request_file(window(), current_filename().to_byte_string(), Core::File::OpenMode::Write); + auto response = FileSystemAccessClient::Client::the().request_file(window(), current_filename(), Core::File::OpenMode::Write); if (response.is_error()) return; diff --git a/Userland/Applications/Calendar/CalendarWidget.h b/Userland/Applications/Calendar/CalendarWidget.h index fdf184ff6f..1712e47fb6 100644 --- a/Userland/Applications/Calendar/CalendarWidget.h +++ b/Userland/Applications/Calendar/CalendarWidget.h @@ -34,7 +34,7 @@ private: void create_on_tile_doubleclick(); - String const& current_filename() const { return m_event_calendar->event_manager().current_filename(); } + ByteString const& current_filename() const { return m_event_calendar->event_manager().current_filename(); } void create_on_events_change(); NonnullRefPtr create_save_as_action(); diff --git a/Userland/Applications/Calendar/EventManager.h b/Userland/Applications/Calendar/EventManager.h index 8f9aecf2bb..08895cbefc 100644 --- a/Userland/Applications/Calendar/EventManager.h +++ b/Userland/Applications/Calendar/EventManager.h @@ -30,8 +30,8 @@ class EventManager { public: static OwnPtr create(); - String const& current_filename() const { return m_current_filename; } - void set_filename(String const& filename) { m_current_filename = filename; } + ByteString const& current_filename() const { return m_current_filename; } + void set_filename(ByteString filename) { m_current_filename = move(filename); } ErrorOr save(FileSystemAccessClient::File& file); ErrorOr load_file(FileSystemAccessClient::File& file); @@ -53,7 +53,7 @@ private: Vector m_events; bool m_dirty { false }; - String m_current_filename; + ByteString m_current_filename; }; } diff --git a/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp b/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp index 44f375a46e..2a936a368d 100644 --- a/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp @@ -110,7 +110,7 @@ ErrorOr BackgroundSettingsWidget::create_frame() if (response.is_error()) return; m_wallpaper_view->selection().clear(); - m_monitor_widget->set_wallpaper(response.release_value().filename()); + m_monitor_widget->set_wallpaper(MUST(String::from_byte_string(response.release_value().filename()))); m_background_settings_changed = true; set_modified(true); }; diff --git a/Userland/Applications/FontEditor/MainWidget.cpp b/Userland/Applications/FontEditor/MainWidget.cpp index 5fd20cd1d4..769e5a30ba 100644 --- a/Userland/Applications/FontEditor/MainWidget.cpp +++ b/Userland/Applications/FontEditor/MainWidget.cpp @@ -167,7 +167,7 @@ ErrorOr MainWidget::create_actions() if (auto result = save_file(file.filename(), file.release_stream()); result.is_error()) show_error(result.release_error(), "Saving"sv, file.filename()); else - GUI::Application::the()->set_most_recently_open_file(file.filename().to_byte_string()); + GUI::Application::the()->set_most_recently_open_file(file.filename()); }); m_cut_action = GUI::CommonActions::make_cut_action([this](auto&) { diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index aa8a5993b0..8140dd68cd 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -596,13 +596,12 @@ void HexEditorWidget::update_title() window()->set_title(builder.to_byte_string()); } -void HexEditorWidget::open_file(String const& filename, NonnullOwnPtr file) +void HexEditorWidget::open_file(ByteString const& filename, NonnullOwnPtr file) { window()->set_modified(false); m_editor->open_file(move(file)); - auto filename_byte_string = filename.to_byte_string(); - set_path(filename_byte_string); - GUI::Application::the()->set_most_recently_open_file(filename_byte_string); + set_path(filename); + GUI::Application::the()->set_most_recently_open_file(filename); } bool HexEditorWidget::request_close() diff --git a/Userland/Applications/HexEditor/HexEditorWidget.h b/Userland/Applications/HexEditor/HexEditorWidget.h index 53d6dc26f1..52a66b7d7e 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.h +++ b/Userland/Applications/HexEditor/HexEditorWidget.h @@ -24,7 +24,7 @@ class HexEditorWidget final : public GUI::Widget { C_OBJECT_ABSTRACT(HexEditorWidget) public: virtual ~HexEditorWidget() override = default; - void open_file(String const& filename, NonnullOwnPtr); + void open_file(ByteString const& filename, NonnullOwnPtr); ErrorOr initialize_menubar(GUI::Window&); bool request_close(); diff --git a/Userland/Applications/ImageViewer/ViewWidget.cpp b/Userland/Applications/ImageViewer/ViewWidget.cpp index 4b6e616656..ec4cadf384 100644 --- a/Userland/Applications/ImageViewer/ViewWidget.cpp +++ b/Userland/Applications/ImageViewer/ViewWidget.cpp @@ -169,7 +169,7 @@ void ViewWidget::navigate(Directions direction) m_current_index = index; auto value = result.release_value(); - open_file(value.filename(), value.stream()); + open_file(MUST(String::from_byte_string(value.filename())), value.stream()); } void ViewWidget::doubleclick_event(GUI::MouseEvent&) diff --git a/Userland/Applications/ImageViewer/main.cpp b/Userland/Applications/ImageViewer/main.cpp index 3b2c75897c..431ad565dc 100644 --- a/Userland/Applications/ImageViewer/main.cpp +++ b/Userland/Applications/ImageViewer/main.cpp @@ -106,7 +106,7 @@ ErrorOr serenity_main(Main::Arguments arguments) return; auto value = result.release_value(); - widget.open_file(value.filename(), value.stream()); + widget.open_file(MUST(String::from_byte_string(value.filename())), value.stream()); for (size_t i = 1; i < urls.size(); ++i) { Desktop::Launcher::open(URL::create_with_file_scheme(urls[i].serialize_path().characters()), "/bin/ImageViewer"); @@ -130,7 +130,7 @@ ErrorOr serenity_main(Main::Arguments arguments) return; auto value = result.release_value(); - widget.open_file(value.filename(), value.stream()); + widget.open_file(MUST(String::from_byte_string(value.filename())), value.stream()); }); auto delete_action = GUI::CommonActions::make_delete_action( @@ -318,7 +318,7 @@ ErrorOr serenity_main(Main::Arguments arguments) return; auto value = result.release_value(); - widget.open_file(value.filename(), value.stream()); + widget.open_file(MUST(String::from_byte_string(value.filename())), value.stream()); }); file_menu->add_action(quit_action); @@ -380,7 +380,7 @@ ErrorOr serenity_main(Main::Arguments arguments) return 1; auto value = result.release_value(); - widget.open_file(value.filename(), value.stream()); + widget.open_file(MUST(String::from_byte_string(value.filename())), value.stream()); } else { widget.clear(); } diff --git a/Userland/Applications/Magnifier/main.cpp b/Userland/Applications/Magnifier/main.cpp index 1157e24df8..24320cb668 100644 --- a/Userland/Applications/Magnifier/main.cpp +++ b/Userland/Applications/Magnifier/main.cpp @@ -63,13 +63,13 @@ ErrorOr serenity_main(Main::Arguments arguments) auto file_menu = window->add_menu("&File"_string); file_menu->add_action(GUI::CommonActions::make_save_as_action([&](auto&) { - AK::ByteString filename = "file for saving"; + ByteString filename = "file for saving"; auto do_save = [&]() -> ErrorOr { auto response = FileSystemAccessClient::Client::the().save_file(window, "Capture", "png"); if (response.is_error()) return {}; auto file = response.value().release_stream(); - auto path = AK::LexicalPath(response.value().filename().to_byte_string()); + auto path = LexicalPath(response.value().filename()); filename = path.basename(); auto encoded = TRY(dump_bitmap(magnifier->current_bitmap(), path.extension())); diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp index 395ad4f88c..296cdfda6a 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.cpp +++ b/Userland/Applications/PixelPaint/ImageEditor.cpp @@ -776,7 +776,7 @@ void ImageEditor::save_project_as() GUI::MessageBox::show_error(window(), MUST(String::formatted("Could not save {}: {}", file.filename(), result.release_error()))); return; } - set_path(file.filename().to_byte_string()); + set_path(file.filename()); set_loaded_from_image(false); set_unmodified(); } diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp index 850019354a..e5fa6589f7 100644 --- a/Userland/Applications/PixelPaint/MainWidget.cpp +++ b/Userland/Applications/PixelPaint/MainWidget.cpp @@ -1304,11 +1304,11 @@ void MainWidget::open_image(FileSystemAccessClient::File file) auto& image = *m_loader.release_image(); auto& editor = create_new_editor(image); editor.set_loaded_from_image(m_loader.is_raw_image()); - editor.set_path(file.filename().to_byte_string()); + editor.set_path(file.filename()); editor.set_unmodified(); m_layer_list_widget->set_image(&image); m_toolbox->ensure_tool_selection(); - GUI::Application::the()->set_most_recently_open_file(file.filename().to_byte_string()); + GUI::Application::the()->set_most_recently_open_file(file.filename()); } ErrorOr MainWidget::create_default_image() diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp index 2baa5faf1e..1888b2544e 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp +++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp @@ -136,7 +136,7 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, Vector SpreadsheetWidget::initialize_menubar(GUI::Window& window) auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(&window, action.text()); if (response.is_error()) return; - load_file(response.value().filename(), response.value().stream()); + load_file(MUST(String::from_byte_string(response.value().filename())), response.value().stream()); }); file_menu->add_action(*m_quit_action); diff --git a/Userland/Applications/Spreadsheet/main.cpp b/Userland/Applications/Spreadsheet/main.cpp index 9f69f9426f..fbc345d84a 100644 --- a/Userland/Applications/Spreadsheet/main.cpp +++ b/Userland/Applications/Spreadsheet/main.cpp @@ -71,7 +71,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (!filename.is_empty()) { auto file = TRY(FileSystemAccessClient::Client::the().request_file_read_only_approved(window, filename)); - spreadsheet_widget->load_file(file.filename(), file.stream()); + spreadsheet_widget->load_file(TRY(String::from_byte_string(file.filename())), file.stream()); } return app->exec(); diff --git a/Userland/Applications/TextEditor/MainWidget.cpp b/Userland/Applications/TextEditor/MainWidget.cpp index c1f9d4e40d..08ddcd5a1c 100644 --- a/Userland/Applications/TextEditor/MainWidget.cpp +++ b/Userland/Applications/TextEditor/MainWidget.cpp @@ -293,7 +293,7 @@ MainWidget::MainWidget() } set_path(file.filename()); - GUI::Application::the()->set_most_recently_open_file(file.filename().to_byte_string()); + GUI::Application::the()->set_most_recently_open_file(file.filename()); dbgln("Wrote document to {}", file.filename()); }); @@ -799,11 +799,11 @@ void MainWidget::update_title() window()->set_title(builder.to_byte_string()); } -ErrorOr MainWidget::read_file(String const& filename, Core::File& file) +ErrorOr MainWidget::read_file(ByteString const& filename, Core::File& file) { m_editor->set_text(TRY(file.read_until_eof())); set_path(filename); - GUI::Application::the()->set_most_recently_open_file(filename.to_byte_string()); + GUI::Application::the()->set_most_recently_open_file(filename); m_editor->set_focus(true); return {}; } diff --git a/Userland/Applications/TextEditor/MainWidget.h b/Userland/Applications/TextEditor/MainWidget.h index 38c8b5c265..a8f29f558d 100644 --- a/Userland/Applications/TextEditor/MainWidget.h +++ b/Userland/Applications/TextEditor/MainWidget.h @@ -25,7 +25,7 @@ class MainWidget final : public GUI::Widget { public: virtual ~MainWidget() override = default; - ErrorOr read_file(String const& filename, Core::File&); + ErrorOr read_file(ByteString const& filename, Core::File&); void open_nonexistent_file(ByteString const& path); bool request_close(); diff --git a/Userland/Applications/ThemeEditor/MainWidget.cpp b/Userland/Applications/ThemeEditor/MainWidget.cpp index 25039a62b8..1e83a57e21 100644 --- a/Userland/Applications/ThemeEditor/MainWidget.cpp +++ b/Userland/Applications/ThemeEditor/MainWidget.cpp @@ -349,9 +349,8 @@ void MainWidget::set_path(ByteString path) update_title(); } -void MainWidget::save_to_file(String const& filename_string, NonnullOwnPtr file) +void MainWidget::save_to_file(ByteString const& filename, NonnullOwnPtr file) { - auto filename = filename_string.to_byte_string(); auto theme = Core::ConfigFile::open(filename, move(file)).release_value_but_fixme_should_propagate_errors(); #define __ENUMERATE_ALIGNMENT_ROLE(role) theme->write_entry("Alignments", to_string(Gfx::AlignmentRole::role), to_string(m_current_palette.alignment(Gfx::AlignmentRole::role))); @@ -635,15 +634,15 @@ void MainWidget::show_path_picker_dialog(StringView property_display_name, GUI:: path_input.set_text(*result); } -ErrorOr MainWidget::load_from_file(String const& filename, NonnullOwnPtr file) +ErrorOr MainWidget::load_from_file(ByteString const& filename, NonnullOwnPtr file) { - auto config_file = TRY(Core::ConfigFile::open(filename.to_byte_string(), move(file))); + auto config_file = TRY(Core::ConfigFile::open(filename, move(file))); auto theme = TRY(Gfx::load_system_theme(config_file)); VERIFY(theme.is_valid()); auto new_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme)); set_palette(move(new_palette)); - set_path(filename.to_byte_string()); + set_path(filename); #define __ENUMERATE_ALIGNMENT_ROLE(role) \ if (auto alignment_input = m_alignment_inputs[to_underlying(Gfx::AlignmentRole::role)]) \ @@ -677,7 +676,7 @@ ErrorOr MainWidget::load_from_file(String const& filename, NonnullOwnPtrset_modified(false); - GUI::Application::the()->set_most_recently_open_file(filename.to_byte_string()); + GUI::Application::the()->set_most_recently_open_file(filename); return {}; } diff --git a/Userland/Applications/ThemeEditor/MainWidget.h b/Userland/Applications/ThemeEditor/MainWidget.h index b394fe8d61..358ccd7ec1 100644 --- a/Userland/Applications/ThemeEditor/MainWidget.h +++ b/Userland/Applications/ThemeEditor/MainWidget.h @@ -86,7 +86,7 @@ public: ErrorOr initialize_menubar(GUI::Window&); GUI::Window::CloseRequestDecision request_close(); void update_title(); - ErrorOr load_from_file(String const& filename, NonnullOwnPtr file); + ErrorOr load_from_file(ByteString const& filename, NonnullOwnPtr file); private: explicit MainWidget(NonnullRefPtr); @@ -94,7 +94,7 @@ private: virtual void drag_enter_event(GUI::DragEvent&) override; virtual void drop_event(GUI::DropEvent&) override; - void save_to_file(String const& filename, NonnullOwnPtr file); + void save_to_file(ByteString const& filename, NonnullOwnPtr file); ErrorOr encode(); void set_path(ByteString); diff --git a/Userland/Applications/VideoPlayer/VideoPlayerWidget.cpp b/Userland/Applications/VideoPlayer/VideoPlayerWidget.cpp index 72c4ef4eab..dba09d83b4 100644 --- a/Userland/Applications/VideoPlayer/VideoPlayerWidget.cpp +++ b/Userland/Applications/VideoPlayer/VideoPlayerWidget.cpp @@ -125,7 +125,7 @@ void VideoPlayerWidget::open_file(FileSystemAccessClient::File file) return; } - m_path = file.filename(); + m_path = MUST(String::from_byte_string(file.filename())); update_title(); close_file(); diff --git a/Userland/DevTools/GMLPlayground/MainWidget.cpp b/Userland/DevTools/GMLPlayground/MainWidget.cpp index 0664558b13..6d1ee29073 100644 --- a/Userland/DevTools/GMLPlayground/MainWidget.cpp +++ b/Userland/DevTools/GMLPlayground/MainWidget.cpp @@ -126,10 +126,10 @@ void MainWidget::load_file(FileSystemAccessClient::File file) m_editor->set_text(buffer_or_error.release_value()); m_editor->set_focus(true); - m_file_path = file.filename().to_byte_string(); + m_file_path = file.filename(); update_title(); - GUI::Application::the()->set_most_recently_open_file(file.filename().to_byte_string()); + GUI::Application::the()->set_most_recently_open_file(file.filename()); } ErrorOr MainWidget::initialize_menubar(GUI::Window& window) @@ -147,10 +147,10 @@ ErrorOr MainWidget::initialize_menubar(GUI::Window& window) GUI::MessageBox::show(&window, ByteString::formatted("Unable to save file: {}\n"sv, result.release_error()), "Error"sv, GUI::MessageBox::Type::Error); return; } - m_file_path = response.value().filename().to_byte_string(); + m_file_path = response.value().filename(); update_title(); - GUI::Application::the()->set_most_recently_open_file(response.value().filename().to_byte_string()); + GUI::Application::the()->set_most_recently_open_file(response.value().filename()); }); m_save_action = GUI::CommonActions::make_save_action([&](auto&) { diff --git a/Userland/Libraries/LibFileSystemAccessClient/Client.cpp b/Userland/Libraries/LibFileSystemAccessClient/Client.cpp index 0063405890..c73ef7f7c1 100644 --- a/Userland/Libraries/LibFileSystemAccessClient/Client.cpp +++ b/Userland/Libraries/LibFileSystemAccessClient/Client.cpp @@ -145,8 +145,7 @@ void Client::handle_prompt_end(i32 request_id, i32 error, Optional co auto file_or_error = [&]() -> ErrorOr { auto stream = TRY(Core::File::adopt_fd(ipc_file->take_fd(), Core::File::OpenMode::ReadWrite)); - auto filename = TRY(String::from_byte_string(*chosen_file)); - return File({}, move(stream), filename); + return File({}, move(stream), *chosen_file); }(); if (file_or_error.is_error()) { auto maybe_message = String::formatted("{} \"{}\" failed: {}", action, *chosen_file, file_or_error.error()); diff --git a/Userland/Libraries/LibFileSystemAccessClient/Client.h b/Userland/Libraries/LibFileSystemAccessClient/Client.h index d2044c5f7f..b0236df855 100644 --- a/Userland/Libraries/LibFileSystemAccessClient/Client.h +++ b/Userland/Libraries/LibFileSystemAccessClient/Client.h @@ -31,7 +31,7 @@ enum ErrorFlag : u32 { class Client; class File { public: - File(Badge, NonnullOwnPtr stream, String filename) + File(Badge, NonnullOwnPtr stream, ByteString filename) : m_stream(move(stream)) , m_filename(filename) { @@ -39,11 +39,11 @@ public: Core::File& stream() const { return *m_stream; } NonnullOwnPtr release_stream() { return move(m_stream); } - String filename() const { return m_filename; } + ByteString const& filename() const { return m_filename; } private: NonnullOwnPtr m_stream; - String m_filename; + ByteString m_filename; }; using Result = ErrorOr;