2020-08-25 22:30:23 -03:00
|
|
|
/*
|
2021-03-21 14:08:08 -03:00
|
|
|
* Copyright (c) 2020-2021, the SerenityOS developers.
|
2020-08-25 22:30:23 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-25 22:30:23 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Workbook.h"
|
2021-03-20 07:30:49 -03:00
|
|
|
#include "ExportDialog.h"
|
2021-03-21 14:08:08 -03:00
|
|
|
#include "ImportDialog.h"
|
2020-08-26 00:02:38 -03:00
|
|
|
#include "JSIntegration.h"
|
2022-01-11 08:26:04 -03:00
|
|
|
#include "LibGUI/MessageBox.h"
|
2020-11-23 09:46:06 -03:00
|
|
|
#include "Readers/CSV.h"
|
2020-11-15 09:11:21 -03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2022-01-11 08:26:04 -03:00
|
|
|
#include <AK/StringView.h>
|
2020-08-25 22:30:23 -03:00
|
|
|
#include <LibCore/File.h>
|
2020-11-23 09:46:06 -03:00
|
|
|
#include <LibCore/MimeData.h>
|
2022-01-11 08:26:04 -03:00
|
|
|
#include <LibFileSystemAccessClient/Client.h>
|
2021-03-21 14:08:08 -03:00
|
|
|
#include <LibGUI/TextBox.h>
|
2022-01-11 08:26:04 -03:00
|
|
|
#include <LibGUI/Window.h>
|
2020-08-26 00:02:38 -03:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-08-25 22:30:23 -03:00
|
|
|
|
|
|
|
|
namespace Spreadsheet {
|
|
|
|
|
|
2022-01-11 08:26:04 -03:00
|
|
|
Workbook::Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_window)
|
2020-08-26 00:02:38 -03:00
|
|
|
: m_sheets(move(sheets))
|
2021-11-20 22:34:55 -03:00
|
|
|
, m_vm(JS::VM::create())
|
|
|
|
|
, m_interpreter(JS::Interpreter::create<JS::GlobalObject>(m_vm))
|
|
|
|
|
, m_interpreter_scope(*m_interpreter)
|
|
|
|
|
, m_main_execution_context(m_vm->heap())
|
2022-01-09 16:31:51 -03:00
|
|
|
, m_parent_window(parent_window)
|
2020-08-26 00:02:38 -03:00
|
|
|
{
|
2021-11-20 22:34:55 -03:00
|
|
|
m_workbook_object = m_vm->heap().allocate<WorkbookObject>(m_interpreter->global_object(), *this);
|
|
|
|
|
m_interpreter->global_object().define_direct_property("workbook", workbook_object(), JS::default_attributes);
|
|
|
|
|
|
|
|
|
|
m_main_execution_context.current_node = nullptr;
|
|
|
|
|
m_main_execution_context.this_value = &m_interpreter->global_object();
|
|
|
|
|
m_main_execution_context.function_name = "(global execution context)"sv;
|
|
|
|
|
m_main_execution_context.lexical_environment = &m_interpreter->realm().global_environment();
|
|
|
|
|
m_main_execution_context.variable_environment = &m_interpreter->realm().global_environment();
|
|
|
|
|
m_main_execution_context.realm = &m_interpreter->realm();
|
|
|
|
|
m_main_execution_context.is_strict_mode = true;
|
|
|
|
|
MUST(m_vm->push_execution_context(m_main_execution_context, m_interpreter->global_object()));
|
2020-08-26 00:02:38 -03:00
|
|
|
}
|
|
|
|
|
|
2020-08-25 22:30:23 -03:00
|
|
|
bool Workbook::set_filename(const String& filename)
|
|
|
|
|
{
|
|
|
|
|
if (m_current_filename == filename)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_current_filename = filename;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 08:26:04 -03:00
|
|
|
Result<bool, String> Workbook::open_file(int fd, StringView filename)
|
2020-08-25 22:30:23 -03:00
|
|
|
{
|
2022-01-11 08:26:04 -03:00
|
|
|
auto file = Core::File::construct();
|
2020-08-25 22:30:23 -03:00
|
|
|
|
2022-01-11 08:26:04 -03:00
|
|
|
if (!file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes) && file->error() != ENOENT) {
|
|
|
|
|
return String::formatted("Opening \"{}\" failed: {}", file, strerror(errno));
|
2020-08-25 22:30:23 -03:00
|
|
|
}
|
|
|
|
|
|
2020-11-23 09:46:06 -03:00
|
|
|
auto mime = Core::guess_mime_type_based_on_filename(filename);
|
2020-08-25 22:30:23 -03:00
|
|
|
|
2021-03-21 14:08:08 -03:00
|
|
|
// Make an import dialog, we might need to import it.
|
2022-01-11 08:26:04 -03:00
|
|
|
auto result = ImportDialog::make_and_run_for(m_parent_window, mime, file, *this);
|
2021-03-21 14:08:08 -03:00
|
|
|
if (result.is_error())
|
|
|
|
|
return result.error();
|
2020-11-23 09:46:06 -03:00
|
|
|
|
2021-03-21 14:08:08 -03:00
|
|
|
m_sheets = result.release_value();
|
2020-08-25 22:30:23 -03:00
|
|
|
|
|
|
|
|
set_filename(filename);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 08:26:04 -03:00
|
|
|
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) {
|
|
|
|
|
StringBuilder sb;
|
|
|
|
|
sb.append("Failed to open ");
|
|
|
|
|
sb.append(filename);
|
|
|
|
|
sb.append(" for reading. Error: ");
|
|
|
|
|
sb.appendff("{}", response.error);
|
|
|
|
|
|
|
|
|
|
return sb.to_string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return open_file(*response.fd, filename);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 20:55:02 -03:00
|
|
|
Result<bool, String> Workbook::save(StringView filename)
|
2020-08-25 22:30:23 -03:00
|
|
|
{
|
2020-11-23 09:46:06 -03:00
|
|
|
auto mime = Core::guess_mime_type_based_on_filename(filename);
|
2020-08-25 22:30:23 -03:00
|
|
|
auto file = Core::File::construct(filename);
|
2021-05-12 06:26:43 -03:00
|
|
|
file->open(Core::OpenMode::WriteOnly);
|
2020-08-25 22:30:23 -03:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 07:30:49 -03:00
|
|
|
// Make an export dialog, we might need to import it.
|
|
|
|
|
auto result = ExportDialog::make_and_run_for(mime, *file, *this);
|
|
|
|
|
if (result.is_error())
|
|
|
|
|
return result.error();
|
2020-08-25 22:30:23 -03:00
|
|
|
|
|
|
|
|
set_filename(filename);
|
2020-12-22 02:15:15 -03:00
|
|
|
set_dirty(false);
|
2020-08-25 22:30:23 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|