mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
Spreadsheet: Make save functions take a Core::File instead of a filename
This allows us to use FileSystemAccessClient functions.
This commit is contained in:
parent
04443eb847
commit
6a4b125fe5
Notes:
sideshowbarker
2024-07-17 10:42:07 +09:00
Author: https://github.com/krkk Commit: https://github.com/SerenityOS/serenity/commit/6a4b125fe5 Pull-request: https://github.com/SerenityOS/serenity/pull/14085 Reviewed-by: https://github.com/kennethmyhra ✅
4 changed files with 15 additions and 25 deletions
|
@ -11,7 +11,6 @@
|
|||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/FilePicker.h>
|
||||
#include <LibGUI/InputBox.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
|
@ -137,16 +136,18 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, NonnullRefPtrVe
|
|||
return;
|
||||
}
|
||||
|
||||
save(current_filename());
|
||||
auto response = FileSystemAccessClient::Client::the().try_request_file(window(), current_filename(), Core::OpenMode::WriteOnly);
|
||||
if (response.is_error())
|
||||
return;
|
||||
save(*response.value());
|
||||
});
|
||||
|
||||
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
|
||||
String name = "workbook";
|
||||
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), name, "sheets");
|
||||
if (!save_path.has_value())
|
||||
auto response = FileSystemAccessClient::Client::the().try_save_file(window(), name, "sheets");
|
||||
if (response.is_error())
|
||||
return;
|
||||
|
||||
save(save_path.value());
|
||||
save(*response.value());
|
||||
update_window_title();
|
||||
});
|
||||
|
||||
|
@ -416,9 +417,9 @@ void SpreadsheetWidget::redo()
|
|||
update();
|
||||
}
|
||||
|
||||
void SpreadsheetWidget::save(StringView filename)
|
||||
void SpreadsheetWidget::save(Core::File& file)
|
||||
{
|
||||
auto result = m_workbook->save(filename);
|
||||
auto result = m_workbook->write_to_file(file);
|
||||
if (result.is_error()) {
|
||||
GUI::MessageBox::show_error(window(), result.error());
|
||||
return;
|
||||
|
|
|
@ -23,7 +23,7 @@ class SpreadsheetWidget final
|
|||
public:
|
||||
virtual ~SpreadsheetWidget() override = default;
|
||||
|
||||
void save(StringView filename);
|
||||
void save(Core::File&);
|
||||
void load_file(Core::File&);
|
||||
bool request_close();
|
||||
void add_sheet();
|
||||
|
|
|
@ -64,25 +64,14 @@ Result<bool, String> Workbook::open_file(Core::File& file)
|
|||
return true;
|
||||
}
|
||||
|
||||
Result<bool, String> Workbook::save(StringView filename)
|
||||
Result<bool, String> Workbook::write_to_file(Core::File& file)
|
||||
{
|
||||
auto mime = Core::guess_mime_type_based_on_filename(filename);
|
||||
auto file = Core::File::construct(filename);
|
||||
file->open(Core::OpenMode::WriteOnly);
|
||||
if (!file->is_open()) {
|
||||
StringBuilder sb;
|
||||
sb.append("Failed to open ");
|
||||
sb.append(filename);
|
||||
sb.append(" for write. Error: ");
|
||||
sb.append(file->error_string());
|
||||
|
||||
return sb.to_string();
|
||||
}
|
||||
auto mime = Core::guess_mime_type_based_on_filename(file.filename());
|
||||
|
||||
// Make an export dialog, we might need to import it.
|
||||
TRY(ExportDialog::make_and_run_for(mime, *file, *this));
|
||||
TRY(ExportDialog::make_and_run_for(mime, file, *this));
|
||||
|
||||
set_filename(filename);
|
||||
set_filename(file.filename());
|
||||
set_dirty(false);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@ class Workbook {
|
|||
public:
|
||||
Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_window);
|
||||
|
||||
Result<bool, String> save(StringView filename);
|
||||
Result<bool, String> open_file(Core::File&);
|
||||
Result<bool, String> write_to_file(Core::File&);
|
||||
|
||||
String const& current_filename() const { return m_current_filename; }
|
||||
bool set_filename(String const& filename);
|
||||
|
|
Loading…
Add table
Reference in a new issue