LibreOffice Module sc (master) 1
filtuno.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 <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
21#include <tools/urlobj.hxx>
22#include <vcl/svapp.hxx>
25#include <osl/diagnose.h>
26
27#include <filtuno.hxx>
28#include <miscuno.hxx>
29#include <scdll.hxx>
30#include <imoptdlg.hxx>
31#include <asciiopt.hxx>
32#include <docsh.hxx>
33#include <globstr.hrc>
34#include <scresid.hxx>
35
36#include <scabstdlg.hxx>
37#include <i18nlangtag/lang.h>
38
39#include <optutil.hxx>
40#include <com/sun/star/uno/Any.hxx>
41#include <com/sun/star/uno/Sequence.hxx>
44#include <memory>
45
46using namespace com::sun::star;
47using namespace com::sun::star::uno;
48using namespace connectivity::dbase;
49
50constexpr OUStringLiteral SCFILTEROPTIONSOBJ_SERVICE = u"com.sun.star.ui.dialogs.FilterOptionsDialog";
51constexpr OUStringLiteral SCFILTEROPTIONSOBJ_IMPLNAME = u"com.sun.star.comp.Calc.FilterOptionsDialog";
52
54
55constexpr OUStringLiteral SC_UNONAME_FILENAME = u"URL";
56constexpr OUStringLiteral SC_UNONAME_FILTERNAME = u"FilterName";
57constexpr OUStringLiteral SC_UNONAME_FILTEROPTIONS = u"FilterOptions";
58constexpr OUStringLiteral SC_UNONAME_INPUTSTREAM = u"InputStream";
59
60constexpr OUStringLiteral DBF_CHAR_SET = u"CharSet";
61constexpr OUStringLiteral DBF_SEP_PATH_IMPORT = u"Office.Calc/Dialogs/DBFImport";
62constexpr OUStringLiteral DBF_SEP_PATH_EXPORT = u"Office.Calc/Dialogs/DBFExport";
63
64namespace
65{
66
67 enum class charsetSource
68 {
69 charset_from_file,
70 charset_from_user_setting,
71 charset_default
72 };
73
74 charsetSource load_CharSet(rtl_TextEncoding &nCharSet, bool bExport, SvStream* dbf_Stream)
75 {
76 if (dbf_Stream && dbfReadCharset(nCharSet, dbf_Stream))
77 {
78 return charsetSource::charset_from_file;
79 }
80
81 Sequence<Any> aValues;
82 const Any *pProperties;
85
86 aValues = aItem.GetProperties( aNames );
87 pProperties = aValues.getConstArray();
88
89 if( pProperties[0].hasValue() )
90 {
91 sal_Int32 nChar = 0;
92 pProperties[0] >>= nChar;
93 if( nChar >= 0)
94 {
95 nCharSet = static_cast<rtl_TextEncoding>(nChar);
96 return charsetSource::charset_from_user_setting;
97 }
98 }
99
100 // Default choice
101 nCharSet = RTL_TEXTENCODING_IBM_850;
102 return charsetSource::charset_default;
103 }
104
105 void save_CharSet( rtl_TextEncoding nCharSet, bool bExport )
106 {
107 Sequence<Any> aValues;
108 Any *pProperties;
111
112 aValues = aItem.GetProperties( aNames );
113 pProperties = aValues.getArray();
114 pProperties[0] <<= static_cast<sal_Int32>(nCharSet);
115
116 aItem.PutProperties(aNames, aValues);
117 }
118}
119
121 bExport( false )
122{
123}
124
126{
127}
128
129extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
130Calc_FilterOptionsDialog_get_implementation(css::uno::XComponentContext*, css::uno::Sequence<css::uno::Any> const &)
131{
132 SolarMutexGuard aGuard;
133 ScDLL::Init();
134 return cppu::acquire(new ScFilterOptionsObj);
135}
136
137// XPropertyAccess
138
139uno::Sequence<beans::PropertyValue> SAL_CALL ScFilterOptionsObj::getPropertyValues()
140{
143 });
144}
145
146void SAL_CALL ScFilterOptionsObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& aProps )
147{
148 for (const beans::PropertyValue& rProp : aProps)
149 {
150 OUString aPropName(rProp.Name);
151
153 rProp.Value >>= aFileName;
154 else if ( aPropName == SC_UNONAME_FILTERNAME )
155 rProp.Value >>= aFilterName;
157 rProp.Value >>= aFilterOptions;
158 else if ( aPropName == SC_UNONAME_INPUTSTREAM )
159 rProp.Value >>= xInputStream;
160 }
161}
162
163// XExecutableDialog
164
165void SAL_CALL ScFilterOptionsObj::setTitle( const OUString& /* aTitle */ )
166{
167 // not used
168}
169
170sal_Int16 SAL_CALL ScFilterOptionsObj::execute()
171{
172 sal_Int16 nRet = ui::dialogs::ExecutableDialogResults::CANCEL;
173
174 OUString aFilterString( aFilterName );
175
177
178 if ( !bExport && aFilterString == ScDocShell::GetAsciiFilterName() )
179 {
180 // ascii import is special...
181
183 // tdf#132421 - don't URL encode filename for the import ASCII dialog title
184 OUString aPrivDatName(aURL.GetLastName(INetURLObject::DecodeMechanism::Unambiguous));
185 std::unique_ptr<SvStream> pInStream;
186 if ( xInputStream.is() )
188
190 pInStream.get(), SC_IMPORTFILE));
191 if ( pDlg->Execute() == RET_OK )
192 {
193 ScAsciiOptions aOptions;
194 pDlg->GetOptions( aOptions );
195 pDlg->SaveParameters();
196 aFilterOptions = aOptions.WriteToString();
197 nRet = ui::dialogs::ExecutableDialogResults::OK;
198 }
199 }
200 else if ( aFilterString == ScDocShell::GetWebQueryFilterName() || aFilterString == ScDocShell::GetHtmlFilterName() )
201 {
202 if (bExport)
203 nRet = ui::dialogs::ExecutableDialogResults::OK; // export HTML without dialog
204 else
205 {
206 // HTML import.
209
210 if (pDlg->Execute() == RET_OK)
211 {
212 LanguageType eLang = pDlg->GetLanguageType();
213 OUStringBuffer aBuf;
214
215 aBuf.append(static_cast<sal_Int32>(static_cast<sal_uInt16>(eLang)));
216 aBuf.append(' ');
217 aBuf.append(pDlg->IsDateConversionSet() ? u'1' : u'0');
218 aBuf.append(' ');
219 aBuf.append(pDlg->IsScientificConversionSet() ? u'1' : u'0');
220 aFilterOptions = aBuf.makeStringAndClear();
221 nRet = ui::dialogs::ExecutableDialogResults::OK;
222 }
223 }
224 }
225 else
226 {
227 bool bDBEnc = false;
228 bool bAscii = false;
229 bool skipDialog = false;
230
231 sal_Unicode const cStrDel = '"';
232 sal_Unicode cAsciiDel = ';';
233 rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
234
235 OUString aTitle;
236 bool bIncludeBOM = false;
237
238 if ( aFilterString == ScDocShell::GetAsciiFilterName() )
239 {
240 // ascii export (import is handled above)
241
243 OUString aExt(aURL.getExtension());
244 if (aExt.equalsIgnoreAsciiCase("CSV"))
245 cAsciiDel = ',';
246 else
247 cAsciiDel = '\t';
248
249 aTitle = ScResId( STR_EXPORT_ASCII );
250 bAscii = true;
251
252 ScAsciiOptions aOptions;
254 bIncludeBOM = aOptions.GetIncludeBOM();
255 }
256 else if ( aFilterString == ScDocShell::GetLotusFilterName() )
257 {
258 // lotus is only imported
259 OSL_ENSURE( !bExport, "Filter Options for Lotus Export is not implemented" );
260
261 aTitle = ScResId( STR_IMPORT_LOTUS );
262 eEncoding = RTL_TEXTENCODING_IBM_437;
263 }
264 else if ( aFilterString == ScDocShell::GetDBaseFilterName() )
265 {
266 if ( bExport )
267 {
268 // dBase export
269 aTitle = ScResId( STR_EXPORT_DBF );
270 }
271 else
272 {
273 // dBase import
274 aTitle = ScResId( STR_IMPORT_DBF );
275 }
276
277 std::unique_ptr<SvStream> pInStream;
278 if ( xInputStream.is() )
280 switch(load_CharSet( eEncoding, bExport, pInStream.get()))
281 {
282 case charsetSource::charset_from_file:
283 skipDialog = true;
284 break;
285 case charsetSource::charset_from_user_setting:
286 case charsetSource::charset_default:
287 break;
288 }
289 bDBEnc = true;
290 // pInStream goes out of scope, the stream is automatically closed
291 }
292 else if ( aFilterString == ScDocShell::GetDifFilterName() )
293 {
294 if ( bExport )
295 {
296 // DIF export
297 aTitle = ScResId( STR_EXPORT_DIF );
298 }
299 else
300 {
301 // DIF import
302 aTitle = ScResId( STR_IMPORT_DIF );
303 }
304 // common for DIF import/export
305 eEncoding = RTL_TEXTENCODING_MS_1252;
306 }
307
308 ScImportOptions aOptions( cAsciiDel, cStrDel, eEncoding);
309 aOptions.bIncludeBOM = bIncludeBOM;
310 if(skipDialog)
311 {
312 // TODO: check we are not missing some of the stuff that ScImportOptionsDlg::GetImportOptions
313 // (file sc/source/ui/dbgui/scuiimoptdlg.cxx) does
314 // that is, if the dialog sets options that are not selected by the user (!)
315 // then we are missing them here.
316 // Then we may need to rip them out of the dialog.
317 // Or we actually change the dialog to not display if skipDialog==true
318 // in that case, add an argument skipDialog to CreateScImportOptionsDlg
319 nRet = ui::dialogs::ExecutableDialogResults::OK;
320 }
321 else
322 {
324 bAscii, &aOptions, &aTitle,
325 bDBEnc, !bExport));
326 if ( pDlg->Execute() == RET_OK )
327 {
328 pDlg->SaveImportOptions();
329 pDlg->GetImportOptions( aOptions );
330 save_CharSet( aOptions.eCharSet, bExport );
331 nRet = ui::dialogs::ExecutableDialogResults::OK;
332 }
333 }
334 if (nRet == ui::dialogs::ExecutableDialogResults::OK)
335 {
336 if ( bAscii )
337 aFilterOptions = aOptions.BuildString();
338 else
339 aFilterOptions = aOptions.aStrFont;
340 }
341 }
342
343 xInputStream.clear(); // don't hold the stream longer than necessary
344
345 return nRet;
346}
347
348// XImporter
349
350void SAL_CALL ScFilterOptionsObj::setTargetDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
351{
352 bExport = false;
353}
354
355// XExporter
356
357void SAL_CALL ScFilterOptionsObj::setSourceDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
358{
359 bExport = true;
360}
361
362// XInitialization
363
364void SAL_CALL ScFilterOptionsObj::initialize(const uno::Sequence<uno::Any>& rArguments)
365{
367 if (aProperties.has("ParentWindow"))
368 aProperties.get("ParentWindow") >>= xDialogParent;
369}
370
371/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
@ SC_IMPORTFILE
Definition: asciiopt.hxx:110
static weld::Window * GetFrameWeld(const css::uno::Reference< css::awt::XWindow > &rWindow)
virtual VclPtr< AbstractScImportOptionsDlg > CreateScImportOptionsDlg(weld::Window *pParent, bool bAscii, const ScImportOptions *pOptions, const OUString *pStrTitle, bool bOnlyDbtoolsEncodings, bool bImport=true)=0
virtual VclPtr< AbstractScTextImportOptionsDlg > CreateScTextImportOptionsDlg(weld::Window *pParent)=0
static SC_DLLPUBLIC ScAbstractDialogFactory * Create()
Definition: scabstdlg.cxx:37
virtual VclPtr< AbstractScImportAsciiDlg > CreateScImportAsciiDlg(weld::Window *pParent, const OUString &aDatName, SvStream *pInStream, ScImportAsciiCall eCall)=0
void ReadFromString(std::u16string_view rString)
Definition: asciiopt.cxx:89
bool GetIncludeBOM() const
Definition: asciiopt.hxx:67
OUString WriteToString() const
Definition: asciiopt.cxx:211
static SC_DLLPUBLIC void Init()
DLL-init/exit-code must be linked to the DLL only.
Definition: scdll.cxx:100
static OUString GetHtmlFilterName()
Definition: docsh.cxx:2847
static OUString GetDifFilterName()
Definition: docsh.cxx:2872
static OUString GetLotusFilterName()
Definition: docsh.cxx:2862
static OUString GetAsciiFilterName()
Definition: docsh.cxx:2857
static OUString GetWebQueryFilterName()
Definition: docsh.cxx:2852
static OUString GetDBaseFilterName()
Definition: docsh.cxx:2867
css::uno::Reference< css::awt::XWindow > xDialogParent
Definition: filtuno.hxx:46
virtual ~ScFilterOptionsObj() override
Definition: filtuno.cxx:125
virtual void SAL_CALL setSourceDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
Definition: filtuno.cxx:357
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &rArguments) override
Definition: filtuno.cxx:364
virtual void SAL_CALL setTitle(const OUString &aTitle) override
Definition: filtuno.cxx:165
OUString aFileName
Definition: filtuno.hxx:42
virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getPropertyValues() override
Definition: filtuno.cxx:139
virtual void SAL_CALL setTargetDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
Definition: filtuno.cxx:350
virtual sal_Int16 SAL_CALL execute() override
Definition: filtuno.cxx:170
OUString aFilterOptions
Definition: filtuno.hxx:44
virtual void SAL_CALL setPropertyValues(const css::uno::Sequence< css::beans::PropertyValue > &aProps) override
Definition: filtuno.cxx:146
css::uno::Reference< css::io::XInputStream > xInputStream
Definition: filtuno.hxx:45
OUString aFilterName
Definition: filtuno.hxx:43
OUString BuildString() const
Definition: imoptdlg.cxx:103
OUString aStrFont
Definition: imoptdlg.hxx:46
rtl_TextEncoding eCharSet
Definition: imoptdlg.hxx:47
void PutProperties(const css::uno::Sequence< OUString > &rNames, const css::uno::Sequence< css::uno::Any > &rValues)
Definition: optutil.hxx:55
css::uno::Sequence< css::uno::Any > GetProperties(const css::uno::Sequence< OUString > &rNames)
Definition: optutil.hxx:53
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
URL aURL
float u
constexpr OUStringLiteral DBF_SEP_PATH_IMPORT
Definition: filtuno.cxx:61
constexpr OUStringLiteral SC_UNONAME_FILTERNAME
Definition: filtuno.cxx:56
constexpr OUStringLiteral SCFILTEROPTIONSOBJ_IMPLNAME
Definition: filtuno.cxx:51
constexpr OUStringLiteral DBF_CHAR_SET
Definition: filtuno.cxx:60
constexpr OUStringLiteral SC_UNONAME_FILTEROPTIONS
Definition: filtuno.cxx:57
constexpr OUStringLiteral SC_UNONAME_INPUTSTREAM
Definition: filtuno.cxx:58
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * Calc_FilterOptionsDialog_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: filtuno.cxx:130
constexpr OUStringLiteral DBF_SEP_PATH_EXPORT
Definition: filtuno.cxx:62
constexpr OUStringLiteral SC_UNONAME_FILENAME
Definition: filtuno.cxx:55
constexpr OUStringLiteral SCFILTEROPTIONSOBJ_SERVICE
Definition: filtuno.cxx:50
aBuf
#define SC_SIMPLE_SERVICE_INFO(ClassName, ClassNameAscii, ServiceAscii)
Definition: miscuno.hxx:63
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
bool dbfReadCharset(rtl_TextEncoding &nCharSet, SvStream *dbf_Stream)
OUString aPropName
OUString FilterName
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
sal_uInt16 sal_Unicode
RET_OK