mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:27:45 +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
4 changed files with 15 additions and 25 deletions
|
@ -11,7 +11,6 @@
|
||||||
#include <LibGUI/Application.h>
|
#include <LibGUI/Application.h>
|
||||||
#include <LibGUI/BoxLayout.h>
|
#include <LibGUI/BoxLayout.h>
|
||||||
#include <LibGUI/Button.h>
|
#include <LibGUI/Button.h>
|
||||||
#include <LibGUI/FilePicker.h>
|
|
||||||
#include <LibGUI/InputBox.h>
|
#include <LibGUI/InputBox.h>
|
||||||
#include <LibGUI/Label.h>
|
#include <LibGUI/Label.h>
|
||||||
#include <LibGUI/Menu.h>
|
#include <LibGUI/Menu.h>
|
||||||
|
@ -137,16 +136,18 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, NonnullRefPtrVe
|
||||||
return;
|
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&) {
|
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
|
||||||
String name = "workbook";
|
String name = "workbook";
|
||||||
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), name, "sheets");
|
auto response = FileSystemAccessClient::Client::the().try_save_file(window(), name, "sheets");
|
||||||
if (!save_path.has_value())
|
if (response.is_error())
|
||||||
return;
|
return;
|
||||||
|
save(*response.value());
|
||||||
save(save_path.value());
|
|
||||||
update_window_title();
|
update_window_title();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -416,9 +417,9 @@ void SpreadsheetWidget::redo()
|
||||||
update();
|
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()) {
|
if (result.is_error()) {
|
||||||
GUI::MessageBox::show_error(window(), result.error());
|
GUI::MessageBox::show_error(window(), result.error());
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -23,7 +23,7 @@ class SpreadsheetWidget final
|
||||||
public:
|
public:
|
||||||
virtual ~SpreadsheetWidget() override = default;
|
virtual ~SpreadsheetWidget() override = default;
|
||||||
|
|
||||||
void save(StringView filename);
|
void save(Core::File&);
|
||||||
void load_file(Core::File&);
|
void load_file(Core::File&);
|
||||||
bool request_close();
|
bool request_close();
|
||||||
void add_sheet();
|
void add_sheet();
|
||||||
|
|
|
@ -64,25 +64,14 @@ Result<bool, String> Workbook::open_file(Core::File& file)
|
||||||
return true;
|
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 mime = Core::guess_mime_type_based_on_filename(file.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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make an export dialog, we might need to import it.
|
// 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);
|
set_dirty(false);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ class Workbook {
|
||||||
public:
|
public:
|
||||||
Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_window);
|
Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_window);
|
||||||
|
|
||||||
Result<bool, String> save(StringView filename);
|
|
||||||
Result<bool, String> open_file(Core::File&);
|
Result<bool, String> open_file(Core::File&);
|
||||||
|
Result<bool, String> write_to_file(Core::File&);
|
||||||
|
|
||||||
String const& current_filename() const { return m_current_filename; }
|
String const& current_filename() const { return m_current_filename; }
|
||||||
bool set_filename(String const& filename);
|
bool set_filename(String const& filename);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue