Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / sc / source / ui / unoobj / filtuno.cxx
blobee29b0f416497a866fe2dbdedddbc94860f999ab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
29 #include "scdll.hxx"
30 #include "imoptdlg.hxx"
31 #include "asciiopt.hxx"
32 #include "docsh.hxx"
33 #include "globstr.hrc"
36 #include "sc.hrc"
37 #include "scabstdlg.hxx"
38 #include <i18nlangtag/lang.h>
40 #include <optutil.hxx>
41 #include <com/sun/star/uno/Any.hxx>
42 #include <com/sun/star/uno/Sequence.hxx>
43 #include <boost/scoped_ptr.hpp>
46 using namespace ::com::sun::star;
47 using namespace rtl;
48 using namespace com::sun::star::uno;
50 #define SCFILTEROPTIONSOBJ_SERVICE "com.sun.star.ui.dialogs.FilterOptionsDialog"
51 #define SCFILTEROPTIONSOBJ_IMPLNAME "com.sun.star.comp.Calc.FilterOptionsDialog"
53 SC_SIMPLE_SERVICE_INFO( ScFilterOptionsObj, SCFILTEROPTIONSOBJ_IMPLNAME, SCFILTEROPTIONSOBJ_SERVICE )
55 #define SC_UNONAME_FILENAME "URL"
56 #define SC_UNONAME_FILTERNAME "FilterName"
57 #define SC_UNONAME_FILTEROPTIONS "FilterOptions"
58 #define SC_UNONAME_INPUTSTREAM "InputStream"
61 #define DBF_CHAR_SET "CharSet"
62 #define DBF_SEP_PATH_IMPORT "Office.Calc/Dialogs/DBFImport"
63 #define DBF_SEP_PATH_EXPORT "Office.Calc/Dialogs/DBFExport"
65 static void load_CharSet( rtl_TextEncoding &nCharSet, bool bExport )
67 Sequence<Any> aValues;
68 const Any *pProperties;
69 Sequence<OUString> aNames(1);
70 OUString* pNames = aNames.getArray();
71 ScLinkConfigItem aItem( OUString::createFromAscii(
72 bExport?DBF_SEP_PATH_EXPORT:DBF_SEP_PATH_IMPORT ) );
74 pNames[0] = OUString::createFromAscii( DBF_CHAR_SET );
75 aValues = aItem.GetProperties( aNames );
76 pProperties = aValues.getConstArray();
78 // Default choice
79 nCharSet = RTL_TEXTENCODING_IBM_850;
81 if( pProperties[0].hasValue() )
83 sal_Int32 nChar = 0;
84 pProperties[0] >>= nChar;
85 if( nChar >= 0)
87 nCharSet = (rtl_TextEncoding) nChar;
92 static void save_CharSet( rtl_TextEncoding nCharSet, bool bExport )
94 Sequence<Any> aValues;
95 Any *pProperties;
96 Sequence<OUString> aNames(1);
97 OUString* pNames = aNames.getArray();
98 ScLinkConfigItem aItem( OUString::createFromAscii(
99 bExport?DBF_SEP_PATH_EXPORT:DBF_SEP_PATH_IMPORT ) );
101 pNames[0] = OUString::createFromAscii( DBF_CHAR_SET );
102 aValues = aItem.GetProperties( aNames );
103 pProperties = aValues.getArray();
104 pProperties[0] <<= (sal_Int32) nCharSet;
106 aItem.PutProperties(aNames, aValues);
109 ScFilterOptionsObj::ScFilterOptionsObj() :
110 bExport( false )
114 ScFilterOptionsObj::~ScFilterOptionsObj()
118 // stuff for exService_...
120 uno::Reference<uno::XInterface> SAL_CALL ScFilterOptionsObj_CreateInstance(
121 const uno::Reference<lang::XMultiServiceFactory>& )
123 SolarMutexGuard aGuard;
124 ScDLL::Init();
125 return (::cppu::OWeakObject*) new ScFilterOptionsObj;
128 OUString ScFilterOptionsObj::getImplementationName_Static()
130 return OUString( SCFILTEROPTIONSOBJ_IMPLNAME );
133 uno::Sequence<OUString> ScFilterOptionsObj::getSupportedServiceNames_Static()
135 uno::Sequence<OUString> aRet(1);
136 OUString* pArray = aRet.getArray();
137 pArray[0] = OUString( SCFILTEROPTIONSOBJ_SERVICE );
138 return aRet;
141 // XPropertyAccess
143 uno::Sequence<beans::PropertyValue> SAL_CALL ScFilterOptionsObj::getPropertyValues() throw(uno::RuntimeException, std::exception)
145 uno::Sequence<beans::PropertyValue> aRet(1);
146 beans::PropertyValue* pArray = aRet.getArray();
148 pArray[0].Name = OUString( SC_UNONAME_FILTEROPTIONS );
149 pArray[0].Value <<= aFilterOptions;
151 return aRet;
154 void SAL_CALL ScFilterOptionsObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& aProps )
155 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
156 lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
158 const beans::PropertyValue* pPropArray = aProps.getConstArray();
159 long nPropCount = aProps.getLength();
160 for (long i = 0; i < nPropCount; i++)
162 const beans::PropertyValue& rProp = pPropArray[i];
163 OUString aPropName(rProp.Name);
165 if ( aPropName.equalsAscii( SC_UNONAME_FILENAME ) )
166 rProp.Value >>= aFileName;
167 else if ( aPropName.equalsAscii( SC_UNONAME_FILTERNAME ) )
168 rProp.Value >>= aFilterName;
169 else if ( aPropName.equalsAscii( SC_UNONAME_FILTEROPTIONS ) )
170 rProp.Value >>= aFilterOptions;
171 else if ( aPropName.equalsAscii( SC_UNONAME_INPUTSTREAM ) )
172 rProp.Value >>= xInputStream;
176 // XExecutableDialog
178 void SAL_CALL ScFilterOptionsObj::setTitle( const OUString& /* aTitle */ ) throw(uno::RuntimeException, std::exception)
180 // not used
183 sal_Int16 SAL_CALL ScFilterOptionsObj::execute() throw(uno::RuntimeException, std::exception)
185 sal_Int16 nRet = ui::dialogs::ExecutableDialogResults::CANCEL;
187 OUString aFilterString( aFilterName );
189 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
190 OSL_ENSURE(pFact, "ScAbstractFactory create fail!");
192 if ( !bExport && aFilterString == ScDocShell::GetAsciiFilterName() )
194 // ascii import is special...
196 INetURLObject aURL( aFileName );
197 OUString aPrivDatName(aURL.getName());
198 boost::scoped_ptr<SvStream> pInStream;
199 if ( xInputStream.is() )
200 pInStream.reset(utl::UcbStreamHelper::CreateStream( xInputStream ));
202 boost::scoped_ptr<AbstractScImportAsciiDlg> pDlg(pFact->CreateScImportAsciiDlg( NULL, aPrivDatName, pInStream.get(), SC_IMPORTFILE));
203 OSL_ENSURE(pDlg, "Dialog create fail!");
204 if ( pDlg->Execute() == RET_OK )
206 ScAsciiOptions aOptions;
207 pDlg->GetOptions( aOptions );
208 pDlg->SaveParameters();
209 aFilterOptions = aOptions.WriteToString();
210 nRet = ui::dialogs::ExecutableDialogResults::OK;
213 else if ( aFilterString == ScDocShell::GetWebQueryFilterName() || aFilterString == ScDocShell::GetHtmlFilterName() )
215 if (bExport)
216 nRet = ui::dialogs::ExecutableDialogResults::OK; // export HTML without dialog
217 else
219 // HTML import.
220 boost::scoped_ptr<AbstractScTextImportOptionsDlg> pDlg(
221 pFact->CreateScTextImportOptionsDlg(NULL));
223 if (pDlg->Execute() == RET_OK)
225 LanguageType eLang = pDlg->GetLanguageType();
226 OUStringBuffer aBuf;
228 aBuf.append(OUString::number(static_cast<sal_Int32>(eLang)));
229 aBuf.append(' ');
230 aBuf.append(pDlg->IsDateConversionSet() ? sal_Unicode('1') : sal_Unicode('0'));
231 aFilterOptions = aBuf.makeStringAndClear();
232 nRet = ui::dialogs::ExecutableDialogResults::OK;
236 else
238 bool bMultiByte = true;
239 bool bDBEnc = false;
240 bool bAscii = false;
242 sal_Unicode cStrDel = '"';
243 sal_Unicode cAsciiDel = ';';
244 rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
246 OUString aTitle;
248 if ( aFilterString == ScDocShell::GetAsciiFilterName() )
250 // ascii export (import is handled above)
252 INetURLObject aURL( aFileName );
253 OUString aExt(aURL.getExtension());
254 if (aExt.equalsIgnoreAsciiCase("CSV"))
255 cAsciiDel = ',';
256 else
257 cAsciiDel = '\t';
259 aTitle = ScGlobal::GetRscString( STR_EXPORT_ASCII );
260 bAscii = true;
262 else if ( aFilterString == ScDocShell::GetLotusFilterName() )
264 // lotus is only imported
265 OSL_ENSURE( !bExport, "Filter Options for Lotus Export is not implemented" );
267 aTitle = ScGlobal::GetRscString( STR_IMPORT_LOTUS );
268 eEncoding = RTL_TEXTENCODING_IBM_437;
270 else if ( aFilterString == ScDocShell::GetDBaseFilterName() )
272 if ( bExport )
274 // dBase export
275 aTitle = ScGlobal::GetRscString( STR_EXPORT_DBF );
277 else
279 // dBase import
280 aTitle = ScGlobal::GetRscString( STR_IMPORT_DBF );
282 load_CharSet( eEncoding, bExport );
283 bDBEnc = true;
285 else if ( aFilterString == ScDocShell::GetDifFilterName() )
287 if ( bExport )
289 // DIF export
290 aTitle = ScGlobal::GetRscString( STR_EXPORT_DIF );
292 else
294 // DIF import
295 aTitle = ScGlobal::GetRscString( STR_IMPORT_DIF );
297 // common for DIF import/export
298 eEncoding = RTL_TEXTENCODING_MS_1252;
301 ScImportOptions aOptions( cAsciiDel, cStrDel, eEncoding);
303 boost::scoped_ptr<AbstractScImportOptionsDlg> pDlg(pFact->CreateScImportOptionsDlg(NULL,
304 bAscii, &aOptions, &aTitle, bMultiByte, bDBEnc,
305 !bExport));
306 OSL_ENSURE(pDlg, "Dialog create fail!");
307 if ( pDlg->Execute() == RET_OK )
309 pDlg->GetImportOptions( aOptions );
310 save_CharSet( aOptions.eCharSet, bExport );
311 if ( bAscii )
312 aFilterOptions = aOptions.BuildString();
313 else
314 aFilterOptions = aOptions.aStrFont;
315 nRet = ui::dialogs::ExecutableDialogResults::OK;
319 xInputStream.clear(); // don't hold the stream longer than necessary
321 return nRet;
324 // XImporter
326 void SAL_CALL ScFilterOptionsObj::setTargetDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
327 throw(lang::IllegalArgumentException, uno::RuntimeException, std::exception)
329 bExport = false;
332 // XExporter
334 void SAL_CALL ScFilterOptionsObj::setSourceDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
335 throw(lang::IllegalArgumentException, uno::RuntimeException, std::exception)
337 bExport = true;
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */