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