xref: /aoo41x/main/sc/source/ui/unoobj/filtuno.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sc.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
34*cdf0e10cSrcweir #include <tools/urlobj.hxx>
35*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
36*cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "editutil.hxx"
39*cdf0e10cSrcweir #include "filtuno.hxx"
40*cdf0e10cSrcweir #include "miscuno.hxx"
41*cdf0e10cSrcweir #include "unoguard.hxx"
42*cdf0e10cSrcweir #include "scdll.hxx"
43*cdf0e10cSrcweir #include "imoptdlg.hxx"
44*cdf0e10cSrcweir #include "asciiopt.hxx"
45*cdf0e10cSrcweir #include "docsh.hxx"
46*cdf0e10cSrcweir #include "globstr.hrc"
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include "sc.hrc" //CHINA001
50*cdf0e10cSrcweir #include "scabstdlg.hxx" //CHINA001
51*cdf0e10cSrcweir #include "i18npool/lang.h"
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #include <memory>
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir using namespace ::com::sun::star;
56*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir //------------------------------------------------------------------------
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #define SCFILTEROPTIONSOBJ_SERVICE		"com.sun.star.ui.dialogs.FilterOptionsDialog"
61*cdf0e10cSrcweir #define SCFILTEROPTIONSOBJ_IMPLNAME		"com.sun.star.comp.Calc.FilterOptionsDialog"
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir SC_SIMPLE_SERVICE_INFO( ScFilterOptionsObj, SCFILTEROPTIONSOBJ_IMPLNAME, SCFILTEROPTIONSOBJ_SERVICE )
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir #define SC_UNONAME_FILENAME         "URL"
66*cdf0e10cSrcweir #define SC_UNONAME_FILTERNAME		"FilterName"
67*cdf0e10cSrcweir #define SC_UNONAME_FILTEROPTIONS	"FilterOptions"
68*cdf0e10cSrcweir #define SC_UNONAME_INPUTSTREAM		"InputStream"
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir //------------------------------------------------------------------------
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir ScFilterOptionsObj::ScFilterOptionsObj() :
73*cdf0e10cSrcweir 	bExport( sal_False )
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir ScFilterOptionsObj::~ScFilterOptionsObj()
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir // stuff for exService_...
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir uno::Reference<uno::XInterface>	SAL_CALL ScFilterOptionsObj_CreateInstance(
84*cdf0e10cSrcweir 						const uno::Reference<lang::XMultiServiceFactory>& )
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir 	ScUnoGuard aGuard;
87*cdf0e10cSrcweir 	ScDLL::Init();
88*cdf0e10cSrcweir 	return (::cppu::OWeakObject*) new ScFilterOptionsObj;
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir rtl::OUString ScFilterOptionsObj::getImplementationName_Static()
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir 	return rtl::OUString::createFromAscii( SCFILTEROPTIONSOBJ_IMPLNAME );
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir uno::Sequence<rtl::OUString> ScFilterOptionsObj::getSupportedServiceNames_Static()
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	uno::Sequence<rtl::OUString> aRet(1);
99*cdf0e10cSrcweir 	rtl::OUString* pArray = aRet.getArray();
100*cdf0e10cSrcweir 	pArray[0] = rtl::OUString::createFromAscii( SCFILTEROPTIONSOBJ_SERVICE );
101*cdf0e10cSrcweir 	return aRet;
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir // XPropertyAccess
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir uno::Sequence<beans::PropertyValue> SAL_CALL ScFilterOptionsObj::getPropertyValues() throw(uno::RuntimeException)
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir 	uno::Sequence<beans::PropertyValue> aRet(1);
109*cdf0e10cSrcweir 	beans::PropertyValue* pArray = aRet.getArray();
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	pArray[0].Name = rtl::OUString::createFromAscii( SC_UNONAME_FILTEROPTIONS );
112*cdf0e10cSrcweir 	pArray[0].Value <<= aFilterOptions;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	return aRet;
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir void SAL_CALL ScFilterOptionsObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& aProps )
118*cdf0e10cSrcweir 					throw(beans::UnknownPropertyException, beans::PropertyVetoException,
119*cdf0e10cSrcweir 							lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir 	const beans::PropertyValue* pPropArray = aProps.getConstArray();
122*cdf0e10cSrcweir 	long nPropCount = aProps.getLength();
123*cdf0e10cSrcweir 	for (long i = 0; i < nPropCount; i++)
124*cdf0e10cSrcweir 	{
125*cdf0e10cSrcweir 		const beans::PropertyValue& rProp = pPropArray[i];
126*cdf0e10cSrcweir 		String aPropName(rProp.Name);
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 		if ( aPropName.EqualsAscii( SC_UNONAME_FILENAME ) )
129*cdf0e10cSrcweir 			rProp.Value >>= aFileName;
130*cdf0e10cSrcweir 		else if ( aPropName.EqualsAscii( SC_UNONAME_FILTERNAME ) )
131*cdf0e10cSrcweir 			rProp.Value >>= aFilterName;
132*cdf0e10cSrcweir 		else if ( aPropName.EqualsAscii( SC_UNONAME_FILTEROPTIONS ) )
133*cdf0e10cSrcweir 			rProp.Value >>= aFilterOptions;
134*cdf0e10cSrcweir 		else if ( aPropName.EqualsAscii( SC_UNONAME_INPUTSTREAM ) )
135*cdf0e10cSrcweir 			rProp.Value >>= xInputStream;
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir // XExecutableDialog
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir void SAL_CALL ScFilterOptionsObj::setTitle( const ::rtl::OUString& /* aTitle */ ) throw(uno::RuntimeException)
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir 	// not used
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir sal_Int16 SAL_CALL ScFilterOptionsObj::execute() throw(uno::RuntimeException)
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir 	sal_Int16 nRet = ui::dialogs::ExecutableDialogResults::CANCEL;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 	String aFilterString( aFilterName );
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
153*cdf0e10cSrcweir     DBG_ASSERT(pFact, "ScAbstractFactory create fail!");
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	if ( !bExport && aFilterString == ScDocShell::GetAsciiFilterName() )
156*cdf0e10cSrcweir 	{
157*cdf0e10cSrcweir 		//	ascii import is special...
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 		INetURLObject aURL( aFileName );
160*cdf0e10cSrcweir 		String aExt(aURL.getExtension());
161*cdf0e10cSrcweir 		String aPrivDatName(aURL.getName());
162*cdf0e10cSrcweir 		sal_Unicode cAsciiDel;
163*cdf0e10cSrcweir 		if (aExt.EqualsIgnoreCaseAscii("CSV"))
164*cdf0e10cSrcweir 			cAsciiDel = ',';
165*cdf0e10cSrcweir 		else
166*cdf0e10cSrcweir 			cAsciiDel = '\t';
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 		SvStream* pInStream = NULL;
169*cdf0e10cSrcweir 		if ( xInputStream.is() )
170*cdf0e10cSrcweir 			pInStream = utl::UcbStreamHelper::CreateStream( xInputStream );
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 		//CHINA001 ScImportAsciiDlg* pDlg = new ScImportAsciiDlg( NULL, aPrivDatName, pInStream, cAsciiDel );
173*cdf0e10cSrcweir 		AbstractScImportAsciiDlg* pDlg = pFact->CreateScImportAsciiDlg( NULL, aPrivDatName, pInStream, RID_SCDLG_ASCII, cAsciiDel);
174*cdf0e10cSrcweir 		DBG_ASSERT(pDlg, "Dialog create fail!");//CHINA001
175*cdf0e10cSrcweir 		if ( pDlg->Execute() == RET_OK )
176*cdf0e10cSrcweir         {
177*cdf0e10cSrcweir             ScAsciiOptions aOptions;
178*cdf0e10cSrcweir             pDlg->GetOptions( aOptions );
179*cdf0e10cSrcweir             pDlg->SaveParameters();
180*cdf0e10cSrcweir             aFilterOptions = aOptions.WriteToString();
181*cdf0e10cSrcweir             nRet = ui::dialogs::ExecutableDialogResults::OK;
182*cdf0e10cSrcweir         }
183*cdf0e10cSrcweir 		delete pDlg;
184*cdf0e10cSrcweir 		delete pInStream;
185*cdf0e10cSrcweir 	}
186*cdf0e10cSrcweir     else if ( aFilterString == ScDocShell::GetWebQueryFilterName() || aFilterString == ScDocShell::GetHtmlFilterName() )
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         if (bExport)
189*cdf0e10cSrcweir             nRet = ui::dialogs::ExecutableDialogResults::OK;    // export HTML without dialog
190*cdf0e10cSrcweir         else
191*cdf0e10cSrcweir         {
192*cdf0e10cSrcweir             // HTML import.
193*cdf0e10cSrcweir             ::std::auto_ptr<AbstractScTextImportOptionsDlg> pDlg(
194*cdf0e10cSrcweir                 pFact->CreateScTextImportOptionsDlg(NULL, RID_SCDLG_TEXT_IMPORT_OPTIONS));
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir             if (pDlg->Execute() == RET_OK)
197*cdf0e10cSrcweir             {
198*cdf0e10cSrcweir                 LanguageType eLang = pDlg->GetLanguageType();
199*cdf0e10cSrcweir                 OUStringBuffer aBuf;
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir                 aBuf.append(String::CreateFromInt32(static_cast<sal_Int32>(eLang)));
202*cdf0e10cSrcweir                 aBuf.append(sal_Unicode(' '));
203*cdf0e10cSrcweir                 aBuf.append(pDlg->IsDateConversionSet() ? sal_Unicode('1') : sal_Unicode('0'));
204*cdf0e10cSrcweir                 aFilterOptions = aBuf.makeStringAndClear();
205*cdf0e10cSrcweir                 nRet = ui::dialogs::ExecutableDialogResults::OK;
206*cdf0e10cSrcweir             }
207*cdf0e10cSrcweir         }
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir 	else
210*cdf0e10cSrcweir 	{
211*cdf0e10cSrcweir 		sal_Bool bMultiByte = sal_True;
212*cdf0e10cSrcweir 		sal_Bool bDBEnc     = sal_False;
213*cdf0e10cSrcweir 		sal_Bool bAscii     = sal_False;
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 		sal_Unicode cStrDel = '"';
216*cdf0e10cSrcweir 		sal_Unicode cAsciiDel = ';';
217*cdf0e10cSrcweir 		rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 		String aTitle;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 		if ( aFilterString == ScDocShell::GetAsciiFilterName() )
222*cdf0e10cSrcweir 		{
223*cdf0e10cSrcweir 			//	ascii export (import is handled above)
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 			INetURLObject aURL( aFileName );
226*cdf0e10cSrcweir 			String aExt(aURL.getExtension());
227*cdf0e10cSrcweir 			if (aExt.EqualsIgnoreCaseAscii("CSV"))
228*cdf0e10cSrcweir 				cAsciiDel = ',';
229*cdf0e10cSrcweir 			else
230*cdf0e10cSrcweir 				cAsciiDel = '\t';
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 			aTitle = ScGlobal::GetRscString( STR_EXPORT_ASCII );
233*cdf0e10cSrcweir 			bAscii = sal_True;
234*cdf0e10cSrcweir 		}
235*cdf0e10cSrcweir 		else if ( aFilterString == ScDocShell::GetLotusFilterName() )
236*cdf0e10cSrcweir 		{
237*cdf0e10cSrcweir 			//	lotus is only imported
238*cdf0e10cSrcweir 			DBG_ASSERT( !bExport, "Filter Options for Lotus Export is not implemented" );
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 			aTitle = ScGlobal::GetRscString( STR_IMPORT_LOTUS );
241*cdf0e10cSrcweir 			eEncoding = RTL_TEXTENCODING_IBM_437;
242*cdf0e10cSrcweir 		}
243*cdf0e10cSrcweir 		else if ( aFilterString == ScDocShell::GetDBaseFilterName() )
244*cdf0e10cSrcweir 		{
245*cdf0e10cSrcweir 			if ( bExport )
246*cdf0e10cSrcweir 			{
247*cdf0e10cSrcweir 				//	dBase export
248*cdf0e10cSrcweir 				aTitle = ScGlobal::GetRscString( STR_EXPORT_DBF );
249*cdf0e10cSrcweir 			}
250*cdf0e10cSrcweir 			else
251*cdf0e10cSrcweir 			{
252*cdf0e10cSrcweir 				//	dBase import
253*cdf0e10cSrcweir 				aTitle = ScGlobal::GetRscString( STR_IMPORT_DBF );
254*cdf0e10cSrcweir 			}
255*cdf0e10cSrcweir 			// common for dBase import/export
256*cdf0e10cSrcweir 			eEncoding = RTL_TEXTENCODING_IBM_850;
257*cdf0e10cSrcweir 			bDBEnc = sal_True;
258*cdf0e10cSrcweir 		}
259*cdf0e10cSrcweir 		else if ( aFilterString == ScDocShell::GetDifFilterName() )
260*cdf0e10cSrcweir 		{
261*cdf0e10cSrcweir 			if ( bExport )
262*cdf0e10cSrcweir 			{
263*cdf0e10cSrcweir 				//	DIF export
264*cdf0e10cSrcweir 				aTitle = ScGlobal::GetRscString( STR_EXPORT_DIF );
265*cdf0e10cSrcweir 			}
266*cdf0e10cSrcweir 			else
267*cdf0e10cSrcweir 			{
268*cdf0e10cSrcweir 				//	DIF import
269*cdf0e10cSrcweir 				aTitle = ScGlobal::GetRscString( STR_IMPORT_DIF );
270*cdf0e10cSrcweir 			}
271*cdf0e10cSrcweir 			// common for DIF import/export
272*cdf0e10cSrcweir 			eEncoding = RTL_TEXTENCODING_MS_1252;
273*cdf0e10cSrcweir 		}
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 		ScImportOptions aOptions( cAsciiDel, cStrDel, eEncoding);
276*cdf0e10cSrcweir //CHINA001		ScImportOptionsDlg* pDlg = new ScImportOptionsDlg( NULL, bAscii,
277*cdf0e10cSrcweir //CHINA001		&aOptions, &aTitle, bMultiByte, bDBEnc,
278*cdf0e10cSrcweir //CHINA001		!bExport );
279*cdf0e10cSrcweir //CHINA001
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir 		AbstractScImportOptionsDlg* pDlg = pFact->CreateScImportOptionsDlg( NULL, RID_SCDLG_IMPORTOPT,
282*cdf0e10cSrcweir 																			bAscii, &aOptions, &aTitle, bMultiByte, bDBEnc,
283*cdf0e10cSrcweir 																			!bExport);
284*cdf0e10cSrcweir 		DBG_ASSERT(pDlg, "Dialog create fail!");//CHINA001
285*cdf0e10cSrcweir 		if ( pDlg->Execute() == RET_OK )
286*cdf0e10cSrcweir 		{
287*cdf0e10cSrcweir 			pDlg->GetImportOptions( aOptions );
288*cdf0e10cSrcweir 			if ( bAscii )
289*cdf0e10cSrcweir 				aFilterOptions = aOptions.BuildString();
290*cdf0e10cSrcweir 			else
291*cdf0e10cSrcweir 				aFilterOptions = aOptions.aStrFont;
292*cdf0e10cSrcweir 			nRet = ui::dialogs::ExecutableDialogResults::OK;
293*cdf0e10cSrcweir 		}
294*cdf0e10cSrcweir 		delete pDlg;
295*cdf0e10cSrcweir 	}
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir 	xInputStream.clear();	// don't hold the stream longer than necessary
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir 	return nRet;
300*cdf0e10cSrcweir }
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir // XImporter
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir void SAL_CALL ScFilterOptionsObj::setTargetDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
305*cdf0e10cSrcweir 							throw(lang::IllegalArgumentException, uno::RuntimeException)
306*cdf0e10cSrcweir {
307*cdf0e10cSrcweir 	bExport = sal_False;
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir // XExporter
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir void SAL_CALL ScFilterOptionsObj::setSourceDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
313*cdf0e10cSrcweir 							throw(lang::IllegalArgumentException, uno::RuntimeException)
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir 	bExport = sal_True;
316*cdf0e10cSrcweir }
317*cdf0e10cSrcweir 
318