LibreOffice Module sw (master)  1
ascfldlg.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 #include <sal/config.h>
21 
22 #include <utility>
23 
24 #include <hintids.hxx>
25 #include <rtl/textenc.h>
26 #include <i18nlangtag/mslangid.hxx>
27 #include <com/sun/star/i18n/ScriptType.hpp>
28 #include <unotools/lingucfg.hxx>
29 #include <unotools/viewoptions.hxx>
30 #include <sfx2/sfxsids.hrc>
31 #include <sfx2/printer.hxx>
32 #include <sfx2/docfile.hxx>
33 #include <svl/languageoptions.hxx>
34 #include <editeng/langitem.hxx>
35 #include <swtypes.hxx>
36 #include <ascfldlg.hxx>
37 #include <shellio.hxx>
38 #include <docsh.hxx>
39 #include <doc.hxx>
41 
42 #include <vcl/metric.hxx>
43 
44 using namespace ::com::sun::star;
45 
46 namespace
47 {
48 
49 const sal_Unicode cDialogExtraDataClose = '}';
50 const char sDialogImpExtraData[] = "EncImpDlg:{";
51 const char sDialogExpExtraData[] = "EncExpDlg:{";
52 const sal_Int32 nDialogExtraDataLen = 11; // 12345678901
53 
54 }
55 
57  SvStream* pStream )
58  : SfxDialogController(pParent, "modules/swriter/ui/asciifilterdialog.ui", "AsciiFilterDialog")
59  , m_bSaveLineStatus(true)
60  , m_xCharSetLB(new SvxTextEncodingBox(m_xBuilder->weld_combo_box("charset")))
61  , m_xFontFT(m_xBuilder->weld_label("fontft"))
62  , m_xFontLB(m_xBuilder->weld_combo_box("font"))
63  , m_xLanguageFT(m_xBuilder->weld_label("languageft"))
64  , m_xLanguageLB(new SvxLanguageBox(m_xBuilder->weld_combo_box("language")))
65  , m_xCRLF_RB(m_xBuilder->weld_radio_button("crlf"))
66  , m_xCR_RB(m_xBuilder->weld_radio_button("cr"))
67  , m_xLF_RB(m_xBuilder->weld_radio_button("lf"))
68  , m_xIncludeBOM_CB(m_xBuilder->weld_check_button("includebom"))
69 {
70  m_xFontLB->make_sorted();
71 
72  SwAsciiOptions aOpt;
73  {
74  SvtViewOptions aDlgOpt(EViewType::Dialog, OStringToOUString(m_xDialog->get_help_id(), RTL_TEXTENCODING_UTF8));
75  if (aDlgOpt.Exists())
76  {
77  css::uno::Any aUserItem = aDlgOpt.GetUserItem("UserItem");
78  aUserItem >>= m_sExtraData;
79  }
80 
81  const SfxStringItem* pItem;
82  OUString sAsciiOptions;
83  if( rDocSh.GetMedium() != nullptr &&
84  rDocSh.GetMedium()->GetItemSet() != nullptr &&
85  (pItem = rDocSh.GetMedium()->GetItemSet()->GetItemIfSet( SID_FILE_FILTEROPTIONS )))
86  {
87  sAsciiOptions = pItem->GetValue();
88  }
89 
90  const OUString sFindNm = OUString::createFromAscii(
91  pStream ? sDialogImpExtraData
92  : sDialogExpExtraData);
93  sal_Int32 nStt = m_sExtraData.indexOf( sFindNm );
94  if( -1 != nStt )
95  {
96  nStt += nDialogExtraDataLen;
97  sal_Int32 nEnd = m_sExtraData.indexOf( cDialogExtraDataClose, nStt );
98  if( -1 != nEnd )
99  {
100  if(sAsciiOptions.isEmpty())
101  sAsciiOptions = m_sExtraData.copy(nStt, nEnd - nStt);
102  nStt -= nDialogExtraDataLen;
103  m_sExtraData = m_sExtraData.replaceAt(nStt, nEnd - nStt + 1, u"");
104  }
105  }
106  if(!sAsciiOptions.isEmpty())
107  aOpt.ReadUserData(sAsciiOptions);
108  }
109 
110  // read the first chars and check the charset, (language - with L&H)
111  if( pStream )
112  {
113  char aBuffer[ 4098 ];
114  const sal_uInt64 nOldPos = pStream->Tell();
115  const size_t nBytesRead = pStream->ReadBytes(aBuffer, 4096);
116  pStream->Seek( nOldPos );
117 
118  if( nBytesRead <= 4096 )
119  {
120  aBuffer[ nBytesRead ] = '0';
121  aBuffer[ nBytesRead+1 ] = '0';
122  }
123 
124  bool bCR = false, bLF = false, bNullChar = false;
125  for( sal_uInt64 nCnt = 0; nCnt < nBytesRead; ++nCnt )
126  switch( aBuffer[ nCnt ] )
127  {
128  case 0x0: bNullChar = true; break;
129  case 0xA: bLF = true; break;
130  case 0xD: bCR = true; break;
131  case 0xC:
132  case 0x1A:
133  case 0x9: break;
134  default: break;
135  }
136 
137  if( !bNullChar )
138  {
139  if( bCR )
140  {
141  if( bLF )
142  {
143  aOpt.SetParaFlags( LINEEND_CRLF );
144  }
145  else
146  {
147  aOpt.SetParaFlags( LINEEND_CR );
148  }
149  }
150  else if( bLF )
151  {
152  aOpt.SetParaFlags( LINEEND_LF );
153  }
154  }
155 
156  const sal_uInt16 nAppScriptType = SvtLanguageOptions::GetI18NScriptTypeOfLanguage( GetAppLanguage() );
157  SwDoc* pDoc = rDocSh.GetDoc();
158 
159  // initialize language
160  {
161  if( !aOpt.GetLanguage() )
162  {
163  if(pDoc)
164  {
165  const sal_uInt16 nWhich = GetWhichOfScript( RES_CHRATR_LANGUAGE, nAppScriptType);
166  aOpt.SetLanguage( static_cast<const SvxLanguageItem&>(pDoc->
167  GetDefault( nWhich )).GetLanguage());
168  }
169  else
170  {
171  SvtLinguOptions aLinguOpt;
172  SvtLinguConfig().GetOptions( aLinguOpt );
173  switch(nAppScriptType)
174  {
175  case css::i18n::ScriptType::ASIAN:
176  aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, css::i18n::ScriptType::ASIAN));
177  break;
178  case css::i18n::ScriptType::COMPLEX:
179  aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, css::i18n::ScriptType::COMPLEX));
180  break;
181  //SvtScriptType::LATIN:
182  default:
183  aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, css::i18n::ScriptType::LATIN));
184  }
185  }
186  }
187 
188  m_xLanguageLB->SetLanguageList( SvxLanguageListFlags::ALL, true );
189  m_xLanguageLB->set_active_id(aOpt.GetLanguage());
190  }
191 
192  {
193  bool bDelPrinter = false;
194  VclPtr<SfxPrinter> pPrt = pDoc ? pDoc->getIDocumentDeviceAccess().getPrinter(false) : nullptr;
195  if( !pPrt )
196  {
197  auto pSet = std::make_unique<SfxItemSetFixed
198  <SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
199  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC>>( rDocSh.GetPool() );
200  pPrt = VclPtr<SfxPrinter>::Create( std::move(pSet) );
201  bDelPrinter = true;
202  }
203 
204  // get the set of distinct available family names
205  std::set< OUString > aFontNames;
206  int nFontNames = pPrt->GetFontFaceCollectionCount();
207  for( int i = 0; i < nFontNames; i++ )
208  {
209  FontMetric aFontMetric( pPrt->GetFontMetricFromCollection( i ) );
210  aFontNames.insert( aFontMetric.GetFamilyName() );
211  }
212 
213  // insert into listbox
214  for( const auto& rFontName : aFontNames )
215  {
216  m_xFontLB->append_text(rFontName);
217  }
218 
219  if( aOpt.GetFontName().isEmpty() )
220  {
221  LanguageType eLang = aOpt.GetLanguage();
222  vcl::Font aTmpFont(OutputDevice::GetDefaultFont(DefaultFontType::FIXED, eLang, GetDefaultFontFlags::OnlyOne, pPrt));
223  aOpt.SetFontName(aTmpFont.GetFamilyName());
224  }
225 
226  m_xFontLB->set_active_text(aOpt.GetFontName());
227 
228  if( bDelPrinter )
229  pPrt.disposeAndClear();
230  }
231 
232  // hide the unused Controls for Export
233  m_xIncludeBOM_CB->hide();
234  }
235  else
236  {
237  // hide the unused Controls for Export
238  m_xFontFT->hide();
239  m_xFontLB->hide();
240  m_xLanguageFT->hide();
241  m_xLanguageLB->hide();
242 
243 
245  m_xIncludeBOM_CB->save_state();
246  }
247 
248  // initialize character set
249  m_xCharSetLB->FillFromTextEncodingTable( pStream != nullptr );
250  m_xCharSetLB->SelectTextEncoding( aOpt.GetCharSet() );
251 
252  m_xCharSetLB->connect_changed( LINK( this, SwAsciiFilterDlg, CharSetSelHdl ));
253  m_xCRLF_RB->connect_toggled( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
254  m_xLF_RB->connect_toggled( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
255  m_xCR_RB->connect_toggled( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
256 
257  SetCRLF( aOpt.GetParaFlags() );
258 
259  m_xCRLF_RB->save_state();
260  m_xLF_RB->save_state();
261  m_xCR_RB->save_state();
262 
264 }
265 
267 {
268  SvtViewOptions aDlgOpt(EViewType::Dialog, OStringToOUString(m_xDialog->get_help_id(), RTL_TEXTENCODING_UTF8));
269  aDlgOpt.SetUserItem("UserItem", uno::Any(m_sExtraData));
270 }
271 
273 {
274  sal_uLong nCCode = m_xCharSetLB->GetSelectTextEncoding();
275  OUString sFont;
277  if (m_xFontLB->get_visible())
278  {
279  sFont = m_xFontLB->get_active_text();
280  nLng = m_xLanguageLB->get_active_id();
281  }
282 
283  rOptions.SetFontName( sFont );
284  rOptions.SetCharSet( rtl_TextEncoding( nCCode ) );
285  rOptions.SetLanguage( nLng );
286  rOptions.SetParaFlags( GetCRLF() );
287  rOptions.SetIncludeBOM( GetIncludeBOM() );
288 
289  // save the user settings
290  OUString sData;
291  rOptions.WriteUserData( sData );
292  if (sData.isEmpty())
293  return;
294 
295  const OUString sFindNm = OUString::createFromAscii(
296  m_xFontLB->get_visible() ? sDialogImpExtraData
297  : sDialogExpExtraData);
298  sal_Int32 nStt = m_sExtraData.indexOf( sFindNm );
299  if( -1 != nStt )
300  {
301  // called twice, so remove "old" settings
302  sal_Int32 nEnd = m_sExtraData.indexOf( cDialogExtraDataClose,
303  nStt + nDialogExtraDataLen );
304  if( -1 != nEnd )
305  m_sExtraData = m_sExtraData.replaceAt( nStt, nEnd - nStt + 1, u"" );
306  }
307  m_sExtraData += sFindNm + sData + OUStringChar(cDialogExtraDataClose);
308 }
309 
311 {
312  switch (eEnd)
313  {
314  case LINEEND_CR:
315  m_xCR_RB->set_active(true);
316  break;
317  case LINEEND_CRLF:
318  m_xCRLF_RB->set_active(true);
319  break;
320  case LINEEND_LF:
321  m_xLF_RB->set_active(true);
322  break;
323  }
324 }
325 
327 {
328  LineEnd eEnd;
329  if(m_xCR_RB->get_active())
330  eEnd = LINEEND_CR;
331  else if (m_xLF_RB->get_active())
332  eEnd = LINEEND_LF;
333  else
334  eEnd = LINEEND_CRLF;
335  return eEnd;
336 }
337 
338 void SwAsciiFilterDlg::SetIncludeBOM( bool bIncludeBOM )
339 {
340  m_xIncludeBOM_CB->set_state(bIncludeBOM ? TRISTATE_TRUE : TRISTATE_FALSE);
341 }
342 
344 {
345  return m_xIncludeBOM_CB->get_state() != TRISTATE_FALSE;
346 }
347 
349 {
350  if (!m_xIncludeBOM_CB->get_visible())
351  return;
352 
353  switch (m_xCharSetLB->GetSelectTextEncoding())
354  {
355  case RTL_TEXTENCODING_UTF8:
356  case RTL_TEXTENCODING_UCS2:
357  m_xIncludeBOM_CB->set_sensitive(true);
358  break;
359  default:
360  m_xIncludeBOM_CB->set_sensitive(false);
361  break;
362  }
363 }
364 
366 {
367  LineEnd eOldEnd = GetCRLF(), eEnd = LineEnd(-1);
368  LanguageType nLng = m_xFontLB->get_visible()
369  ? m_xLanguageLB->get_active_id()
370  : LANGUAGE_SYSTEM,
371  nOldLng = nLng;
372 
373  rtl_TextEncoding nChrSet = m_xCharSetLB->GetSelectTextEncoding();
374  if( nChrSet == osl_getThreadTextEncoding() )
375  eEnd = GetSystemLineEnd();
376  else
377  {
378  switch( nChrSet )
379  {
380  case RTL_TEXTENCODING_MS_1252:
381 #ifdef UNX
382  eEnd = LINEEND_LF;
383 #else
384  eEnd = LINEEND_CRLF; // ANSI
385 #endif
386  break;
387 
388  case RTL_TEXTENCODING_APPLE_ROMAN: // MAC
389  eEnd = LINEEND_CR;
390  break;
391 
392  case RTL_TEXTENCODING_IBM_850: // DOS
393  eEnd = LINEEND_CRLF;
394  break;
395 
396  case RTL_TEXTENCODING_APPLE_ARABIC:
397  case RTL_TEXTENCODING_APPLE_CENTEURO:
398  case RTL_TEXTENCODING_APPLE_CROATIAN:
399  case RTL_TEXTENCODING_APPLE_CYRILLIC:
400  case RTL_TEXTENCODING_APPLE_DEVANAGARI:
401  case RTL_TEXTENCODING_APPLE_FARSI:
402  case RTL_TEXTENCODING_APPLE_GREEK:
403  case RTL_TEXTENCODING_APPLE_GUJARATI:
404  case RTL_TEXTENCODING_APPLE_GURMUKHI:
405  case RTL_TEXTENCODING_APPLE_HEBREW:
406  case RTL_TEXTENCODING_APPLE_ICELAND:
407  case RTL_TEXTENCODING_APPLE_ROMANIAN:
408  case RTL_TEXTENCODING_APPLE_THAI:
409  case RTL_TEXTENCODING_APPLE_TURKISH:
410  case RTL_TEXTENCODING_APPLE_UKRAINIAN:
411  case RTL_TEXTENCODING_APPLE_CHINSIMP:
412  case RTL_TEXTENCODING_APPLE_CHINTRAD:
413  case RTL_TEXTENCODING_APPLE_JAPANESE:
414  case RTL_TEXTENCODING_APPLE_KOREAN:
415  eEnd = LINEEND_CR;
416  break;
417  }
418  }
419 
420  m_bSaveLineStatus = false;
421  if( eEnd != LineEnd(-1) ) // changed?
422  {
423  if( eOldEnd != eEnd )
424  SetCRLF( eEnd );
425  }
426  else
427  {
428  // restore old user choice (not the automatic!)
429  m_xCRLF_RB->set_state(m_xCRLF_RB->get_saved_state());
430  m_xCR_RB->set_state(m_xCR_RB->get_saved_state());
431  m_xLF_RB->set_state(m_xLF_RB->get_saved_state());
432  }
433  m_bSaveLineStatus = true;
434 
435  if (nOldLng != nLng && m_xFontLB->get_visible())
436  m_xLanguageLB->set_active_id(nLng);
437 
438  UpdateIncludeBOMSensitiveState();
439 }
440 
441 IMPL_LINK(SwAsciiFilterDlg, LineEndHdl, weld::Toggleable&, rBtn, void)
442 {
443  if (m_bSaveLineStatus)
444  rBtn.save_state();
445 }
446 
447 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetUserItem(const OUString &sName, const css::uno::Any &aValue)
rtl_TextEncoding GetCharSet() const
Definition: shellio.hxx:72
static vcl::Font GetDefaultFont(DefaultFontType nType, LanguageType eLang, GetDefaultFontFlags nFlags, const OutputDevice *pOutDev=nullptr)
bool GetIncludeBOM() const
Definition: shellio.hxx:81
const T * GetItemIfSet(TypedWhichId< T > nWhich, bool bSrchInParent=true) const
const OUString & GetFamilyName() const
IMPL_LINK_NOARG(SwAsciiFilterDlg, CharSetSelHdl, weld::ComboBox &, void)
Definition: ascfldlg.cxx:365
std::unique_ptr< weld::Label > m_xLanguageFT
Definition: ascfldlg.hxx:39
std::unique_ptr< weld::Label > m_xFontFT
Definition: ascfldlg.hxx:37
IDocumentDeviceAccess const & getIDocumentDeviceAccess() const
Definition: doc.cxx:238
const OUString & GetFontName() const
Definition: shellio.hxx:69
sal_uIntPtr sal_uLong
constexpr TypedWhichId< SvxLanguageItem > RES_CHRATR_LANGUAGE(10)
void disposeAndClear()
LanguageType GetLanguage(SfxItemSet const &aSet, sal_uInt16 nLangWhichId)
Definition: langhelper.cxx:390
Definition: doc.hxx:187
IMPL_LINK(SwAsciiFilterDlg, LineEndHdl, weld::Toggleable &, rBtn, void)
Definition: ascfldlg.cxx:441
LanguageType GetLanguage() const
Definition: shellio.hxx:75
LanguageType nDefaultLanguage_CJK
sal_uInt64 Seek(sal_uInt64 nPos)
static LanguageType resolveSystemLanguageByScriptType(LanguageType nLang, sal_Int16 nType)
void SetParaFlags(LineEnd eVal)
Definition: shellio.hxx:79
LINEEND_CR
TRISTATE_TRUE
std::unique_ptr< SvxLanguageBox > m_xLanguageLB
Definition: ascfldlg.hxx:40
void WriteUserData(OUString &) const
Definition: fltini.cxx:573
void ReadUserData(std::u16string_view)
Definition: fltini.cxx:547
void FillOptions(SwAsciiOptions &rOptions)
Definition: ascfldlg.cxx:272
sal_uInt16 sal_Unicode
void SetLanguage(LanguageType nVal)
Definition: shellio.hxx:76
std::unique_ptr< SvxTextEncodingBox > m_xCharSetLB
Definition: ascfldlg.hxx:36
std::unique_ptr< weld::ComboBox > m_xFontLB
Definition: ascfldlg.hxx:38
LineEnd GetCRLF() const
Definition: ascfldlg.cxx:326
LineEnd GetSystemLineEnd()
void SetCRLF(LineEnd eEnd)
Definition: ascfldlg.cxx:310
std::unique_ptr< weld::RadioButton > m_xCR_RB
Definition: ascfldlg.hxx:42
sal_Int16 GetI18NScriptTypeOfLanguage(LanguageType nLang)
SfxItemPool & GetPool() const
void SetCharSet(rtl_TextEncoding nVal)
Definition: shellio.hxx:73
SwDoc * GetDoc()
returns Doc. But be careful!
Definition: docsh.hxx:204
LINEEND_LF
OUString m_sExtraData
Definition: ascfldlg.hxx:34
int i
void GetOptions(SvtLinguOptions &rOptions) const
#define LANGUAGE_SYSTEM
SwAsciiFilterDlg(weld::Window *pParent, SwDocShell &rDocSh, SvStream *pStream)
Definition: ascfldlg.cxx:56
TRISTATE_FALSE
const OUString & GetValue() const
std::unique_ptr< weld::CheckButton > m_xIncludeBOM_CB
Definition: ascfldlg.hxx:44
float u
void SetIncludeBOM(bool bIncludeBOM)
Definition: ascfldlg.cxx:338
sal_uInt16 GetWhichOfScript(sal_uInt16 nWhich, sal_uInt16 nScript)
Definition: hints.cxx:193
SfxItemSet * GetItemSet() const
void UpdateIncludeBOMSensitiveState()
Definition: ascfldlg.cxx:348
virtual SfxPrinter * getPrinter(bool bCreate) const =0
Return the printer set at the document.
LineEnd
std::size_t ReadBytes(void *pData, std::size_t nSize)
std::unique_ptr< char[]> aBuffer
LanguageType nDefaultLanguage
LanguageType GetAppLanguage()
Definition: init.cxx:725
static VclPtr< reference_type > Create(Arg &&...arg)
bool Exists() const
Reference< XExecutableDialog > m_xDialog
sal_uInt64 Tell() const
css::uno::Any GetUserItem(const OUString &sName) const
std::unique_ptr< weld::RadioButton > m_xLF_RB
Definition: ascfldlg.hxx:43
void SetIncludeBOM(bool bVal)
Definition: shellio.hxx:82
LINEEND_CRLF
LineEnd GetParaFlags() const
Definition: shellio.hxx:78
virtual ~SwAsciiFilterDlg() override
Definition: ascfldlg.cxx:266
std::unique_ptr< weld::RadioButton > m_xCRLF_RB
Definition: ascfldlg.hxx:41
LanguageType nDefaultLanguage_CTL
void SetFontName(const OUString &rFont)
Definition: shellio.hxx:70
bool GetIncludeBOM() const
Definition: ascfldlg.cxx:343
SfxMedium * GetMedium() const