LibreOffice Module sc (master)  1
scuiimoptdlg.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #undef SC_DLLIMPLEMENTATION
21 
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>
32 
33 // ScDelimiterTable
34 
36 {
37 public:
38  explicit ScDelimiterTable( const OUString& rDelTab )
39  : theDelTab ( rDelTab ),
40  nDelIdx ( 0 )
41  {}
42 
43  sal_uInt16 GetCode( std::u16string_view rDelimiter ) const;
44  OUString GetDelimiter( sal_Unicode nCode ) const;
45 
46  OUString FirstDel() { nDelIdx = 0; return theDelTab.getToken( 0, cSep, nDelIdx ); }
47  OUString NextDel() { return theDelTab.getToken( 1, cSep, nDelIdx ); }
48 
49 private:
50  const OUString theDelTab;
51  static constexpr sal_Unicode cSep {'\t'};
52  sal_Int32 nDelIdx;
53 };
54 
55 sal_uInt16 ScDelimiterTable::GetCode( std::u16string_view rDel ) const
56 {
57  if (!theDelTab.isEmpty())
58  {
59  sal_Int32 nIdx {0};
60 
61  // Check even tokens: start from 0 and then skip 1 token at each iteration
62  if (rDel != o3tl::getToken(theDelTab, 0, cSep, nIdx ))
63  while (nIdx>0 && rDel != o3tl::getToken(theDelTab, 1, cSep, nIdx ));
64 
65  if (nIdx>0)
66  return static_cast<sal_Unicode>(o3tl::toInt32(o3tl::getToken(theDelTab, 0, cSep, nIdx )));
67  }
68 
69  return 0;
70 }
71 
73 {
74  if (!theDelTab.isEmpty())
75  {
76  sal_Int32 nIdx {0};
77  // Check odd tokens: start from 1 and then skip 1 token at each iteration
78  do
79  {
80  sal_Int32 nPrevIdx {nIdx};
81  if (nCode == static_cast<sal_Unicode>(o3tl::toInt32(o3tl::getToken(theDelTab, 1, cSep, nIdx ))))
82  return theDelTab.getToken( 0, cSep, nPrevIdx );
83  }
84  while (nIdx>0);
85  }
86 
87  return OUString();
88 }
89 
90 void ScImportOptionsDlg::FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
91 {
92  if (m_bIsAsciiImport)
93  m_xLbCharset->FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags);
94  else
95  m_xTvCharset->FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags);
96 }
97 
98 void ScImportOptionsDlg::FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
99 {
100  if (m_bIsAsciiImport)
101  m_xLbCharset->FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
102  else
103  m_xTvCharset->FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
104 }
105 
106 // ScImportOptionsDlg
108  const ScImportOptions* pOptions,
109  const OUString* pStrTitle,
110  bool bMultiByte, bool bOnlyDbtoolsEncodings,
111  bool bImport)
112  : GenericDialogController(pParent, "modules/scalc/ui/imoptdialog.ui", "ImOptDialog")
113  , m_bIsAsciiImport(bAscii)
114  , m_xFieldFrame(m_xBuilder->weld_frame("fieldframe"))
115  , m_xFtCharset(m_xBuilder->weld_label("charsetft"))
116  , m_xEncGrid(m_xBuilder->weld_widget("grid2"))
117  , m_xFtFieldSep(m_xBuilder->weld_label("fieldft"))
118  , m_xEdFieldSep(m_xBuilder->weld_combo_box("field"))
119  , m_xFtTextSep(m_xBuilder->weld_label("textft"))
120  , m_xEdTextSep(m_xBuilder->weld_combo_box("text"))
121  , m_xCbShown(m_xBuilder->weld_check_button("asshown"))
122  , m_xCbFormulas(m_xBuilder->weld_check_button("formulas"))
123  , m_xCbQuoteAll(m_xBuilder->weld_check_button("quoteall"))
124  , m_xCbFixed(m_xBuilder->weld_check_button("fixedwidth"))
125  , m_xBtnOk(m_xBuilder->weld_button("ok"))
126  , m_xLbCharset(new SvxTextEncodingBox(m_xBuilder->weld_combo_box("charsetdropdown")))
127  , m_xTvCharset(new SvxTextEncodingTreeView(m_xBuilder->weld_tree_view("charsetlist")))
128 {
129  if (bAscii)
130  {
131  m_xDialog->set_help_id(m_xDialog->get_help_id() + "?config=NonTextImport");
132  m_xLbCharset->show();
133  m_xTvCharset->hide();
134  }
135  else
136  {
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();
141  }
142 
143  OUString sFieldSep(SCSTR_FIELDSEP);
144  sFieldSep = sFieldSep.replaceFirst( "%TAB", ScResId(SCSTR_FIELDSEP_TAB) );
145  sFieldSep = sFieldSep.replaceFirst( "%SPACE", ScResId(SCSTR_FIELDSEP_SPACE) );
146 
147  // not possible in the Ctor initializer (MSC cannot do that):
148  pFieldSepTab.reset( new ScDelimiterTable(sFieldSep) );
150 
151  OUString aStr = pFieldSepTab->FirstDel();
152 
153  while (!aStr.isEmpty())
154  {
155  m_xEdFieldSep->append_text(aStr);
156  aStr = pFieldSepTab->NextDel();
157  }
158 
159  aStr = pTextSepTab->FirstDel();
160 
161  while (!aStr.isEmpty())
162  {
163  m_xEdTextSep->append_text(aStr);
164  aStr = pTextSepTab->NextDel();
165  }
166 
167  m_xEdFieldSep->set_active(0);
168  m_xEdTextSep->set_active(0);
169 
170  if ( bOnlyDbtoolsEncodings )
171  {
172  // Even dBase export allows multibyte now
173  if ( bMultiByte )
174  FillFromDbTextEncodingMap( bImport );
175  else
176  FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE );
177  }
178  else if ( !bAscii )
179  {
180  if ( bMultiByte )
181  FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE );
182  else
183  FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE |
184  RTL_TEXTENCODING_INFO_MULTIBYTE );
185  }
186  else
187  {
188  if ( pOptions )
189  {
190  sal_Unicode nCode = pOptions->nFieldSepCode;
191  aStr = pFieldSepTab->GetDelimiter( nCode );
192 
193  if ( aStr.isEmpty() )
194  m_xEdFieldSep->set_entry_text(OUString(nCode));
195  else
196  m_xEdFieldSep->set_entry_text(aStr);
197 
198  nCode = pOptions->nTextSepCode;
199  aStr = pTextSepTab->GetDelimiter( nCode );
200 
201  if ( aStr.isEmpty() )
202  m_xEdTextSep->set_entry_text(OUString(nCode));
203  else
204  m_xEdTextSep->set_entry_text(aStr);
205  }
206  // all encodings allowed, even Unicode
207  FillFromTextEncodingTable( bImport );
208  }
209 
210  if( bAscii )
211  {
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();
219 
220  m_xCbFixed->show();
221  m_xCbFixed->connect_toggled(LINK(this, ScImportOptionsDlg, FixedWidthHdl));
222  m_xCbFixed->set_active( bFixedWidth );
223  FixedWidthHdl(*m_xCbFixed);
224  m_xCbShown->show();
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());
238  else
239  m_xLbCharset->SelectTextEncoding(nCharSet);
240  }
241  else
242  {
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();
249  m_xCbFixed->hide();
250  m_xCbShown->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());
256  }
257 
258  // optional title:
259  if (pStrTitle)
260  m_xDialog->set_title(*pStrTitle);
261 }
262 
264 {
265 }
266 
268 {
269  auto nEncoding = m_bIsAsciiImport ? m_xLbCharset->GetSelectTextEncoding() : m_xTvCharset->GetSelectTextEncoding();
270  rOptions.SetTextEncoding(nEncoding);
271 
272  if (m_xCbFixed->get_visible())
273  {
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();
280  }
281 }
282 
284 {
285  ScDelimiterTable* pTab;
286  OUString aStr( rEd.get_active_text() );
287  sal_uInt16 nCode;
288 
289  if (&rEd == m_xEdTextSep.get())
290  pTab = pTextSepTab.get();
291  else
292  pTab = pFieldSepTab.get();
293 
294  if ( aStr.isEmpty() )
295  {
296  nCode = 0; // no separator
297  }
298  else
299  {
300  nCode = pTab->GetCode( aStr );
301 
302  if ( nCode == 0 )
303  nCode = static_cast<sal_uInt16>(aStr[0]);
304  }
305 
306  return nCode;
307 }
308 
310 {
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 );
318 }
319 
321 {
322  m_xDialog->response(RET_OK);
323  return true;
324 }
325 
327 {
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);
337  batch->commit();
338 }
339 
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Unicode nFieldSepCode
Definition: imoptdlg.hxx:44
bool bSaveFormulas
Definition: imoptdlg.hxx:52
std::shared_ptr< weld::Dialog > m_xDialog
ScImportOptionsDlg(weld::Window *pParent, bool bAscii, const ScImportOptions *pOptions, const OUString *pStrTitle, bool bMultiByte, bool bOnlyDbtoolsEncodings, bool bImport)
sal_uInt16 GetCodeFromCombo(const weld::ComboBox &rEd) const
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
const OUString theDelTab
static std::shared_ptr< ConfigurationChanges > create()
std::unique_ptr< weld::Label > m_xFtCharset
std::unique_ptr< weld::CheckButton > m_xCbQuoteAll
std::unique_ptr< weld::CheckButton > m_xCbFixed
std::unique_ptr< ScDelimiterTable > pTextSepTab
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
std::unique_ptr< weld::ComboBox > m_xEdFieldSep
void GetImportOptions(ScImportOptions &rOptions) const
sal_uInt16 GetCode(std::u16string_view rDelimiter) const
sal_uInt16 sal_Unicode
std::unique_ptr< SvxTextEncodingBox > m_xLbCharset
OUString NextDel()
OUString GetDelimiter(sal_Unicode nCode) const
sal_uInt16 nCode
virtual ~ScImportOptionsDlg() override
sal_Unicode nTextSepCode
Definition: imoptdlg.hxx:45
void FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
std::unique_ptr< weld::CheckButton > m_xCbFormulas
std::unique_ptr< SvxTextEncodingTreeView > m_xTvCharset
void SaveImportOptions() const
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
constexpr OUStringLiteral SCSTR_FIELDSEP
Definition: strings.hxx:14
std::unique_ptr< weld::CheckButton > m_xCbShown
void FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
OUString FirstDel()
bool bQuoteAllText
Definition: imoptdlg.hxx:50
std::unique_ptr< ScDelimiterTable > pFieldSepTab
std::unique_ptr< weld::ComboBox > m_xEdTextSep
std::unique_ptr< weld::Label > m_xFtTextSep
RET_OK
Reference< XExecutableDialog > m_xDialog
std::unique_ptr< weld::Widget > m_xEncGrid
void SetTextEncoding(rtl_TextEncoding nEnc)
Definition: imoptdlg.cxx:128
rtl_TextEncoding eCharSet
Definition: imoptdlg.hxx:47
ScDelimiterTable(const OUString &rDelTab)
IMPL_LINK_NOARG(ScImportOptionsDlg, FixedWidthHdl, weld::Toggleable &, void)
aStr
std::unique_ptr< weld::Frame > m_xFieldFrame
constexpr OUStringLiteral SCSTR_TEXTSEP
Definition: strings.hxx:15
std::unique_ptr< weld::Label > m_xFtFieldSep
virtual OUString get_active_text() const =0