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>
33 #include <vcl/svapp.hxx>
37 class ScDelimiterTable
40 explicit ScDelimiterTable( OUString aDelTab
)
41 : theDelTab (std::move( aDelTab
)),
45 sal_uInt16
GetCode( std::u16string_view rDelimiter
) const;
46 OUString
GetDelimiter( sal_Unicode nCode
) const;
48 OUString
FirstDel() { nDelIdx
= 0; return theDelTab
.getToken( 0, cSep
, nDelIdx
); }
49 OUString
NextDel() { return theDelTab
.getToken( 1, cSep
, nDelIdx
); }
52 const OUString theDelTab
;
53 static constexpr sal_Unicode cSep
{'\t'};
57 sal_uInt16
ScDelimiterTable::GetCode( std::u16string_view rDel
) const
59 if (!theDelTab
.isEmpty())
63 // Check even tokens: start from 0 and then skip 1 token at each iteration
64 if (rDel
!= o3tl::getToken(theDelTab
, 0, cSep
, nIdx
))
65 while (nIdx
>0 && rDel
!= o3tl::getToken(theDelTab
, 1, cSep
, nIdx
));
68 return static_cast<sal_Unicode
>(o3tl::toInt32(o3tl::getToken(theDelTab
, 0, cSep
, nIdx
)));
74 OUString
ScDelimiterTable::GetDelimiter( sal_Unicode nCode
) const
76 if (!theDelTab
.isEmpty())
79 // Check odd tokens: start from 1 and then skip 1 token at each iteration
82 sal_Int32 nPrevIdx
{nIdx
};
83 if (nCode
== static_cast<sal_Unicode
>(o3tl::toInt32(o3tl::getToken(theDelTab
, 1, cSep
, nIdx
))))
84 return theDelTab
.getToken( 0, cSep
, nPrevIdx
);
92 void ScImportOptionsDlg::FillFromTextEncodingTable(bool bExcludeImportSubsets
, sal_uInt32 nExcludeInfoFlags
)
95 m_xLbCharset
->FillFromTextEncodingTable(bExcludeImportSubsets
, nExcludeInfoFlags
);
97 m_xTvCharset
->FillFromTextEncodingTable(bExcludeImportSubsets
, nExcludeInfoFlags
);
100 void ScImportOptionsDlg::FillFromDbTextEncodingMap(bool bExcludeImportSubsets
, sal_uInt32 nExcludeInfoFlags
)
102 if (m_bIsAsciiImport
)
103 m_xLbCharset
->FillFromDbTextEncodingMap(bExcludeImportSubsets
, nExcludeInfoFlags
);
105 m_xTvCharset
->FillFromDbTextEncodingMap(bExcludeImportSubsets
, nExcludeInfoFlags
);
108 // ScImportOptionsDlg
109 ScImportOptionsDlg::ScImportOptionsDlg(weld::Window
* pParent
, bool bAscii
,
110 const ScImportOptions
* pOptions
,
111 const OUString
* pStrTitle
,
112 bool bMultiByte
, bool bOnlyDbtoolsEncodings
,
114 : GenericDialogController(pParent
, u
"modules/scalc/ui/imoptdialog.ui"_ustr
, u
"ImOptDialog"_ustr
)
115 , m_bIsAsciiImport(bAscii
)
116 , m_xFieldFrame(m_xBuilder
->weld_frame(u
"fieldframe"_ustr
))
117 , m_xFtCharset(m_xBuilder
->weld_label(u
"charsetft"_ustr
))
118 , m_xEncGrid(m_xBuilder
->weld_widget(u
"grid2"_ustr
))
119 , m_xFtFieldSep(m_xBuilder
->weld_label(u
"fieldft"_ustr
))
120 , m_xEdFieldSep(m_xBuilder
->weld_combo_box(u
"field"_ustr
))
121 , m_xFtTextSep(m_xBuilder
->weld_label(u
"textft"_ustr
))
122 , m_xEdTextSep(m_xBuilder
->weld_combo_box(u
"text"_ustr
))
123 , m_xCbShown(m_xBuilder
->weld_check_button(u
"asshown"_ustr
))
124 , m_xCbFormulas(m_xBuilder
->weld_check_button(u
"formulas"_ustr
))
125 , m_xCbQuoteAll(m_xBuilder
->weld_check_button(u
"quoteall"_ustr
))
126 , m_xCbFixed(m_xBuilder
->weld_check_button(u
"fixedwidth"_ustr
))
127 , m_xLbCharset(new SvxTextEncodingBox(m_xBuilder
->weld_combo_box(u
"charsetdropdown"_ustr
)))
128 , m_xTvCharset(new SvxTextEncodingTreeView(m_xBuilder
->weld_tree_view(u
"charsetlist"_ustr
)))
132 m_xDialog
->set_help_id(m_xDialog
->get_help_id() + "?config=NonTextImport");
133 m_xLbCharset
->show();
134 m_xTvCharset
->hide();
138 m_xTvCharset
->set_size_request(-1, m_xTvCharset
->get_height_rows(6));
139 m_xEncGrid
->set_vexpand(true);
140 m_xLbCharset
->hide();
141 m_xTvCharset
->show();
144 OUString
sFieldSep(SCSTR_FIELDSEP
);
145 sFieldSep
= sFieldSep
.replaceFirst( "%TAB", ScResId(SCSTR_FIELDSEP_TAB
) );
146 sFieldSep
= sFieldSep
.replaceFirst( "%SPACE", ScResId(SCSTR_FIELDSEP_SPACE
) );
148 // not possible in the Ctor initializer (MSC cannot do that):
149 pFieldSepTab
.reset( new ScDelimiterTable(sFieldSep
) );
150 pTextSepTab
.reset( new ScDelimiterTable(SCSTR_TEXTSEP
) );
152 OUString aStr
= pFieldSepTab
->FirstDel();
154 while (!aStr
.isEmpty())
156 m_xEdFieldSep
->append_text(aStr
);
157 aStr
= pFieldSepTab
->NextDel();
160 aStr
= pTextSepTab
->FirstDel();
162 while (!aStr
.isEmpty())
164 m_xEdTextSep
->append_text(aStr
);
165 aStr
= pTextSepTab
->NextDel();
168 m_xEdFieldSep
->set_active(0);
169 m_xEdTextSep
->set_active(0);
171 if ( bOnlyDbtoolsEncodings
)
173 // Even dBase export allows multibyte now
175 FillFromDbTextEncodingMap( bImport
);
177 FillFromDbTextEncodingMap( bImport
, RTL_TEXTENCODING_INFO_MULTIBYTE
);
180 { //!TODO: Unicode would need work in each filter
182 FillFromTextEncodingTable( bImport
, RTL_TEXTENCODING_INFO_UNICODE
);
184 FillFromTextEncodingTable( bImport
, RTL_TEXTENCODING_INFO_UNICODE
|
185 RTL_TEXTENCODING_INFO_MULTIBYTE
);
191 sal_Unicode nCode
= pOptions
->nFieldSepCode
;
192 aStr
= pFieldSepTab
->GetDelimiter( nCode
);
194 if ( aStr
.isEmpty() )
195 m_xEdFieldSep
->set_entry_text(OUString(nCode
));
197 m_xEdFieldSep
->set_entry_text(aStr
);
199 nCode
= pOptions
->nTextSepCode
;
200 aStr
= pTextSepTab
->GetDelimiter( nCode
);
202 if ( aStr
.isEmpty() )
203 m_xEdTextSep
->set_entry_text(OUString(nCode
));
205 m_xEdTextSep
->set_entry_text(aStr
);
207 // all encodings allowed, even Unicode
208 FillFromTextEncodingTable( bImport
);
213 sal_Int32 nCharSet
= officecfg::Office::Calc::Dialogs::CSVExport::CharSet::get();
214 OUString strFieldSeparator
= officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::get();
215 OUString strTextSeparator
= officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::get();
216 bool bSaveTrueCellContent
= officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::get();
217 bool bSaveCellFormulas
= officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::get();
218 bool bQuoteAllTextCells
= officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::get();
219 bool bFixedWidth
= officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::get();
222 m_xCbFixed
->connect_toggled(LINK(this, ScImportOptionsDlg
, FixedWidthHdl
));
223 m_xCbFixed
->set_active( bFixedWidth
);
224 FixedWidthHdl(*m_xCbFixed
);
226 m_xCbShown
->set_active( bSaveTrueCellContent
);
227 m_xCbQuoteAll
->show();
228 m_xCbQuoteAll
->set_active( bQuoteAllTextCells
);
229 m_xCbFormulas
->show();
230 // default option for "save formulas" no longer taken from view shell but from persisted dialog settings
231 m_xCbFormulas
->set_active( bSaveCellFormulas
);
232 // if no charset, text separator or field separator exist, keep the values from dialog initialization
233 if (strFieldSeparator
.getLength() > 0)
234 m_xEdFieldSep
->set_entry_text(strFieldSeparator
);
235 if (strTextSeparator
.getLength() > 0)
236 m_xEdTextSep
->set_entry_text(strTextSeparator
);
237 if (nCharSet
< 0 || nCharSet
== RTL_TEXTENCODING_DONTKNOW
)
238 m_xLbCharset
->SelectTextEncoding(pOptions
? pOptions
->eCharSet
: osl_getThreadTextEncoding());
240 m_xLbCharset
->SelectTextEncoding(nCharSet
);
244 m_xFieldFrame
->set_label(m_xFtCharset
->get_label());
245 m_xFtFieldSep
->hide();
246 m_xFtTextSep
->hide();
247 m_xFtCharset
->hide();
248 m_xEdFieldSep
->hide();
249 m_xEdTextSep
->hide();
252 m_xCbQuoteAll
->hide();
253 m_xCbFormulas
->hide();
254 m_xTvCharset
->grab_focus();
255 m_xTvCharset
->connect_row_activated(LINK(this, ScImportOptionsDlg
, DoubleClickHdl
));
256 m_xTvCharset
->SelectTextEncoding(pOptions
? pOptions
->eCharSet
: osl_getThreadTextEncoding());
261 m_xDialog
->set_title(*pStrTitle
);
263 m_xDialog
->SetInstallLOKNotifierHdl(LINK(this, ScImportOptionsDlg
, InstallLOKNotifierHdl
));
266 IMPL_STATIC_LINK_NOARG(ScImportOptionsDlg
, InstallLOKNotifierHdl
, void*, vcl::ILibreOfficeKitNotifier
*)
271 ScImportOptionsDlg::~ScImportOptionsDlg()
275 void ScImportOptionsDlg::GetImportOptions( ScImportOptions
& rOptions
) const
277 auto nEncoding
= m_bIsAsciiImport
? m_xLbCharset
->GetSelectTextEncoding() : m_xTvCharset
->GetSelectTextEncoding();
278 rOptions
.SetTextEncoding(nEncoding
);
280 if (m_xCbFixed
->get_visible())
282 rOptions
.nFieldSepCode
= GetCodeFromCombo( *m_xEdFieldSep
);
283 rOptions
.nTextSepCode
= GetCodeFromCombo( *m_xEdTextSep
);
284 rOptions
.bFixedWidth
= m_xCbFixed
->get_active();
285 rOptions
.bSaveAsShown
= m_xCbShown
->get_active();
286 rOptions
.bQuoteAllText
= m_xCbQuoteAll
->get_active();
287 rOptions
.bSaveFormulas
= m_xCbFormulas
->get_active();
291 sal_uInt16
ScImportOptionsDlg::GetCodeFromCombo(const weld::ComboBox
& rEd
) const
293 ScDelimiterTable
* pTab
;
294 OUString
aStr( rEd
.get_active_text() );
297 if (&rEd
== m_xEdTextSep
.get())
298 pTab
= pTextSepTab
.get();
300 pTab
= pFieldSepTab
.get();
302 if ( aStr
.isEmpty() )
304 nCode
= 0; // no separator
308 nCode
= pTab
->GetCode( aStr
);
311 nCode
= static_cast<sal_uInt16
>(aStr
[0]);
317 IMPL_LINK_NOARG(ScImportOptionsDlg
, FixedWidthHdl
, weld::Toggleable
&, void)
319 bool bEnable
= !m_xCbFixed
->get_active();
320 m_xFtFieldSep
->set_sensitive( bEnable
);
321 m_xEdFieldSep
->set_sensitive( bEnable
);
322 m_xFtTextSep
->set_sensitive( bEnable
);
323 m_xEdTextSep
->set_sensitive( bEnable
);
324 m_xCbShown
->set_sensitive( bEnable
);
325 m_xCbQuoteAll
->set_sensitive( bEnable
);
328 IMPL_LINK_NOARG(ScImportOptionsDlg
, DoubleClickHdl
, weld::TreeView
&, bool)
330 m_xDialog
->response(RET_OK
);
334 void ScImportOptionsDlg::SaveImportOptions() const
336 std::shared_ptr
< comphelper::ConfigurationChanges
> batch(comphelper::ConfigurationChanges::create());
337 auto nEncoding
= m_bIsAsciiImport
? m_xLbCharset
->GetSelectTextEncoding() : m_xTvCharset
->GetSelectTextEncoding();
338 officecfg::Office::Calc::Dialogs::CSVExport::CharSet::set(nEncoding
, batch
);
339 officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::set(m_xEdFieldSep
->get_active_text(), batch
);
340 officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::set(m_xEdTextSep
->get_active_text(), batch
);
341 officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::set(m_xCbFixed
->get_active(), batch
);
342 officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::set(m_xCbFormulas
->get_active(), batch
);
343 officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::set(m_xCbShown
->get_active(), batch
);
344 officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::set(m_xCbQuoteAll
->get_active(), batch
);
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */