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