summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMustafa Quraish <mustafaq9@gmail.com>2022-01-16 20:47:11 -0500
committerAndreas Kling <kling@serenityos.org>2022-01-20 10:39:12 +0100
commiteffb19f9966eead7f1d93fecced3b62fb40f30bc (patch)
treed0022abe5c6314ba10da9108cee517ca7e313c6e
parent1c3e93c6e07cd6b7a3a029ae3c1786afaa6b4abc (diff)
downloadserenity-effb19f9966eead7f1d93fecced3b62fb40f30bc.zip
PDFViewer: Use FileSystemAccessClient::try_* APIs
-rw-r--r--Userland/Applications/PDFViewer/PDFViewerWidget.cpp24
-rw-r--r--Userland/Applications/PDFViewer/PDFViewerWidget.h2
-rw-r--r--Userland/Applications/PDFViewer/main.cpp10
3 files changed, 11 insertions, 25 deletions
diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp
index dcc11b4f65..3510f51eac 100644
--- a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp
+++ b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp
@@ -41,15 +41,10 @@ void PDFViewerWidget::initialize_menubar(GUI::Window& window)
{
auto& file_menu = window.add_menu("&File");
file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
- auto response = FileSystemAccessClient::Client::the().open_file(window.window_id());
-
- if (response.error != 0) {
- if (response.error != -1)
- GUI::MessageBox::show_error(&window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
+ auto response = FileSystemAccessClient::Client::the().try_open_file(&window);
+ if (response.is_error())
return;
- }
-
- open_file(*response.fd, *response.chosen_file);
+ open_file(*response.value());
}));
file_menu.add_separator();
file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
@@ -151,19 +146,14 @@ void PDFViewerWidget::create_toolbar()
toolbar.add_action(*m_rotate_clockwise_action);
}
-void PDFViewerWidget::open_file(int fd, String const& path)
+void PDFViewerWidget::open_file(Core::File& file)
{
- auto file = Core::File::construct();
- if (!file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes) && file->error() != ENOENT) {
- GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
- return;
- }
- window()->set_title(String::formatted("{} - PDF Viewer", path));
+ window()->set_title(String::formatted("{} - PDF Viewer", file.filename()));
- m_buffer = file->read_all();
+ m_buffer = file.read_all();
auto document = PDF::Document::create(m_buffer);
if (!document) {
- GUI::MessageBox::show_error(nullptr, String::formatted("Couldn't load PDF: {}", path));
+ GUI::MessageBox::show_error(nullptr, String::formatted("Couldn't load PDF: {}", file.filename()));
return;
}
diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.h b/Userland/Applications/PDFViewer/PDFViewerWidget.h
index b482f1876c..3bf3a7d42a 100644
--- a/Userland/Applications/PDFViewer/PDFViewerWidget.h
+++ b/Userland/Applications/PDFViewer/PDFViewerWidget.h
@@ -23,7 +23,7 @@ public:
void initialize_menubar(GUI::Window&);
void create_toolbar();
- void open_file(int fd, const String& path);
+ void open_file(Core::File&);
private:
PDFViewerWidget();
diff --git a/Userland/Applications/PDFViewer/main.cpp b/Userland/Applications/PDFViewer/main.cpp
index 5a866cbf85..5fc41301cc 100644
--- a/Userland/Applications/PDFViewer/main.cpp
+++ b/Userland/Applications/PDFViewer/main.cpp
@@ -38,14 +38,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->set_icon(app_icon.bitmap_for_size(16));
if (arguments.argc >= 2) {
- auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), arguments.argv[1]);
-
- if (response.error != 0) {
- if (response.error != -1)
- GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
+ auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, arguments.argv[1]);
+ if (response.is_error())
return 1;
- }
- pdf_viewer_widget->open_file(*response.fd, *response.chosen_file);
+ pdf_viewer_widget->open_file(*response.value());
}
return app->exec();