diff options
author | Mustafa Quraish <mustafaq9@gmail.com> | 2022-01-17 06:09:19 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-20 10:39:12 +0100 |
commit | bba32de857ae392a0271fdcc0bfe0f57b52d136c (patch) | |
tree | 9486317a8d5df8e19141ca0f78662dcd6e77312f /Userland/Applications | |
parent | ca2c7dced5b2a6ff7648cb6f3c3a7d7c3bfdce44 (diff) | |
download | serenity-bba32de857ae392a0271fdcc0bfe0f57b52d136c.zip |
SpreadSheet: Use FileSystemAccessClient::try_* APIs
Diffstat (limited to 'Userland/Applications')
-rw-r--r-- | Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp | 15 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/SpreadsheetWidget.h | 2 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/Workbook.cpp | 21 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/Workbook.h | 2 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/main.cpp | 10 |
5 files changed, 17 insertions, 33 deletions
diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp index 767626625a..5f20f9281a 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp +++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp @@ -124,15 +124,10 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, NonnullRefPtrVe if (!load_path.has_value()) return; - auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window()->window_id(), *load_path); - - 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(), *load_path); + if (response.is_error()) return; - } - - load_file(*response.fd, *load_path); + load_file(*response.value()); }); m_save_action = GUI::CommonActions::make_save_action([&](auto&) { @@ -400,9 +395,9 @@ void SpreadsheetWidget::save(StringView filename) GUI::MessageBox::show_error(window(), result.error()); } -void SpreadsheetWidget::load_file(int fd, StringView filename) +void SpreadsheetWidget::load_file(Core::File& file) { - auto result = m_workbook->open_file(fd, filename); + auto result = m_workbook->open_file(file); if (result.is_error()) { GUI::MessageBox::show_error(window(), result.error()); return; diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.h b/Userland/Applications/Spreadsheet/SpreadsheetWidget.h index f9bbae7dae..0908293239 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.h +++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.h @@ -20,7 +20,7 @@ public: ~SpreadsheetWidget(); void save(StringView filename); - void load_file(int fd, StringView filename); + void load_file(Core::File&); bool request_close(); void add_sheet(); void add_sheet(NonnullRefPtr<Sheet>&&); diff --git a/Userland/Applications/Spreadsheet/Workbook.cpp b/Userland/Applications/Spreadsheet/Workbook.cpp index d67e64ae5a..c79b872119 100644 --- a/Userland/Applications/Spreadsheet/Workbook.cpp +++ b/Userland/Applications/Spreadsheet/Workbook.cpp @@ -51,15 +51,9 @@ bool Workbook::set_filename(const String& filename) return true; } -Result<bool, String> Workbook::open_file(int fd, StringView filename) +Result<bool, String> Workbook::open_file(Core::File& file) { - auto file = Core::File::construct(); - - if (!file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes) && file->error() != ENOENT) { - return String::formatted("Opening \"{}\" failed: {}", file, strerror(errno)); - } - - auto mime = Core::guess_mime_type_based_on_filename(filename); + auto mime = Core::guess_mime_type_based_on_filename(file.filename()); // Make an import dialog, we might need to import it. auto result = ImportDialog::make_and_run_for(m_parent_window, mime, file, *this); @@ -68,25 +62,24 @@ Result<bool, String> Workbook::open_file(int fd, StringView filename) m_sheets = result.release_value(); - set_filename(filename); + set_filename(file.filename()); return true; } Result<bool, String> Workbook::load(StringView filename) { - auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(m_parent_window.window_id(), filename); - if (response.error != 0) { + auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(&m_parent_window, filename); + if (response.is_error()) { StringBuilder sb; sb.append("Failed to open "); sb.append(filename); sb.append(" for reading. Error: "); - sb.appendff("{}", response.error); - + sb.appendff("{}", response.error()); return sb.to_string(); } - return open_file(*response.fd, filename); + return open_file(*response.value()); } Result<bool, String> Workbook::save(StringView filename) diff --git a/Userland/Applications/Spreadsheet/Workbook.h b/Userland/Applications/Spreadsheet/Workbook.h index c0c49d3f44..5c570b21e0 100644 --- a/Userland/Applications/Spreadsheet/Workbook.h +++ b/Userland/Applications/Spreadsheet/Workbook.h @@ -19,7 +19,7 @@ public: Result<bool, String> save(StringView filename); Result<bool, String> load(StringView filename); - Result<bool, String> open_file(int fd, StringView filename); + Result<bool, String> open_file(Core::File&); const String& current_filename() const { return m_current_filename; } bool set_filename(const String& filename); diff --git a/Userland/Applications/Spreadsheet/main.cpp b/Userland/Applications/Spreadsheet/main.cpp index 29b4084f87..277e3d2b8a 100644 --- a/Userland/Applications/Spreadsheet/main.cpp +++ b/Userland/Applications/Spreadsheet/main.cpp @@ -94,14 +94,10 @@ int main(int argc, char* argv[]) window->show(); if (filename) { - auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), filename); - - 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, filename); + if (response.is_error()) return 1; - } - spreadsheet_widget.load_file(*response.fd, filename); + spreadsheet_widget.load_file(*response.value()); } return app->exec(); |