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