Userland: Filter out unsupported file types in open dialogs in more apps

This commit is contained in:
Karol Kosek 2023-05-28 17:57:02 +02:00 committed by Sam Atkins
parent 27011cf55d
commit 8bd68198d6
Notes: sideshowbarker 2024-07-16 23:57:20 +09:00
10 changed files with 69 additions and 16 deletions

View file

@ -378,7 +378,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto& file_menu = window->add_menu("&File"_short_string);
file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
auto response = FileSystemAccessClient::Client::the().open_file(window);
FileSystemAccessClient::OpenFileOptions options {
.allowed_file_types = { { GUI::FileTypeFilter { "Object Files", { { "obj" } } }, GUI::FileTypeFilter::all_files() } },
};
auto response = FileSystemAccessClient::Client::the().open_file(window, options);
if (response.is_error())
return;

View file

@ -92,15 +92,17 @@ ErrorOr<size_t> CertificateStoreModel::add(Vector<Certificate> const& certificat
ErrorOr<void> CertificateStoreWidget::import_pem()
{
auto fsac_result = FileSystemAccessClient::Client::the().open_file(window(), { .window_title = "Import"sv });
FileSystemAccessClient::OpenFileOptions options {
.window_title = "Import"sv,
.allowed_file_types = Vector {
GUI::FileTypeFilter { "Certificate Files", { { "pem", "crt" } } },
},
};
auto fsac_result = FileSystemAccessClient::Client::the().open_file(window(), options);
if (fsac_result.is_error())
return {};
auto fsac_file = fsac_result.release_value();
auto filename = fsac_file.filename();
if (!(filename.ends_with_bytes(".pem"sv) || filename.ends_with_bytes(".crt"sv)))
return Error::from_string_view("File is not a .pem or .crt file."sv);
auto data = TRY(fsac_file.release_stream()->read_until_eof());
auto count = TRY(m_root_ca_model->add(TRY(DefaultRootCACertificates::parse_pem_root_certificate_authorities(data))));

View file

@ -121,7 +121,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
// Actions
auto open_action = GUI::CommonActions::make_open_action(
[&](auto&) {
auto result = FileSystemAccessClient::Client::the().open_file(window, { .window_title = "Open Image"sv });
FileSystemAccessClient::OpenFileOptions options {
.window_title = "Open Image"sv,
.allowed_file_types = Vector { GUI::FileTypeFilter::image_files(), GUI::FileTypeFilter::all_files() },
};
auto result = FileSystemAccessClient::Client::the().open_file(window, options);
if (result.is_error())
return;

View file

@ -210,7 +210,13 @@ ErrorOr<void> PDFViewerWidget::initialize_menubar(GUI::Window& window)
{
auto file_menu = TRY(window.try_add_menu("&File"_short_string));
TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {
auto response = FileSystemAccessClient::Client::the().open_file(&window);
FileSystemAccessClient::OpenFileOptions options {
.allowed_file_types = Vector {
GUI::FileTypeFilter { "PDF Files", { { "pdf" } } },
GUI::FileTypeFilter::all_files(),
},
};
auto response = FileSystemAccessClient::Client::the().open_file(&window, options);
if (!response.is_error())
open_file(response.value().filename(), response.value().release_stream());
})));

View file

@ -192,7 +192,9 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
});
m_open_image_action = GUI::CommonActions::make_open_action([&](auto&) {
auto response = FileSystemAccessClient::Client::the().open_file(&window);
auto image_files = GUI::FileTypeFilter::image_files();
image_files.extensions->append("pp");
auto response = FileSystemAccessClient::Client::the().open_file(&window, { .allowed_file_types = Vector { image_files, GUI::FileTypeFilter::all_files() } });
if (response.is_error())
return;
open_image(response.release_value());
@ -455,7 +457,14 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
})));
TRY(m_edit_menu->try_add_action(GUI::Action::create(
"&Load Color Palette...", g_icon_bag.load_color_palette, [&](auto&) {
auto response = FileSystemAccessClient::Client::the().open_file(&window, { .window_title = "Load Color Palette"sv });
FileSystemAccessClient::OpenFileOptions options {
.window_title = "Load Color Palette"sv,
.allowed_file_types = Vector {
{ "Palette Files", { { "palette" } } },
GUI::FileTypeFilter::all_files(),
},
};
auto response = FileSystemAccessClient::Client::the().open_file(&window, options);
if (response.is_error())
return;

View file

@ -57,7 +57,10 @@ ErrorOr<void> PresenterWidget::initialize_menubar()
// Set up the menu bar.
auto file_menu = TRY(window->try_add_menu("&File"_short_string));
auto open_action = GUI::CommonActions::make_open_action([this](auto&) {
auto response = FileSystemAccessClient::Client::the().open_file(this->window());
FileSystemAccessClient::OpenFileOptions options {
.allowed_file_types = { { GUI::FileTypeFilter { "Presentation Files", { { "presenter" } } }, GUI::FileTypeFilter::all_files() } },
};
auto response = FileSystemAccessClient::Client::the().open_file(this->window(), options);
if (response.is_error())
return;
this->set_file(response.value().filename());

View file

@ -128,14 +128,26 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, Vector<NonnullR
if (!request_close())
return;
auto response = FileSystemAccessClient::Client::the().open_file(window());
FileSystemAccessClient::OpenFileOptions options {
.allowed_file_types = Vector {
{ "Spreadsheets", { { "sheets", "csv" } } },
GUI::FileTypeFilter::all_files(),
},
};
auto response = FileSystemAccessClient::Client::the().open_file(window(), options);
if (response.is_error())
return;
load_file(response.value().filename(), response.value().stream());
});
m_import_action = GUI::Action::create("Import Sheets...", [&](auto&) {
auto response = FileSystemAccessClient::Client::the().open_file(window());
FileSystemAccessClient::OpenFileOptions options {
.allowed_file_types = Vector {
{ "Spreadsheets", { { "sheets", "csv" } } },
GUI::FileTypeFilter::all_files(),
},
};
auto response = FileSystemAccessClient::Client::the().open_file(window(), options);
if (response.is_error())
return;

View file

@ -242,7 +242,12 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {
if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
return;
auto response = FileSystemAccessClient::Client::the().open_file(&window, { .window_title = "Select Theme"sv, .path = "/res/themes"sv });
FileSystemAccessClient::OpenFileOptions options {
.window_title = "Select Theme"sv,
.path = "/res/themes"sv,
.allowed_file_types = Vector { { "Theme Files", { { "ini" } } }, GUI::FileTypeFilter::all_files() },
};
auto response = FileSystemAccessClient::Client::the().open_file(&window, options);
if (response.is_error())
return;
auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());

View file

@ -384,7 +384,10 @@ ErrorOr<void> VideoPlayerWidget::initialize_menubar(GUI::Window& window)
// File menu
auto file_menu = TRY(window.try_add_menu("&File"_short_string));
TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {
auto response = FileSystemAccessClient::Client::the().open_file(&window);
FileSystemAccessClient::OpenFileOptions options {
.allowed_file_types = { { GUI::FileTypeFilter { "Video Files", { { "mkv", "webm" } } }, GUI::FileTypeFilter::all_files() } },
};
auto response = FileSystemAccessClient::Client::the().open_file(&window, options);
if (response.is_error())
return;

View file

@ -99,7 +99,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(game_menu->try_add_separator());
TRY(game_menu->try_add_action(GUI::Action::create("&Import PGN...", { Mod_Ctrl, Key_O }, [&](auto&) {
auto result = FileSystemAccessClient::Client::the().open_file(window);
FileSystemAccessClient::OpenFileOptions options {
.allowed_file_types = Vector {
GUI::FileTypeFilter { "PGN Files", { { "pgn" } } },
GUI::FileTypeFilter::all_files(),
}
};
auto result = FileSystemAccessClient::Client::the().open_file(window, options);
if (result.is_error())
return;