1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #undef SC_DLLIMPLEMENTATION
22 #include <scuiimoptdlg.hxx>
23 #include <scresid.hxx>
24 #include <strings.hrc>
25 #include <strings.hxx>
26 #include <officecfg/Office/Calc.hxx>
27 #include <osl/thread.h>
28 #include <rtl/tencinfo.h>
29 #include <imoptdlg.hxx>
30 #include <svx/txencbox.hxx>
31 #include <o3tl/string_view.hxx>
36 class ScDelimiterTable
39 explicit ScDelimiterTable( OUString aDelTab
)
40 : theDelTab (std::move( aDelTab
)),
44 sal_uInt16
GetCode( std::u16string_view rDelimiter
) const;
45 OUString
GetDelimiter( sal_Unicode nCode
) const;
47 OUString
FirstDel() { nDelIdx
= 0; return theDelTab
.getToken( 0, cSep
, nDelIdx
); }
48 OUString
NextDel() { return theDelTab
.getToken( 1, cSep
, nDelIdx
); }
51 const OUString theDelTab
;
52 static constexpr sal_Unicode cSep
{'\t'};
56 sal_uInt16
ScDelimiterTable::GetCode( std::u16string_view rDel
) const
58 if (!theDelTab
.isEmpty())
62 // Check even tokens: start from 0 and then skip 1 token at each iteration
63 if (rDel
!= o3tl::getToken(theDelTab
, 0, cSep
, nIdx
))
64 while (nIdx
>0 && rDel
!= o3tl::getToken(theDelTab
, 1, cSep
, nIdx
));
67 return static_cast<sal_Unicode
>(o3tl::toInt32(o3tl::getToken(theDelTab
, 0, cSep
, nIdx
)));
73 OUString
ScDelimiterTable::GetDelimiter( sal_Unicode nCode
) const
75 if (!theDelTab
.isEmpty())
78 // Check odd tokens: start from 1 and then skip 1 token at each iteration
81 sal_Int32 nPrevIdx
{nIdx
};
82 if (nCode
== static_cast<sal_Unicode
>(o3tl::toInt32(o3tl::getToken(theDelTab
, 1, cSep
, nIdx
))))
83 return theDelTab
.getToken( 0, cSep
, nPrevIdx
);
91 void ScImportOptionsDlg::FillFromTextEncodingTable(bool bExcludeImportSubsets
, sal_uInt32 nExcludeInfoFlags
)
94 m_xLbCharset
->FillFromTextEncodingTable(bExcludeImportSubsets
, nExcludeInfoFlags
);
96 m_xTvCharset
->FillFromTextEncodingTable(bExcludeImportSubsets
, nExcludeInfoFlags
);
99 void ScImportOptionsDlg::FillFromDbTextEncodingMap(bool bExcludeImportSubsets
, sal_uInt32 nExcludeInfoFlags
)
101 if (m_bIsAsciiImport
)
102 m_xLbCharset
->FillFromDbTextEncodingMap(bExcludeImportSubsets
, nExcludeInfoFlags
);
104 m_xTvCharset
->FillFromDbTextEncodingMap(bExcludeImportSubsets
, nExcludeInfoFlags
);
107 // ScImportOptionsDlg
108 ScImportOptionsDlg::ScImportOptionsDlg(weld::Window
* pParent
, bool bAscii
,
109 const ScImportOptions
* pOptions
,
110 const OUString
* pStrTitle
,
111 bool bMultiByte
, bool bOnlyDbtoolsEncodings
,
113 : GenericDialogController(pParent
, "modules/scalc/ui/imoptdialog.ui", "ImOptDialog")
114 , m_bIsAsciiImport(bAscii
)
115 , m_xFieldFrame(m_xBuilder
->weld_frame("fieldframe"))
116 , m_xFtCharset(m_xBuilder
->weld_label("charsetft"))
117 , m_xEncGrid(m_xBuilder
->weld_widget("grid2"))
118 , m_xFtFieldSep(m_xBuilder
->weld_label("fieldft"))
119 , m_xEdFieldSep(m_xBuilder
->weld_combo_box("field"))
120 , m_xFtTextSep(m_xBuilder
->weld_label("textft"))
121 , m_xEdTextSep(m_xBuilder
->weld_combo_box("text"))
122 , m_xCbShown(m_xBuilder
->weld_check_button("asshown"))
123 , m_xCbFormulas(m_xBuilder
->weld_check_button("formulas"))
124 , m_xCbQuoteAll(m_xBuilder
->weld_check_button("quoteall"))
125 , m_xCbFixed(m_xBuilder
->weld_check_button("fixedwidth"))
126 , m_xLbCharset(new SvxTextEncodingBox(m_xBuilder
->weld_combo_box("charsetdropdown")))
127 , m_xTvCharset(new SvxTextEncodingTreeView(m_xBuilder
->weld_tree_view("charsetlist")))
131 m_xDialog
->set_help_id(m_xDialog
->get_help_id() + "?config=NonTextImport");
132 m_xLbCharset
->show();
133 m_xTvCharset
->hide();
137 m_xTvCharset
->set_size_request(-1, m_xTvCharset
->get_height_rows(6));
138 m_xEncGrid
->set_vexpand(true);
139 m_xLbCharset
->hide();
140 m_xTvCharset
->show();
143 OUString
sFieldSep(SCSTR_FIELDSEP
);
144 sFieldSep
= sFieldSep
.replaceFirst( "%TAB", ScResId(SCSTR_FIELDSEP_TAB
) );
145 sFieldSep
= sFieldSep
.replaceFirst( "%SPACE", ScResId(SCSTR_FIELDSEP_SPACE
) );
147 // not possible in the Ctor initializer (MSC cannot do that):
148 pFieldSepTab
.reset( new ScDelimiterTable(sFieldSep
) );
149 pTextSepTab
.reset( new ScDelimiterTable(SCSTR_TEXTSEP
) );
151 OUString aStr
= pFieldSepTab
->FirstDel();
153 while (!aStr
.isEmpty())
155 m_xEdFieldSep
->append_text(aStr
);
156 aStr
= pFieldSepTab
->NextDel();
159 aStr
= pTextSepTab
->FirstDel();
161 while (!aStr
.isEmpty())
163 m_xEdTextSep
->append_text(aStr
);
164 aStr
= pTextSepTab
->NextDel();
167 m_xEdFieldSep
->set_active(0);
168 m_xEdTextSep
->set_active(0);
170 if ( bOnlyDbtoolsEncodings
)
172 // Even dBase export allows multibyte now
174 FillFromDbTextEncodingMap( bImport
);
176 FillFromDbTextEncodingMap( bImport
, RTL_TEXTENCODING_INFO_MULTIBYTE
);
179 { //!TODO: Unicode would need work in each filter
181 FillFromTextEncodingTable( bImport
, RTL_TEXTENCODING_INFO_UNICODE
);
183 FillFromTextEncodingTable( bImport
, RTL_TEXTENCODING_INFO_UNICODE
|
184 RTL_TEXTENCODING_INFO_MULTIBYTE
);
190 sal_Unicode nCode
= pOptions
->nFieldSepCode
;
191 aStr
= pFieldSepTab
->GetDelimiter( nCode
);
193 if ( aStr
.isEmpty() )
194 m_xEdFieldSep
->set_entry_text(OUString(nCode
));
196 m_xEdFieldSep
->set_entry_text(aStr
);
198 nCode
= pOptions
->nTextSepCode
;
199 aStr
= pTextSepTab
->GetDelimiter( nCode
);
201 if ( aStr
.isEmpty() )
202 m_xEdTextSep
->set_entry_text(OUString(nCode
));
204 m_xEdTextSep
->set_entry_text(aStr
);
206 // all encodings allowed, even Unicode
207 FillFromTextEncodingTable( bImport
);
212 sal_Int32 nCharSet
= officecfg::Office::Calc::Dialogs::CSVExport::CharSet::get();
213 OUString strFieldSeparator
= officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::get();
214 OUString strTextSeparator
= officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::get();
215 bool bSaveTrueCellContent
= officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::get();
216 bool bSaveCellFormulas
= officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::get();
217 bool bQuoteAllTextCells
= officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::get();
218 bool bFixedWidth
= officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::get();
221 m_xCbFixed
->connect_toggled(LINK(this, ScImportOptionsDlg
, FixedWidthHdl
));
222 m_xCbFixed
->set_active( bFixedWidth
);
223 FixedWidthHdl(*m_xCbFixed
);
225 m_xCbShown
->set_active( bSaveTrueCellContent
);
226 m_xCbQuoteAll
->show();
227 m_xCbQuoteAll
->set_active( bQuoteAllTextCells
);
228 m_xCbFormulas
->show();
229 // default option for "save formulas" no longer taken from view shell but from persisted dialog settings
230 m_xCbFormulas
->set_active( bSaveCellFormulas
);
231 // if no charset, text separator or field separator exist, keep the values from dialog initialization
232 if (strFieldSeparator
.getLength() > 0)
233 m_xEdFieldSep
->set_entry_text(strFieldSeparator
);
234 if (strTextSeparator
.getLength() > 0)
235 m_xEdTextSep
->set_entry_text(strTextSeparator
);
236 if (nCharSet
< 0 || nCharSet
== RTL_TEXTENCODING_DONTKNOW
)
237 m_xLbCharset
->SelectTextEncoding(pOptions
? pOptions
->eCharSet
: osl_getThreadTextEncoding());
239 m_xLbCharset
->SelectTextEncoding(nCharSet
);
243 m_xFieldFrame
->set_label(m_xFtCharset
->get_label());
244 m_xFtFieldSep
->hide();
245 m_xFtTextSep
->hide();
246 m_xFtCharset
->hide();
247 m_xEdFieldSep
->hide();
248 m_xEdTextSep
->hide();
251 m_xCbQuoteAll
->hide();
252 m_xCbFormulas
->hide();
253 m_xTvCharset
->grab_focus();
254 m_xTvCharset
->connect_row_activated(LINK(this, ScImportOptionsDlg
, DoubleClickHdl
));
255 m_xTvCharset
->SelectTextEncoding(pOptions
? pOptions
->eCharSet
: osl_getThreadTextEncoding());
260 m_xDialog
->set_title(*pStrTitle
);
263 ScImportOptionsDlg::~ScImportOptionsDlg()
267 void ScImportOptionsDlg::GetImportOptions( ScImportOptions
& rOptions
) const
269 auto nEncoding
= m_bIsAsciiImport
? m_xLbCharset
->GetSelectTextEncoding() : m_xTvCharset
->GetSelectTextEncoding();
270 rOptions
.SetTextEncoding(nEncoding
);
272 if (m_xCbFixed
->get_visible())
274 rOptions
.nFieldSepCode
= GetCodeFromCombo( *m_xEdFieldSep
);
275 rOptions
.nTextSepCode
= GetCodeFromCombo( *m_xEdTextSep
);
276 rOptions
.bFixedWidth
= m_xCbFixed
->get_active();
277 rOptions
.bSaveAsShown
= m_xCbShown
->get_active();
278 rOptions
.bQuoteAllText
= m_xCbQuoteAll
->get_active();
279 rOptions
.bSaveFormulas
= m_xCbFormulas
->get_active();
283 sal_uInt16
ScImportOptionsDlg::GetCodeFromCombo(const weld::ComboBox
& rEd
) const
285 ScDelimiterTable
* pTab
;
286 OUString
aStr( rEd
.get_active_text() );
289 if (&rEd
== m_xEdTextSep
.get())
290 pTab
= pTextSepTab
.get();
292 pTab
= pFieldSepTab
.get();
294 if ( aStr
.isEmpty() )
296 nCode
= 0; // no separator
300 nCode
= pTab
->GetCode( aStr
);
303 nCode
= static_cast<sal_uInt16
>(aStr
[0]);
309 IMPL_LINK_NOARG(ScImportOptionsDlg
, FixedWidthHdl
, weld::Toggleable
&, void)
311 bool bEnable
= !m_xCbFixed
->get_active();
312 m_xFtFieldSep
->set_sensitive( bEnable
);
313 m_xEdFieldSep
->set_sensitive( bEnable
);
314 m_xFtTextSep
->set_sensitive( bEnable
);
315 m_xEdTextSep
->set_sensitive( bEnable
);
316 m_xCbShown
->set_sensitive( bEnable
);
317 m_xCbQuoteAll
->set_sensitive( bEnable
);
320 IMPL_LINK_NOARG(ScImportOptionsDlg
, DoubleClickHdl
, weld::TreeView
&, bool)
322 m_xDialog
->response(RET_OK
);
326 void ScImportOptionsDlg::SaveImportOptions() const
328 std::shared_ptr
< comphelper::ConfigurationChanges
> batch(comphelper::ConfigurationChanges::create());
329 auto nEncoding
= m_bIsAsciiImport
? m_xLbCharset
->GetSelectTextEncoding() : m_xTvCharset
->GetSelectTextEncoding();
330 officecfg::Office::Calc::Dialogs::CSVExport::CharSet::set(nEncoding
, batch
);
331 officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::set(m_xEdFieldSep
->get_active_text(), batch
);
332 officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::set(m_xEdTextSep
->get_active_text(), batch
);
333 officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::set(m_xCbFixed
->get_active(), batch
);
334 officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::set(m_xCbFormulas
->get_active(), batch
);
335 officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::set(m_xCbShown
->get_active(), batch
);
336 officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::set(m_xCbQuoteAll
->get_active(), batch
);
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */