Serenity Operating System
1/*
2 * Copyright (c) 2020-2021, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include "Workbook.h"
8#include "ExportDialog.h"
9#include "ImportDialog.h"
10#include "JSIntegration.h"
11#include <AK/ByteBuffer.h>
12#include <AK/StringView.h>
13#include <LibCore/MimeData.h>
14#include <LibFileSystemAccessClient/Client.h>
15#include <LibGUI/TextBox.h>
16#include <LibGUI/Window.h>
17#include <LibJS/Runtime/GlobalObject.h>
18
19namespace Spreadsheet {
20
21Workbook::Workbook(Vector<NonnullRefPtr<Sheet>>&& sheets, GUI::Window& parent_window)
22 : m_sheets(move(sheets))
23 , m_vm(JS::VM::create())
24 , m_interpreter(JS::Interpreter::create<JS::GlobalObject>(m_vm))
25 , m_interpreter_scope(*m_interpreter)
26 , m_main_execution_context(m_vm->heap())
27 , m_parent_window(parent_window)
28{
29 m_workbook_object = m_vm->heap().allocate<WorkbookObject>(m_interpreter->realm(), m_interpreter->realm(), *this).release_allocated_value_but_fixme_should_propagate_errors();
30 m_interpreter->realm().global_object().define_direct_property("workbook", workbook_object(), JS::default_attributes);
31
32 m_main_execution_context.current_node = nullptr;
33 m_main_execution_context.this_value = &m_interpreter->realm().global_object();
34 m_main_execution_context.function_name = "(global execution context)"sv;
35 m_main_execution_context.lexical_environment = &m_interpreter->realm().global_environment();
36 m_main_execution_context.variable_environment = &m_interpreter->realm().global_environment();
37 m_main_execution_context.realm = &m_interpreter->realm();
38 m_main_execution_context.is_strict_mode = true;
39 m_vm->push_execution_context(m_main_execution_context);
40 m_vm->enable_default_host_import_module_dynamically_hook();
41}
42
43bool Workbook::set_filename(DeprecatedString const& filename)
44{
45 if (m_current_filename == filename)
46 return false;
47
48 m_current_filename = filename;
49 return true;
50}
51
52ErrorOr<void, DeprecatedString> Workbook::open_file(String const& filename, Core::File& file)
53{
54 auto mime = Core::guess_mime_type_based_on_filename(filename);
55
56 // Make an import dialog, we might need to import it.
57 m_sheets = TRY(ImportDialog::make_and_run_for(m_parent_window, mime, filename, file, *this));
58
59 set_filename(filename.to_deprecated_string());
60
61 return {};
62}
63
64ErrorOr<void> Workbook::write_to_file(String const& filename, Core::File& stream)
65{
66 auto mime = Core::guess_mime_type_based_on_filename(filename);
67
68 // Make an export dialog, we might need to import it.
69 TRY(ExportDialog::make_and_run_for(mime, stream, filename.to_deprecated_string(), *this));
70
71 set_filename(filename.to_deprecated_string());
72 set_dirty(false);
73 return {};
74}
75
76ErrorOr<bool, DeprecatedString> Workbook::import_file(String const& filename, Core::File& file)
77{
78 auto mime = Core::guess_mime_type_based_on_filename(filename);
79
80 auto sheets = TRY(ImportDialog::make_and_run_for(m_parent_window, mime, filename, file, *this));
81 auto has_any_changes = !sheets.is_empty();
82 m_sheets.extend(move(sheets));
83
84 return has_any_changes;
85}
86
87}