/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- *//* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>#include <tools/urlobj.hxx>#include <vcl/svapp.hxx>#include <unotools/ucbstreamhelper.hxx>#include <connectivity/dbtools.hxx>#include <editutil.hxx>#include <filtuno.hxx>#include <miscuno.hxx>#include <scdll.hxx>#include <imoptdlg.hxx>#include <asciiopt.hxx>#include <docsh.hxx>#include <globstr.hrc>#include <scresid.hxx>#include <sc.hrc>#include <scabstdlg.hxx>#include <i18nlangtag/lang.h>#include <optutil.hxx>#include <com/sun/star/uno/Any.hxx>#include <com/sun/star/uno/Sequence.hxx>#include <comphelper/propertysequence.hxx>#include <memory>usingnamespacecom::sun::star;usingnamespacecom::sun::star::uno;usingnamespaceconnectivity::dbase;#define SCFILTEROPTIONSOBJ_SERVICE "com.sun.star.ui.dialogs.FilterOptionsDialog"#define SCFILTEROPTIONSOBJ_IMPLNAME "com.sun.star.comp.Calc.FilterOptionsDialog"SC_SIMPLE_SERVICE_INFO(ScFilterOptionsObj,SCFILTEROPTIONSOBJ_IMPLNAME,SCFILTEROPTIONSOBJ_SERVICE)#define SC_UNONAME_FILENAME "URL"#define SC_UNONAME_FILTERNAME "FilterName"#define SC_UNONAME_FILTEROPTIONS "FilterOptions"#define SC_UNONAME_INPUTSTREAM "InputStream"#define DBF_CHAR_SET "CharSet"#define DBF_SEP_PATH_IMPORT "Office.Calc/Dialogs/DBFImport"#define DBF_SEP_PATH_EXPORT "Office.Calc/Dialogs/DBFExport"namespace{enumclasscharsetSource{charset_from_file,charset_from_user_setting,charset_default};charsetSourceload_CharSet(rtl_TextEncoding&nCharSet,boolbExport,SvStream*dbf_Stream){if(dbf_Stream&&dbfReadCharset(nCharSet,dbf_Stream)){returncharsetSource::charset_from_file;}Sequence<Any>aValues;constAny*pProperties;Sequence<OUString>aNames{DBF_CHAR_SET};ScLinkConfigItemaItem(OUString::createFromAscii(bExport?DBF_SEP_PATH_EXPORT:DBF_SEP_PATH_IMPORT));aValues=aItem.GetProperties(aNames);pProperties=aValues.getConstArray();if(pProperties[0].hasValue()){sal_Int32nChar=0;pProperties[0]>>=nChar;if(nChar>=0){nCharSet=static_cast<rtl_TextEncoding>(nChar);returncharsetSource::charset_from_user_setting;}}// Default choicenCharSet=RTL_TEXTENCODING_IBM_850;returncharsetSource::charset_default;}voidsave_CharSet(rtl_TextEncodingnCharSet,boolbExport){Sequence<Any>aValues;Any*pProperties;Sequence<OUString>aNames{DBF_CHAR_SET};ScLinkConfigItemaItem(OUString::createFromAscii(bExport?DBF_SEP_PATH_EXPORT:DBF_SEP_PATH_IMPORT));aValues=aItem.GetProperties(aNames);pProperties=aValues.getArray();pProperties[0]<<=static_cast<sal_Int32>(nCharSet);aItem.PutProperties(aNames,aValues);}}ScFilterOptionsObj::ScFilterOptionsObj():bExport(false){}ScFilterOptionsObj::~ScFilterOptionsObj(){}extern"C"SAL_DLLPUBLIC_EXPORTcss::uno::XInterface*Calc_FilterOptionsDialog_get_implementation(css::uno::XComponentContext*,css::uno::Sequence<css::uno::Any>const&)<--- The function 'Calc_FilterOptionsDialog_get_implementation' is never used.{SolarMutexGuardaGuard;ScDLL::Init();returncppu::acquire(newScFilterOptionsObj);}// XPropertyAccessuno::Sequence<beans::PropertyValue>SAL_CALLScFilterOptionsObj::getPropertyValues(){returncomphelper::InitPropertySequence({{SC_UNONAME_FILTEROPTIONS,Any(aFilterOptions)}});}voidSAL_CALLScFilterOptionsObj::setPropertyValues(constuno::Sequence<beans::PropertyValue>&aProps){constbeans::PropertyValue*pPropArray=aProps.getConstArray();longnPropCount=aProps.getLength();for(longi=0;i<nPropCount;i++){constbeans::PropertyValue&rProp=pPropArray[i];OUStringaPropName(rProp.Name);if(aPropName==SC_UNONAME_FILENAME)rProp.Value>>=aFileName;elseif(aPropName==SC_UNONAME_FILTERNAME)rProp.Value>>=aFilterName;elseif(aPropName==SC_UNONAME_FILTEROPTIONS)rProp.Value>>=aFilterOptions;elseif(aPropName==SC_UNONAME_INPUTSTREAM)rProp.Value>>=xInputStream;}}// XExecutableDialogvoidSAL_CALLScFilterOptionsObj::setTitle(constOUString&/* aTitle */){// not used}sal_Int16SAL_CALLScFilterOptionsObj::execute(){sal_Int16nRet=ui::dialogs::ExecutableDialogResults::CANCEL;OUStringaFilterString(aFilterName);ScAbstractDialogFactory*pFact=ScAbstractDialogFactory::Create();if(!bExport&&aFilterString==ScDocShell::GetAsciiFilterName()){// ascii import is special...INetURLObjectaURL(aFileName);OUStringaPrivDatName(aURL.getName());std::unique_ptr<SvStream>pInStream;if(xInputStream.is())pInStream=utl::UcbStreamHelper::CreateStream(xInputStream);ScopedVclPtr<AbstractScImportAsciiDlg>pDlg(pFact->CreateScImportAsciiDlg(nullptr,aPrivDatName,pInStream.get(),SC_IMPORTFILE));if(pDlg->Execute()==RET_OK){ScAsciiOptionsaOptions;pDlg->GetOptions(aOptions);pDlg->SaveParameters();aFilterOptions=aOptions.WriteToString();nRet=ui::dialogs::ExecutableDialogResults::OK;}}elseif(aFilterString==ScDocShell::GetWebQueryFilterName()||aFilterString==ScDocShell::GetHtmlFilterName()){if(bExport)nRet=ui::dialogs::ExecutableDialogResults::OK;// export HTML without dialogelse{// HTML import.ScopedVclPtr<AbstractScTextImportOptionsDlg>pDlg(pFact->CreateScTextImportOptionsDlg());if(pDlg->Execute()==RET_OK){LanguageTypeeLang=pDlg->GetLanguageType();OUStringBufferaBuf;aBuf.append(OUString::number(static_cast<sal_uInt16>(eLang)));aBuf.append(' ');aBuf.append(pDlg->IsDateConversionSet()?u'1':u'0');aFilterOptions=aBuf.makeStringAndClear();nRet=ui::dialogs::ExecutableDialogResults::OK;}}}else{boolbDBEnc=false;boolbAscii=false;boolskipDialog=false;sal_UnicodeconstcStrDel='"';sal_UnicodecAsciiDel=';';rtl_TextEncodingeEncoding=RTL_TEXTENCODING_DONTKNOW;OUStringaTitle;if(aFilterString==ScDocShell::GetAsciiFilterName()){// ascii export (import is handled above)INetURLObjectaURL(aFileName);OUStringaExt(aURL.getExtension());if(aExt.equalsIgnoreAsciiCase("CSV"))cAsciiDel=',';elsecAsciiDel='\t';aTitle=ScResId(STR_EXPORT_ASCII);bAscii=true;}elseif(aFilterString==ScDocShell::GetLotusFilterName()){// lotus is only importedOSL_ENSURE(!bExport,"Filter Options for Lotus Export is not implemented");aTitle=ScResId(STR_IMPORT_LOTUS);eEncoding=RTL_TEXTENCODING_IBM_437;}elseif(aFilterString==ScDocShell::GetDBaseFilterName()){if(bExport){// dBase exportaTitle=ScResId(STR_EXPORT_DBF);}else{// dBase importaTitle=ScResId(STR_IMPORT_DBF);}std::unique_ptr<SvStream>pInStream;if(xInputStream.is())pInStream=utl::UcbStreamHelper::CreateStream(xInputStream);switch(load_CharSet(eEncoding,bExport,pInStream.get())){casecharsetSource::charset_from_file:skipDialog=true;break;casecharsetSource::charset_from_user_setting:casecharsetSource::charset_default:break;}bDBEnc=true;// pInStream goes out of scope, the stream is automatically closed}elseif(aFilterString==ScDocShell::GetDifFilterName()){if(bExport){// DIF exportaTitle=ScResId(STR_EXPORT_DIF);}else{// DIF importaTitle=ScResId(STR_IMPORT_DIF);}// common for DIF import/exporteEncoding=RTL_TEXTENCODING_MS_1252;}ScImportOptionsaOptions(cAsciiDel,cStrDel,eEncoding);if(skipDialog){// TODO: check we are not missing some of the stuff that ScImportOptionsDlg::GetImportOptions// (file sc/source/ui/dbgui/scuiimoptdlg.cxx) does// that is, if the dialog sets options that are not selected by the user (!)// then we are missing them here.// Then we may need to rip them out of the dialog.// Or we actually change the dialog to not display if skipDialog==true// in that case, add an argument skipDialog to CreateScImportOptionsDlgnRet=ui::dialogs::ExecutableDialogResults::OK;}else{ScopedVclPtr<AbstractScImportOptionsDlg>pDlg(pFact->CreateScImportOptionsDlg(bAscii,&aOptions,&aTitle,bDBEnc,!bExport));if(pDlg->Execute()==RET_OK){pDlg->SaveImportOptions();pDlg->GetImportOptions(aOptions);save_CharSet(aOptions.eCharSet,bExport);nRet=ui::dialogs::ExecutableDialogResults::OK;}}if(nRet==ui::dialogs::ExecutableDialogResults::OK){if(bAscii)aFilterOptions=aOptions.BuildString();elseaFilterOptions=aOptions.aStrFont;}}xInputStream.clear();// don't hold the stream longer than necessaryreturnnRet;}// XImportervoidSAL_CALLScFilterOptionsObj::setTargetDocument(constuno::Reference<lang::XComponent>&/* xDoc */){bExport=false;}// XExportervoidSAL_CALLScFilterOptionsObj::setSourceDocument(constuno::Reference<lang::XComponent>&/* xDoc */){bExport=true;}/* vim:set shiftwidth=4 softtabstop=4 expandtab: */