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/msgbox.hxx>
23 #include <vcl/svapp.hxx>
24 #include <unotools/ucbstreamhelper.hxx>
26 #include "editutil.hxx"
27 #include "filtuno.hxx"
28 #include "miscuno.hxx"
30 #include "imoptdlg.hxx"
31 #include "asciiopt.hxx"
33 #include "globstr.hrc"
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 <boost/scoped_ptr.hpp>
44 using namespace com::sun::star
;
45 using namespace com::sun::star::uno
;
47 #define SCFILTEROPTIONSOBJ_SERVICE "com.sun.star.ui.dialogs.FilterOptionsDialog"
48 #define SCFILTEROPTIONSOBJ_IMPLNAME "com.sun.star.comp.Calc.FilterOptionsDialog"
50 SC_SIMPLE_SERVICE_INFO( ScFilterOptionsObj
, SCFILTEROPTIONSOBJ_IMPLNAME
, SCFILTEROPTIONSOBJ_SERVICE
)
52 #define SC_UNONAME_FILENAME "URL"
53 #define SC_UNONAME_FILTERNAME "FilterName"
54 #define SC_UNONAME_FILTEROPTIONS "FilterOptions"
55 #define SC_UNONAME_INPUTSTREAM "InputStream"
57 #define DBF_CHAR_SET "CharSet"
58 #define DBF_SEP_PATH_IMPORT "Office.Calc/Dialogs/DBFImport"
59 #define DBF_SEP_PATH_EXPORT "Office.Calc/Dialogs/DBFExport"
61 static void load_CharSet( rtl_TextEncoding
&nCharSet
, bool bExport
)
63 Sequence
<Any
> aValues
;
64 const Any
*pProperties
;
65 Sequence
<OUString
> aNames(1);
66 OUString
* pNames
= aNames
.getArray();
67 ScLinkConfigItem
aItem( OUString::createFromAscii(
68 bExport
?DBF_SEP_PATH_EXPORT
:DBF_SEP_PATH_IMPORT
) );
70 pNames
[0] = DBF_CHAR_SET
;
71 aValues
= aItem
.GetProperties( aNames
);
72 pProperties
= aValues
.getConstArray();
75 nCharSet
= RTL_TEXTENCODING_IBM_850
;
77 if( pProperties
[0].hasValue() )
80 pProperties
[0] >>= nChar
;
83 nCharSet
= (rtl_TextEncoding
) nChar
;
88 static void save_CharSet( rtl_TextEncoding nCharSet
, bool bExport
)
90 Sequence
<Any
> aValues
;
92 Sequence
<OUString
> aNames(1);
93 OUString
* pNames
= aNames
.getArray();
94 ScLinkConfigItem
aItem( OUString::createFromAscii(
95 bExport
?DBF_SEP_PATH_EXPORT
:DBF_SEP_PATH_IMPORT
) );
97 pNames
[0] = DBF_CHAR_SET
;
98 aValues
= aItem
.GetProperties( aNames
);
99 pProperties
= aValues
.getArray();
100 pProperties
[0] <<= (sal_Int32
) nCharSet
;
102 aItem
.PutProperties(aNames
, aValues
);
105 ScFilterOptionsObj::ScFilterOptionsObj() :
110 ScFilterOptionsObj::~ScFilterOptionsObj()
114 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
115 Calc_FilterOptionsDialog_get_implementation(css::uno::XComponentContext
*, css::uno::Sequence
<css::uno::Any
> const &)
117 SolarMutexGuard aGuard
;
119 return cppu::acquire(new ScFilterOptionsObj
);
124 uno::Sequence
<beans::PropertyValue
> SAL_CALL
ScFilterOptionsObj::getPropertyValues() throw(uno::RuntimeException
, std::exception
)
126 uno::Sequence
<beans::PropertyValue
> aRet(1);
127 beans::PropertyValue
* pArray
= aRet
.getArray();
129 pArray
[0].Name
= SC_UNONAME_FILTEROPTIONS
;
130 pArray
[0].Value
<<= aFilterOptions
;
135 void SAL_CALL
ScFilterOptionsObj::setPropertyValues( const uno::Sequence
<beans::PropertyValue
>& aProps
)
136 throw(beans::UnknownPropertyException
, beans::PropertyVetoException
,
137 lang::IllegalArgumentException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
139 const beans::PropertyValue
* pPropArray
= aProps
.getConstArray();
140 long nPropCount
= aProps
.getLength();
141 for (long i
= 0; i
< nPropCount
; i
++)
143 const beans::PropertyValue
& rProp
= pPropArray
[i
];
144 OUString
aPropName(rProp
.Name
);
146 if ( aPropName
== SC_UNONAME_FILENAME
)
147 rProp
.Value
>>= aFileName
;
148 else if ( aPropName
== SC_UNONAME_FILTERNAME
)
149 rProp
.Value
>>= aFilterName
;
150 else if ( aPropName
== SC_UNONAME_FILTEROPTIONS
)
151 rProp
.Value
>>= aFilterOptions
;
152 else if ( aPropName
== SC_UNONAME_INPUTSTREAM
)
153 rProp
.Value
>>= xInputStream
;
159 void SAL_CALL
ScFilterOptionsObj::setTitle( const OUString
& /* aTitle */ ) throw(uno::RuntimeException
, std::exception
)
164 sal_Int16 SAL_CALL
ScFilterOptionsObj::execute() throw(uno::RuntimeException
, std::exception
)
166 sal_Int16 nRet
= ui::dialogs::ExecutableDialogResults::CANCEL
;
168 OUString
aFilterString( aFilterName
);
170 ScAbstractDialogFactory
* pFact
= ScAbstractDialogFactory::Create();
171 OSL_ENSURE(pFact
, "ScAbstractFactory create fail!");
173 if ( !bExport
&& aFilterString
== ScDocShell::GetAsciiFilterName() )
175 // ascii import is special...
177 INetURLObject
aURL( aFileName
);
178 OUString
aPrivDatName(aURL
.getName());
179 boost::scoped_ptr
<SvStream
> pInStream
;
180 if ( xInputStream
.is() )
181 pInStream
.reset(utl::UcbStreamHelper::CreateStream( xInputStream
));
183 boost::scoped_ptr
<AbstractScImportAsciiDlg
> pDlg(pFact
->CreateScImportAsciiDlg( NULL
, aPrivDatName
, pInStream
.get(), SC_IMPORTFILE
));
184 OSL_ENSURE(pDlg
, "Dialog create fail!");
185 if ( pDlg
->Execute() == RET_OK
)
187 ScAsciiOptions aOptions
;
188 pDlg
->GetOptions( aOptions
);
189 pDlg
->SaveParameters();
190 aFilterOptions
= aOptions
.WriteToString();
191 nRet
= ui::dialogs::ExecutableDialogResults::OK
;
194 else if ( aFilterString
== ScDocShell::GetWebQueryFilterName() || aFilterString
== ScDocShell::GetHtmlFilterName() )
197 nRet
= ui::dialogs::ExecutableDialogResults::OK
; // export HTML without dialog
201 boost::scoped_ptr
<AbstractScTextImportOptionsDlg
> pDlg(
202 pFact
->CreateScTextImportOptionsDlg(NULL
));
204 if (pDlg
->Execute() == RET_OK
)
206 LanguageType eLang
= pDlg
->GetLanguageType();
209 aBuf
.append(OUString::number(static_cast<sal_Int32
>(eLang
)));
211 aBuf
.append(pDlg
->IsDateConversionSet() ? sal_Unicode('1') : sal_Unicode('0'));
212 aFilterOptions
= aBuf
.makeStringAndClear();
213 nRet
= ui::dialogs::ExecutableDialogResults::OK
;
219 bool bMultiByte
= true;
223 sal_Unicode cStrDel
= '"';
224 sal_Unicode cAsciiDel
= ';';
225 rtl_TextEncoding eEncoding
= RTL_TEXTENCODING_DONTKNOW
;
229 if ( aFilterString
== ScDocShell::GetAsciiFilterName() )
231 // ascii export (import is handled above)
233 INetURLObject
aURL( aFileName
);
234 OUString
aExt(aURL
.getExtension());
235 if (aExt
.equalsIgnoreAsciiCase("CSV"))
240 aTitle
= ScGlobal::GetRscString( STR_EXPORT_ASCII
);
243 else if ( aFilterString
== ScDocShell::GetLotusFilterName() )
245 // lotus is only imported
246 OSL_ENSURE( !bExport
, "Filter Options for Lotus Export is not implemented" );
248 aTitle
= ScGlobal::GetRscString( STR_IMPORT_LOTUS
);
249 eEncoding
= RTL_TEXTENCODING_IBM_437
;
251 else if ( aFilterString
== ScDocShell::GetDBaseFilterName() )
256 aTitle
= ScGlobal::GetRscString( STR_EXPORT_DBF
);
261 aTitle
= ScGlobal::GetRscString( STR_IMPORT_DBF
);
263 load_CharSet( eEncoding
, bExport
);
266 else if ( aFilterString
== ScDocShell::GetDifFilterName() )
271 aTitle
= ScGlobal::GetRscString( STR_EXPORT_DIF
);
276 aTitle
= ScGlobal::GetRscString( STR_IMPORT_DIF
);
278 // common for DIF import/export
279 eEncoding
= RTL_TEXTENCODING_MS_1252
;
282 ScImportOptions
aOptions( cAsciiDel
, cStrDel
, eEncoding
);
284 boost::scoped_ptr
<AbstractScImportOptionsDlg
> pDlg(pFact
->CreateScImportOptionsDlg(NULL
,
285 bAscii
, &aOptions
, &aTitle
, bMultiByte
, bDBEnc
,
287 OSL_ENSURE(pDlg
, "Dialog create fail!");
288 if ( pDlg
->Execute() == RET_OK
)
290 pDlg
->GetImportOptions( aOptions
);
291 save_CharSet( aOptions
.eCharSet
, bExport
);
293 aFilterOptions
= aOptions
.BuildString();
295 aFilterOptions
= aOptions
.aStrFont
;
296 nRet
= ui::dialogs::ExecutableDialogResults::OK
;
300 xInputStream
.clear(); // don't hold the stream longer than necessary
307 void SAL_CALL
ScFilterOptionsObj::setTargetDocument( const uno::Reference
<lang::XComponent
>& /* xDoc */ )
308 throw(lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
)
315 void SAL_CALL
ScFilterOptionsObj::setSourceDocument( const uno::Reference
<lang::XComponent
>& /* xDoc */ )
316 throw(lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
)
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */