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