Serenity Operating System
1/*
2 * Copyright (c) 2020-2021, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include "ImportDialog.h"
8#include "Spreadsheet.h"
9#include <AK/JsonParser.h>
10#include <AK/LexicalPath.h>
11#include <Applications/Spreadsheet/CSVImportGML.h>
12#include <Applications/Spreadsheet/FormatSelectionPageGML.h>
13#include <LibGUI/Application.h>
14#include <LibGUI/CheckBox.h>
15#include <LibGUI/ComboBox.h>
16#include <LibGUI/ItemListModel.h>
17#include <LibGUI/RadioButton.h>
18#include <LibGUI/StackWidget.h>
19#include <LibGUI/TableView.h>
20#include <LibGUI/TextBox.h>
21#include <LibGUI/Window.h>
22#include <LibGUI/Wizards/WizardDialog.h>
23#include <LibGUI/Wizards/WizardPage.h>
24
25namespace Spreadsheet {
26
27CSVImportDialogPage::CSVImportDialogPage(StringView csv)
28 : m_csv(csv)
29{
30 m_page = GUI::WizardPage::construct(
31 "CSV Import Options",
32 "Please select the options for the csv file you wish to import");
33
34 m_page->body_widget().load_from_gml(csv_import_gml).release_value_but_fixme_should_propagate_errors();
35 m_page->set_is_final_page(true);
36
37 m_delimiter_comma_radio = m_page->body_widget().find_descendant_of_type_named<GUI::RadioButton>("delimiter_comma_radio");
38 m_delimiter_semicolon_radio = m_page->body_widget().find_descendant_of_type_named<GUI::RadioButton>("delimiter_semicolon_radio");
39 m_delimiter_tab_radio = m_page->body_widget().find_descendant_of_type_named<GUI::RadioButton>("delimiter_tab_radio");
40 m_delimiter_space_radio = m_page->body_widget().find_descendant_of_type_named<GUI::RadioButton>("delimiter_space_radio");
41 m_delimiter_other_radio = m_page->body_widget().find_descendant_of_type_named<GUI::RadioButton>("delimiter_other_radio");
42 m_delimiter_other_text_box = m_page->body_widget().find_descendant_of_type_named<GUI::TextBox>("delimiter_other_text_box");
43 m_quote_single_radio = m_page->body_widget().find_descendant_of_type_named<GUI::RadioButton>("quote_single_radio");
44 m_quote_double_radio = m_page->body_widget().find_descendant_of_type_named<GUI::RadioButton>("quote_double_radio");
45 m_quote_other_radio = m_page->body_widget().find_descendant_of_type_named<GUI::RadioButton>("quote_other_radio");
46 m_quote_other_text_box = m_page->body_widget().find_descendant_of_type_named<GUI::TextBox>("quote_other_text_box");
47 m_quote_escape_combo_box = m_page->body_widget().find_descendant_of_type_named<GUI::ComboBox>("quote_escape_combo_box");
48 m_read_header_check_box = m_page->body_widget().find_descendant_of_type_named<GUI::CheckBox>("read_header_check_box");
49 m_trim_leading_field_spaces_check_box = m_page->body_widget().find_descendant_of_type_named<GUI::CheckBox>("trim_leading_field_spaces_check_box");
50 m_trim_trailing_field_spaces_check_box = m_page->body_widget().find_descendant_of_type_named<GUI::CheckBox>("trim_trailing_field_spaces_check_box");
51 m_data_preview_table_view = m_page->body_widget().find_descendant_of_type_named<GUI::TableView>("data_preview_table_view");
52 m_data_preview_error_label = m_page->body_widget().find_descendant_of_type_named<GUI::Label>("data_preview_error_label");
53 m_data_preview_widget = m_page->body_widget().find_descendant_of_type_named<GUI::StackWidget>("data_preview_widget");
54
55 m_quote_escape_combo_box->set_model(GUI::ItemListModel<DeprecatedString>::create(m_quote_escape_items));
56
57 // By default, use commas, double quotes with repeat, and disable headers.
58 m_delimiter_comma_radio->set_checked(true);
59 m_quote_double_radio->set_checked(true);
60 m_quote_escape_combo_box->set_selected_index(0); // Repeat
61
62 m_delimiter_comma_radio->on_checked = [&](auto) { update_preview(); };
63 m_delimiter_semicolon_radio->on_checked = [&](auto) { update_preview(); };
64 m_delimiter_tab_radio->on_checked = [&](auto) { update_preview(); };
65 m_delimiter_space_radio->on_checked = [&](auto) { update_preview(); };
66 m_delimiter_other_radio->on_checked = [&](auto) { update_preview(); };
67 m_delimiter_other_text_box->on_change = [&] {
68 if (m_delimiter_other_radio->is_checked())
69 update_preview();
70 };
71 m_quote_single_radio->on_checked = [&](auto) { update_preview(); };
72 m_quote_double_radio->on_checked = [&](auto) { update_preview(); };
73 m_quote_other_radio->on_checked = [&](auto) { update_preview(); };
74 m_quote_other_text_box->on_change = [&] {
75 if (m_quote_other_radio->is_checked())
76 update_preview();
77 };
78 m_quote_escape_combo_box->on_change = [&](auto&, auto&) { update_preview(); };
79 m_read_header_check_box->on_checked = [&](auto) { update_preview(); };
80 m_trim_leading_field_spaces_check_box->on_checked = [&](auto) { update_preview(); };
81 m_trim_trailing_field_spaces_check_box->on_checked = [&](auto) { update_preview(); };
82
83 update_preview();
84}
85
86auto CSVImportDialogPage::make_reader() -> Optional<Reader::XSV>
87{
88 DeprecatedString delimiter;
89 DeprecatedString quote;
90 Reader::ParserTraits::QuoteEscape quote_escape;
91
92 // Delimiter
93 if (m_delimiter_other_radio->is_checked())
94 delimiter = m_delimiter_other_text_box->text();
95 else if (m_delimiter_comma_radio->is_checked())
96 delimiter = ",";
97 else if (m_delimiter_semicolon_radio->is_checked())
98 delimiter = ";";
99 else if (m_delimiter_tab_radio->is_checked())
100 delimiter = "\t";
101 else if (m_delimiter_space_radio->is_checked())
102 delimiter = " ";
103 else
104 return {};
105
106 // Quote separator
107 if (m_quote_other_radio->is_checked())
108 quote = m_quote_other_text_box->text();
109 else if (m_quote_single_radio->is_checked())
110 quote = "'";
111 else if (m_quote_double_radio->is_checked())
112 quote = "\"";
113 else
114 return {};
115
116 // Quote escape
117 auto index = m_quote_escape_combo_box->selected_index();
118 if (index == 0)
119 quote_escape = Reader::ParserTraits::Repeat;
120 else if (index == 1)
121 quote_escape = Reader::ParserTraits::Backslash;
122 else
123 return {};
124
125 auto should_read_headers = m_read_header_check_box->is_checked();
126 auto should_trim_leading = m_trim_leading_field_spaces_check_box->is_checked();
127 auto should_trim_trailing = m_trim_trailing_field_spaces_check_box->is_checked();
128
129 if (quote.is_empty() || delimiter.is_empty())
130 return {};
131
132 Reader::ParserTraits traits {
133 move(delimiter),
134 move(quote),
135 quote_escape,
136 };
137
138 auto behaviors = Reader::default_behaviors() | Reader::ParserBehavior::Lenient;
139
140 if (should_read_headers)
141 behaviors = behaviors | Reader::ParserBehavior::ReadHeaders;
142 if (should_trim_leading)
143 behaviors = behaviors | Reader::ParserBehavior::TrimLeadingFieldSpaces;
144 if (should_trim_trailing)
145 behaviors = behaviors | Reader::ParserBehavior::TrimTrailingFieldSpaces;
146
147 return Reader::XSV(m_csv, move(traits), behaviors);
148};
149
150void CSVImportDialogPage::update_preview()
151
152{
153 m_previously_made_reader = make_reader();
154 if (!m_previously_made_reader.has_value()) {
155 m_data_preview_table_view->set_model(nullptr);
156 m_data_preview_error_label->set_text("Could not read the given file");
157 m_data_preview_widget->set_active_widget(m_data_preview_error_label);
158 return;
159 }
160
161 auto& reader = *m_previously_made_reader;
162 if (reader.has_error()) {
163 m_data_preview_table_view->set_model(nullptr);
164 m_data_preview_error_label->set_text(DeprecatedString::formatted("XSV parse error:\n{}", reader.error_string()));
165 m_data_preview_widget->set_active_widget(m_data_preview_error_label);
166 return;
167 }
168
169 auto headers = reader.headers();
170
171 m_data_preview_table_view->set_model(
172 GUI::ItemListModel<Reader::XSV::Row, Reader::XSV, Vector<DeprecatedString>>::create(reader, headers, min(8ul, reader.size())));
173 m_data_preview_widget->set_active_widget(m_data_preview_table_view);
174 m_data_preview_table_view->update();
175}
176
177ErrorOr<Vector<NonnullRefPtr<Sheet>>, DeprecatedString> ImportDialog::make_and_run_for(GUI::Window& parent, StringView mime, String const& filename, Core::File& file, Workbook& workbook)
178{
179 auto wizard = GUI::WizardDialog::construct(&parent);
180 wizard->set_title("File Import Wizard");
181 wizard->set_icon(GUI::Icon::default_icon("app-spreadsheet"sv).bitmap_for_size(16));
182
183 auto import_xsv = [&]() -> ErrorOr<Vector<NonnullRefPtr<Sheet>>, DeprecatedString> {
184 auto contents_or_error = file.read_until_eof();
185 if (contents_or_error.is_error())
186 return DeprecatedString::formatted("{}", contents_or_error.release_error());
187 CSVImportDialogPage page { contents_or_error.release_value() };
188 wizard->replace_page(page.page());
189 auto result = wizard->exec();
190
191 if (result == GUI::Dialog::ExecResult::OK) {
192 auto& reader = page.reader();
193
194 Vector<NonnullRefPtr<Sheet>> sheets;
195
196 if (reader.has_value()) {
197 reader->parse();
198 if (reader.value().has_error())
199 return DeprecatedString::formatted("CSV Import failed: {}", reader.value().error_string());
200
201 auto sheet = Sheet::from_xsv(reader.value(), workbook);
202 if (sheet)
203 sheets.append(sheet.release_nonnull());
204 }
205
206 return sheets;
207 }
208
209 return DeprecatedString { "CSV Import was cancelled" };
210 };
211
212 auto import_worksheet = [&]() -> ErrorOr<Vector<NonnullRefPtr<Sheet>>, DeprecatedString> {
213 auto contents_or_error = file.read_until_eof();
214 if (contents_or_error.is_error())
215 return DeprecatedString::formatted("{}", contents_or_error.release_error());
216 auto json_value_option = JsonParser(contents_or_error.release_value()).parse();
217 if (json_value_option.is_error())
218 return DeprecatedString::formatted("Failed to parse {}", filename);
219
220 auto& json_value = json_value_option.value();
221 if (!json_value.is_array())
222 return DeprecatedString::formatted("Did not find a spreadsheet in {}", filename);
223
224 Vector<NonnullRefPtr<Sheet>> sheets;
225
226 auto& json_array = json_value.as_array();
227 json_array.for_each([&](auto& sheet_json) {
228 if (!sheet_json.is_object())
229 return IterationDecision::Continue;
230
231 if (auto sheet = Sheet::from_json(sheet_json.as_object(), workbook))
232 sheets.append(sheet.release_nonnull());
233
234 return IterationDecision::Continue;
235 });
236
237 return sheets;
238 };
239
240 if (mime == "text/csv") {
241 return import_xsv();
242 } else if (mime == "application/x-sheets+json") {
243 return import_worksheet();
244 } else {
245 auto page = GUI::WizardPage::construct(
246 "Import File Format",
247 DeprecatedString::formatted("Select the format you wish to import '{}' as", LexicalPath::basename(filename.to_deprecated_string())));
248
249 page->on_next_page = [] { return nullptr; };
250
251 page->body_widget().load_from_gml(select_format_page_gml).release_value_but_fixme_should_propagate_errors();
252 auto format_combo_box = page->body_widget().find_descendant_of_type_named<GUI::ComboBox>("select_format_page_format_combo_box");
253
254 Vector<DeprecatedString> supported_formats {
255 "CSV (text/csv)",
256 "Spreadsheet Worksheet",
257 };
258 format_combo_box->set_model(GUI::ItemListModel<DeprecatedString>::create(supported_formats));
259
260 wizard->push_page(page);
261
262 if (wizard->exec() != GUI::Dialog::ExecResult::OK)
263 return DeprecatedString { "Import was cancelled" };
264
265 if (format_combo_box->selected_index() == 0)
266 return import_xsv();
267
268 if (format_combo_box->selected_index() == 1)
269 return import_worksheet();
270
271 VERIFY_NOT_REACHED();
272 }
273}
274
275};