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"
|
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-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>
|
2023-08-08 02:12:40 -03:00
|
|
|
#include <LibJS/Runtime/GlobalEnvironment.h>
|
2020-08-26 00:02:38 -03:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-08-25 22:30:23 -03:00
|
|
|
|
|
|
|
|
namespace Spreadsheet {
|
|
|
|
|
|
2023-03-06 10:17:01 -03:00
|
|
|
Workbook::Workbook(Vector<NonnullRefPtr<Sheet>>&& sheets, GUI::Window& parent_window)
|
2020-08-26 00:02:38 -03:00
|
|
|
: m_sheets(move(sheets))
|
2023-03-17 11:44:47 -03:00
|
|
|
, m_vm(JS::VM::create().release_value_but_fixme_should_propagate_errors())
|
2023-08-08 02:12:40 -03:00
|
|
|
, m_root_execution_context(JS::create_simple_execution_context<JS::GlobalObject>(m_vm))
|
2021-11-20 22:34:55 -03:00
|
|
|
, 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
|
|
|
{
|
2023-08-08 02:12:40 -03:00
|
|
|
auto& realm = *m_root_execution_context->realm;
|
|
|
|
|
auto& vm = realm.vm();
|
2023-08-13 08:05:26 -03:00
|
|
|
m_workbook_object = vm.heap().allocate<WorkbookObject>(realm, realm, *this);
|
2023-08-08 02:12:40 -03:00
|
|
|
realm.global_object().define_direct_property("workbook", workbook_object(), JS::default_attributes);
|
2021-11-20 22:34:55 -03:00
|
|
|
|
2023-08-08 02:12:40 -03:00
|
|
|
m_main_execution_context.this_value = &realm.global_object();
|
2021-11-20 22:34:55 -03:00
|
|
|
m_main_execution_context.function_name = "(global execution context)"sv;
|
2023-08-08 02:12:40 -03:00
|
|
|
m_main_execution_context.lexical_environment = &realm.global_environment();
|
|
|
|
|
m_main_execution_context.variable_environment = &realm.global_environment();
|
|
|
|
|
m_main_execution_context.realm = &realm;
|
2021-11-20 22:34:55 -03:00
|
|
|
m_main_execution_context.is_strict_mode = true;
|
2022-03-17 19:40:17 -03:00
|
|
|
m_vm->push_execution_context(m_main_execution_context);
|
2022-01-19 06:49:51 -03:00
|
|
|
m_vm->enable_default_host_import_module_dynamically_hook();
|
2020-08-26 00:02:38 -03:00
|
|
|
}
|
|
|
|
|
|
2022-12-04 15:02:33 -03:00
|
|
|
bool Workbook::set_filename(DeprecatedString const& filename)
|
2020-08-25 22:30:23 -03:00
|
|
|
{
|
|
|
|
|
if (m_current_filename == filename)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_current_filename = filename;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 23:02:46 -03:00
|
|
|
ErrorOr<void, DeprecatedString> Workbook::open_file(String const& filename, Core::File& file)
|
2020-08-25 22:30:23 -03:00
|
|
|
{
|
2023-01-14 16:28:24 -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.
|
2023-01-14 16:28:24 -03:00
|
|
|
m_sheets = TRY(ImportDialog::make_and_run_for(m_parent_window, mime, filename, file, *this));
|
2020-08-25 22:30:23 -03:00
|
|
|
|
2023-01-14 16:28:24 -03:00
|
|
|
set_filename(filename.to_deprecated_string());
|
2020-08-25 22:30:23 -03:00
|
|
|
|
2023-01-14 20:25:51 -03:00
|
|
|
return {};
|
2020-08-25 22:30:23 -03:00
|
|
|
}
|
|
|
|
|
|
2023-02-08 23:02:46 -03:00
|
|
|
ErrorOr<void> Workbook::write_to_file(String const& filename, Core::File& stream)
|
2020-08-25 22:30:23 -03:00
|
|
|
{
|
2023-01-14 16:28:24 -03:00
|
|
|
auto mime = Core::guess_mime_type_based_on_filename(filename);
|
2022-12-26 19:55:00 -03:00
|
|
|
|
2021-03-20 07:30:49 -03:00
|
|
|
// Make an export dialog, we might need to import it.
|
2023-01-14 16:28:24 -03:00
|
|
|
TRY(ExportDialog::make_and_run_for(mime, stream, filename.to_deprecated_string(), *this));
|
2020-08-25 22:30:23 -03:00
|
|
|
|
2023-01-14 16:28:24 -03:00
|
|
|
set_filename(filename.to_deprecated_string());
|
2020-12-22 02:15:15 -03:00
|
|
|
set_dirty(false);
|
2022-12-31 11:38:12 -03:00
|
|
|
return {};
|
2020-08-25 22:30:23 -03:00
|
|
|
}
|
|
|
|
|
|
2023-02-08 23:02:46 -03:00
|
|
|
ErrorOr<bool, DeprecatedString> Workbook::import_file(String const& filename, Core::File& file)
|
2022-06-25 14:36:18 -03:00
|
|
|
{
|
2023-01-14 16:28:24 -03:00
|
|
|
auto mime = Core::guess_mime_type_based_on_filename(filename);
|
2022-06-25 14:36:18 -03:00
|
|
|
|
2023-01-14 16:28:24 -03:00
|
|
|
auto sheets = TRY(ImportDialog::make_and_run_for(m_parent_window, mime, filename, file, *this));
|
2022-06-25 14:36:18 -03:00
|
|
|
auto has_any_changes = !sheets.is_empty();
|
|
|
|
|
m_sheets.extend(move(sheets));
|
|
|
|
|
|
|
|
|
|
return has_any_changes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 22:30:23 -03:00
|
|
|
}
|