diff options
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/PDFViewer/PDFViewerWidget.cpp | 35 | ||||
-rw-r--r-- | Userland/Applications/PDFViewer/PDFViewerWidget.h | 1 |
2 files changed, 16 insertions, 20 deletions
diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp index 2f60ae1a4a..e1265d1002 100644 --- a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp +++ b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp @@ -336,35 +336,28 @@ void PDFViewerWidget::initialize_toolbar(GUI::Toolbar& toolbar) void PDFViewerWidget::open_file(Core::File& file) { - window()->set_title(DeprecatedString::formatted("{} - PDF Viewer", file.filename())); + auto maybe_error = try_open_file(file); + if (maybe_error.is_error()) { + auto error = maybe_error.release_error(); + warnln("{}", error.message()); + GUI::MessageBox::show_error(nullptr, "Failed to load the document."sv); + } +} - auto handle_error = [&](auto&& maybe_error) { - if (maybe_error.is_error()) { - auto error = maybe_error.release_error(); - warnln("{}", error.message()); - GUI::MessageBox::show_error(nullptr, "Failed to load the document."sv); - return true; - } - return false; - }; +PDF::PDFErrorOr<void> PDFViewerWidget::try_open_file(Core::File& file) +{ + window()->set_title(DeprecatedString::formatted("{} - PDF Viewer", file.filename())); m_buffer = file.read_all(); - auto maybe_document = PDF::Document::create(m_buffer); - if (handle_error(maybe_document)) - return; - - auto document = maybe_document.release_value(); + auto document = TRY(PDF::Document::create(m_buffer)); if (auto sh = document->security_handler(); sh && !sh->has_user_password()) { // FIXME: Prompt the user for a password VERIFY_NOT_REACHED(); } - if (handle_error(document->initialize())) - return; - - if (handle_error(m_viewer->set_document(document))) - return; + TRY(document->initialize()); + TRY(m_viewer->set_document(document)); m_total_page_label->set_text(DeprecatedString::formatted("of {}", document->get_page_count())); @@ -391,4 +384,6 @@ void PDFViewerWidget::open_file(Core::File& file) m_sidebar->set_visible(false); m_sidebar_open = false; } + + return {}; } diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.h b/Userland/Applications/PDFViewer/PDFViewerWidget.h index 7d6c93dcbe..8a4ece9d9a 100644 --- a/Userland/Applications/PDFViewer/PDFViewerWidget.h +++ b/Userland/Applications/PDFViewer/PDFViewerWidget.h @@ -32,6 +32,7 @@ private: PDFViewerWidget(); void initialize_toolbar(GUI::Toolbar&); + PDF::PDFErrorOr<void> try_open_file(Core::File&); RefPtr<PDFViewer> m_viewer; RefPtr<SidebarWidget> m_sidebar; |