1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
21 #include <tools/urlobj.hxx>
22 #include <vcl/svapp.hxx>
23 #include <unotools/ucbstreamhelper.hxx>
24 #include <connectivity/dbtools.hxx>
25 #include <osl/diagnose.h>
27 #include <filtuno.hxx>
28 #include <miscuno.hxx>
30 #include <imoptdlg.hxx>
31 #include <asciiopt.hxx>
33 #include <globstr.hrc>
34 #include <scresid.hxx>
36 #include <scabstdlg.hxx>
37 #include <i18nlangtag/lang.h>
39 #include <optutil.hxx>
40 #include <com/sun/star/uno/Any.hxx>
41 #include <com/sun/star/uno/Sequence.hxx>
42 #include <comphelper/namedvaluecollection.hxx>
43 #include <comphelper/propertysequence.hxx>
46 using namespace com::sun::star
;
47 using namespace com::sun::star::uno
;
48 using namespace connectivity::dbase
;
50 constexpr OUString SCFILTEROPTIONSOBJ_SERVICE
= u
"com.sun.star.ui.dialogs.FilterOptionsDialog"_ustr
;
51 constexpr OUStringLiteral SCFILTEROPTIONSOBJ_IMPLNAME
= u
"com.sun.star.comp.Calc.FilterOptionsDialog";
53 SC_SIMPLE_SERVICE_INFO( ScFilterOptionsObj
, SCFILTEROPTIONSOBJ_IMPLNAME
, SCFILTEROPTIONSOBJ_SERVICE
)
55 constexpr OUStringLiteral SC_UNONAME_FILENAME
= u
"URL";
56 constexpr OUStringLiteral SC_UNONAME_FILTERNAME
= u
"FilterName";
57 constexpr OUString SC_UNONAME_FILTEROPTIONS
= u
"FilterOptions"_ustr
;
58 constexpr OUStringLiteral SC_UNONAME_INPUTSTREAM
= u
"InputStream";
60 constexpr OUString DBF_CHAR_SET
= u
"CharSet"_ustr
;
61 constexpr OUString DBF_SEP_PATH_IMPORT
= u
"Office.Calc/Dialogs/DBFImport"_ustr
;
62 constexpr OUString DBF_SEP_PATH_EXPORT
= u
"Office.Calc/Dialogs/DBFExport"_ustr
;
67 enum class charsetSource
70 charset_from_user_setting
,
74 charsetSource
load_CharSet(rtl_TextEncoding
&nCharSet
, bool bExport
, SvStream
* dbf_Stream
)
76 if (dbf_Stream
&& dbfReadCharset(nCharSet
, dbf_Stream
))
78 return charsetSource::charset_from_file
;
81 Sequence
<Any
> aValues
;
82 const Any
*pProperties
;
83 Sequence
<OUString
> aNames
{ DBF_CHAR_SET
};
84 ScLinkConfigItem
aItem( bExport
? DBF_SEP_PATH_EXPORT
: DBF_SEP_PATH_IMPORT
);
86 aValues
= aItem
.GetProperties( aNames
);
87 pProperties
= aValues
.getConstArray();
89 if( pProperties
[0].hasValue() )
92 pProperties
[0] >>= nChar
;
95 nCharSet
= static_cast<rtl_TextEncoding
>(nChar
);
96 return charsetSource::charset_from_user_setting
;
101 nCharSet
= RTL_TEXTENCODING_IBM_850
;
102 return charsetSource::charset_default
;
105 void save_CharSet( rtl_TextEncoding nCharSet
, bool bExport
)
107 Sequence
<Any
> aValues
;
109 Sequence
<OUString
> aNames
{ DBF_CHAR_SET
};
110 ScLinkConfigItem
aItem( bExport
? DBF_SEP_PATH_EXPORT
: DBF_SEP_PATH_IMPORT
);
112 aValues
= aItem
.GetProperties( aNames
);
113 pProperties
= aValues
.getArray();
114 pProperties
[0] <<= static_cast<sal_Int32
>(nCharSet
);
116 aItem
.PutProperties(aNames
, aValues
);
120 ScFilterOptionsObj::ScFilterOptionsObj() :
125 ScFilterOptionsObj::~ScFilterOptionsObj()
129 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
130 Calc_FilterOptionsDialog_get_implementation(css::uno::XComponentContext
*, css::uno::Sequence
<css::uno::Any
> const &)
132 SolarMutexGuard aGuard
;
134 return cppu::acquire(new ScFilterOptionsObj
);
139 uno::Sequence
<beans::PropertyValue
> SAL_CALL
ScFilterOptionsObj::getPropertyValues()
141 return comphelper::InitPropertySequence({
142 { SC_UNONAME_FILTEROPTIONS
, Any(aFilterOptions
) }
146 void SAL_CALL
ScFilterOptionsObj::setPropertyValues( const uno::Sequence
<beans::PropertyValue
>& aProps
)
148 for (const beans::PropertyValue
& rProp
: aProps
)
150 OUString
aPropName(rProp
.Name
);
152 if ( aPropName
== SC_UNONAME_FILENAME
)
153 rProp
.Value
>>= aFileName
;
154 else if ( aPropName
== SC_UNONAME_FILTERNAME
)
155 rProp
.Value
>>= aFilterName
;
156 else if ( aPropName
== SC_UNONAME_FILTEROPTIONS
)
157 rProp
.Value
>>= aFilterOptions
;
158 else if ( aPropName
== SC_UNONAME_INPUTSTREAM
)
159 rProp
.Value
>>= xInputStream
;
165 void SAL_CALL
ScFilterOptionsObj::setTitle( const OUString
& /* aTitle */ )
170 sal_Int16 SAL_CALL
ScFilterOptionsObj::execute()
172 sal_Int16 nRet
= ui::dialogs::ExecutableDialogResults::CANCEL
;
174 OUString
aFilterString( aFilterName
);
176 ScAbstractDialogFactory
* pFact
= ScAbstractDialogFactory::Create();
178 if ( !bExport
&& aFilterString
== ScDocShell::GetAsciiFilterName() )
180 // ascii import is special...
182 INetURLObject
aURL( aFileName
);
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() )
187 pInStream
= utl::UcbStreamHelper::CreateStream( xInputStream
);
189 ScopedVclPtr
<AbstractScImportAsciiDlg
> pDlg(pFact
->CreateScImportAsciiDlg(Application::GetFrameWeld(xDialogParent
), aPrivDatName
,
190 pInStream
.get(), SC_IMPORTFILE
));
191 if ( pDlg
->Execute() == RET_OK
)
193 ScAsciiOptions aOptions
;
194 pDlg
->GetOptions( aOptions
);
195 pDlg
->SaveParameters();
196 aFilterOptions
= aOptions
.WriteToString();
197 nRet
= ui::dialogs::ExecutableDialogResults::OK
;
200 else if ( aFilterString
== ScDocShell::GetWebQueryFilterName() || aFilterString
== ScDocShell::GetHtmlFilterName() )
203 nRet
= ui::dialogs::ExecutableDialogResults::OK
; // export HTML without dialog
207 ScopedVclPtr
<AbstractScTextImportOptionsDlg
> pDlg(
208 pFact
->CreateScTextImportOptionsDlg(Application::GetFrameWeld(xDialogParent
)));
210 if (pDlg
->Execute() == RET_OK
)
212 LanguageType eLang
= pDlg
->GetLanguageType();
215 aBuf
.append(static_cast<sal_Int32
>(static_cast<sal_uInt16
>(eLang
)));
217 aBuf
.append(pDlg
->IsDateConversionSet() ? u
'1' : u
'0');
219 aBuf
.append(pDlg
->IsScientificConversionSet() ? u
'1' : u
'0');
220 aFilterOptions
= aBuf
.makeStringAndClear();
221 nRet
= ui::dialogs::ExecutableDialogResults::OK
;
229 bool skipDialog
= false;
231 sal_Unicode
const cStrDel
= '"';
232 sal_Unicode cAsciiDel
= ';';
233 rtl_TextEncoding eEncoding
= RTL_TEXTENCODING_DONTKNOW
;
236 bool bIncludeBOM
= false;
238 if ( aFilterString
== ScDocShell::GetAsciiFilterName() )
240 // ascii export (import is handled above)
242 INetURLObject
aURL( aFileName
);
243 OUString
aExt(aURL
.getExtension());
244 if (aExt
.equalsIgnoreAsciiCase("CSV"))
249 aTitle
= ScResId( STR_EXPORT_ASCII
);
252 ScAsciiOptions aOptions
;
253 aOptions
.ReadFromString(aFilterOptions
);
254 bIncludeBOM
= aOptions
.GetIncludeBOM();
256 else if ( aFilterString
== ScDocShell::GetLotusFilterName() )
258 // lotus is only imported
259 OSL_ENSURE( !bExport
, "Filter Options for Lotus Export is not implemented" );
261 aTitle
= ScResId( STR_IMPORT_LOTUS
);
262 eEncoding
= RTL_TEXTENCODING_IBM_437
;
264 else if ( aFilterString
== ScDocShell::GetDBaseFilterName() )
269 aTitle
= ScResId( STR_EXPORT_DBF
);
274 aTitle
= ScResId( STR_IMPORT_DBF
);
277 std::unique_ptr
<SvStream
> pInStream
;
278 if ( xInputStream
.is() )
279 pInStream
= utl::UcbStreamHelper::CreateStream( xInputStream
);
280 switch(load_CharSet( eEncoding
, bExport
, pInStream
.get()))
282 case charsetSource::charset_from_file
:
285 case charsetSource::charset_from_user_setting
:
286 case charsetSource::charset_default
:
290 // pInStream goes out of scope, the stream is automatically closed
292 else if ( aFilterString
== ScDocShell::GetDifFilterName() )
297 aTitle
= ScResId( STR_EXPORT_DIF
);
302 aTitle
= ScResId( STR_IMPORT_DIF
);
304 // common for DIF import/export
305 eEncoding
= RTL_TEXTENCODING_MS_1252
;
308 ScImportOptions
aOptions( cAsciiDel
, cStrDel
, eEncoding
);
309 aOptions
.bIncludeBOM
= bIncludeBOM
;
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
;
323 ScopedVclPtr
<AbstractScImportOptionsDlg
> pDlg(pFact
->CreateScImportOptionsDlg(Application::GetFrameWeld(xDialogParent
),
324 bAscii
, &aOptions
, &aTitle
,
326 if ( pDlg
->Execute() == RET_OK
)
328 pDlg
->SaveImportOptions();
329 pDlg
->GetImportOptions( aOptions
);
330 save_CharSet( aOptions
.eCharSet
, bExport
);
331 nRet
= ui::dialogs::ExecutableDialogResults::OK
;
334 if (nRet
== ui::dialogs::ExecutableDialogResults::OK
)
337 aFilterOptions
= aOptions
.BuildString();
339 aFilterOptions
= aOptions
.aStrFont
;
343 xInputStream
.clear(); // don't hold the stream longer than necessary
350 void SAL_CALL
ScFilterOptionsObj::setTargetDocument( const uno::Reference
<lang::XComponent
>& /* xDoc */ )
357 void SAL_CALL
ScFilterOptionsObj::setSourceDocument( const uno::Reference
<lang::XComponent
>& /* xDoc */ )
364 void SAL_CALL
ScFilterOptionsObj::initialize(const uno::Sequence
<uno::Any
>& rArguments
)
366 ::comphelper::NamedValueCollection
aProperties(rArguments
);
367 if (aProperties
.has(u
"ParentWindow"_ustr
))
368 aProperties
.get(u
"ParentWindow"_ustr
) >>= xDialogParent
;
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */