Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
25 :
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"
34 :
35 :
36 : #include "sc.hrc"
37 : #include "scabstdlg.hxx"
38 : #include "i18npool/lang.h"
39 :
40 : #include <memory>
41 :
42 : #include <optutil.hxx>
43 : #include <com/sun/star/uno/Any.hxx>
44 : #include <com/sun/star/uno/Sequence.hxx>
45 :
46 :
47 : using namespace ::com::sun::star;
48 : using ::rtl::OUStringBuffer;
49 : using namespace rtl;
50 : using namespace com::sun::star::uno;
51 :
52 : //------------------------------------------------------------------------
53 :
54 : #define SCFILTEROPTIONSOBJ_SERVICE "com.sun.star.ui.dialogs.FilterOptionsDialog"
55 : #define SCFILTEROPTIONSOBJ_IMPLNAME "com.sun.star.comp.Calc.FilterOptionsDialog"
56 :
57 0 : SC_SIMPLE_SERVICE_INFO( ScFilterOptionsObj, SCFILTEROPTIONSOBJ_IMPLNAME, SCFILTEROPTIONSOBJ_SERVICE )
58 :
59 : #define SC_UNONAME_FILENAME "URL"
60 : #define SC_UNONAME_FILTERNAME "FilterName"
61 : #define SC_UNONAME_FILTEROPTIONS "FilterOptions"
62 : #define SC_UNONAME_INPUTSTREAM "InputStream"
63 :
64 :
65 : #define DBF_CHAR_SET "CharSet"
66 : #define DBF_SEP_PATH_IMPORT "Office.Calc/Dialogs/DBFImport"
67 : #define DBF_SEP_PATH_EXPORT "Office.Calc/Dialogs/DBFExport"
68 :
69 : //------------------------------------------------------------------------
70 :
71 0 : static void load_CharSet( rtl_TextEncoding &nCharSet, bool bExport )
72 : {
73 0 : Sequence<Any> aValues;
74 : const Any *pProperties;
75 0 : Sequence<OUString> aNames(1);
76 0 : OUString* pNames = aNames.getArray();
77 : ScLinkConfigItem aItem( OUString::createFromAscii(
78 0 : bExport?DBF_SEP_PATH_EXPORT:DBF_SEP_PATH_IMPORT ) );
79 :
80 0 : pNames[0] = OUString::createFromAscii( DBF_CHAR_SET );
81 0 : aValues = aItem.GetProperties( aNames );
82 0 : pProperties = aValues.getConstArray();
83 :
84 : // Default choice
85 0 : nCharSet = RTL_TEXTENCODING_IBM_850;
86 :
87 0 : if( pProperties[0].hasValue() )
88 : {
89 0 : sal_Int32 nChar = 0;
90 0 : pProperties[0] >>= nChar;
91 0 : if( nChar >= 0)
92 : {
93 0 : nCharSet = (rtl_TextEncoding) nChar;
94 : }
95 0 : }
96 0 : }
97 :
98 0 : static void save_CharSet( rtl_TextEncoding nCharSet, bool bExport )
99 : {
100 0 : Sequence<Any> aValues;
101 : Any *pProperties;
102 0 : Sequence<OUString> aNames(1);
103 0 : OUString* pNames = aNames.getArray();
104 : ScLinkConfigItem aItem( OUString::createFromAscii(
105 0 : bExport?DBF_SEP_PATH_EXPORT:DBF_SEP_PATH_IMPORT ) );
106 :
107 0 : pNames[0] = OUString::createFromAscii( DBF_CHAR_SET );
108 0 : aValues = aItem.GetProperties( aNames );
109 0 : pProperties = aValues.getArray();
110 0 : pProperties[0] <<= (sal_Int32) nCharSet;
111 :
112 0 : aItem.PutProperties(aNames, aValues);
113 0 : }
114 :
115 : //------------------------------------------------------------------------
116 :
117 0 : ScFilterOptionsObj::ScFilterOptionsObj() :
118 0 : bExport( false )
119 : {
120 0 : }
121 :
122 0 : ScFilterOptionsObj::~ScFilterOptionsObj()
123 : {
124 0 : }
125 :
126 : // stuff for exService_...
127 :
128 0 : uno::Reference<uno::XInterface> SAL_CALL ScFilterOptionsObj_CreateInstance(
129 : const uno::Reference<lang::XMultiServiceFactory>& )
130 : {
131 0 : SolarMutexGuard aGuard;
132 0 : ScDLL::Init();
133 0 : return (::cppu::OWeakObject*) new ScFilterOptionsObj;
134 : }
135 :
136 20 : rtl::OUString ScFilterOptionsObj::getImplementationName_Static()
137 : {
138 20 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( SCFILTEROPTIONSOBJ_IMPLNAME ));
139 : }
140 :
141 0 : uno::Sequence<rtl::OUString> ScFilterOptionsObj::getSupportedServiceNames_Static()
142 : {
143 0 : uno::Sequence<rtl::OUString> aRet(1);
144 0 : rtl::OUString* pArray = aRet.getArray();
145 0 : pArray[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( SCFILTEROPTIONSOBJ_SERVICE ));
146 0 : return aRet;
147 : }
148 :
149 : // XPropertyAccess
150 :
151 0 : uno::Sequence<beans::PropertyValue> SAL_CALL ScFilterOptionsObj::getPropertyValues() throw(uno::RuntimeException)
152 : {
153 0 : uno::Sequence<beans::PropertyValue> aRet(1);
154 0 : beans::PropertyValue* pArray = aRet.getArray();
155 :
156 0 : pArray[0].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_FILTEROPTIONS ));
157 0 : pArray[0].Value <<= aFilterOptions;
158 :
159 0 : return aRet;
160 : }
161 :
162 0 : void SAL_CALL ScFilterOptionsObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& aProps )
163 : throw(beans::UnknownPropertyException, beans::PropertyVetoException,
164 : lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
165 : {
166 0 : const beans::PropertyValue* pPropArray = aProps.getConstArray();
167 0 : long nPropCount = aProps.getLength();
168 0 : for (long i = 0; i < nPropCount; i++)
169 : {
170 0 : const beans::PropertyValue& rProp = pPropArray[i];
171 0 : String aPropName(rProp.Name);
172 :
173 0 : if ( aPropName.EqualsAscii( SC_UNONAME_FILENAME ) )
174 0 : rProp.Value >>= aFileName;
175 0 : else if ( aPropName.EqualsAscii( SC_UNONAME_FILTERNAME ) )
176 0 : rProp.Value >>= aFilterName;
177 0 : else if ( aPropName.EqualsAscii( SC_UNONAME_FILTEROPTIONS ) )
178 0 : rProp.Value >>= aFilterOptions;
179 0 : else if ( aPropName.EqualsAscii( SC_UNONAME_INPUTSTREAM ) )
180 0 : rProp.Value >>= xInputStream;
181 0 : }
182 0 : }
183 :
184 : // XExecutableDialog
185 :
186 0 : void SAL_CALL ScFilterOptionsObj::setTitle( const ::rtl::OUString& /* aTitle */ ) throw(uno::RuntimeException)
187 : {
188 : // not used
189 0 : }
190 :
191 0 : sal_Int16 SAL_CALL ScFilterOptionsObj::execute() throw(uno::RuntimeException)
192 : {
193 0 : sal_Int16 nRet = ui::dialogs::ExecutableDialogResults::CANCEL;
194 :
195 0 : String aFilterString( aFilterName );
196 :
197 0 : ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
198 : OSL_ENSURE(pFact, "ScAbstractFactory create fail!");
199 :
200 0 : if ( !bExport && aFilterString == ScDocShell::GetAsciiFilterName() )
201 : {
202 : // ascii import is special...
203 :
204 0 : INetURLObject aURL( aFileName );
205 0 : String aPrivDatName(aURL.getName());
206 0 : SvStream* pInStream = NULL;
207 0 : if ( xInputStream.is() )
208 0 : pInStream = utl::UcbStreamHelper::CreateStream( xInputStream );
209 :
210 0 : AbstractScImportAsciiDlg* pDlg = pFact->CreateScImportAsciiDlg( NULL, aPrivDatName, pInStream, RID_SCDLG_ASCII,SC_IMPORTFILE);
211 : OSL_ENSURE(pDlg, "Dialog create fail!");
212 0 : if ( pDlg->Execute() == RET_OK )
213 : {
214 0 : ScAsciiOptions aOptions;
215 0 : pDlg->GetOptions( aOptions );
216 0 : pDlg->SaveParameters();
217 0 : aFilterOptions = aOptions.WriteToString();
218 0 : nRet = ui::dialogs::ExecutableDialogResults::OK;
219 : }
220 0 : delete pDlg;
221 0 : delete pInStream;
222 : }
223 0 : else if ( aFilterString == ScDocShell::GetWebQueryFilterName() || aFilterString == ScDocShell::GetHtmlFilterName() )
224 : {
225 0 : if (bExport)
226 0 : nRet = ui::dialogs::ExecutableDialogResults::OK; // export HTML without dialog
227 : else
228 : {
229 : // HTML import.
230 : boost::scoped_ptr<AbstractScTextImportOptionsDlg> pDlg(
231 0 : pFact->CreateScTextImportOptionsDlg(NULL));
232 :
233 0 : if (pDlg->Execute() == RET_OK)
234 : {
235 0 : LanguageType eLang = pDlg->GetLanguageType();
236 0 : OUStringBuffer aBuf;
237 :
238 0 : aBuf.append(String::CreateFromInt32(static_cast<sal_Int32>(eLang)));
239 0 : aBuf.append(sal_Unicode(' '));
240 0 : aBuf.append(pDlg->IsDateConversionSet() ? sal_Unicode('1') : sal_Unicode('0'));
241 0 : aFilterOptions = aBuf.makeStringAndClear();
242 0 : nRet = ui::dialogs::ExecutableDialogResults::OK;
243 0 : }
244 : }
245 : }
246 : else
247 : {
248 0 : sal_Bool bMultiByte = sal_True;
249 0 : sal_Bool bDBEnc = false;
250 0 : sal_Bool bAscii = false;
251 :
252 0 : sal_Unicode cStrDel = '"';
253 0 : sal_Unicode cAsciiDel = ';';
254 0 : rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
255 :
256 0 : String aTitle;
257 :
258 0 : if ( aFilterString == ScDocShell::GetAsciiFilterName() )
259 : {
260 : // ascii export (import is handled above)
261 :
262 0 : INetURLObject aURL( aFileName );
263 0 : String aExt(aURL.getExtension());
264 0 : if (aExt.EqualsIgnoreCaseAscii("CSV"))
265 0 : cAsciiDel = ',';
266 : else
267 0 : cAsciiDel = '\t';
268 :
269 0 : aTitle = ScGlobal::GetRscString( STR_EXPORT_ASCII );
270 0 : bAscii = sal_True;
271 : }
272 0 : else if ( aFilterString == ScDocShell::GetLotusFilterName() )
273 : {
274 : // lotus is only imported
275 : OSL_ENSURE( !bExport, "Filter Options for Lotus Export is not implemented" );
276 :
277 0 : aTitle = ScGlobal::GetRscString( STR_IMPORT_LOTUS );
278 0 : eEncoding = RTL_TEXTENCODING_IBM_437;
279 : }
280 0 : else if ( aFilterString == ScDocShell::GetDBaseFilterName() )
281 : {
282 0 : if ( bExport )
283 : {
284 : // dBase export
285 0 : aTitle = ScGlobal::GetRscString( STR_EXPORT_DBF );
286 : }
287 : else
288 : {
289 : // dBase import
290 0 : aTitle = ScGlobal::GetRscString( STR_IMPORT_DBF );
291 : }
292 0 : load_CharSet( eEncoding, bExport );
293 0 : bDBEnc = sal_True;
294 : }
295 0 : else if ( aFilterString == ScDocShell::GetDifFilterName() )
296 : {
297 0 : if ( bExport )
298 : {
299 : // DIF export
300 0 : aTitle = ScGlobal::GetRscString( STR_EXPORT_DIF );
301 : }
302 : else
303 : {
304 : // DIF import
305 0 : aTitle = ScGlobal::GetRscString( STR_IMPORT_DIF );
306 : }
307 : // common for DIF import/export
308 0 : eEncoding = RTL_TEXTENCODING_MS_1252;
309 : }
310 :
311 0 : ScImportOptions aOptions( cAsciiDel, cStrDel, eEncoding);
312 :
313 : AbstractScImportOptionsDlg* pDlg = pFact->CreateScImportOptionsDlg( NULL, RID_SCDLG_IMPORTOPT,
314 : bAscii, &aOptions, &aTitle, bMultiByte, bDBEnc,
315 0 : !bExport);
316 : OSL_ENSURE(pDlg, "Dialog create fail!");
317 0 : if ( pDlg->Execute() == RET_OK )
318 : {
319 0 : pDlg->GetImportOptions( aOptions );
320 0 : save_CharSet( aOptions.eCharSet, bExport );
321 0 : if ( bAscii )
322 0 : aFilterOptions = aOptions.BuildString();
323 : else
324 0 : aFilterOptions = aOptions.aStrFont;
325 0 : nRet = ui::dialogs::ExecutableDialogResults::OK;
326 : }
327 0 : delete pDlg;
328 : }
329 :
330 0 : xInputStream.clear(); // don't hold the stream longer than necessary
331 :
332 0 : return nRet;
333 : }
334 :
335 : // XImporter
336 :
337 0 : void SAL_CALL ScFilterOptionsObj::setTargetDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
338 : throw(lang::IllegalArgumentException, uno::RuntimeException)
339 : {
340 0 : bExport = false;
341 0 : }
342 :
343 : // XExporter
344 :
345 0 : void SAL_CALL ScFilterOptionsObj::setSourceDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
346 : throw(lang::IllegalArgumentException, uno::RuntimeException)
347 : {
348 0 : bExport = sal_True;
349 15 : }
350 :
351 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|