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()
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 {
115 const beans::PropertyValue* pPropArray = aProps.getConstArray();
116 long nPropCount = aProps.getLength();
117 for (long i = 0; i < nPropCount; i++)
118 {
119 const beans::PropertyValue& rProp = pPropArray[i];
120 String aPropName(rProp.Name);
121
122 if ( aPropName.EqualsAscii( SC_UNONAME_FILENAME ) )
123 rProp.Value >>= aFileName;
124 else if ( aPropName.EqualsAscii( SC_UNONAME_FILTERNAME ) )
125 rProp.Value >>= aFilterName;
126 else if ( aPropName.EqualsAscii( SC_UNONAME_FILTEROPTIONS ) )
127 rProp.Value >>= aFilterOptions;
128 else if ( aPropName.EqualsAscii( SC_UNONAME_INPUTSTREAM ) )
129 rProp.Value >>= xInputStream;
130 }
131 }
132
133 // XExecutableDialog
134
setTitle(const::rtl::OUString &)135 void SAL_CALL ScFilterOptionsObj::setTitle( const ::rtl::OUString& /* aTitle */ )
136 {
137 // not used
138 }
139
execute()140 sal_Int16 SAL_CALL ScFilterOptionsObj::execute()
141 {
142 sal_Int16 nRet = ui::dialogs::ExecutableDialogResults::CANCEL;
143
144 String aFilterString( aFilterName );
145
146 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
147 DBG_ASSERT(pFact, "ScAbstractFactory create fail!");
148
149 if ( !bExport && aFilterString == ScDocShell::GetAsciiFilterName() )
150 {
151 // ascii import is special...
152
153 INetURLObject aURL( aFileName );
154 String aExt(aURL.getExtension());
155 String aPrivDatName(aURL.getName());
156 sal_Unicode cAsciiDel;
157 if (aExt.EqualsIgnoreCaseAscii("CSV"))
158 cAsciiDel = ',';
159 else
160 cAsciiDel = '\t';
161
162 SvStream* pInStream = NULL;
163 if ( xInputStream.is() )
164 pInStream = utl::UcbStreamHelper::CreateStream( xInputStream );
165
166 //CHINA001 ScImportAsciiDlg* pDlg = new ScImportAsciiDlg( NULL, aPrivDatName, pInStream, cAsciiDel );
167 AbstractScImportAsciiDlg* pDlg = pFact->CreateScImportAsciiDlg( NULL, aPrivDatName, pInStream, RID_SCDLG_ASCII, cAsciiDel);
168 DBG_ASSERT(pDlg, "Dialog create fail!");//CHINA001
169 if ( pDlg->Execute() == RET_OK )
170 {
171 ScAsciiOptions aOptions;
172 pDlg->GetOptions( aOptions );
173 pDlg->SaveParameters();
174 aFilterOptions = aOptions.WriteToString();
175 nRet = ui::dialogs::ExecutableDialogResults::OK;
176 }
177 delete pDlg;
178 delete pInStream;
179 }
180 else if ( aFilterString == ScDocShell::GetWebQueryFilterName() || aFilterString == ScDocShell::GetHtmlFilterName() )
181 {
182 if (bExport)
183 nRet = ui::dialogs::ExecutableDialogResults::OK; // export HTML without dialog
184 else
185 {
186 // HTML import.
187 ::std::auto_ptr<AbstractScTextImportOptionsDlg> pDlg(
188 pFact->CreateScTextImportOptionsDlg(NULL, RID_SCDLG_TEXT_IMPORT_OPTIONS));
189
190 if (pDlg->Execute() == RET_OK)
191 {
192 LanguageType eLang = pDlg->GetLanguageType();
193 OUStringBuffer aBuf;
194
195 aBuf.append(String::CreateFromInt32(static_cast<sal_Int32>(eLang)));
196 aBuf.append(sal_Unicode(' '));
197 aBuf.append(pDlg->IsDateConversionSet() ? sal_Unicode('1') : sal_Unicode('0'));
198 aFilterOptions = aBuf.makeStringAndClear();
199 nRet = ui::dialogs::ExecutableDialogResults::OK;
200 }
201 }
202 }
203 else
204 {
205 sal_Bool bMultiByte = sal_True;
206 sal_Bool bDBEnc = sal_False;
207 sal_Bool bAscii = sal_False;
208
209 sal_Unicode cStrDel = '"';
210 sal_Unicode cAsciiDel = ';';
211 rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
212
213 String aTitle;
214
215 if ( aFilterString == ScDocShell::GetAsciiFilterName() )
216 {
217 // ascii export (import is handled above)
218
219 INetURLObject aURL( aFileName );
220 String aExt(aURL.getExtension());
221 if (aExt.EqualsIgnoreCaseAscii("CSV"))
222 cAsciiDel = ',';
223 else
224 cAsciiDel = '\t';
225
226 aTitle = ScGlobal::GetRscString( STR_EXPORT_ASCII );
227 bAscii = sal_True;
228 }
229 else if ( aFilterString == ScDocShell::GetLotusFilterName() )
230 {
231 // lotus is only imported
232 DBG_ASSERT( !bExport, "Filter Options for Lotus Export is not implemented" );
233
234 aTitle = ScGlobal::GetRscString( STR_IMPORT_LOTUS );
235 eEncoding = RTL_TEXTENCODING_IBM_437;
236 }
237 else if ( aFilterString == ScDocShell::GetDBaseFilterName() )
238 {
239 if ( bExport )
240 {
241 // dBase export
242 aTitle = ScGlobal::GetRscString( STR_EXPORT_DBF );
243 }
244 else
245 {
246 // dBase import
247 aTitle = ScGlobal::GetRscString( STR_IMPORT_DBF );
248 }
249 // common for dBase import/export
250 eEncoding = RTL_TEXTENCODING_IBM_850;
251 bDBEnc = sal_True;
252 }
253 else if ( aFilterString == ScDocShell::GetDifFilterName() )
254 {
255 if ( bExport )
256 {
257 // DIF export
258 aTitle = ScGlobal::GetRscString( STR_EXPORT_DIF );
259 }
260 else
261 {
262 // DIF import
263 aTitle = ScGlobal::GetRscString( STR_IMPORT_DIF );
264 }
265 // common for DIF import/export
266 eEncoding = RTL_TEXTENCODING_MS_1252;
267 }
268
269 ScImportOptions aOptions( cAsciiDel, cStrDel, eEncoding);
270 //CHINA001 ScImportOptionsDlg* pDlg = new ScImportOptionsDlg( NULL, bAscii,
271 //CHINA001 &aOptions, &aTitle, bMultiByte, bDBEnc,
272 //CHINA001 !bExport );
273 //CHINA001
274
275 AbstractScImportOptionsDlg* pDlg = pFact->CreateScImportOptionsDlg( NULL, RID_SCDLG_IMPORTOPT,
276 bAscii, &aOptions, &aTitle, bMultiByte, bDBEnc,
277 !bExport);
278 DBG_ASSERT(pDlg, "Dialog create fail!");//CHINA001
279 if ( pDlg->Execute() == RET_OK )
280 {
281 pDlg->GetImportOptions( aOptions );
282 if ( bAscii )
283 aFilterOptions = aOptions.BuildString();
284 else
285 aFilterOptions = aOptions.aStrFont;
286 nRet = ui::dialogs::ExecutableDialogResults::OK;
287 }
288 delete pDlg;
289 }
290
291 xInputStream.clear(); // don't hold the stream longer than necessary
292
293 return nRet;
294 }
295
296 // XImporter
297
setTargetDocument(const uno::Reference<lang::XComponent> &)298 void SAL_CALL ScFilterOptionsObj::setTargetDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
299 {
300 bExport = sal_False;
301 }
302
303 // XExporter
304
setSourceDocument(const uno::Reference<lang::XComponent> &)305 void SAL_CALL ScFilterOptionsObj::setSourceDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
306 {
307 bExport = sal_True;
308 }
309