1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 05:00:46 +00:00

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.
This commit is contained in:
Sam Atkins 2024-01-23 16:05:59 +00:00 committed by Sam Atkins
parent 5a99a6afb4
commit 44ca55aaf8
23 changed files with 50 additions and 53 deletions

View File

@ -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<int> 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<int> 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();
}

View File

@ -168,7 +168,7 @@ NonnullRefPtr<GUI::Action> 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;

View File

@ -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<GUI::Action> create_save_as_action();

View File

@ -30,8 +30,8 @@ class EventManager {
public:
static OwnPtr<EventManager> 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<void> save(FileSystemAccessClient::File& file);
ErrorOr<void> load_file(FileSystemAccessClient::File& file);
@ -53,7 +53,7 @@ private:
Vector<Event> m_events;
bool m_dirty { false };
String m_current_filename;
ByteString m_current_filename;
};
}

View File

@ -110,7 +110,7 @@ ErrorOr<void> 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);
};

View File

@ -167,7 +167,7 @@ ErrorOr<void> 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&) {

View File

@ -596,13 +596,12 @@ void HexEditorWidget::update_title()
window()->set_title(builder.to_byte_string());
}
void HexEditorWidget::open_file(String const& filename, NonnullOwnPtr<Core::File> file)
void HexEditorWidget::open_file(ByteString const& filename, NonnullOwnPtr<Core::File> 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()

View File

@ -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<Core::File>);
void open_file(ByteString const& filename, NonnullOwnPtr<Core::File>);
ErrorOr<void> initialize_menubar(GUI::Window&);
bool request_close();

View File

@ -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&)

View File

@ -106,7 +106,7 @@ ErrorOr<int> 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<int> 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<int> 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<int> 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();
}

View File

@ -63,13 +63,13 @@ ErrorOr<int> 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<void> {
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()));

View File

@ -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();
}

View File

@ -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<void> MainWidget::create_default_image()

View File

@ -136,7 +136,7 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, Vector<NonnullR
auto response = FileSystemAccessClient::Client::the().open_file(window(), options);
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());
});
m_import_action = GUI::Action::create("Import Sheets...", [&](auto&) {
@ -150,7 +150,7 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, Vector<NonnullR
if (response.is_error())
return;
import_sheets(response.value().filename(), response.value().stream());
import_sheets(MUST(String::from_byte_string(response.value().filename())), response.value().stream());
});
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
@ -162,7 +162,7 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, Vector<NonnullR
auto response = FileSystemAccessClient::Client::the().request_file(window(), current_filename(), Core::File::OpenMode::Write);
if (response.is_error())
return;
save(response.value().filename(), response.value().stream());
save(MUST(String::from_byte_string(response.value().filename())), response.value().stream());
});
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
@ -170,7 +170,7 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, Vector<NonnullR
auto response = FileSystemAccessClient::Client::the().save_file(window(), name, "sheets");
if (response.is_error())
return;
save(response.value().filename(), response.value().stream());
save(MUST(String::from_byte_string(response.value().filename())), response.value().stream());
update_window_title();
});
@ -733,7 +733,7 @@ ErrorOr<void> 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);

View File

@ -71,7 +71,7 @@ ErrorOr<int> 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();

View File

@ -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<void> MainWidget::read_file(String const& filename, Core::File& file)
ErrorOr<void> 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 {};
}

View File

@ -25,7 +25,7 @@ class MainWidget final : public GUI::Widget {
public:
virtual ~MainWidget() override = default;
ErrorOr<void> read_file(String const& filename, Core::File&);
ErrorOr<void> read_file(ByteString const& filename, Core::File&);
void open_nonexistent_file(ByteString const& path);
bool request_close();

View File

@ -349,9 +349,8 @@ void MainWidget::set_path(ByteString path)
update_title();
}
void MainWidget::save_to_file(String const& filename_string, NonnullOwnPtr<Core::File> file)
void MainWidget::save_to_file(ByteString const& filename, NonnullOwnPtr<Core::File> 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<void> MainWidget::load_from_file(String const& filename, NonnullOwnPtr<Core::File> file)
ErrorOr<void> MainWidget::load_from_file(ByteString const& filename, NonnullOwnPtr<Core::File> 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<void> MainWidget::load_from_file(String const& filename, NonnullOwnPtr<C
m_last_modified_time = MonotonicTime::now();
window()->set_modified(false);
GUI::Application::the()->set_most_recently_open_file(filename.to_byte_string());
GUI::Application::the()->set_most_recently_open_file(filename);
return {};
}

View File

@ -86,7 +86,7 @@ public:
ErrorOr<void> initialize_menubar(GUI::Window&);
GUI::Window::CloseRequestDecision request_close();
void update_title();
ErrorOr<void> load_from_file(String const& filename, NonnullOwnPtr<Core::File> file);
ErrorOr<void> load_from_file(ByteString const& filename, NonnullOwnPtr<Core::File> file);
private:
explicit MainWidget(NonnullRefPtr<AlignmentModel>);
@ -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<Core::File> file);
void save_to_file(ByteString const& filename, NonnullOwnPtr<Core::File> file);
ErrorOr<Core::AnonymousBuffer> encode();
void set_path(ByteString);

View File

@ -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();

View File

@ -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<void> MainWidget::initialize_menubar(GUI::Window& window)
@ -147,10 +147,10 @@ ErrorOr<void> 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&) {

View File

@ -145,8 +145,7 @@ void Client::handle_prompt_end(i32 request_id, i32 error, Optional<IPC::File> co
auto file_or_error = [&]() -> ErrorOr<File> {
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());

View File

@ -31,7 +31,7 @@ enum ErrorFlag : u32 {
class Client;
class File {
public:
File(Badge<Client>, NonnullOwnPtr<Core::File> stream, String filename)
File(Badge<Client>, NonnullOwnPtr<Core::File> stream, ByteString filename)
: m_stream(move(stream))
, m_filename(filename)
{
@ -39,11 +39,11 @@ public:
Core::File& stream() const { return *m_stream; }
NonnullOwnPtr<Core::File> release_stream() { return move(m_stream); }
String filename() const { return m_filename; }
ByteString const& filename() const { return m_filename; }
private:
NonnullOwnPtr<Core::File> m_stream;
String m_filename;
ByteString m_filename;
};
using Result = ErrorOr<File>;