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 
32 // ScDelimiterTable
33 
35 {
36 public:
37  explicit ScDelimiterTable( const OUString& rDelTab )
38  : theDelTab ( rDelTab ),
39  nDelIdx ( 0 )
40  {}
41 
42  sal_uInt16 GetCode( const OUString& rDelimiter ) const;
43  OUString GetDelimiter( sal_Unicode nCode ) const;
44 
45  OUString FirstDel() { nDelIdx = 0; return theDelTab.getToken( 0, cSep, nDelIdx ); }
46  OUString NextDel() { return theDelTab.getToken( 1, cSep, nDelIdx ); }
47 
48 private:
49  const OUString theDelTab;
50  static constexpr sal_Unicode cSep {'\t'};
51  sal_Int32 nDelIdx;
52 };
53 
54 sal_uInt16 ScDelimiterTable::GetCode( const OUString& rDel ) const
55 {
56  if (!theDelTab.isEmpty())
57  {
58  sal_Int32 nIdx {0};
59 
60  // Check even tokens: start from 0 and then skip 1 token at each iteration
61  if (rDel != theDelTab.getToken( 0, cSep, nIdx ))
62  while (nIdx>0 && rDel != theDelTab.getToken( 1, cSep, nIdx ));
63 
64  if (nIdx>0)
65  return static_cast<sal_Unicode>(theDelTab.getToken( 0, cSep, nIdx ).toInt32());
66  }
67 
68  return 0;
69 }
70 
72 {
73  if (!theDelTab.isEmpty())
74  {
75  sal_Int32 nIdx {0};
76  // Check odd tokens: start from 1 and then skip 1 token at each iteration
77  do
78  {
79  sal_Int32 nPrevIdx {nIdx};
80  if (nCode == static_cast<sal_Unicode>(theDelTab.getToken( 1, cSep, nIdx ).toInt32()))
81  return theDelTab.getToken( 0, cSep, nPrevIdx );
82  }
83  while (nIdx>0);
84  }
85 
86  return OUString();
87 }
88 
89 void ScImportOptionsDlg::FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
90 {
91  if (m_bIsAsciiImport)
92  m_xLbCharset->FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags);
93  else
94  m_xTvCharset->FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags);
95 }
96 
97 void ScImportOptionsDlg::FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
98 {
99  if (m_bIsAsciiImport)
100  m_xLbCharset->FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
101  else
102  m_xTvCharset->FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
103 }
104 
105 // ScImportOptionsDlg
107  const ScImportOptions* pOptions,
108  const OUString* pStrTitle,
109  bool bMultiByte, bool bOnlyDbtoolsEncodings,
110  bool bImport)
111  : GenericDialogController(pParent, "modules/scalc/ui/imoptdialog.ui", "ImOptDialog")
112  , m_bIsAsciiImport(bAscii)
113  , m_xFieldFrame(m_xBuilder->weld_frame("fieldframe"))
114  , m_xFtCharset(m_xBuilder->weld_label("charsetft"))
115  , m_xEncGrid(m_xBuilder->weld_widget("grid2"))
116  , m_xFtFieldSep(m_xBuilder->weld_label("fieldft"))
117  , m_xEdFieldSep(m_xBuilder->weld_combo_box("field"))
118  , m_xFtTextSep(m_xBuilder->weld_label("textft"))
119  , m_xEdTextSep(m_xBuilder->weld_combo_box("text"))
120  , m_xCbShown(m_xBuilder->weld_check_button("asshown"))
121  , m_xCbFormulas(m_xBuilder->weld_check_button("formulas"))
122  , m_xCbQuoteAll(m_xBuilder->weld_check_button("quoteall"))
123  , m_xCbFixed(m_xBuilder->weld_check_button("fixedwidth"))
124  , m_xBtnOk(m_xBuilder->weld_button("ok"))
125  , m_xLbCharset(new SvxTextEncodingBox(m_xBuilder->weld_combo_box("charsetdropdown")))
126  , m_xTvCharset(new SvxTextEncodingTreeView(m_xBuilder->weld_tree_view("charsetlist")))
127 {
128  if (bAscii)
129  {
130  m_xDialog->set_help_id(m_xDialog->get_help_id() + "?config=NonTextImport");
131  m_xLbCharset->show();
132  m_xTvCharset->hide();
133  }
134  else
135  {
136  m_xTvCharset->set_size_request(-1, m_xTvCharset->get_height_rows(6));
137  m_xEncGrid->set_vexpand(true);
138  m_xLbCharset->hide();
139  m_xTvCharset->show();
140  }
141 
142  OUString sFieldSep(SCSTR_FIELDSEP);
143  sFieldSep = sFieldSep.replaceFirst( "%TAB", ScResId(SCSTR_FIELDSEP_TAB) );
144  sFieldSep = sFieldSep.replaceFirst( "%SPACE", ScResId(SCSTR_FIELDSEP_SPACE) );
145 
146  // not possible in the Ctor initializer (MSC cannot do that):
147  pFieldSepTab.reset( new ScDelimiterTable(sFieldSep) );
149 
150  OUString aStr = pFieldSepTab->FirstDel();
151 
152  while (!aStr.isEmpty())
153  {
154  m_xEdFieldSep->append_text(aStr);
155  aStr = pFieldSepTab->NextDel();
156  }
157 
158  aStr = pTextSepTab->FirstDel();
159 
160  while (!aStr.isEmpty())
161  {
162  m_xEdTextSep->append_text(aStr);
163  aStr = pTextSepTab->NextDel();
164  }
165 
166  m_xEdFieldSep->set_active(0);
167  m_xEdTextSep->set_active(0);
168 
169  if ( bOnlyDbtoolsEncodings )
170  {
171  // Even dBase export allows multibyte now
172  if ( bMultiByte )
173  FillFromDbTextEncodingMap( bImport );
174  else
175  FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE );
176  }
177  else if ( !bAscii )
178  {
179  if ( bMultiByte )
180  FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE );
181  else
182  FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE |
183  RTL_TEXTENCODING_INFO_MULTIBYTE );
184  }
185  else
186  {
187  if ( pOptions )
188  {
189  sal_Unicode nCode = pOptions->nFieldSepCode;
190  aStr = pFieldSepTab->GetDelimiter( nCode );
191 
192  if ( aStr.isEmpty() )
193  m_xEdFieldSep->set_entry_text(OUString(nCode));
194  else
195  m_xEdFieldSep->set_entry_text(aStr);
196 
197  nCode = pOptions->nTextSepCode;
198  aStr = pTextSepTab->GetDelimiter( nCode );
199 
200  if ( aStr.isEmpty() )
201  m_xEdTextSep->set_entry_text(OUString(nCode));
202  else
203  m_xEdTextSep->set_entry_text(aStr);
204  }
205  // all encodings allowed, even Unicode
206  FillFromTextEncodingTable( bImport );
207  }
208 
209  if( bAscii )
210  {
211  sal_Int32 nCharSet = officecfg::Office::Calc::Dialogs::CSVExport::CharSet::get();
212  OUString strFieldSeparator = officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::get();
213  OUString strTextSeparator = officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::get();
214  bool bSaveTrueCellContent = officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::get();
215  bool bSaveCellFormulas = officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::get();
216  bool bQuoteAllTextCells = officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::get();
217  bool bFixedWidth = officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::get();
218 
219  m_xCbFixed->show();
220  m_xCbFixed->connect_toggled(LINK(this, ScImportOptionsDlg, FixedWidthHdl));
221  m_xCbFixed->set_active( bFixedWidth );
222  FixedWidthHdl(*m_xCbFixed);
223  m_xCbShown->show();
224  m_xCbShown->set_active( bSaveTrueCellContent );
225  m_xCbQuoteAll->show();
226  m_xCbQuoteAll->set_active( bQuoteAllTextCells );
227  m_xCbFormulas->show();
228  // default option for "save formulas" no longer taken from view shell but from persisted dialog settings
229  m_xCbFormulas->set_active( bSaveCellFormulas );
230  // if no charset, text separator or field separator exist, keep the values from dialog initialization
231  if (strFieldSeparator.getLength() > 0)
232  m_xEdFieldSep->set_entry_text(strFieldSeparator);
233  if (strTextSeparator.getLength() > 0)
234  m_xEdTextSep->set_entry_text(strTextSeparator);
235  if (nCharSet < 0 || nCharSet == RTL_TEXTENCODING_DONTKNOW )
236  m_xLbCharset->SelectTextEncoding(pOptions ? pOptions->eCharSet : osl_getThreadTextEncoding());
237  else
238  m_xLbCharset->SelectTextEncoding(nCharSet);
239  }
240  else
241  {
242  m_xFieldFrame->set_label(m_xFtCharset->get_label());
243  m_xFtFieldSep->hide();
244  m_xFtTextSep->hide();
245  m_xFtCharset->hide();
246  m_xEdFieldSep->hide();
247  m_xEdTextSep->hide();
248  m_xCbFixed->hide();
249  m_xCbShown->hide();
250  m_xCbQuoteAll->hide();
251  m_xCbFormulas->hide();
252  m_xTvCharset->grab_focus();
253  m_xTvCharset->connect_row_activated(LINK(this, ScImportOptionsDlg, DoubleClickHdl));
254  m_xTvCharset->SelectTextEncoding(pOptions ? pOptions->eCharSet : osl_getThreadTextEncoding());
255  }
256 
257  // optional title:
258  if (pStrTitle)
259  m_xDialog->set_title(*pStrTitle);
260 }
261 
263 {
264 }
265 
267 {
268  auto nEncoding = m_bIsAsciiImport ? m_xLbCharset->GetSelectTextEncoding() : m_xTvCharset->GetSelectTextEncoding();
269  rOptions.SetTextEncoding(nEncoding);
270 
271  if (m_xCbFixed->get_visible())
272  {
275  rOptions.bFixedWidth = m_xCbFixed->get_active();
276  rOptions.bSaveAsShown = m_xCbShown->get_active();
277  rOptions.bQuoteAllText = m_xCbQuoteAll->get_active();
278  rOptions.bSaveFormulas = m_xCbFormulas->get_active();
279  }
280 }
281 
283 {
284  ScDelimiterTable* pTab;
285  OUString aStr( rEd.get_active_text() );
286  sal_uInt16 nCode;
287 
288  if (&rEd == m_xEdTextSep.get())
289  pTab = pTextSepTab.get();
290  else
291  pTab = pFieldSepTab.get();
292 
293  if ( aStr.isEmpty() )
294  {
295  nCode = 0; // no separator
296  }
297  else
298  {
299  nCode = pTab->GetCode( aStr );
300 
301  if ( nCode == 0 )
302  nCode = static_cast<sal_uInt16>(aStr[0]);
303  }
304 
305  return nCode;
306 }
307 
309 {
310  bool bEnable = !m_xCbFixed->get_active();
311  m_xFtFieldSep->set_sensitive( bEnable );
312  m_xEdFieldSep->set_sensitive( bEnable );
313  m_xFtTextSep->set_sensitive( bEnable );
314  m_xEdTextSep->set_sensitive( bEnable );
315  m_xCbShown->set_sensitive( bEnable );
316  m_xCbQuoteAll->set_sensitive( bEnable );
317 }
318 
320 {
321  m_xDialog->response(RET_OK);
322  return true;
323 }
324 
326 {
327  std::shared_ptr < comphelper::ConfigurationChanges > batch(comphelper::ConfigurationChanges::create());
328  auto nEncoding = m_bIsAsciiImport ? m_xLbCharset->GetSelectTextEncoding() : m_xTvCharset->GetSelectTextEncoding();
329  officecfg::Office::Calc::Dialogs::CSVExport::CharSet::set(nEncoding, batch);
330  officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::set(m_xEdFieldSep->get_active_text(), batch);
331  officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::set(m_xEdTextSep->get_active_text(), batch);
332  officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::set(m_xCbFixed->get_active(), batch);
333  officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::set(m_xCbFormulas->get_active(), batch);
334  officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::set(m_xCbShown->get_active(), batch);
335  officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::set(m_xCbQuoteAll->get_active(), batch);
336  batch->commit();
337 }
338 
339 /* 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
const OUString theDelTab
IMPL_LINK_NOARG(ScImportOptionsDlg, FixedWidthHdl, weld::ToggleButton &, void)
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::unique_ptr< weld::ComboBox > m_xEdFieldSep
void GetImportOptions(ScImportOptions &rOptions) const
sal_uInt16 sal_Unicode
std::unique_ptr< SvxTextEncodingBox > m_xLbCharset
OUString NextDel()
static std::shared_ptr< ConfigurationChanges > create(css::uno::Reference< css::uno::XComponentContext > const &context=comphelper::getProcessComponentContext())
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
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
std::unique_ptr< SvxTextEncodingTreeView > m_xTvCharset
void SaveImportOptions() const
std::unique_ptr< weld::CheckButton > m_xCbShown
void FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
#define SCSTR_TEXTSEP
Definition: strings.hxx:13
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
sal_uInt16 GetCode(const OUString &rDelimiter) const
Reference< XExecutableDialog > m_xDialog
std::unique_ptr< weld::Widget > m_xEncGrid
void SetTextEncoding(rtl_TextEncoding nEnc)
Definition: imoptdlg.cxx:107
rtl_TextEncoding eCharSet
Definition: imoptdlg.hxx:47
ScDelimiterTable(const OUString &rDelTab)
#define SCSTR_FIELDSEP
Definition: strings.hxx:12
aStr
std::unique_ptr< weld::Frame > m_xFieldFrame
std::unique_ptr< weld::Label > m_xFtFieldSep
virtual OUString get_active_text() const =0