Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / unoobj / filtuno.cxx
blob068d01c2a319271dfe978f0d47fabbfbef32cc35
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 <memory>
42 #include <optutil.hxx>
43 #include <com/sun/star/uno/Any.hxx>
44 #include <com/sun/star/uno/Sequence.hxx>
47 using namespace ::com::sun::star;
48 using namespace rtl;
49 using namespace com::sun::star::uno;
51 //------------------------------------------------------------------------
53 #define SCFILTEROPTIONSOBJ_SERVICE "com.sun.star.ui.dialogs.FilterOptionsDialog"
54 #define SCFILTEROPTIONSOBJ_IMPLNAME "com.sun.star.comp.Calc.FilterOptionsDialog"
56 SC_SIMPLE_SERVICE_INFO( ScFilterOptionsObj, SCFILTEROPTIONSOBJ_IMPLNAME, SCFILTEROPTIONSOBJ_SERVICE )
58 #define SC_UNONAME_FILENAME "URL"
59 #define SC_UNONAME_FILTERNAME "FilterName"
60 #define SC_UNONAME_FILTEROPTIONS "FilterOptions"
61 #define SC_UNONAME_INPUTSTREAM "InputStream"
64 #define DBF_CHAR_SET "CharSet"
65 #define DBF_SEP_PATH_IMPORT "Office.Calc/Dialogs/DBFImport"
66 #define DBF_SEP_PATH_EXPORT "Office.Calc/Dialogs/DBFExport"
68 //------------------------------------------------------------------------
70 static void load_CharSet( rtl_TextEncoding &nCharSet, bool bExport )
72 Sequence<Any> aValues;
73 const Any *pProperties;
74 Sequence<OUString> aNames(1);
75 OUString* pNames = aNames.getArray();
76 ScLinkConfigItem aItem( OUString::createFromAscii(
77 bExport?DBF_SEP_PATH_EXPORT:DBF_SEP_PATH_IMPORT ) );
79 pNames[0] = OUString::createFromAscii( DBF_CHAR_SET );
80 aValues = aItem.GetProperties( aNames );
81 pProperties = aValues.getConstArray();
83 // Default choice
84 nCharSet = RTL_TEXTENCODING_IBM_850;
86 if( pProperties[0].hasValue() )
88 sal_Int32 nChar = 0;
89 pProperties[0] >>= nChar;
90 if( nChar >= 0)
92 nCharSet = (rtl_TextEncoding) nChar;
97 static void save_CharSet( rtl_TextEncoding nCharSet, bool bExport )
99 Sequence<Any> aValues;
100 Any *pProperties;
101 Sequence<OUString> aNames(1);
102 OUString* pNames = aNames.getArray();
103 ScLinkConfigItem aItem( OUString::createFromAscii(
104 bExport?DBF_SEP_PATH_EXPORT:DBF_SEP_PATH_IMPORT ) );
106 pNames[0] = OUString::createFromAscii( DBF_CHAR_SET );
107 aValues = aItem.GetProperties( aNames );
108 pProperties = aValues.getArray();
109 pProperties[0] <<= (sal_Int32) nCharSet;
111 aItem.PutProperties(aNames, aValues);
114 //------------------------------------------------------------------------
116 ScFilterOptionsObj::ScFilterOptionsObj() :
117 bExport( false )
121 ScFilterOptionsObj::~ScFilterOptionsObj()
125 // stuff for exService_...
127 uno::Reference<uno::XInterface> SAL_CALL ScFilterOptionsObj_CreateInstance(
128 const uno::Reference<lang::XMultiServiceFactory>& )
130 SolarMutexGuard aGuard;
131 ScDLL::Init();
132 return (::cppu::OWeakObject*) new ScFilterOptionsObj;
135 OUString ScFilterOptionsObj::getImplementationName_Static()
137 return OUString( SCFILTEROPTIONSOBJ_IMPLNAME );
140 uno::Sequence<OUString> ScFilterOptionsObj::getSupportedServiceNames_Static()
142 uno::Sequence<OUString> aRet(1);
143 OUString* pArray = aRet.getArray();
144 pArray[0] = OUString( SCFILTEROPTIONSOBJ_SERVICE );
145 return aRet;
148 // XPropertyAccess
150 uno::Sequence<beans::PropertyValue> SAL_CALL ScFilterOptionsObj::getPropertyValues() throw(uno::RuntimeException)
152 uno::Sequence<beans::PropertyValue> aRet(1);
153 beans::PropertyValue* pArray = aRet.getArray();
155 pArray[0].Name = OUString( SC_UNONAME_FILTEROPTIONS );
156 pArray[0].Value <<= aFilterOptions;
158 return aRet;
161 void SAL_CALL ScFilterOptionsObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& aProps )
162 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
163 lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
165 const beans::PropertyValue* pPropArray = aProps.getConstArray();
166 long nPropCount = aProps.getLength();
167 for (long i = 0; i < nPropCount; i++)
169 const beans::PropertyValue& rProp = pPropArray[i];
170 OUString aPropName(rProp.Name);
172 if ( aPropName.equalsAscii( SC_UNONAME_FILENAME ) )
173 rProp.Value >>= aFileName;
174 else if ( aPropName.equalsAscii( SC_UNONAME_FILTERNAME ) )
175 rProp.Value >>= aFilterName;
176 else if ( aPropName.equalsAscii( SC_UNONAME_FILTEROPTIONS ) )
177 rProp.Value >>= aFilterOptions;
178 else if ( aPropName.equalsAscii( SC_UNONAME_INPUTSTREAM ) )
179 rProp.Value >>= xInputStream;
183 // XExecutableDialog
185 void SAL_CALL ScFilterOptionsObj::setTitle( const OUString& /* aTitle */ ) throw(uno::RuntimeException)
187 // not used
190 sal_Int16 SAL_CALL ScFilterOptionsObj::execute() throw(uno::RuntimeException)
192 sal_Int16 nRet = ui::dialogs::ExecutableDialogResults::CANCEL;
194 OUString aFilterString( aFilterName );
196 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
197 OSL_ENSURE(pFact, "ScAbstractFactory create fail!");
199 if ( !bExport && aFilterString == ScDocShell::GetAsciiFilterName() )
201 // ascii import is special...
203 INetURLObject aURL( aFileName );
204 OUString aPrivDatName(aURL.getName());
205 SvStream* pInStream = NULL;
206 if ( xInputStream.is() )
207 pInStream = utl::UcbStreamHelper::CreateStream( xInputStream );
209 AbstractScImportAsciiDlg* pDlg = pFact->CreateScImportAsciiDlg( NULL, aPrivDatName, pInStream, SC_IMPORTFILE);
210 OSL_ENSURE(pDlg, "Dialog create fail!");
211 if ( pDlg->Execute() == RET_OK )
213 ScAsciiOptions aOptions;
214 pDlg->GetOptions( aOptions );
215 pDlg->SaveParameters();
216 aFilterOptions = aOptions.WriteToString();
217 nRet = ui::dialogs::ExecutableDialogResults::OK;
219 delete pDlg;
220 delete pInStream;
222 else if ( aFilterString == ScDocShell::GetWebQueryFilterName() || aFilterString == ScDocShell::GetHtmlFilterName() )
224 if (bExport)
225 nRet = ui::dialogs::ExecutableDialogResults::OK; // export HTML without dialog
226 else
228 // HTML import.
229 boost::scoped_ptr<AbstractScTextImportOptionsDlg> pDlg(
230 pFact->CreateScTextImportOptionsDlg(NULL));
232 if (pDlg->Execute() == RET_OK)
234 LanguageType eLang = pDlg->GetLanguageType();
235 OUStringBuffer aBuf;
237 aBuf.append(OUString::number(static_cast<sal_Int32>(eLang)));
238 aBuf.append(' ');
239 aBuf.append(pDlg->IsDateConversionSet() ? sal_Unicode('1') : sal_Unicode('0'));
240 aFilterOptions = aBuf.makeStringAndClear();
241 nRet = ui::dialogs::ExecutableDialogResults::OK;
245 else
247 sal_Bool bMultiByte = sal_True;
248 sal_Bool bDBEnc = false;
249 sal_Bool bAscii = false;
251 sal_Unicode cStrDel = '"';
252 sal_Unicode cAsciiDel = ';';
253 rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
255 OUString aTitle;
257 if ( aFilterString == ScDocShell::GetAsciiFilterName() )
259 // ascii export (import is handled above)
261 INetURLObject aURL( aFileName );
262 OUString aExt(aURL.getExtension());
263 if (aExt.equalsIgnoreAsciiCase("CSV"))
264 cAsciiDel = ',';
265 else
266 cAsciiDel = '\t';
268 aTitle = ScGlobal::GetRscString( STR_EXPORT_ASCII );
269 bAscii = sal_True;
271 else if ( aFilterString == ScDocShell::GetLotusFilterName() )
273 // lotus is only imported
274 OSL_ENSURE( !bExport, "Filter Options for Lotus Export is not implemented" );
276 aTitle = ScGlobal::GetRscString( STR_IMPORT_LOTUS );
277 eEncoding = RTL_TEXTENCODING_IBM_437;
279 else if ( aFilterString == ScDocShell::GetDBaseFilterName() )
281 if ( bExport )
283 // dBase export
284 aTitle = ScGlobal::GetRscString( STR_EXPORT_DBF );
286 else
288 // dBase import
289 aTitle = ScGlobal::GetRscString( STR_IMPORT_DBF );
291 load_CharSet( eEncoding, bExport );
292 bDBEnc = sal_True;
294 else if ( aFilterString == ScDocShell::GetDifFilterName() )
296 if ( bExport )
298 // DIF export
299 aTitle = ScGlobal::GetRscString( STR_EXPORT_DIF );
301 else
303 // DIF import
304 aTitle = ScGlobal::GetRscString( STR_IMPORT_DIF );
306 // common for DIF import/export
307 eEncoding = RTL_TEXTENCODING_MS_1252;
310 ScImportOptions aOptions( cAsciiDel, cStrDel, eEncoding);
312 AbstractScImportOptionsDlg* pDlg = pFact->CreateScImportOptionsDlg( NULL, RID_SCDLG_IMPORTOPT,
313 bAscii, &aOptions, &aTitle, bMultiByte, bDBEnc,
314 !bExport);
315 OSL_ENSURE(pDlg, "Dialog create fail!");
316 if ( pDlg->Execute() == RET_OK )
318 pDlg->GetImportOptions( aOptions );
319 save_CharSet( aOptions.eCharSet, bExport );
320 if ( bAscii )
321 aFilterOptions = aOptions.BuildString();
322 else
323 aFilterOptions = aOptions.aStrFont;
324 nRet = ui::dialogs::ExecutableDialogResults::OK;
326 delete pDlg;
329 xInputStream.clear(); // don't hold the stream longer than necessary
331 return nRet;
334 // XImporter
336 void SAL_CALL ScFilterOptionsObj::setTargetDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
337 throw(lang::IllegalArgumentException, uno::RuntimeException)
339 bExport = false;
342 // XExporter
344 void SAL_CALL ScFilterOptionsObj::setSourceDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
345 throw(lang::IllegalArgumentException, uno::RuntimeException)
347 bExport = sal_True;
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */