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