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