calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / sc / source / ui / unoobj / filtuno.cxx
bloba2b4d0f7e1c3acdfdc6b70a3331d76d21573a38c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
21 #include <tools/urlobj.hxx>
22 #include <vcl/svapp.hxx>
23 #include <unotools/ucbstreamhelper.hxx>
24 #include <connectivity/dbtools.hxx>
25 #include <osl/diagnose.h>
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 #include <scresid.hxx>
36 #include <scabstdlg.hxx>
37 #include <i18nlangtag/lang.h>
39 #include <optutil.hxx>
40 #include <com/sun/star/uno/Any.hxx>
41 #include <com/sun/star/uno/Sequence.hxx>
42 #include <comphelper/namedvaluecollection.hxx>
43 #include <comphelper/propertysequence.hxx>
44 #include <memory>
46 using namespace com::sun::star;
47 using namespace com::sun::star::uno;
48 using namespace connectivity::dbase;
50 constexpr OUStringLiteral SCFILTEROPTIONSOBJ_SERVICE = u"com.sun.star.ui.dialogs.FilterOptionsDialog";
51 constexpr OUStringLiteral SCFILTEROPTIONSOBJ_IMPLNAME = u"com.sun.star.comp.Calc.FilterOptionsDialog";
53 SC_SIMPLE_SERVICE_INFO( ScFilterOptionsObj, SCFILTEROPTIONSOBJ_IMPLNAME, SCFILTEROPTIONSOBJ_SERVICE )
55 constexpr OUStringLiteral SC_UNONAME_FILENAME = u"URL";
56 constexpr OUStringLiteral SC_UNONAME_FILTERNAME = u"FilterName";
57 constexpr OUStringLiteral SC_UNONAME_FILTEROPTIONS = u"FilterOptions";
58 constexpr OUStringLiteral SC_UNONAME_INPUTSTREAM = u"InputStream";
60 constexpr OUStringLiteral DBF_CHAR_SET = u"CharSet";
61 constexpr OUStringLiteral DBF_SEP_PATH_IMPORT = u"Office.Calc/Dialogs/DBFImport";
62 constexpr OUStringLiteral DBF_SEP_PATH_EXPORT = u"Office.Calc/Dialogs/DBFExport";
64 namespace
67 enum class charsetSource
69 charset_from_file,
70 charset_from_user_setting,
71 charset_default
74 charsetSource load_CharSet(rtl_TextEncoding &nCharSet, bool bExport, SvStream* dbf_Stream)
76 if (dbf_Stream && dbfReadCharset(nCharSet, dbf_Stream))
78 return charsetSource::charset_from_file;
81 Sequence<Any> aValues;
82 const Any *pProperties;
83 Sequence<OUString> aNames { DBF_CHAR_SET };
84 ScLinkConfigItem aItem( bExport ? DBF_SEP_PATH_EXPORT : DBF_SEP_PATH_IMPORT );
86 aValues = aItem.GetProperties( aNames );
87 pProperties = aValues.getConstArray();
89 if( pProperties[0].hasValue() )
91 sal_Int32 nChar = 0;
92 pProperties[0] >>= nChar;
93 if( nChar >= 0)
95 nCharSet = static_cast<rtl_TextEncoding>(nChar);
96 return charsetSource::charset_from_user_setting;
100 // Default choice
101 nCharSet = RTL_TEXTENCODING_IBM_850;
102 return charsetSource::charset_default;
105 void save_CharSet( rtl_TextEncoding nCharSet, bool bExport )
107 Sequence<Any> aValues;
108 Any *pProperties;
109 Sequence<OUString> aNames { DBF_CHAR_SET };
110 ScLinkConfigItem aItem( bExport ? DBF_SEP_PATH_EXPORT : DBF_SEP_PATH_IMPORT );
112 aValues = aItem.GetProperties( aNames );
113 pProperties = aValues.getArray();
114 pProperties[0] <<= static_cast<sal_Int32>(nCharSet);
116 aItem.PutProperties(aNames, aValues);
120 ScFilterOptionsObj::ScFilterOptionsObj() :
121 bExport( false )
125 ScFilterOptionsObj::~ScFilterOptionsObj()
129 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
130 Calc_FilterOptionsDialog_get_implementation(css::uno::XComponentContext*, css::uno::Sequence<css::uno::Any> const &)
132 SolarMutexGuard aGuard;
133 ScDLL::Init();
134 return cppu::acquire(new ScFilterOptionsObj);
137 // XPropertyAccess
139 uno::Sequence<beans::PropertyValue> SAL_CALL ScFilterOptionsObj::getPropertyValues()
141 return comphelper::InitPropertySequence({
142 { SC_UNONAME_FILTEROPTIONS, Any(aFilterOptions) }
146 void SAL_CALL ScFilterOptionsObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& aProps )
148 for (const beans::PropertyValue& rProp : aProps)
150 OUString aPropName(rProp.Name);
152 if ( aPropName == SC_UNONAME_FILENAME )
153 rProp.Value >>= aFileName;
154 else if ( aPropName == SC_UNONAME_FILTERNAME )
155 rProp.Value >>= aFilterName;
156 else if ( aPropName == SC_UNONAME_FILTEROPTIONS )
157 rProp.Value >>= aFilterOptions;
158 else if ( aPropName == SC_UNONAME_INPUTSTREAM )
159 rProp.Value >>= xInputStream;
163 // XExecutableDialog
165 void SAL_CALL ScFilterOptionsObj::setTitle( const OUString& /* aTitle */ )
167 // not used
170 sal_Int16 SAL_CALL ScFilterOptionsObj::execute()
172 sal_Int16 nRet = ui::dialogs::ExecutableDialogResults::CANCEL;
174 OUString aFilterString( aFilterName );
176 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
178 if ( !bExport && aFilterString == ScDocShell::GetAsciiFilterName() )
180 // ascii import is special...
182 INetURLObject aURL( aFileName );
183 // tdf#132421 - don't URL encode filename for the import ASCII dialog title
184 OUString aPrivDatName(aURL.GetLastName(INetURLObject::DecodeMechanism::Unambiguous));
185 std::unique_ptr<SvStream> pInStream;
186 if ( xInputStream.is() )
187 pInStream = utl::UcbStreamHelper::CreateStream( xInputStream );
189 ScopedVclPtr<AbstractScImportAsciiDlg> pDlg(pFact->CreateScImportAsciiDlg(Application::GetFrameWeld(xDialogParent), aPrivDatName,
190 pInStream.get(), SC_IMPORTFILE));
191 if ( pDlg->Execute() == RET_OK )
193 ScAsciiOptions aOptions;
194 pDlg->GetOptions( aOptions );
195 pDlg->SaveParameters();
196 aFilterOptions = aOptions.WriteToString();
197 nRet = ui::dialogs::ExecutableDialogResults::OK;
200 else if ( aFilterString == ScDocShell::GetWebQueryFilterName() || aFilterString == ScDocShell::GetHtmlFilterName() )
202 if (bExport)
203 nRet = ui::dialogs::ExecutableDialogResults::OK; // export HTML without dialog
204 else
206 // HTML import.
207 ScopedVclPtr<AbstractScTextImportOptionsDlg> pDlg(
208 pFact->CreateScTextImportOptionsDlg(Application::GetFrameWeld(xDialogParent)));
210 if (pDlg->Execute() == RET_OK)
212 LanguageType eLang = pDlg->GetLanguageType();
213 OUStringBuffer aBuf;
215 aBuf.append(static_cast<sal_Int32>(static_cast<sal_uInt16>(eLang)));
216 aBuf.append(' ');
217 aBuf.append(pDlg->IsDateConversionSet() ? u'1' : u'0');
218 aFilterOptions = aBuf.makeStringAndClear();
219 nRet = ui::dialogs::ExecutableDialogResults::OK;
223 else
225 bool bDBEnc = false;
226 bool bAscii = false;
227 bool skipDialog = false;
229 sal_Unicode const cStrDel = '"';
230 sal_Unicode cAsciiDel = ';';
231 rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
233 OUString aTitle;
235 if ( aFilterString == ScDocShell::GetAsciiFilterName() )
237 // ascii export (import is handled above)
239 INetURLObject aURL( aFileName );
240 OUString aExt(aURL.getExtension());
241 if (aExt.equalsIgnoreAsciiCase("CSV"))
242 cAsciiDel = ',';
243 else
244 cAsciiDel = '\t';
246 aTitle = ScResId( STR_EXPORT_ASCII );
247 bAscii = true;
249 else if ( aFilterString == ScDocShell::GetLotusFilterName() )
251 // lotus is only imported
252 OSL_ENSURE( !bExport, "Filter Options for Lotus Export is not implemented" );
254 aTitle = ScResId( STR_IMPORT_LOTUS );
255 eEncoding = RTL_TEXTENCODING_IBM_437;
257 else if ( aFilterString == ScDocShell::GetDBaseFilterName() )
259 if ( bExport )
261 // dBase export
262 aTitle = ScResId( STR_EXPORT_DBF );
264 else
266 // dBase import
267 aTitle = ScResId( STR_IMPORT_DBF );
270 std::unique_ptr<SvStream> pInStream;
271 if ( xInputStream.is() )
272 pInStream = utl::UcbStreamHelper::CreateStream( xInputStream );
273 switch(load_CharSet( eEncoding, bExport, pInStream.get()))
275 case charsetSource::charset_from_file:
276 skipDialog = true;
277 break;
278 case charsetSource::charset_from_user_setting:
279 case charsetSource::charset_default:
280 break;
282 bDBEnc = true;
283 // pInStream goes out of scope, the stream is automatically closed
285 else if ( aFilterString == ScDocShell::GetDifFilterName() )
287 if ( bExport )
289 // DIF export
290 aTitle = ScResId( STR_EXPORT_DIF );
292 else
294 // DIF import
295 aTitle = ScResId( STR_IMPORT_DIF );
297 // common for DIF import/export
298 eEncoding = RTL_TEXTENCODING_MS_1252;
301 ScImportOptions aOptions( cAsciiDel, cStrDel, eEncoding);
302 if(skipDialog)
304 // TODO: check we are not missing some of the stuff that ScImportOptionsDlg::GetImportOptions
305 // (file sc/source/ui/dbgui/scuiimoptdlg.cxx) does
306 // that is, if the dialog sets options that are not selected by the user (!)
307 // then we are missing them here.
308 // Then we may need to rip them out of the dialog.
309 // Or we actually change the dialog to not display if skipDialog==true
310 // in that case, add an argument skipDialog to CreateScImportOptionsDlg
311 nRet = ui::dialogs::ExecutableDialogResults::OK;
313 else
315 ScopedVclPtr<AbstractScImportOptionsDlg> pDlg(pFact->CreateScImportOptionsDlg(Application::GetFrameWeld(xDialogParent),
316 bAscii, &aOptions, &aTitle,
317 bDBEnc, !bExport));
318 if ( pDlg->Execute() == RET_OK )
320 pDlg->SaveImportOptions();
321 pDlg->GetImportOptions( aOptions );
322 save_CharSet( aOptions.eCharSet, bExport );
323 nRet = ui::dialogs::ExecutableDialogResults::OK;
326 if (nRet == ui::dialogs::ExecutableDialogResults::OK)
328 if ( bAscii )
329 aFilterOptions = aOptions.BuildString();
330 else
331 aFilterOptions = aOptions.aStrFont;
335 xInputStream.clear(); // don't hold the stream longer than necessary
337 return nRet;
340 // XImporter
342 void SAL_CALL ScFilterOptionsObj::setTargetDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
344 bExport = false;
347 // XExporter
349 void SAL_CALL ScFilterOptionsObj::setSourceDocument( const uno::Reference<lang::XComponent>& /* xDoc */ )
351 bExport = true;
354 // XInitialization
356 void SAL_CALL ScFilterOptionsObj::initialize(const uno::Sequence<uno::Any>& rArguments)
358 ::comphelper::NamedValueCollection aProperties(rArguments);
359 if (aProperties.has("ParentWindow"))
360 aProperties.get("ParentWindow") >>= xDialogParent;
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */