LibreOffice Module svx (master)  1
txencbox.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 <config_features.h>
21 
22 #include <svx/txencbox.hxx>
23 #include <svx/dialmgr.hxx>
24 #include <svx/txenctab.hxx>
25 #if HAVE_FEATURE_DBCONNECTIVITY
26 #include <dbcharsethelper.hxx>
27 #endif
28 #include <unotools/syslocale.hxx>
29 #include <rtl/tencinfo.h>
30 #include <sal/log.hxx>
31 #include <txenctab.hrc>
32 
33 namespace
34 {
35  std::vector<rtl_TextEncoding> FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
36  {
37  std::vector<rtl_TextEncoding> aRet;
38 #if !HAVE_FEATURE_DBCONNECTIVITY
39  (void)bExcludeImportSubsets;
40  (void)nExcludeInfoFlags;
41 #else
42  rtl_TextEncodingInfo aInfo;
43  aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
44  ::std::vector< rtl_TextEncoding > aEncs;
46  for ( sal_Int32 j=0; j<nCount; j++ )
47  {
48  bool bInsert = true;
49  rtl_TextEncoding nEnc = rtl_TextEncoding( aEncs[j] );
50  if ( nExcludeInfoFlags )
51  {
52  if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
53  bInsert = false;
54  else
55  {
56  if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
57  {
58  if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
59  ((nEnc == RTL_TEXTENCODING_UCS2) ||
60  nEnc == RTL_TEXTENCODING_UCS4) )
61  bInsert = false; // InfoFlags don't work for Unicode :-(
62  }
63  else
64  bInsert = false;
65  }
66  }
67  if ( bInsert )
68  {
69  if ( bExcludeImportSubsets )
70  {
71  switch ( nEnc )
72  {
73  // subsets of RTL_TEXTENCODING_GB_18030
74  case RTL_TEXTENCODING_GB_2312 :
75  case RTL_TEXTENCODING_GBK :
76  case RTL_TEXTENCODING_MS_936 :
77  bInsert = false;
78  break;
79  }
80  }
81  // CharsetMap offers a RTL_TEXTENCODING_DONTKNOW for internal use,
82  // makes no sense here and would result in an empty string as list
83  // entry.
84  if ( bInsert && nEnc != RTL_TEXTENCODING_DONTKNOW )
85  aRet.push_back(nEnc);
86  }
87  }
88 #endif
89  return aRet;
90  }
91 }
92 
94  bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
95 {
96  m_xControl->freeze();
97  auto aEncs = ::FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
98  for (auto nEnc : aEncs)
99  InsertTextEncoding(nEnc);
100  m_xControl->thaw();
101 }
102 
104  bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
105 {
106  m_xControl->freeze();
107  auto aEncs = ::FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
108  for (auto nEnc : aEncs)
109  InsertTextEncoding(nEnc);
110  m_xControl->thaw();
111 }
112 
113 SvxTextEncodingBox::SvxTextEncodingBox(std::unique_ptr<weld::ComboBox> pControl)
114  : m_xControl(std::move(pControl))
115 {
116  m_xControl->make_sorted();
117 }
118 
119 SvxTextEncodingTreeView::SvxTextEncodingTreeView(std::unique_ptr<weld::TreeView> pControl)
120  : m_xControl(std::move(pControl))
121 {
122  m_xControl->make_sorted();
123 }
124 
126 {
127 }
128 
130 {
131 }
132 
133 namespace
134 {
135  std::vector<int> FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags, sal_uInt32 nButIncludeInfoFlags)
136  {
137  std::vector<int> aRet;
138 
139  rtl_TextEncodingInfo aInfo;
140  aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
141  const sal_uInt32 nCount = SAL_N_ELEMENTS(RID_SVXSTR_TEXTENCODING_TABLE);
142  for (sal_uInt32 j = 0; j < nCount; ++j)
143  {
144  bool bInsert = true;
145  rtl_TextEncoding nEnc = RID_SVXSTR_TEXTENCODING_TABLE[j].second;
146  if ( nExcludeInfoFlags )
147  {
148  if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
149  bInsert = false;
150  else
151  {
152  if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
153  {
154  if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
155  ((nEnc == RTL_TEXTENCODING_UCS2) ||
156  nEnc == RTL_TEXTENCODING_UCS4) )
157  bInsert = false; // InfoFlags don't work for Unicode :-(
158  }
159  else if ( (aInfo.Flags & nButIncludeInfoFlags) == 0 )
160  bInsert = false;
161  }
162  }
163  if ( bExcludeImportSubsets )
164  {
165  switch ( nEnc )
166  {
167  // subsets of RTL_TEXTENCODING_GB_18030
168  case RTL_TEXTENCODING_GB_2312 :
169  case RTL_TEXTENCODING_GBK :
170  case RTL_TEXTENCODING_MS_936 :
171  bInsert = false;
172  break;
173  }
174  }
175  if ( bInsert )
176  aRet.push_back(j);
177  }
178  return aRet;
179  }
180 }
181 
183  bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags,
184  sal_uInt32 nButIncludeInfoFlags )
185 {
186  std::vector<int> aRet(::FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags, nButIncludeInfoFlags));
187  m_xControl->freeze();
188  for (auto j : aRet)
189  {
190  rtl_TextEncoding nEnc = RID_SVXSTR_TEXTENCODING_TABLE[j].second;
191  InsertTextEncoding(nEnc, SvxResId(RID_SVXSTR_TEXTENCODING_TABLE[j].first));
192  }
193  m_xControl->thaw();
194 }
195 
197  bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
198 {
199  std::vector<int> aRet(::FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags, /*nButIncludeInfoFlags*/0));
200  m_xControl->freeze();
201  for (auto j : aRet)
202  {
203  rtl_TextEncoding nEnc = RID_SVXSTR_TEXTENCODING_TABLE[j].second;
204  InsertTextEncoding(nEnc, SvxResId(RID_SVXSTR_TEXTENCODING_TABLE[j].first));
205  }
206  m_xControl->thaw();
207 }
208 
210 {
211  FillFromTextEncodingTable( false, 0xffffffff, RTL_TEXTENCODING_INFO_MIME );
212  rtl_TextEncoding nEnc = SvtSysLocale::GetBestMimeEncoding();
213  SelectTextEncoding( nEnc );
214 }
215 
216 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc,
217  const OUString& rEntry )
218 {
219  m_xControl->append(OUString::number(nEnc), rEntry);
220 }
221 
222 void SvxTextEncodingTreeView::InsertTextEncoding( const rtl_TextEncoding nEnc,
223  const OUString& rEntry )
224 {
225  m_xControl->append(OUString::number(nEnc), rEntry);
226 }
227 
228 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc )
229 {
230  const OUString& rEntry = SvxTextEncodingTable::GetTextString(nEnc);
231  if (!rEntry.isEmpty())
232  InsertTextEncoding( nEnc, rEntry );
233  else
234  SAL_WARN( "svx.dialog", "SvxTextEncodingBox::InsertTextEncoding: no resource string for text encoding: " << static_cast<sal_Int32>( nEnc ) );
235 }
236 
237 void SvxTextEncodingTreeView::InsertTextEncoding( const rtl_TextEncoding nEnc )
238 {
239  const OUString& rEntry = SvxTextEncodingTable::GetTextString(nEnc);
240  if (!rEntry.isEmpty())
241  InsertTextEncoding( nEnc, rEntry );
242  else
243  SAL_WARN( "svx.dialog", "SvxTextEncodingTreeView::InsertTextEncoding: no resource string for text encoding: " << static_cast<sal_Int32>( nEnc ) );
244 }
245 
247 {
248  OUString sId(m_xControl->get_active_id());
249  if (!sId.isEmpty())
250  return rtl_TextEncoding(sId.toInt32());
251  else
252  return RTL_TEXTENCODING_DONTKNOW;
253 }
254 
256 {
257  OUString sId(m_xControl->get_selected_id());
258  if (!sId.isEmpty())
259  return rtl_TextEncoding(sId.toInt32());
260  else
261  return RTL_TEXTENCODING_DONTKNOW;
262 }
263 
264 void SvxTextEncodingBox::SelectTextEncoding( const rtl_TextEncoding nEnc )
265 {
266  m_xControl->set_active_id(OUString::number(nEnc));
267 }
268 
269 void SvxTextEncodingTreeView::SelectTextEncoding( const rtl_TextEncoding nEnc )
270 {
271  m_xControl->select_id(OUString::number(nEnc));
272 }
273 
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::ComboBox > m_xControl
Definition: txencbox.hxx:29
Reference< XControl > m_xControl
void SelectTextEncoding(const rtl_TextEncoding nEnc)
Definition: txencbox.cxx:269
rtl_TextEncoding GetSelectTextEncoding() const
Definition: txencbox.cxx:246
void FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0, sal_uInt32 nButIncludeInfoFlags=0)
Fill with all known encodings but exclude those matching one or more given flags as defined in rtl/te...
Definition: txencbox.cxx:182
sal_Int32 getSupportedTextEncodings(::std::vector< rtl_TextEncoding > &_rEncs)
enumerates all supported charsets
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:28
int nCount
void SelectTextEncoding(const rtl_TextEncoding nEnc)
Definition: txencbox.cxx:264
void FillWithMimeAndSelectBest()
Fill with all known MIME encodings and select the best according to GetBestMimeEncoding
Definition: txencbox.cxx:209
#define SAL_N_ELEMENTS(arr)
rtl_TextEncoding GetSelectTextEncoding() const
Definition: txencbox.cxx:255
void InsertTextEncoding(const rtl_TextEncoding nEnc)
Definition: txencbox.cxx:237
void InsertTextEncoding(const rtl_TextEncoding nEnc)
Definition: txencbox.cxx:228
void FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
Fill with all encodings known to the dbtools::OCharsetMap but exclude those matching one or more give...
Definition: txencbox.cxx:103
std::unique_ptr< weld::TreeView > m_xControl
Definition: txencbox.hxx:102
SvxTextEncodingTreeView(std::unique_ptr< weld::TreeView > pControl)
Definition: txencbox.cxx:119
SvxTextEncodingBox(std::unique_ptr< weld::ComboBox > pControl)
Definition: txencbox.cxx:113
void FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
Fill with all known encodings but exclude those matching one or more given flags as defined in rtl/te...
Definition: txencbox.cxx:196
OUString sId
void SAL_CALL first(const css::awt::SpinEvent &rEvent) override
#define SAL_WARN(area, stream)
void FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
Fill with all encodings known to the dbtools::OCharsetMap but exclude those matching one or more give...
Definition: txencbox.cxx:93
static rtl_TextEncoding GetBestMimeEncoding()
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo
static OUString GetTextString(const rtl_TextEncoding nEnc)
Definition: txenctab.cxx:24