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